File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.345: download - view: text, annotated - select for diffs
Wed Dec 26 20:10:24 2018 UTC (5 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6400 Deep-linking
  - URLs like /adm/lti/tiny/domain/uniqueID can be used to restrict use of
    deep links to access from another LTI-enabled application (no user data
    passed in this context).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.345 2018/12/26 20:10:24 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, 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'],$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','quotas','autoenroll',
  250:                        'autoupdate','autocreate','directorysrch','contacts',
  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 => 'Internal Authentication',
  295:                                   col2 => 'Value'},
  296:                                  {col1 => 'Institutional user types',
  297:                                   col2 => 'Name displayed'}],
  298:                       print => \&print_defaults,
  299:                       modify => \&modify_defaults,
  300:                     },
  301:         'quotas' => 
  302:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  303:                       help => 'Domain_Configuration_Quotas',
  304:                       header => [{col1 => 'User affiliation',
  305:                                   col2 => 'Available tools',
  306:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  307:                       print => \&print_quotas,
  308:                       modify => \&modify_quotas,
  309:                     },
  310:         'autoenroll' =>
  311:                    { text => 'Auto-enrollment settings',
  312:                      help => 'Domain_Configuration_Auto_Enrollment',
  313:                      header => [{col1 => 'Configuration setting',
  314:                                  col2 => 'Value(s)'}],
  315:                      print => \&print_autoenroll,
  316:                      modify => \&modify_autoenroll,
  317:                    },
  318:         'autoupdate' => 
  319:                    { text => 'Auto-update settings',
  320:                      help => 'Domain_Configuration_Auto_Updates',
  321:                      header => [{col1 => 'Setting',
  322:                                  col2 => 'Value',},
  323:                                 {col1 => 'Setting',
  324:                                  col2 => 'Affiliation'},
  325:                                 {col1 => 'User population',
  326:                                  col2 => 'Updatable user data'}],
  327:                      print => \&print_autoupdate,
  328:                      modify => \&modify_autoupdate,
  329:                   },
  330:         'autocreate' => 
  331:                   { text => 'Auto-course creation settings',
  332:                      help => 'Domain_Configuration_Auto_Creation',
  333:                      header => [{col1 => 'Configuration Setting',
  334:                                  col2 => 'Value',}],
  335:                      print => \&print_autocreate,
  336:                      modify => \&modify_autocreate,
  337:                   },
  338:         'directorysrch' => 
  339:                   { text => 'Directory searches',
  340:                     help => 'Domain_Configuration_InstDirectory_Search',
  341:                     header => [{col1 => 'Institutional Directory Setting',
  342:                                 col2 => 'Value',},
  343:                                {col1 => 'LON-CAPA Directory Setting',
  344:                                 col2 => 'Value',}],
  345:                     print => \&print_directorysrch,
  346:                     modify => \&modify_directorysrch,
  347:                   },
  348:         'contacts' =>
  349:                   { text => 'E-mail addresses and helpform',
  350:                     help => 'Domain_Configuration_Contact_Info',
  351:                     header => [{col1 => 'Default e-mail addresses',
  352:                                 col2 => 'Value',},
  353:                                {col1 => 'Recipient(s) for notifications',
  354:                                 col2 => 'Value',},
  355:                                {col1 => 'Nightly status check e-mail',
  356:                                 col2 => 'Settings',},
  357:                                {col1 => 'Ask helpdesk form settings',
  358:                                 col2 => 'Value',},],
  359:                     print => \&print_contacts,
  360:                     modify => \&modify_contacts,
  361:                   },
  362:         'usercreation' => 
  363:                   { text => 'User creation',
  364:                     help => 'Domain_Configuration_User_Creation',
  365:                     header => [{col1 => 'Format rule type',
  366:                                 col2 => 'Format rules in force'},
  367:                                {col1 => 'User account creation',
  368:                                 col2 => 'Usernames which may be created',},
  369:                                {col1 => 'Context',
  370:                                 col2 => 'Assignable authentication types'}],
  371:                     print => \&print_usercreation,
  372:                     modify => \&modify_usercreation,
  373:                   },
  374:         'selfcreation' => 
  375:                   { text => 'Users self-creating accounts',
  376:                     help => 'Domain_Configuration_Self_Creation', 
  377:                     header => [{col1 => 'Self-creation with institutional username',
  378:                                 col2 => 'Enabled?'},
  379:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  380:                                 col2 => 'Information user can enter'},
  381:                                {col1 => 'Self-creation with e-mail verification',
  382:                                 col2 => 'Settings'}],
  383:                     print => \&print_selfcreation,
  384:                     modify => \&modify_selfcreation,
  385:                   },
  386:         'usermodification' =>
  387:                   { text => 'User modification',
  388:                     help => 'Domain_Configuration_User_Modification',
  389:                     header => [{col1 => 'Target user has role',
  390:                                 col2 => 'User information updatable in author context'},
  391:                                {col1 => 'Target user has role',
  392:                                 col2 => 'User information updatable in course context'}],
  393:                     print => \&print_usermodification,
  394:                     modify => \&modify_usermodification,
  395:                   },
  396:         'scantron' =>
  397:                   { text => 'Bubblesheet format file',
  398:                     help => 'Domain_Configuration_Scantron_Format',
  399:                     header => [ {col1 => 'Item',
  400:                                  col2 => '',
  401:                               }],
  402:                     print => \&print_scantron,
  403:                     modify => \&modify_scantron,
  404:                   },
  405:         'requestcourses' => 
  406:                  {text => 'Request creation of courses',
  407:                   help => 'Domain_Configuration_Request_Courses',
  408:                   header => [{col1 => 'User affiliation',
  409:                               col2 => 'Availability/Processing of requests',},
  410:                              {col1 => 'Setting',
  411:                               col2 => 'Value'},
  412:                              {col1 => 'Available textbooks',
  413:                               col2 => ''},
  414:                              {col1 => 'Available templates',
  415:                               col2 => ''},
  416:                              {col1 => 'Validation (not official courses)',
  417:                               col2 => 'Value'},],
  418:                   print => \&print_quotas,
  419:                   modify => \&modify_quotas,
  420:                  },
  421:         'requestauthor' =>
  422:                  {text => 'Request Authoring Space',
  423:                   help => 'Domain_Configuration_Request_Author',
  424:                   header => [{col1 => 'User affiliation',
  425:                               col2 => 'Availability/Processing of requests',},
  426:                              {col1 => 'Setting',
  427:                               col2 => 'Value'}],
  428:                   print => \&print_quotas,
  429:                   modify => \&modify_quotas,
  430:                  },
  431:         'coursecategories' =>
  432:                   { text => 'Cataloging of courses/communities',
  433:                     help => 'Domain_Configuration_Cataloging_Courses',
  434:                     header => [{col1 => 'Catalog type/availability',
  435:                                 col2 => '',},
  436:                                {col1 => 'Category settings for standard catalog',
  437:                                 col2 => '',},
  438:                                {col1 => 'Categories',
  439:                                 col2 => '',
  440:                                }],
  441:                     print => \&print_coursecategories,
  442:                     modify => \&modify_coursecategories,
  443:                   },
  444:         'serverstatuses' =>
  445:                  {text   => 'Access to server status pages',
  446:                   help   => 'Domain_Configuration_Server_Status',
  447:                   header => [{col1 => 'Status Page',
  448:                               col2 => 'Other named users',
  449:                               col3 => 'Specific IPs',
  450:                             }],
  451:                   print => \&print_serverstatuses,
  452:                   modify => \&modify_serverstatuses,
  453:                  },
  454:         'helpsettings' =>
  455:                  {text   => 'Support settings',
  456:                   help   => 'Domain_Configuration_Help_Settings',
  457:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  458:                               col2 => 'Value'},
  459:                              {col1 => 'Helpdesk Roles',
  460:                               col2 => 'Settings'},],
  461:                   print  => \&print_helpsettings,
  462:                   modify => \&modify_helpsettings,
  463:                  },
  464:         'coursedefaults' => 
  465:                  {text => 'Course/Community defaults',
  466:                   help => 'Domain_Configuration_Course_Defaults',
  467:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  468:                               col2 => 'Value',},
  469:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  470:                               col2 => 'Value',},],
  471:                   print => \&print_coursedefaults,
  472:                   modify => \&modify_coursedefaults,
  473:                  },
  474:         'selfenrollment' => 
  475:                  {text   => 'Self-enrollment in Course/Community',
  476:                   help   => 'Domain_Configuration_Selfenrollment',
  477:                   header => [{col1 => 'Configuration Rights',
  478:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  479:                              {col1 => 'Defaults',
  480:                               col2 => 'Value'},
  481:                              {col1 => 'Self-enrollment validation (optional)',
  482:                               col2 => 'Value'},],
  483:                   print => \&print_selfenrollment,
  484:                   modify => \&modify_selfenrollment,
  485:                  },
  486:         'privacy' => 
  487:                  {text   => 'User Privacy',
  488:                   help   => 'Domain_Configuration_User_Privacy',
  489:                   header => [{col1 => 'Setting',
  490:                               col2 => 'Value',}],
  491:                   print => \&print_privacy,
  492:                   modify => \&modify_privacy,
  493:                  },
  494:         'usersessions' =>
  495:                  {text  => 'User session hosting/offloading',
  496:                   help  => 'Domain_Configuration_User_Sessions',
  497:                   header => [{col1 => 'Domain server',
  498:                               col2 => 'Servers to offload sessions to when busy'},
  499:                              {col1 => 'Hosting of users from other domains',
  500:                               col2 => 'Rules'},
  501:                              {col1 => "Hosting domain's own users elsewhere",
  502:                               col2 => 'Rules'}],
  503:                   print => \&print_usersessions,
  504:                   modify => \&modify_usersessions,
  505:                  },
  506:         'loadbalancing' =>
  507:                  {text  => 'Dedicated Load Balancer(s)',
  508:                   help  => 'Domain_Configuration_Load_Balancing',
  509:                   header => [{col1 => 'Balancers',
  510:                               col2 => 'Default destinations',
  511:                               col3 => 'User affiliation',
  512:                               col4 => 'Overrides'},
  513:                             ],
  514:                   print => \&print_loadbalancing,
  515:                   modify => \&modify_loadbalancing,
  516:                  },
  517:         'ltitools' => 
  518:                  {text => 'External Tools (LTI)',
  519:                   help => 'Domain_Configuration_LTI_Tools',
  520:                   header => [{col1 => 'Setting',
  521:                               col2 => 'Value',}],
  522:                   print => \&print_ltitools,
  523:                   modify => \&modify_ltitools,
  524:                  },
  525:         'ssl' =>
  526:                  {text  => 'LON-CAPA Network (SSL)',
  527:                   help  => 'Domain_Configuration_Network_SSL',
  528:                   header => [{col1 => 'Server',
  529:                               col2 => 'Certificate Status'},
  530:                              {col1 => 'Connections to other servers',
  531:                               col2 => 'Rules'},
  532:                              {col1 => 'Connections from other servers',
  533:                               col2 => 'Rules'},
  534:                              {col1 => "Replicating domain's published content",
  535:                               col2 => 'Rules'}],
  536:                   print => \&print_ssl,
  537:                   modify => \&modify_ssl,
  538:                  },
  539:         'trust' =>
  540:                  {text   => 'Trust Settings',
  541:                   help   => 'Domain_Configuration_Trust',
  542:                   header => [{col1 => "Access to this domain's content by others",
  543:                               col2 => 'Rules'},
  544:                              {col1 => "Access to other domain's content by this domain",
  545:                               col2 => 'Rules'},
  546:                              {col1 => "Enrollment in this domain's courses by others",
  547:                               col2 => 'Rules',},
  548:                              {col1 => "Co-author roles in this domain for others",
  549:                               col2 => 'Rules',},
  550:                              {col1 => "Co-author roles for this domain's users elsewhere",
  551:                               col2 => 'Rules',},
  552:                              {col1 => "Domain roles in this domain assignable to others",
  553:                               col2 => 'Rules'},
  554:                              {col1 => "Course catalog for this domain displayed elsewhere",
  555:                               col2 => 'Rules'},
  556:                              {col1 => "Requests for creation of courses in this domain by others",
  557:                               col2 => 'Rules'},
  558:                              {col1 => "Users in other domains can send messages to this domain",
  559:                               col2 => 'Rules'},],
  560:                   print => \&print_trust,
  561:                   modify => \&modify_trust,
  562:                  },
  563:           'lti' =>
  564:                  {text => 'LTI Provider',
  565:                   help => 'Domain_Configuration_LTI_Provider',
  566:                   header => [{col1 => 'Setting',
  567:                               col2 => 'Value',}],
  568:                   print => \&print_lti,
  569:                   modify => \&modify_lti,
  570:                  },
  571:     );
  572:     if (keys(%servers) > 1) {
  573:         $prefs{'login'}  = { text   => 'Log-in page options',
  574:                              help   => 'Domain_Configuration_Login_Page',
  575:                             header => [{col1 => 'Log-in Service',
  576:                                         col2 => 'Server Setting',},
  577:                                        {col1 => 'Log-in Page Items',
  578:                                         col2 => ''},
  579:                                        {col1 => 'Log-in Help',
  580:                                         col2 => 'Value'},
  581:                                        {col1 => 'Custom HTML in document head',
  582:                                         col2 => 'Value'}],
  583:                             print => \&print_login,
  584:                             modify => \&modify_login,
  585:                            };
  586:     }
  587: 
  588:     my @roles = ('student','coordinator','author','admin');
  589:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  590:     &Apache::lonhtmlcommon::add_breadcrumb
  591:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  592:       text=>"Settings to display/modify"});
  593:     my $confname = $dom.'-domainconfig';
  594: 
  595:     if ($phase eq 'process') {
  596:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  597:                                                               \%prefs,\%domconfig,$confname,\@roles);
  598:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  599:             $r->rflush();
  600:             &devalidate_remote_domconfs($dom,$result);
  601:         }
  602:     } elsif ($phase eq 'display') {
  603:         my $js = &recaptcha_js().
  604:                  &toggle_display_js();
  605:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  606:             my ($othertitle,$usertypes,$types) =
  607:                 &Apache::loncommon::sorted_inst_types($dom);
  608:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  609:                                           $domconfig{'loadbalancing'}).
  610:                    &new_spares_js().
  611:                    &common_domprefs_js().
  612:                    &Apache::loncommon::javascript_array_indexof();
  613:         }
  614:         if (grep(/^requestcourses$/,@actions)) {
  615:             my $javascript_validations;
  616:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  617:             $js .= <<END;
  618: <script type="text/javascript">
  619: $javascript_validations
  620: </script>
  621: $coursebrowserjs
  622: END
  623:         }
  624:         if (grep(/^selfcreation$/,@actions)) {
  625:             $js .= &selfcreate_javascript();
  626:         }
  627:         if (grep(/^contacts$/,@actions)) {
  628:             $js .= &contacts_javascript();
  629:         }
  630:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  631:     } else {
  632: # check if domconfig user exists for the domain.
  633:         my $servadm = $r->dir_config('lonAdmEMail');
  634:         my ($configuserok,$author_ok,$switchserver) =
  635:             &config_check($dom,$confname,$servadm);
  636:         unless ($configuserok eq 'ok') {
  637:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  638:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  639:                           $confname).
  640:                       '<br />'
  641:             );
  642:             if ($switchserver) {
  643:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  644:                           '<br />'.
  645:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  646:                           '<br />'.
  647:                           &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).
  648:                           '<br />'.
  649:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  650:                 );
  651:             } else {
  652:                 $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.').
  653:                           '<br />'.
  654:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  655:                 );
  656:             }
  657:             $r->print(&Apache::loncommon::end_page());
  658:             return OK;
  659:         }
  660:         if (keys(%domconfig) == 0) {
  661:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  662:             my @ids=&Apache::lonnet::current_machine_ids();
  663:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  664:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  665:                 my @loginimages = ('img','logo','domlogo','login');
  666:                 my $custom_img_count = 0;
  667:                 foreach my $img (@loginimages) {
  668:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  669:                         $custom_img_count ++;
  670:                     }
  671:                 }
  672:                 foreach my $role (@roles) {
  673:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  674:                         $custom_img_count ++;
  675:                     }
  676:                 }
  677:                 if ($custom_img_count > 0) {
  678:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  679:                     my $switch_server = &check_switchserver($dom,$confname);
  680:                     $r->print(
  681:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  682:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  683:     &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 />'.
  684:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  685:                     if ($switch_server) {
  686:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  687:                     }
  688:                     $r->print(&Apache::loncommon::end_page());
  689:                     return OK;
  690:                 }
  691:             }
  692:         }
  693:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  694:     }
  695:     return OK;
  696: }
  697: 
  698: sub process_changes {
  699:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  700:     my %domconfig;
  701:     if (ref($values) eq 'HASH') {
  702:         %domconfig = %{$values};
  703:     }
  704:     my $output;
  705:     if ($action eq 'login') {
  706:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  707:     } elsif ($action eq 'rolecolors') {
  708:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  709:                                      $lastactref,%domconfig);
  710:     } elsif ($action eq 'quotas') {
  711:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  712:     } elsif ($action eq 'autoenroll') {
  713:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  714:     } elsif ($action eq 'autoupdate') {
  715:         $output = &modify_autoupdate($dom,%domconfig);
  716:     } elsif ($action eq 'autocreate') {
  717:         $output = &modify_autocreate($dom,%domconfig);
  718:     } elsif ($action eq 'directorysrch') {
  719:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  720:     } elsif ($action eq 'usercreation') {
  721:         $output = &modify_usercreation($dom,%domconfig);
  722:     } elsif ($action eq 'selfcreation') {
  723:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  724:     } elsif ($action eq 'usermodification') {
  725:         $output = &modify_usermodification($dom,%domconfig);
  726:     } elsif ($action eq 'contacts') {
  727:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  728:     } elsif ($action eq 'defaults') {
  729:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  730:     } elsif ($action eq 'scantron') {
  731:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  732:     } elsif ($action eq 'coursecategories') {
  733:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  734:     } elsif ($action eq 'serverstatuses') {
  735:         $output = &modify_serverstatuses($dom,%domconfig);
  736:     } elsif ($action eq 'requestcourses') {
  737:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  738:     } elsif ($action eq 'requestauthor') {
  739:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  740:     } elsif ($action eq 'helpsettings') {
  741:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  742:     } elsif ($action eq 'coursedefaults') {
  743:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  744:     } elsif ($action eq 'selfenrollment') {
  745:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  746:     } elsif ($action eq 'usersessions') {
  747:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  748:     } elsif ($action eq 'loadbalancing') {
  749:         $output = &modify_loadbalancing($dom,%domconfig);
  750:     } elsif ($action eq 'ltitools') {
  751:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  752:     } elsif ($action eq 'ssl') {
  753:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  754:     } elsif ($action eq 'trust') {
  755:         $output = &modify_trust($dom,$lastactref,%domconfig);
  756:     } elsif ($action eq 'lti') {
  757:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  758:     }
  759:     return $output;
  760: }
  761: 
  762: sub print_config_box {
  763:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  764:     my $rowtotal = 0;
  765:     my $output;
  766:     if ($action eq 'coursecategories') {
  767:         $output = &coursecategories_javascript($settings);
  768:     } elsif ($action eq 'defaults') {
  769:         $output = &defaults_javascript($settings); 
  770:     } elsif ($action eq 'helpsettings') {
  771:         my (%privs,%levelscurrent);
  772:         my %full=();
  773:         my %levels=(
  774:                      course => {},
  775:                      domain => {},
  776:                      system => {},
  777:                    );
  778:         my $context = 'domain';
  779:         my $crstype = 'Course';
  780:         my $formname = 'display';
  781:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  782:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  783:         $output =
  784:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  785:                                                       \@templateroles);
  786:     } elsif ($action eq 'ltitools') {
  787:         $output .= &ltitools_javascript($settings);
  788:     } elsif ($action eq 'lti') {
  789:         $output .= &lti_javascript($settings);
  790:     }
  791:     $output .=
  792:          '<table class="LC_nested_outer">
  793:           <tr>
  794:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  795:            &mt($item->{text}).'&nbsp;'.
  796:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  797:           '</tr>';
  798:     $rowtotal ++;
  799:     my $numheaders = 1;
  800:     if (ref($item->{'header'}) eq 'ARRAY') {
  801:         $numheaders = scalar(@{$item->{'header'}});
  802:     }
  803:     if ($numheaders > 1) {
  804:         my $colspan = '';
  805:         my $rightcolspan = '';
  806:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  807:             ($action eq 'directorysrch') ||
  808:             (($action eq 'login') && ($numheaders < 4))) {
  809:             $colspan = ' colspan="2"';
  810:         }
  811:         if ($action eq 'usersessions') {
  812:             $rightcolspan = ' colspan="3"'; 
  813:         }
  814:         $output .= '
  815:           <tr>
  816:            <td>
  817:             <table class="LC_nested">
  818:              <tr class="LC_info_row">
  819:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  820:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  821:              </tr>';
  822:         $rowtotal ++;
  823:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  824:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  825:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  826:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  827:             ($action eq 'contacts')) {
  828:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  829:         } elsif ($action eq 'coursecategories') {
  830:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  831:         } elsif ($action eq 'login') {
  832:             if ($numheaders == 4) {
  833:                 $colspan = ' colspan="2"';
  834:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  835:             } else {
  836:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  837:             }
  838:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  839:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  840:         } elsif ($action eq 'rolecolors') {
  841:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  842:         }
  843:         $output .= '
  844:            </table>
  845:           </td>
  846:          </tr>
  847:          <tr>
  848:            <td>
  849:             <table class="LC_nested">
  850:              <tr class="LC_info_row">
  851:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  852:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  853:              </tr>';
  854:             $rowtotal ++;
  855:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  856:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  857:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  858:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults')) {
  859:             if ($action eq 'coursecategories') {
  860:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  861:                 $colspan = ' colspan="2"';
  862:             } elsif ($action eq 'trust') {
  863:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  864:             } else {
  865:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  866:             }
  867:             if ($action eq 'trust') {
  868:                 $output .= '
  869:             </table>
  870:           </td>
  871:          </tr>';
  872:                 my @trusthdrs = qw(2 3 4 5 6 7);
  873:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  874:                 for (my $i=0; $i<@trusthdrs; $i++) {
  875:                     $output .= '
  876:          <tr>
  877:            <td>
  878:             <table class="LC_nested">
  879:              <tr class="LC_info_row">
  880:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  881:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  882:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  883:             </table>
  884:           </td>
  885:          </tr>';
  886:                 }
  887:                 $output .= '
  888:          <tr>
  889:            <td>
  890:             <table class="LC_nested">
  891:              <tr class="LC_info_row">
  892:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  893:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  894:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  895:             } else {
  896:                 $output .= '
  897:            </table>
  898:           </td>
  899:          </tr>
  900:          <tr>
  901:            <td>
  902:             <table class="LC_nested">
  903:              <tr class="LC_info_row">
  904:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  905:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  906:              </tr>'."\n";
  907:                 if ($action eq 'coursecategories') {
  908:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  909:                 } elsif ($action eq 'contacts') {
  910:                     $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal).'
  911:              </tr>
  912:             </table>
  913:            </td>
  914:           </tr>
  915:           <tr>
  916:            <td>
  917:             <table class="LC_nested">
  918:              <tr class="LC_info_row">
  919:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  920:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  921:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal).'
  922:             </table>
  923:           </td>
  924:          </tr>
  925:          <tr>';
  926:                 } else {
  927:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  928:                 }
  929:             }
  930:             $rowtotal ++;
  931:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  932:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  933:                  ($action eq 'helpsettings')) {
  934:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  935:         } elsif ($action eq 'ssl') {
  936:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  937:             </table>
  938:           </td>
  939:          </tr>
  940:          <tr>
  941:            <td>
  942:             <table class="LC_nested">
  943:              <tr class="LC_info_row">
  944:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  945:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  946:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  947:             </table>
  948:           </td>
  949:          </tr>
  950:          <tr>
  951:            <td>
  952:             <table class="LC_nested">
  953:              <tr class="LC_info_row">
  954:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  955:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  956:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  957:         } elsif ($action eq 'login') {
  958:             if ($numheaders == 4) {
  959:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  960:            </table>
  961:           </td>
  962:          </tr>
  963:          <tr>
  964:            <td>
  965:             <table class="LC_nested">
  966:              <tr class="LC_info_row">
  967:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  968:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  969:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  970:                 $rowtotal ++;
  971:             } else {
  972:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  973:             }
  974:             $output .= '
  975:            </table>
  976:           </td>
  977:          </tr>
  978:          <tr>
  979:            <td>
  980:             <table class="LC_nested">
  981:              <tr class="LC_info_row">';
  982:             if ($numheaders == 4) {
  983:                 $output .= '
  984:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  985:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  986:              </tr>';
  987:             } else {
  988:                 $output .= '
  989:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  990:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  991:              </tr>';
  992:             }
  993:             $rowtotal ++;
  994:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  995:         } elsif ($action eq 'requestcourses') {
  996:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  997:             $rowtotal ++;
  998:             $output .= &print_studentcode($settings,\$rowtotal).'
  999:            </table>
 1000:           </td>
 1001:          </tr>
 1002:          <tr>
 1003:            <td>
 1004:             <table class="LC_nested">
 1005:              <tr class="LC_info_row">
 1006:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1007:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1008:                        &textbookcourses_javascript($settings).
 1009:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1010:             </table>
 1011:            </td>
 1012:           </tr>
 1013:          <tr>
 1014:            <td>
 1015:             <table class="LC_nested">
 1016:              <tr class="LC_info_row">
 1017:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1018:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1019:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1020:             </table>
 1021:            </td>
 1022:           </tr>
 1023:           <tr>
 1024:            <td>
 1025:             <table class="LC_nested">
 1026:              <tr class="LC_info_row">
 1027:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1028:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1029:              </tr>'.
 1030:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1031:         } elsif ($action eq 'requestauthor') {
 1032:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1033:             $rowtotal ++;
 1034:         } elsif ($action eq 'rolecolors') {
 1035:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1036:            </table>
 1037:           </td>
 1038:          </tr>
 1039:          <tr>
 1040:            <td>
 1041:             <table class="LC_nested">
 1042:              <tr class="LC_info_row">
 1043:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1044:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1045:               <td class="LC_right_item" style="vertical-align: top">'.
 1046:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1047:              </tr>'.
 1048:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1049:            </table>
 1050:           </td>
 1051:          </tr>
 1052:          <tr>
 1053:            <td>
 1054:             <table class="LC_nested">
 1055:              <tr class="LC_info_row">
 1056:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1057:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1058:              </tr>'.
 1059:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1060:             $rowtotal += 2;
 1061:         }
 1062:     } else {
 1063:         $output .= '
 1064:           <tr>
 1065:            <td>
 1066:             <table class="LC_nested">
 1067:              <tr class="LC_info_row">';
 1068:         if ($action eq 'login') {
 1069:             $output .= '  
 1070:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1071:         } elsif ($action eq 'serverstatuses') {
 1072:             $output .= '
 1073:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1074:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1075: 
 1076:         } else {
 1077:             $output .= '
 1078:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1079:         }
 1080:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1081:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1082:                        &mt($item->{'header'}->[0]->{'col2'});
 1083:             if ($action eq 'serverstatuses') {
 1084:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1085:             } 
 1086:         } else {
 1087:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1088:                        &mt($item->{'header'}->[0]->{'col2'});
 1089:         }
 1090:         $output .= '</td>';
 1091:         if ($item->{'header'}->[0]->{'col3'}) {
 1092:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1093:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1094:                             &mt($item->{'header'}->[0]->{'col3'});
 1095:             } else {
 1096:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1097:                            &mt($item->{'header'}->[0]->{'col3'});
 1098:             }
 1099:             if ($action eq 'serverstatuses') {
 1100:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1101:             }
 1102:             $output .= '</td>';
 1103:         }
 1104:         if ($item->{'header'}->[0]->{'col4'}) {
 1105:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1106:                        &mt($item->{'header'}->[0]->{'col4'});
 1107:         }
 1108:         $output .= '</tr>';
 1109:         $rowtotal ++;
 1110:         if ($action eq 'quotas') {
 1111:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1112:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1113:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1114:                  ($action eq 'ltitools') || ($action eq 'lti')) {
 1115:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1116:         } elsif ($action eq 'scantron') {
 1117:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1118:         }
 1119:     }
 1120:     $output .= '
 1121:    </table>
 1122:   </td>
 1123:  </tr>
 1124: </table><br />';
 1125:     return ($output,$rowtotal);
 1126: }
 1127: 
 1128: sub print_login {
 1129:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1130:     my ($css_class,$datatable);
 1131:     my %choices = &login_choices();
 1132: 
 1133:     if ($caller eq 'service') {
 1134:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1135:         my $choice = $choices{'disallowlogin'};
 1136:         $css_class = ' class="LC_odd_row"';
 1137:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1138:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1139:                       '<th>'.$choices{'server'}.'</th>'.
 1140:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1141:                       '<th>'.$choices{'custompath'}.'</th>'.
 1142:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1143:         my %disallowed;
 1144:         if (ref($settings) eq 'HASH') {
 1145:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1146:                %disallowed = %{$settings->{'loginvia'}};
 1147:             }
 1148:         }
 1149:         foreach my $lonhost (sort(keys(%servers))) {
 1150:             my $direct = 'selected="selected"';
 1151:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1152:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1153:                     $direct = '';
 1154:                 }
 1155:             }
 1156:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1157:                           '<td><select name="'.$lonhost.'_server">'.
 1158:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1159:                           '</option>';
 1160:             foreach my $hostid (sort(keys(%servers))) {
 1161:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1162:                 my $selected = '';
 1163:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1164:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1165:                         $selected = 'selected="selected"';
 1166:                     }
 1167:                 }
 1168:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1169:                               $servers{$hostid}.'</option>';
 1170:             }
 1171:             $datatable .= '</select></td>'.
 1172:                           '<td><select name="'.$lonhost.'_serverpath">';
 1173:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1174:                 my $pathname = $path;
 1175:                 if ($path eq 'custom') {
 1176:                     $pathname = &mt('Custom Path').' ->';
 1177:                 }
 1178:                 my $selected = '';
 1179:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1180:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1181:                         $selected = 'selected="selected"';
 1182:                     }
 1183:                 } elsif ($path eq '') {
 1184:                     $selected = 'selected="selected"';
 1185:                 }
 1186:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1187:             }
 1188:             $datatable .= '</select></td>';
 1189:             my ($custom,$exempt);
 1190:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1191:                 $custom = $disallowed{$lonhost}{'custompath'};
 1192:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1193:             }
 1194:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1195:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1196:                           '</tr>';
 1197:         }
 1198:         $datatable .= '</table></td></tr>';
 1199:         return $datatable;
 1200:     } elsif ($caller eq 'page') {
 1201:         my %defaultchecked = ( 
 1202:                                'coursecatalog' => 'on',
 1203:                                'helpdesk'      => 'on',
 1204:                                'adminmail'     => 'off',
 1205:                                'newuser'       => 'off',
 1206:                              );
 1207:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1208:         my (%checkedon,%checkedoff);
 1209:         foreach my $item (@toggles) {
 1210:             if ($defaultchecked{$item} eq 'on') { 
 1211:                 $checkedon{$item} = ' checked="checked" ';
 1212:                 $checkedoff{$item} = ' ';
 1213:             } elsif ($defaultchecked{$item} eq 'off') {
 1214:                 $checkedoff{$item} = ' checked="checked" ';
 1215:                 $checkedon{$item} = ' ';
 1216:             }
 1217:         }
 1218:         my @images = ('img','logo','domlogo','login');
 1219:         my @logintext = ('textcol','bgcol');
 1220:         my @bgs = ('pgbg','mainbg','sidebg');
 1221:         my @links = ('link','alink','vlink');
 1222:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1223:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1224:         my (%is_custom,%designs);
 1225:         my %defaults = (
 1226:                        font => $defaultdesign{'login.font'},
 1227:                        );
 1228:         foreach my $item (@images) {
 1229:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1230:             $defaults{'showlogo'}{$item} = 1;
 1231:         }
 1232:         foreach my $item (@bgs) {
 1233:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1234:         }
 1235:         foreach my $item (@logintext) {
 1236:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1237:         }
 1238:         foreach my $item (@links) {
 1239:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1240:         }
 1241:         if (ref($settings) eq 'HASH') {
 1242:             foreach my $item (@toggles) {
 1243:                 if ($settings->{$item} eq '1') {
 1244:                     $checkedon{$item} =  ' checked="checked" ';
 1245:                     $checkedoff{$item} = ' ';
 1246:                 } elsif ($settings->{$item} eq '0') {
 1247:                     $checkedoff{$item} =  ' checked="checked" ';
 1248:                     $checkedon{$item} = ' ';
 1249:                 }
 1250:             }
 1251:             foreach my $item (@images) {
 1252:                 if (defined($settings->{$item})) {
 1253:                     $designs{$item} = $settings->{$item};
 1254:                     $is_custom{$item} = 1;
 1255:                 }
 1256:                 if (defined($settings->{'showlogo'}{$item})) {
 1257:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1258:                 }
 1259:             }
 1260:             foreach my $item (@logintext) {
 1261:                 if ($settings->{$item} ne '') {
 1262:                     $designs{'logintext'}{$item} = $settings->{$item};
 1263:                     $is_custom{$item} = 1;
 1264:                 }
 1265:             }
 1266:             if ($settings->{'font'} ne '') {
 1267:                 $designs{'font'} = $settings->{'font'};
 1268:                 $is_custom{'font'} = 1;
 1269:             }
 1270:             foreach my $item (@bgs) {
 1271:                 if ($settings->{$item} ne '') {
 1272:                     $designs{'bgs'}{$item} = $settings->{$item};
 1273:                     $is_custom{$item} = 1;
 1274:                 }
 1275:             }
 1276:             foreach my $item (@links) {
 1277:                 if ($settings->{$item} ne '') {
 1278:                     $designs{'links'}{$item} = $settings->{$item};
 1279:                     $is_custom{$item} = 1;
 1280:                 }
 1281:             }
 1282:         } else {
 1283:             if ($designhash{$dom.'.login.font'} ne '') {
 1284:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1285:                 $is_custom{'font'} = 1;
 1286:             }
 1287:             foreach my $item (@images) {
 1288:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1289:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1290:                     $is_custom{$item} = 1;
 1291:                 }
 1292:             }
 1293:             foreach my $item (@bgs) {
 1294:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1295:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1296:                     $is_custom{$item} = 1;
 1297:                 }
 1298:             }
 1299:             foreach my $item (@links) {
 1300:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1301:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1302:                     $is_custom{$item} = 1;
 1303:                 }
 1304:             }
 1305:         }
 1306:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1307:                                                       logo => 'Institution Logo',
 1308:                                                       domlogo => 'Domain Logo',
 1309:                                                       login => 'Login box');
 1310:         my $itemcount = 1;
 1311:         foreach my $item (@toggles) {
 1312:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1313:             $datatable .=  
 1314:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1315:                 '</td><td>'.
 1316:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1317:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1318:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1319:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1320:                 '</tr>';
 1321:             $itemcount ++;
 1322:         }
 1323:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1324:         $datatable .= '</tr></table></td></tr>';
 1325:     } elsif ($caller eq 'help') {
 1326:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1327:         my $switchserver = &check_switchserver($dom,$confname);
 1328:         my $itemcount = 1;
 1329:         $defaulturl = '/adm/loginproblems.html';
 1330:         $defaulttype = 'default';
 1331:         %lt = &Apache::lonlocal::texthash (
 1332:                      del     => 'Delete?',
 1333:                      rep     => 'Replace:',
 1334:                      upl     => 'Upload:',
 1335:                      default => 'Default',
 1336:                      custom  => 'Custom',
 1337:                                              );
 1338:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1339:         my @currlangs;
 1340:         if (ref($settings) eq 'HASH') {
 1341:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1342:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1343:                     next if ($settings->{'helpurl'}{$key} eq '');
 1344:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1345:                     $type{$key} = 'custom';
 1346:                     unless ($key eq 'nolang') {
 1347:                         push(@currlangs,$key);
 1348:                     }
 1349:                 }
 1350:             } elsif ($settings->{'helpurl'} ne '') {
 1351:                 $type{'nolang'} = 'custom';
 1352:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1353:             }
 1354:         }
 1355:         foreach my $lang ('nolang',sort(@currlangs)) {
 1356:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1357:             $datatable .= '<tr'.$css_class.'>';
 1358:             if ($url{$lang} eq '') {
 1359:                 $url{$lang} = $defaulturl;
 1360:             }
 1361:             if ($type{$lang} eq '') {
 1362:                 $type{$lang} = $defaulttype;
 1363:             }
 1364:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1365:             if ($lang eq 'nolang') {
 1366:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1367:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1368:             } else {
 1369:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1370:                                   $langchoices{$lang},
 1371:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1372:             }
 1373:             $datatable .= '</span></td>'."\n".
 1374:                           '<td class="LC_left_item">';
 1375:             if ($type{$lang} eq 'custom') {
 1376:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1377:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1378:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1379:             } else {
 1380:                 $datatable .= $lt{'upl'};
 1381:             }
 1382:             $datatable .='<br />';
 1383:             if ($switchserver) {
 1384:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1385:             } else {
 1386:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1387:             }
 1388:             $datatable .= '</td></tr>';
 1389:             $itemcount ++;
 1390:         }
 1391:         my @addlangs;
 1392:         foreach my $lang (sort(keys(%langchoices))) {
 1393:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1394:             push(@addlangs,$lang);
 1395:         }
 1396:         if (@addlangs > 0) {
 1397:             my %toadd;
 1398:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1399:             $toadd{''} = &mt('Select');
 1400:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1401:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1402:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1403:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1404:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1405:             if ($switchserver) {
 1406:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1407:             } else {
 1408:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1409:             }
 1410:             $datatable .= '</td></tr>';
 1411:             $itemcount ++;
 1412:         }
 1413:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1414:     } elsif ($caller eq 'headtag') {
 1415:         my %domservers = &Apache::lonnet::get_servers($dom);
 1416:         my $choice = $choices{'headtag'};
 1417:         $css_class = ' class="LC_odd_row"';
 1418:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1419:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1420:                       '<th>'.$choices{'current'}.'</th>'.
 1421:                       '<th>'.$choices{'action'}.'</th>'.
 1422:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1423:         my (%currurls,%currexempt);
 1424:         if (ref($settings) eq 'HASH') {
 1425:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1426:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1427:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1428:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1429:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1430:                     }
 1431:                 }
 1432:             }
 1433:         }
 1434:         my %lt = &Apache::lonlocal::texthash(
 1435:                                                del  => 'Delete?',
 1436:                                                rep  => 'Replace:',
 1437:                                                upl  => 'Upload:',
 1438:                                                curr => 'View contents',
 1439:                                                none => 'None',
 1440:         );
 1441:         my $switchserver = &check_switchserver($dom,$confname);
 1442:         foreach my $lonhost (sort(keys(%domservers))) {
 1443:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1444:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1445:             if ($currurls{$lonhost}) {
 1446:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1447:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1448:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1449:                               '">'.$lt{'curr'}.'</a></td>'.
 1450:                               '<td><span class="LC_nobreak"><label>'.
 1451:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1452:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1453:             } else {
 1454:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1455:             }
 1456:             $datatable .='<br />';
 1457:             if ($switchserver) {
 1458:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1459:             } else {
 1460:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1461:             }
 1462:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1463:         }
 1464:         $datatable .= '</table></td></tr>';
 1465:     }
 1466:     return $datatable;
 1467: }
 1468: 
 1469: sub login_choices {
 1470:     my %choices =
 1471:         &Apache::lonlocal::texthash (
 1472:             coursecatalog => 'Display Course/Community Catalog link?',
 1473:             adminmail     => "Display Administrator's E-mail Address?",
 1474:             helpdesk      => 'Display "Contact Helpdesk" link',
 1475:             disallowlogin => "Login page requests redirected",
 1476:             hostid        => "Server",
 1477:             server        => "Redirect to:",
 1478:             serverpath    => "Path",
 1479:             custompath    => "Custom", 
 1480:             exempt        => "Exempt IP(s)",
 1481:             directlogin   => "No redirect",
 1482:             newuser       => "Link to create a user account",
 1483:             img           => "Header",
 1484:             logo          => "Main Logo",
 1485:             domlogo       => "Domain Logo",
 1486:             login         => "Log-in Header", 
 1487:             textcol       => "Text color",
 1488:             bgcol         => "Box color",
 1489:             bgs           => "Background colors",
 1490:             links         => "Link colors",
 1491:             font          => "Font color",
 1492:             pgbg          => "Header",
 1493:             mainbg        => "Page",
 1494:             sidebg        => "Login box",
 1495:             link          => "Link",
 1496:             alink         => "Active link",
 1497:             vlink         => "Visited link",
 1498:             headtag       => "Custom markup",
 1499:             action        => "Action",
 1500:             current       => "Current",
 1501:         );
 1502:     return %choices;
 1503: }
 1504: 
 1505: sub print_rolecolors {
 1506:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1507:     my %choices = &color_font_choices();
 1508:     my @bgs = ('pgbg','tabbg','sidebg');
 1509:     my @links = ('link','alink','vlink');
 1510:     my @images = ('img');
 1511:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1512:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1513:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1514:     my (%is_custom,%designs);
 1515:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1516:     if (ref($settings) eq 'HASH') {
 1517:         if (ref($settings->{$role}) eq 'HASH') {
 1518:             if ($settings->{$role}->{'img'} ne '') {
 1519:                 $designs{'img'} = $settings->{$role}->{'img'};
 1520:                 $is_custom{'img'} = 1;
 1521:             }
 1522:             if ($settings->{$role}->{'font'} ne '') {
 1523:                 $designs{'font'} = $settings->{$role}->{'font'};
 1524:                 $is_custom{'font'} = 1;
 1525:             }
 1526:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1527:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1528:                 $is_custom{'fontmenu'} = 1;
 1529:             }
 1530:             foreach my $item (@bgs) {
 1531:                 if ($settings->{$role}->{$item} ne '') {
 1532:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1533:                     $is_custom{$item} = 1;
 1534:                 }
 1535:             }
 1536:             foreach my $item (@links) {
 1537:                 if ($settings->{$role}->{$item} ne '') {
 1538:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1539:                     $is_custom{$item} = 1;
 1540:                 }
 1541:             }
 1542:         }
 1543:     } else {
 1544:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1545:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1546:             $is_custom{'img'} = 1;
 1547:         }
 1548:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1549:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1550:             $is_custom{'fontmenu'} = 1; 
 1551:         }
 1552:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1553:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1554:             $is_custom{'font'} = 1;
 1555:         }
 1556:         foreach my $item (@bgs) {
 1557:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1558:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1559:                 $is_custom{$item} = 1;
 1560:             
 1561:             }
 1562:         }
 1563:         foreach my $item (@links) {
 1564:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1565:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1566:                 $is_custom{$item} = 1;
 1567:             }
 1568:         }
 1569:     }
 1570:     my $itemcount = 1;
 1571:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1572:     $datatable .= '</tr></table></td></tr>';
 1573:     return $datatable;
 1574: }
 1575: 
 1576: sub role_defaults {
 1577:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1578:     my %defaults;
 1579:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1580:         return %defaults;
 1581:     }
 1582:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1583:     if ($role eq 'login') {
 1584:         %defaults = (
 1585:                        font => $defaultdesign{$role.'.font'},
 1586:                     );
 1587:         if (ref($logintext) eq 'ARRAY') {
 1588:             foreach my $item (@{$logintext}) {
 1589:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1590:             }
 1591:         }
 1592:         foreach my $item (@{$images}) {
 1593:             $defaults{'showlogo'}{$item} = 1;
 1594:         }
 1595:     } else {
 1596:         %defaults = (
 1597:                        img => $defaultdesign{$role.'.img'},
 1598:                        font => $defaultdesign{$role.'.font'},
 1599:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1600:                     );
 1601:     }
 1602:     foreach my $item (@{$bgs}) {
 1603:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1604:     }
 1605:     foreach my $item (@{$links}) {
 1606:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1607:     }
 1608:     foreach my $item (@{$images}) {
 1609:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1610:     }
 1611:     return %defaults;
 1612: }
 1613: 
 1614: sub display_color_options {
 1615:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1616:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1617:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1618:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1619:     my $datatable = '<tr'.$css_class.'>'.
 1620:         '<td>'.$choices->{'font'}.'</td>';
 1621:     if (!$is_custom->{'font'}) {
 1622:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1623:     } else {
 1624:         $datatable .= '<td>&nbsp;</td>';
 1625:     }
 1626:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1627: 
 1628:     $datatable .= '<td><span class="LC_nobreak">'.
 1629:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1630:                   ' value="'.$current_color.'" />&nbsp;'.
 1631:                   '&nbsp;</span></td></tr>';
 1632:     unless ($role eq 'login') { 
 1633:         $datatable .= '<tr'.$css_class.'>'.
 1634:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1635:         if (!$is_custom->{'fontmenu'}) {
 1636:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1637:         } else {
 1638:             $datatable .= '<td>&nbsp;</td>';
 1639:         }
 1640: 	$current_color = $designs->{'fontmenu'} ?
 1641: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1642:         $datatable .= '<td><span class="LC_nobreak">'.
 1643:                       '<input class="colorchooser" type="text" size="10" name="'
 1644: 		      .$role.'_fontmenu"'.
 1645:                       ' value="'.$current_color.'" />&nbsp;'.
 1646:                       '&nbsp;</span></td></tr>';
 1647:     }
 1648:     my $switchserver = &check_switchserver($dom,$confname);
 1649:     foreach my $img (@{$images}) {
 1650: 	$itemcount ++;
 1651:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1652:         $datatable .= '<tr'.$css_class.'>'.
 1653:                       '<td>'.$choices->{$img};
 1654:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1655:         if ($role eq 'login') {
 1656:             if ($img eq 'login') {
 1657:                 $login_hdr_pick =
 1658:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1659:                 $logincolors =
 1660:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1661:                                        $designs,$defaults);
 1662:             } elsif ($img ne 'domlogo') {
 1663:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1664:             }
 1665:         }
 1666:         $datatable .= '</td>';
 1667:         if ($designs->{$img} ne '') {
 1668:             $imgfile = $designs->{$img};
 1669: 	    $img_import = ($imgfile =~ m{^/adm/});
 1670:         } else {
 1671:             $imgfile = $defaults->{$img};
 1672:         }
 1673:         if ($imgfile) {
 1674:             my ($showfile,$fullsize);
 1675:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1676:                 my $urldir = $1;
 1677:                 my $filename = $2;
 1678:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1679:                 if (@info) {
 1680:                     my $thumbfile = 'tn-'.$filename;
 1681:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1682:                     if (@thumb) {
 1683:                         $showfile = $urldir.'/'.$thumbfile;
 1684:                     } else {
 1685:                         $showfile = $imgfile;
 1686:                     }
 1687:                 } else {
 1688:                     $showfile = '';
 1689:                 }
 1690:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1691:                 $showfile = $imgfile;
 1692:                 my $imgdir = $1;
 1693:                 my $filename = $2;
 1694:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1695:                     $showfile = "/$imgdir/tn-".$filename;
 1696:                 } else {
 1697:                     my $input = $londocroot.$imgfile;
 1698:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1699:                     if (!-e $output) {
 1700:                         my ($width,$height) = &thumb_dimensions();
 1701:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1702:                         if ($fullwidth ne '' && $fullheight ne '') {
 1703:                             if ($fullwidth > $width && $fullheight > $height) { 
 1704:                                 my $size = $width.'x'.$height;
 1705:                                 my @args = ('convert','-sample',$size,$input,$output);
 1706:                                 system({$args[0]} @args);
 1707:                                 $showfile = "/$imgdir/tn-".$filename;
 1708:                             }
 1709:                         }
 1710:                     }
 1711:                 }
 1712:             }
 1713:             if ($showfile) {
 1714:                 if ($showfile =~ m{^/(adm|res)/}) {
 1715:                     if ($showfile =~ m{^/res/}) {
 1716:                         my $local_showfile =
 1717:                             &Apache::lonnet::filelocation('',$showfile);
 1718:                         &Apache::lonnet::repcopy($local_showfile);
 1719:                     }
 1720:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1721:                 }
 1722:                 if ($imgfile) {
 1723:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1724:                         if ($imgfile =~ m{^/res/}) {
 1725:                             my $local_imgfile =
 1726:                                 &Apache::lonnet::filelocation('',$imgfile);
 1727:                             &Apache::lonnet::repcopy($local_imgfile);
 1728:                         }
 1729:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1730:                     } else {
 1731:                         $fullsize = $imgfile;
 1732:                     }
 1733:                 }
 1734:                 $datatable .= '<td>';
 1735:                 if ($img eq 'login') {
 1736:                     $datatable .= $login_hdr_pick;
 1737:                 } 
 1738:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1739:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1740:             } else {
 1741:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1742:                               &mt('Upload:').'<br />';
 1743:             }
 1744:         } else {
 1745:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1746:                           &mt('Upload:').'<br />';
 1747:         }
 1748:         if ($switchserver) {
 1749:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1750:         } else {
 1751:             if ($img ne 'login') { # suppress file selection for Log-in header
 1752:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1753:             }
 1754:         }
 1755:         $datatable .= '</td></tr>';
 1756:     }
 1757:     $itemcount ++;
 1758:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1759:     $datatable .= '<tr'.$css_class.'>'.
 1760:                   '<td>'.$choices->{'bgs'}.'</td>';
 1761:     my $bgs_def;
 1762:     foreach my $item (@{$bgs}) {
 1763:         if (!$is_custom->{$item}) {
 1764:             $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>';
 1765:         }
 1766:     }
 1767:     if ($bgs_def) {
 1768:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1769:     } else {
 1770:         $datatable .= '<td>&nbsp;</td>';
 1771:     }
 1772:     $datatable .= '<td class="LC_right_item">'.
 1773:                   '<table border="0"><tr>';
 1774: 
 1775:     foreach my $item (@{$bgs}) {
 1776:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1777: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1778:         if ($designs->{'bgs'}{$item}) {
 1779:             $datatable .= '&nbsp;';
 1780:         }
 1781:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1782:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1783:     }
 1784:     $datatable .= '</tr></table></td></tr>';
 1785:     $itemcount ++;
 1786:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1787:     $datatable .= '<tr'.$css_class.'>'.
 1788:                   '<td>'.$choices->{'links'}.'</td>';
 1789:     my $links_def;
 1790:     foreach my $item (@{$links}) {
 1791:         if (!$is_custom->{$item}) {
 1792:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1793:         }
 1794:     }
 1795:     if ($links_def) {
 1796:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1797:     } else {
 1798:         $datatable .= '<td>&nbsp;</td>';
 1799:     }
 1800:     $datatable .= '<td class="LC_right_item">'.
 1801:                   '<table border="0"><tr>';
 1802:     foreach my $item (@{$links}) {
 1803: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1804:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1805:         if ($designs->{'links'}{$item}) {
 1806:             $datatable.='&nbsp;';
 1807:         }
 1808:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1809:                       '" /></td>';
 1810:     }
 1811:     $$rowtotal += $itemcount;
 1812:     return $datatable;
 1813: }
 1814: 
 1815: sub logo_display_options {
 1816:     my ($img,$defaults,$designs) = @_;
 1817:     my $checkedon;
 1818:     if (ref($defaults) eq 'HASH') {
 1819:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1820:             if ($defaults->{'showlogo'}{$img}) {
 1821:                 $checkedon = 'checked="checked" ';     
 1822:             }
 1823:         } 
 1824:     }
 1825:     if (ref($designs) eq 'HASH') {
 1826:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1827:             if (defined($designs->{'showlogo'}{$img})) {
 1828:                 if ($designs->{'showlogo'}{$img} == 0) {
 1829:                     $checkedon = '';
 1830:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1831:                     $checkedon = 'checked="checked" ';
 1832:                 }
 1833:             }
 1834:         }
 1835:     }
 1836:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1837:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1838:            &mt('show').'</label>'."\n";
 1839: }
 1840: 
 1841: sub login_header_options  {
 1842:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1843:     my $output = '';
 1844:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1845:         $output .= &mt('Text default(s):').'<br />';
 1846:         if (!$is_custom->{'textcol'}) {
 1847:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1848:                        '&nbsp;&nbsp;&nbsp;';
 1849:         }
 1850:         if (!$is_custom->{'bgcol'}) {
 1851:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1852:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1853:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1854:         }
 1855:         $output .= '<br />';
 1856:     }
 1857:     $output .='<br />';
 1858:     return $output;
 1859: }
 1860: 
 1861: sub login_text_colors {
 1862:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1863:     my $color_menu = '<table border="0"><tr>';
 1864:     foreach my $item (@{$logintext}) {
 1865:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1866:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1867:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1868:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1869:     }
 1870:     $color_menu .= '</tr></table><br />';
 1871:     return $color_menu;
 1872: }
 1873: 
 1874: sub image_changes {
 1875:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1876:     my $output;
 1877:     if ($img eq 'login') {
 1878:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1879:     } elsif (!$is_custom) {
 1880:         if ($img ne 'domlogo') {
 1881:             $output = &mt('Default image:').'<br />';
 1882:         } else {
 1883:             $output = &mt('Default in use:').'<br />';
 1884:         }
 1885:     }
 1886:     if ($img ne 'login') {
 1887:         if ($img_import) {
 1888:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1889:         }
 1890:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1891:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1892:         if ($is_custom) {
 1893:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1894:                        '<input type="checkbox" name="'.
 1895:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1896:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1897:         } else {
 1898:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1899:         }
 1900:     }
 1901:     return $output;
 1902: }
 1903: 
 1904: sub print_quotas {
 1905:     my ($dom,$settings,$rowtotal,$action) = @_;
 1906:     my $context;
 1907:     if ($action eq 'quotas') {
 1908:         $context = 'tools';
 1909:     } else {
 1910:         $context = $action;
 1911:     }
 1912:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1913:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1914:     my $typecount = 0;
 1915:     my ($css_class,%titles);
 1916:     if ($context eq 'requestcourses') {
 1917:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 1918:         @options =('norequest','approval','validate','autolimit');
 1919:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1920:         %titles = &courserequest_titles();
 1921:     } elsif ($context eq 'requestauthor') {
 1922:         @usertools = ('author');
 1923:         @options = ('norequest','approval','automatic');
 1924:         %titles = &authorrequest_titles();
 1925:     } else {
 1926:         @usertools = ('aboutme','blog','webdav','portfolio');
 1927:         %titles = &tool_titles();
 1928:     }
 1929:     if (ref($types) eq 'ARRAY') {
 1930:         foreach my $type (@{$types}) {
 1931:             my ($currdefquota,$currauthorquota);
 1932:             unless (($context eq 'requestcourses') ||
 1933:                     ($context eq 'requestauthor')) {
 1934:                 if (ref($settings) eq 'HASH') {
 1935:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1936:                         $currdefquota = $settings->{defaultquota}->{$type};
 1937:                     } else {
 1938:                         $currdefquota = $settings->{$type};
 1939:                     }
 1940:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1941:                         $currauthorquota = $settings->{authorquota}->{$type};
 1942:                     }
 1943:                 }
 1944:             }
 1945:             if (defined($usertypes->{$type})) {
 1946:                 $typecount ++;
 1947:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1948:                 $datatable .= '<tr'.$css_class.'>'.
 1949:                               '<td>'.$usertypes->{$type}.'</td>'.
 1950:                               '<td class="LC_left_item">';
 1951:                 if ($context eq 'requestcourses') {
 1952:                     $datatable .= '<table><tr>';
 1953:                 }
 1954:                 my %cell;  
 1955:                 foreach my $item (@usertools) {
 1956:                     if ($context eq 'requestcourses') {
 1957:                         my ($curroption,$currlimit);
 1958:                         if (ref($settings) eq 'HASH') {
 1959:                             if (ref($settings->{$item}) eq 'HASH') {
 1960:                                 $curroption = $settings->{$item}->{$type};
 1961:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1962:                                     $currlimit = $1; 
 1963:                                 }
 1964:                             }
 1965:                         }
 1966:                         if (!$curroption) {
 1967:                             $curroption = 'norequest';
 1968:                         }
 1969:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1970:                         foreach my $option (@options) {
 1971:                             my $val = $option;
 1972:                             if ($option eq 'norequest') {
 1973:                                 $val = 0;  
 1974:                             }
 1975:                             if ($option eq 'validate') {
 1976:                                 my $canvalidate = 0;
 1977:                                 if (ref($validations{$item}) eq 'HASH') { 
 1978:                                     if ($validations{$item}{$type}) {
 1979:                                         $canvalidate = 1;
 1980:                                     }
 1981:                                 }
 1982:                                 next if (!$canvalidate);
 1983:                             }
 1984:                             my $checked = '';
 1985:                             if ($option eq $curroption) {
 1986:                                 $checked = ' checked="checked"';
 1987:                             } elsif ($option eq 'autolimit') {
 1988:                                 if ($curroption =~ /^autolimit/) {
 1989:                                     $checked = ' checked="checked"';
 1990:                                 }                       
 1991:                             } 
 1992:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1993:                                   '<input type="radio" name="crsreq_'.$item.
 1994:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1995:                                   $titles{$option}.'</label>';
 1996:                             if ($option eq 'autolimit') {
 1997:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1998:                                                 $item.'_limit_'.$type.'" size="1" '.
 1999:                                                 'value="'.$currlimit.'" />';
 2000:                             }
 2001:                             $cell{$item} .= '</span> ';
 2002:                             if ($option eq 'autolimit') {
 2003:                                 $cell{$item} .= $titles{'unlimited'};
 2004:                             }
 2005:                         }
 2006:                     } elsif ($context eq 'requestauthor') {
 2007:                         my $curroption;
 2008:                         if (ref($settings) eq 'HASH') {
 2009:                             $curroption = $settings->{$type};
 2010:                         }
 2011:                         if (!$curroption) {
 2012:                             $curroption = 'norequest';
 2013:                         }
 2014:                         foreach my $option (@options) {
 2015:                             my $val = $option;
 2016:                             if ($option eq 'norequest') {
 2017:                                 $val = 0;
 2018:                             }
 2019:                             my $checked = '';
 2020:                             if ($option eq $curroption) {
 2021:                                 $checked = ' checked="checked"';
 2022:                             }
 2023:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2024:                                   '<input type="radio" name="authorreq_'.$type.
 2025:                                   '" value="'.$val.'"'.$checked.' />'.
 2026:                                   $titles{$option}.'</label></span>&nbsp; ';
 2027:                         }
 2028:                     } else {
 2029:                         my $checked = 'checked="checked" ';
 2030:                         if (ref($settings) eq 'HASH') {
 2031:                             if (ref($settings->{$item}) eq 'HASH') {
 2032:                                 if ($settings->{$item}->{$type} == 0) {
 2033:                                     $checked = '';
 2034:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2035:                                     $checked =  'checked="checked" ';
 2036:                                 }
 2037:                             }
 2038:                         }
 2039:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2040:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2041:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2042:                                       '</label></span>&nbsp; ';
 2043:                     }
 2044:                 }
 2045:                 if ($context eq 'requestcourses') {
 2046:                     $datatable .= '</tr><tr>';
 2047:                     foreach my $item (@usertools) {
 2048:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2049:                     }
 2050:                     $datatable .= '</tr></table>';
 2051:                 }
 2052:                 $datatable .= '</td>';
 2053:                 unless (($context eq 'requestcourses') ||
 2054:                         ($context eq 'requestauthor')) {
 2055:                     $datatable .= 
 2056:                               '<td class="LC_right_item">'.
 2057:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2058:                               '<input type="text" name="quota_'.$type.
 2059:                               '" value="'.$currdefquota.
 2060:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2061:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2062:                               '<input type="text" name="authorquota_'.$type.
 2063:                               '" value="'.$currauthorquota.
 2064:                               '" size="5" /></span></td>';
 2065:                 }
 2066:                 $datatable .= '</tr>';
 2067:             }
 2068:         }
 2069:     }
 2070:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2071:         $defaultquota = '20';
 2072:         $authorquota = '500';
 2073:         if (ref($settings) eq 'HASH') {
 2074:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2075:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2076:             } elsif (defined($settings->{'default'})) {
 2077:                 $defaultquota = $settings->{'default'};
 2078:             }
 2079:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2080:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2081:             }
 2082:         }
 2083:     }
 2084:     $typecount ++;
 2085:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2086:     $datatable .= '<tr'.$css_class.'>'.
 2087:                   '<td>'.$othertitle.'</td>'.
 2088:                   '<td class="LC_left_item">';
 2089:     if ($context eq 'requestcourses') {
 2090:         $datatable .= '<table><tr>';
 2091:     }
 2092:     my %defcell;
 2093:     foreach my $item (@usertools) {
 2094:         if ($context eq 'requestcourses') {
 2095:             my ($curroption,$currlimit);
 2096:             if (ref($settings) eq 'HASH') {
 2097:                 if (ref($settings->{$item}) eq 'HASH') {
 2098:                     $curroption = $settings->{$item}->{'default'};
 2099:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2100:                         $currlimit = $1;
 2101:                     }
 2102:                 }
 2103:             }
 2104:             if (!$curroption) {
 2105:                 $curroption = 'norequest';
 2106:             }
 2107:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2108:             foreach my $option (@options) {
 2109:                 my $val = $option;
 2110:                 if ($option eq 'norequest') {
 2111:                     $val = 0;
 2112:                 }
 2113:                 if ($option eq 'validate') {
 2114:                     my $canvalidate = 0;
 2115:                     if (ref($validations{$item}) eq 'HASH') {
 2116:                         if ($validations{$item}{'default'}) {
 2117:                             $canvalidate = 1;
 2118:                         }
 2119:                     }
 2120:                     next if (!$canvalidate);
 2121:                 }
 2122:                 my $checked = '';
 2123:                 if ($option eq $curroption) {
 2124:                     $checked = ' checked="checked"';
 2125:                 } elsif ($option eq 'autolimit') {
 2126:                     if ($curroption =~ /^autolimit/) {
 2127:                         $checked = ' checked="checked"';
 2128:                     }
 2129:                 }
 2130:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2131:                                   '<input type="radio" name="crsreq_'.$item.
 2132:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2133:                                   $titles{$option}.'</label>';
 2134:                 if ($option eq 'autolimit') {
 2135:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2136:                                        $item.'_limit_default" size="1" '.
 2137:                                        'value="'.$currlimit.'" />';
 2138:                 }
 2139:                 $defcell{$item} .= '</span> ';
 2140:                 if ($option eq 'autolimit') {
 2141:                     $defcell{$item} .= $titles{'unlimited'};
 2142:                 }
 2143:             }
 2144:         } elsif ($context eq 'requestauthor') {
 2145:             my $curroption;
 2146:             if (ref($settings) eq 'HASH') {
 2147:                 $curroption = $settings->{'default'};
 2148:             }
 2149:             if (!$curroption) {
 2150:                 $curroption = 'norequest';
 2151:             }
 2152:             foreach my $option (@options) {
 2153:                 my $val = $option;
 2154:                 if ($option eq 'norequest') {
 2155:                     $val = 0;
 2156:                 }
 2157:                 my $checked = '';
 2158:                 if ($option eq $curroption) {
 2159:                     $checked = ' checked="checked"';
 2160:                 }
 2161:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2162:                               '<input type="radio" name="authorreq_default"'.
 2163:                               ' value="'.$val.'"'.$checked.' />'.
 2164:                               $titles{$option}.'</label></span>&nbsp; ';
 2165:             }
 2166:         } else {
 2167:             my $checked = 'checked="checked" ';
 2168:             if (ref($settings) eq 'HASH') {
 2169:                 if (ref($settings->{$item}) eq 'HASH') {
 2170:                     if ($settings->{$item}->{'default'} == 0) {
 2171:                         $checked = '';
 2172:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2173:                         $checked = 'checked="checked" ';
 2174:                     }
 2175:                 }
 2176:             }
 2177:             $datatable .= '<span class="LC_nobreak"><label>'.
 2178:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2179:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2180:                           '</label></span>&nbsp; ';
 2181:         }
 2182:     }
 2183:     if ($context eq 'requestcourses') {
 2184:         $datatable .= '</tr><tr>';
 2185:         foreach my $item (@usertools) {
 2186:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2187:         }
 2188:         $datatable .= '</tr></table>';
 2189:     }
 2190:     $datatable .= '</td>';
 2191:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2192:         $datatable .= '<td class="LC_right_item">'.
 2193:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2194:                       '<input type="text" name="defaultquota" value="'.
 2195:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2196:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2197:                       '<input type="text" name="authorquota" value="'.
 2198:                       $authorquota.'" size="5" /></span></td>';
 2199:     }
 2200:     $datatable .= '</tr>';
 2201:     $typecount ++;
 2202:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2203:     $datatable .= '<tr'.$css_class.'>'.
 2204:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2205:     if ($context eq 'requestcourses') {
 2206:         $datatable .= &mt('(overrides affiliation, if set)').
 2207:                       '</td>'.
 2208:                       '<td class="LC_left_item">'.
 2209:                       '<table><tr>';
 2210:     } else {
 2211:         $datatable .= &mt('(overrides affiliation, if checked)').
 2212:                       '</td>'.
 2213:                       '<td class="LC_left_item" colspan="2">'.
 2214:                       '<br />';
 2215:     }
 2216:     my %advcell;
 2217:     foreach my $item (@usertools) {
 2218:         if ($context eq 'requestcourses') {
 2219:             my ($curroption,$currlimit);
 2220:             if (ref($settings) eq 'HASH') {
 2221:                 if (ref($settings->{$item}) eq 'HASH') {
 2222:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2223:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2224:                         $currlimit = $1;
 2225:                     }
 2226:                 }
 2227:             }
 2228:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2229:             my $checked = '';
 2230:             if ($curroption eq '') {
 2231:                 $checked = ' checked="checked"';
 2232:             }
 2233:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2234:                                '<input type="radio" name="crsreq_'.$item.
 2235:                                '__LC_adv" value=""'.$checked.' />'.
 2236:                                &mt('No override set').'</label></span>&nbsp; ';
 2237:             foreach my $option (@options) {
 2238:                 my $val = $option;
 2239:                 if ($option eq 'norequest') {
 2240:                     $val = 0;
 2241:                 }
 2242:                 if ($option eq 'validate') {
 2243:                     my $canvalidate = 0;
 2244:                     if (ref($validations{$item}) eq 'HASH') {
 2245:                         if ($validations{$item}{'_LC_adv'}) {
 2246:                             $canvalidate = 1;
 2247:                         }
 2248:                     }
 2249:                     next if (!$canvalidate);
 2250:                 }
 2251:                 my $checked = '';
 2252:                 if ($val eq $curroption) {
 2253:                     $checked = ' checked="checked"';
 2254:                 } elsif ($option eq 'autolimit') {
 2255:                     if ($curroption =~ /^autolimit/) {
 2256:                         $checked = ' checked="checked"';
 2257:                     }
 2258:                 }
 2259:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2260:                                   '<input type="radio" name="crsreq_'.$item.
 2261:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2262:                                   $titles{$option}.'</label>';
 2263:                 if ($option eq 'autolimit') {
 2264:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2265:                                        $item.'_limit__LC_adv" size="1" '.
 2266:                                        'value="'.$currlimit.'" />';
 2267:                 }
 2268:                 $advcell{$item} .= '</span> ';
 2269:                 if ($option eq 'autolimit') {
 2270:                     $advcell{$item} .= $titles{'unlimited'};
 2271:                 }
 2272:             }
 2273:         } elsif ($context eq 'requestauthor') {
 2274:             my $curroption;
 2275:             if (ref($settings) eq 'HASH') {
 2276:                 $curroption = $settings->{'_LC_adv'};
 2277:             }
 2278:             my $checked = '';
 2279:             if ($curroption eq '') {
 2280:                 $checked = ' checked="checked"';
 2281:             }
 2282:             $datatable .= '<span class="LC_nobreak"><label>'.
 2283:                           '<input type="radio" name="authorreq__LC_adv"'.
 2284:                           ' value=""'.$checked.' />'.
 2285:                           &mt('No override set').'</label></span>&nbsp; ';
 2286:             foreach my $option (@options) {
 2287:                 my $val = $option;
 2288:                 if ($option eq 'norequest') {
 2289:                     $val = 0;
 2290:                 }
 2291:                 my $checked = '';
 2292:                 if ($val eq $curroption) {
 2293:                     $checked = ' checked="checked"';
 2294:                 }
 2295:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2296:                               '<input type="radio" name="authorreq__LC_adv"'.
 2297:                               ' value="'.$val.'"'.$checked.' />'.
 2298:                               $titles{$option}.'</label></span>&nbsp; ';
 2299:             }
 2300:         } else {
 2301:             my $checked = 'checked="checked" ';
 2302:             if (ref($settings) eq 'HASH') {
 2303:                 if (ref($settings->{$item}) eq 'HASH') {
 2304:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2305:                         $checked = '';
 2306:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2307:                         $checked = 'checked="checked" ';
 2308:                     }
 2309:                 }
 2310:             }
 2311:             $datatable .= '<span class="LC_nobreak"><label>'.
 2312:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2313:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2314:                           '</label></span>&nbsp; ';
 2315:         }
 2316:     }
 2317:     if ($context eq 'requestcourses') {
 2318:         $datatable .= '</tr><tr>';
 2319:         foreach my $item (@usertools) {
 2320:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2321:         }
 2322:         $datatable .= '</tr></table>';
 2323:     }
 2324:     $datatable .= '</td></tr>';
 2325:     $$rowtotal += $typecount;
 2326:     return $datatable;
 2327: }
 2328: 
 2329: sub print_requestmail {
 2330:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2331:     my ($now,$datatable,%currapp);
 2332:     $now = time;
 2333:     if (ref($settings) eq 'HASH') {
 2334:         if (ref($settings->{'notify'}) eq 'HASH') {
 2335:             if ($settings->{'notify'}{'approval'} ne '') {
 2336:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2337:             }
 2338:         }
 2339:     }
 2340:     my $numinrow = 2;
 2341:     my $css_class;
 2342:     if ($$rowtotal%2) {
 2343:         $css_class = 'LC_odd_row';
 2344:     }
 2345:     if ($customcss) {
 2346:         $css_class .= " $customcss";
 2347:     }
 2348:     $css_class =~ s/^\s+//;
 2349:     if ($css_class) {
 2350:         $css_class = ' class="'.$css_class.'"';
 2351:     }
 2352:     if ($rowstyle) {
 2353:         $css_class .= ' style="'.$rowstyle.'"';
 2354:     }
 2355:     my $text;
 2356:     if ($action eq 'requestcourses') {
 2357:         $text = &mt('Receive notification of course requests requiring approval');
 2358:     } elsif ($action eq 'requestauthor') {
 2359:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2360:     } else {
 2361:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2362:     }
 2363:     $datatable = '<tr'.$css_class.'>'.
 2364:                  ' <td>'.$text.'</td>'.
 2365:                  ' <td class="LC_left_item">';
 2366:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2367:                                                  $action.'notifyapproval',%currapp);
 2368:     if ($numdc > 0) {
 2369:         $datatable .= $table;
 2370:     } else {
 2371:         $datatable .= &mt('There are no active Domain Coordinators');
 2372:     }
 2373:     $datatable .='</td></tr>';
 2374:     return $datatable;
 2375: }
 2376: 
 2377: sub print_studentcode {
 2378:     my ($settings,$rowtotal) = @_;
 2379:     my $rownum = 0; 
 2380:     my ($output,%current);
 2381:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2382:     if (ref($settings) eq 'HASH') {
 2383:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2384:             foreach my $type (@crstypes) {
 2385:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2386:             }
 2387:         }
 2388:     }
 2389:     $output .= '<tr>'.
 2390:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2391:                '<td class="LC_left_item">';
 2392:     foreach my $type (@crstypes) {
 2393:         my $check = ' ';
 2394:         if ($current{$type}) {
 2395:             $check = ' checked="checked" ';
 2396:         }
 2397:         $output .= '<span class="LC_nobreak"><label>'.
 2398:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2399:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2400:     }
 2401:     $output .= '</td></tr>';
 2402:     $$rowtotal ++;
 2403:     return $output;
 2404: }
 2405: 
 2406: sub print_textbookcourses {
 2407:     my ($dom,$type,$settings,$rowtotal) = @_;
 2408:     my $rownum = 0;
 2409:     my $css_class;
 2410:     my $itemcount = 1;
 2411:     my $maxnum = 0;
 2412:     my $bookshash;
 2413:     if (ref($settings) eq 'HASH') {
 2414:         $bookshash = $settings->{$type};
 2415:     }
 2416:     my %ordered;
 2417:     if (ref($bookshash) eq 'HASH') {
 2418:         foreach my $item (keys(%{$bookshash})) {
 2419:             if (ref($bookshash->{$item}) eq 'HASH') {
 2420:                 my $num = $bookshash->{$item}{'order'};
 2421:                 $ordered{$num} = $item;
 2422:             }
 2423:         }
 2424:     }
 2425:     my $confname = $dom.'-domainconfig';
 2426:     my $switchserver = &check_switchserver($dom,$confname);
 2427:     my $maxnum = scalar(keys(%ordered));
 2428:     my $datatable;
 2429:     if (keys(%ordered)) {
 2430:         my @items = sort { $a <=> $b } keys(%ordered);
 2431:         for (my $i=0; $i<@items; $i++) {
 2432:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2433:             my $key = $ordered{$items[$i]};
 2434:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2435:             my $coursetitle = $coursehash{'description'};
 2436:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2437:             if (ref($bookshash->{$key}) eq 'HASH') {
 2438:                 $subject = $bookshash->{$key}->{'subject'};
 2439:                 $title = $bookshash->{$key}->{'title'};
 2440:                 if ($type eq 'textbooks') {
 2441:                     $publisher = $bookshash->{$key}->{'publisher'};
 2442:                     $author = $bookshash->{$key}->{'author'};
 2443:                     $image = $bookshash->{$key}->{'image'};
 2444:                     if ($image ne '') {
 2445:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2446:                         my $imagethumb = "$path/tn-".$imagefile;
 2447:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2448:                     }
 2449:                 }
 2450:             }
 2451:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2452:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2453:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2454:             for (my $k=0; $k<=$maxnum; $k++) {
 2455:                 my $vpos = $k+1;
 2456:                 my $selstr;
 2457:                 if ($k == $i) {
 2458:                     $selstr = ' selected="selected" ';
 2459:                 }
 2460:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2461:             }
 2462:             $datatable .= '</select>'.('&nbsp;'x2).
 2463:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2464:                 &mt('Delete?').'</label></span></td>'.
 2465:                 '<td colspan="2">'.
 2466:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2467:                 ('&nbsp;'x2).
 2468:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2469:             if ($type eq 'textbooks') {
 2470:                 $datatable .= ('&nbsp;'x2).
 2471:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2472:                               ('&nbsp;'x2).
 2473:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2474:                               ('&nbsp;'x2).
 2475:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2476:                 if ($image) {
 2477:                     $datatable .= $imgsrc.
 2478:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2479:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2480:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2481:                 }
 2482:                 if ($switchserver) {
 2483:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2484:                 } else {
 2485:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2486:                 }
 2487:             }
 2488:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2489:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2490:                           $coursetitle.'</span></td></tr>'."\n";
 2491:             $itemcount ++;
 2492:         }
 2493:     }
 2494:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2495:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2496:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2497:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2498:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2499:     for (my $k=0; $k<$maxnum+1; $k++) {
 2500:         my $vpos = $k+1;
 2501:         my $selstr;
 2502:         if ($k == $maxnum) {
 2503:             $selstr = ' selected="selected" ';
 2504:         }
 2505:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2506:     }
 2507:     $datatable .= '</select>&nbsp;'."\n".
 2508:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2509:                   '<td colspan="2">'.
 2510:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2511:                   ('&nbsp;'x2).
 2512:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2513:                   ('&nbsp;'x2);
 2514:     if ($type eq 'textbooks') {
 2515:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2516:                       ('&nbsp;'x2).
 2517:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2518:                       ('&nbsp;'x2).
 2519:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2520:         if ($switchserver) {
 2521:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2522:         } else {
 2523:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2524:         }
 2525:         $datatable .= '</span>'."\n";
 2526:     }
 2527:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2528:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2529:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2530:                   &Apache::loncommon::selectcourse_link
 2531:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2532:                   '</span></td>'."\n".
 2533:                   '</tr>'."\n";
 2534:     $itemcount ++;
 2535:     return $datatable;
 2536: }
 2537: 
 2538: sub textbookcourses_javascript {
 2539:     my ($settings) = @_;
 2540:     return unless(ref($settings) eq 'HASH');
 2541:     my (%ordered,%total,%jstext);
 2542:     foreach my $type ('textbooks','templates') {
 2543:         $total{$type} = 0;
 2544:         if (ref($settings->{$type}) eq 'HASH') {
 2545:             foreach my $item (keys(%{$settings->{$type}})) {
 2546:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2547:                     my $num = $settings->{$type}->{$item}{'order'};
 2548:                     $ordered{$type}{$num} = $item;
 2549:                 }
 2550:             }
 2551:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2552:         }
 2553:         my @jsarray = ();
 2554:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2555:             push(@jsarray,$ordered{$type}{$item});
 2556:         }
 2557:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2558:     }
 2559:     return <<"ENDSCRIPT";
 2560: <script type="text/javascript">
 2561: // <![CDATA[
 2562: function reorderBooks(form,item,caller) {
 2563:     var changedVal;
 2564: $jstext{'textbooks'};
 2565: $jstext{'templates'};
 2566:     var newpos;
 2567:     var maxh;
 2568:     if (caller == 'textbooks') {  
 2569:         newpos = 'textbooks_addbook_pos';
 2570:         maxh = 1 + $total{'textbooks'};
 2571:     } else {
 2572:         newpos = 'templates_addbook_pos';
 2573:         maxh = 1 + $total{'templates'};
 2574:     }
 2575:     var current = new Array;
 2576:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2577:     if (item == newpos) {
 2578:         changedVal = newitemVal;
 2579:     } else {
 2580:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2581:         current[newitemVal] = newpos;
 2582:     }
 2583:     if (caller == 'textbooks') {
 2584:         for (var i=0; i<textbooks.length; i++) {
 2585:             var elementName = 'textbooks_'+textbooks[i];
 2586:             if (elementName != item) {
 2587:                 if (form.elements[elementName]) {
 2588:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2589:                     current[currVal] = elementName;
 2590:                 }
 2591:             }
 2592:         }
 2593:     }
 2594:     if (caller == 'templates') {
 2595:         for (var i=0; i<templates.length; i++) {
 2596:             var elementName = 'templates_'+templates[i];
 2597:             if (elementName != item) {
 2598:                 if (form.elements[elementName]) {
 2599:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2600:                     current[currVal] = elementName;
 2601:                 }
 2602:             }
 2603:         }
 2604:     }
 2605:     var oldVal;
 2606:     for (var j=0; j<maxh; j++) {
 2607:         if (current[j] == undefined) {
 2608:             oldVal = j;
 2609:         }
 2610:     }
 2611:     if (oldVal < changedVal) {
 2612:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2613:            var elementName = current[k];
 2614:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2615:         }
 2616:     } else {
 2617:         for (var k=changedVal; k<oldVal; k++) {
 2618:             var elementName = current[k];
 2619:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2620:         }
 2621:     }
 2622:     return;
 2623: }
 2624: 
 2625: // ]]>
 2626: </script>
 2627: 
 2628: ENDSCRIPT
 2629: }
 2630: 
 2631: sub ltitools_javascript {
 2632:     my ($settings) = @_;
 2633:     my $togglejs = &ltitools_toggle_js();
 2634:     unless (ref($settings) eq 'HASH') {
 2635:         return $togglejs;
 2636:     }
 2637:     my (%ordered,$total,%jstext);
 2638:     $total = 0;
 2639:     foreach my $item (keys(%{$settings})) {
 2640:         if (ref($settings->{$item}) eq 'HASH') {
 2641:             my $num = $settings->{$item}{'order'};
 2642:             $ordered{$num} = $item;
 2643:         }
 2644:     }
 2645:     $total = scalar(keys(%{$settings}));
 2646:     my @jsarray = ();
 2647:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2648:         push(@jsarray,$ordered{$item});
 2649:     }
 2650:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2651:     return <<"ENDSCRIPT";
 2652: <script type="text/javascript">
 2653: // <![CDATA[
 2654: function reorderLTITools(form,item) {
 2655:     var changedVal;
 2656: $jstext
 2657:     var newpos = 'ltitools_add_pos';
 2658:     var maxh = 1 + $total;
 2659:     var current = new Array;
 2660:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2661:     if (item == newpos) {
 2662:         changedVal = newitemVal;
 2663:     } else {
 2664:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2665:         current[newitemVal] = newpos;
 2666:     }
 2667:     for (var i=0; i<ltitools.length; i++) {
 2668:         var elementName = 'ltitools_'+ltitools[i];
 2669:         if (elementName != item) {
 2670:             if (form.elements[elementName]) {
 2671:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2672:                 current[currVal] = elementName;
 2673:             }
 2674:         }
 2675:     }
 2676:     var oldVal;
 2677:     for (var j=0; j<maxh; j++) {
 2678:         if (current[j] == undefined) {
 2679:             oldVal = j;
 2680:         }
 2681:     }
 2682:     if (oldVal < changedVal) {
 2683:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2684:            var elementName = current[k];
 2685:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2686:         }
 2687:     } else {
 2688:         for (var k=changedVal; k<oldVal; k++) {
 2689:             var elementName = current[k];
 2690:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2691:         }
 2692:     }
 2693:     return;
 2694: }
 2695: 
 2696: // ]]>
 2697: </script>
 2698: 
 2699: $togglejs
 2700: 
 2701: ENDSCRIPT
 2702: }
 2703: 
 2704: sub ltitools_toggle_js {
 2705:     return <<"ENDSCRIPT";
 2706: <script type="text/javascript">
 2707: // <![CDATA[
 2708: 
 2709: function toggleLTITools(form,setting,item) {
 2710:     var radioname = '';
 2711:     var divid = '';
 2712:     if ((setting == 'passback') || (setting == 'roster')) {
 2713:         radioname = 'ltitools_'+setting+'_'+item;
 2714:         divid = 'ltitools_'+setting+'time_'+item;
 2715:         var num = form.elements[radioname].length;
 2716:         if (num) {
 2717:             var setvis = '';
 2718:             for (var i=0; i<num; i++) {
 2719:                 if (form.elements[radioname][i].checked) {
 2720:                     if (form.elements[radioname][i].value == '1') {
 2721:                         if (document.getElementById(divid)) {
 2722:                             document.getElementById(divid).style.display = 'inline-block';
 2723:                         }
 2724:                         setvis = 1;
 2725:                     }
 2726:                     break;
 2727:                 }
 2728:             }
 2729:         }
 2730:         if (!setvis) {
 2731:             if (document.getElementById(divid)) {
 2732:                 document.getElementById(divid).style.display = 'none';
 2733:             }
 2734:         }
 2735:     }
 2736:     if (setting == 'user') {
 2737:         divid = 'ltitools_'+setting+'_div_'+item;
 2738:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2739:         if (document.getElementById(divid)) {
 2740:             if (document.getElementById(checkid)) {
 2741:                 if (document.getElementById(checkid).checked) {
 2742:                     document.getElementById(divid).style.display = 'inline-block';
 2743:                 } else {
 2744:                     document.getElementById(divid).style.display = 'none';
 2745:                 }
 2746:             }
 2747:         }
 2748:     }
 2749:     return;
 2750: }
 2751: // ]]>
 2752: </script>
 2753: 
 2754: ENDSCRIPT
 2755: }
 2756: 
 2757: sub lti_javascript {
 2758:     my ($settings) = @_;
 2759:     my $togglejs = &lti_toggle_js();
 2760:     unless (ref($settings) eq 'HASH') {
 2761:         return $togglejs;
 2762:     }
 2763:     my (%ordered,$total,%jstext);
 2764:     $total = 0;
 2765:     foreach my $item (keys(%{$settings})) {
 2766:         if (ref($settings->{$item}) eq 'HASH') {
 2767:             my $num = $settings->{$item}{'order'};
 2768:             $ordered{$num} = $item;
 2769:         }
 2770:     }
 2771:     $total = scalar(keys(%{$settings}));
 2772:     my @jsarray = ();
 2773:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2774:         push(@jsarray,$ordered{$item});
 2775:     }
 2776:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 2777:     return <<"ENDSCRIPT";
 2778: <script type="text/javascript">
 2779: // <![CDATA[
 2780: function reorderLTI(form,item) {
 2781:     var changedVal;
 2782: $jstext
 2783:     var newpos = 'lti_pos_add';
 2784:     var maxh = 1 + $total;
 2785:     var current = new Array;
 2786:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2787:     if (item == newpos) {
 2788:         changedVal = newitemVal;
 2789:     } else {
 2790:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2791:         current[newitemVal] = newpos;
 2792:     }
 2793:     for (var i=0; i<lti.length; i++) {
 2794:         var elementName = 'lti_pos_'+lti[i];
 2795:         if (elementName != item) {
 2796:             if (form.elements[elementName]) {
 2797:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2798:                 current[currVal] = elementName;
 2799:             }
 2800:         }
 2801:     }
 2802:     var oldVal;
 2803:     for (var j=0; j<maxh; j++) {
 2804:         if (current[j] == undefined) {
 2805:             oldVal = j;
 2806:         }
 2807:     }
 2808:     if (oldVal < changedVal) {
 2809:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2810:            var elementName = current[k];
 2811:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2812:         }
 2813:     } else {
 2814:         for (var k=changedVal; k<oldVal; k++) {
 2815:             var elementName = current[k];
 2816:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2817:         }
 2818:     }
 2819:     return;
 2820: }
 2821: // ]]>
 2822: </script>
 2823: 
 2824: $togglejs
 2825: 
 2826: ENDSCRIPT
 2827: }
 2828: 
 2829: sub lti_toggle_js {
 2830:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 2831:                             localauth => 'Local auth argument',
 2832:                             krb       => 'Kerberos domain',
 2833:                          );
 2834:     return <<"ENDSCRIPT";
 2835: <script type="text/javascript">
 2836: // <![CDATA[
 2837: 
 2838: function toggleLTI(form,setting,item) {
 2839:     if (setting == 'requser') {
 2840:         var fieldsets = document.getElementsByClassName('ltioption_'+item);
 2841:         if (fieldsets.length) {
 2842:             var radioname = 'lti_'+setting+'_'+item;
 2843:             var num = form.elements[radioname].length;
 2844:             if (num) {
 2845:                 var setvis = '';
 2846:                 for (var i=0; i<num; i++) {
 2847:                     if (form.elements[radioname][i].checked) {
 2848:                         if (form.elements[radioname][i].value == '1') {
 2849:                            setvis = 1;
 2850:                            break;
 2851:                         }
 2852:                     }
 2853:                 }
 2854:                 for (var j=0; j<fieldsets.length; j++) { 
 2855:                     if (setvis) {
 2856:                         fieldsets[j].style.display = 'block';
 2857:                     } else {
 2858:                         fieldsets[j].style.display = 'none';
 2859:                     }
 2860:                 }
 2861:             }
 2862:         }
 2863:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback')) {
 2864:         var radioname = '';
 2865:         var divid = '';
 2866:         if (setting == 'user') {
 2867:             radioname = 'lti_mapuser_'+item;
 2868:             divid = 'lti_userfield_'+item;
 2869:         } else if (setting == 'crs') {
 2870:             radioname = 'lti_mapcrs_'+item;
 2871:             divid = 'lti_crsfield_'+item;
 2872:         } else {
 2873:             radioname = 'lti_passbackformat_'+item;
 2874:             divid =  'lti_passback_'+item;
 2875:         }
 2876:         var num = form.elements[radioname].length;
 2877:         if (num) {
 2878:             var setvis = '';
 2879:             for (var i=0; i<num; i++) {
 2880:                if (form.elements[radioname][i].checked) {
 2881:                    if (setting == 'passback') {
 2882:                        if (form.elements[radioname][i].value == '1') {
 2883:                            if (document.getElementById(divid)) {
 2884:                                document.getElementById(divid).style.display = 'inline-block';
 2885:                            }
 2886:                            setvis = 1;
 2887:                            break;
 2888:                        }
 2889:                    } else {
 2890:                        if (form.elements[radioname][i].value == 'other') {
 2891:                            if (document.getElementById(divid)) {
 2892:                                document.getElementById(divid).style.display = 'inline-block';
 2893:                            }
 2894:                            setvis = 1;
 2895:                            break;
 2896:                        }
 2897:                    }
 2898:                } 
 2899:             }
 2900:             if (!setvis) {
 2901:                 if (document.getElementById(divid)) {
 2902:                     document.getElementById(divid).style.display = 'none';
 2903:                 }
 2904:             }
 2905:         }
 2906:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 2907:         var numsec = form.elements['lti_crssec_'+item].length;
 2908:         if (numsec) {
 2909:             var setvis = '';
 2910:             for (var i=0; i<numsec; i++) {
 2911:                 if (form.elements['lti_crssec_'+item][i].checked) {
 2912:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 2913:                         if (document.getElementById('lti_crssecfield_'+item)) {
 2914:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 2915:                             setvis = 1;
 2916:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 2917:                             if (numsrcsec) {
 2918:                                 var setsrcvis = '';
 2919:                                 for (var j=0; j<numsrcsec; j++) {
 2920:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 2921:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 2922:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 2923:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 2924:                                                 setsrcvis = 1;
 2925:                                             }
 2926:                                         }
 2927:                                     }
 2928:                                 }
 2929:                                 if (!setsrcvis) {
 2930:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 2931:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2932:                                     }
 2933:                                 }
 2934:                             }
 2935:                         }
 2936:                     }
 2937:                 }
 2938:             }
 2939:             if (!setvis) {
 2940:                 if (document.getElementById('lti_crssecfield_'+item)) {
 2941:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 2942:                 }
 2943:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 2944:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2945:                 }
 2946:             }
 2947:         }
 2948:     } else if (setting == 'lcauth') {
 2949:         var numauth = form.elements['lti_lcauth_'+item].length;
 2950:         if (numauth) {
 2951:             for (var i=0; i<numauth; i++) {
 2952:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 2953:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 2954:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 2955:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 2956:                         } else {
 2957:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 2958:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 2959:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 2960:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 2961:                                 } else {
 2962:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 2963:                                 }
 2964:                             }
 2965:                         }
 2966:                     }
 2967:                 }
 2968:             }
 2969:         }
 2970:     } else if (setting == 'lcmenu') {
 2971:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 2972:         var divid = 'lti_menufield_'+item;
 2973:         var setvis = '';
 2974:         for (var i=0; i<menus.length; i++) {
 2975:             var radioname = menus[i];  
 2976:             var num = form.elements[radioname].length;
 2977:             if (num) {
 2978:                 for (var j=0; j<num; j++) {
 2979:                     if (form.elements[radioname][j].checked) {
 2980:                         if (form.elements[radioname][j].value == '1') {
 2981:                             if (document.getElementById(divid)) {
 2982:                                 document.getElementById(divid).style.display = 'inline-block';
 2983:                             }
 2984:                             setvis = 1;
 2985:                             break;
 2986:                         }
 2987:                     }
 2988:                 }
 2989:             }
 2990:             if (setvis == 1) {
 2991:                 break;
 2992:             }
 2993:         }
 2994:         if (!setvis) {
 2995:             if (document.getElementById(divid)) {
 2996:                 document.getElementById(divid).style.display = 'none';
 2997:             }
 2998:         }
 2999:     }
 3000:     return;
 3001: }
 3002: // ]]>
 3003: </script>
 3004: 
 3005: ENDSCRIPT
 3006: }
 3007: 
 3008: sub print_autoenroll {
 3009:     my ($dom,$settings,$rowtotal) = @_;
 3010:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3011:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3012:     if (ref($settings) eq 'HASH') {
 3013:         if (exists($settings->{'run'})) {
 3014:             if ($settings->{'run'} eq '0') {
 3015:                 $runoff = ' checked="checked" ';
 3016:                 $runon = ' ';
 3017:             } else {
 3018:                 $runon = ' checked="checked" ';
 3019:                 $runoff = ' ';
 3020:             }
 3021:         } else {
 3022:             if ($autorun) {
 3023:                 $runon = ' checked="checked" ';
 3024:                 $runoff = ' ';
 3025:             } else {
 3026:                 $runoff = ' checked="checked" ';
 3027:                 $runon = ' ';
 3028:             }
 3029:         }
 3030:         if (exists($settings->{'co-owners'})) {
 3031:             if ($settings->{'co-owners'} eq '0') {
 3032:                 $coownersoff = ' checked="checked" ';
 3033:                 $coownerson = ' ';
 3034:             } else {
 3035:                 $coownerson = ' checked="checked" ';
 3036:                 $coownersoff = ' ';
 3037:             }
 3038:         } else {
 3039:             $coownersoff = ' checked="checked" ';
 3040:             $coownerson = ' ';
 3041:         }
 3042:         if (exists($settings->{'sender_domain'})) {
 3043:             $defdom = $settings->{'sender_domain'};
 3044:         }
 3045:         if (exists($settings->{'autofailsafe'})) {
 3046:             $failsafe = $settings->{'autofailsafe'};
 3047:         }
 3048:     } else {
 3049:         if ($autorun) {
 3050:             $runon = ' checked="checked" ';
 3051:             $runoff = ' ';
 3052:         } else {
 3053:             $runoff = ' checked="checked" ';
 3054:             $runon = ' ';
 3055:         }
 3056:     }
 3057:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3058:     my $notif_sender;
 3059:     if (ref($settings) eq 'HASH') {
 3060:         $notif_sender = $settings->{'sender_uname'};
 3061:     }
 3062:     my $datatable='<tr class="LC_odd_row">'.
 3063:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3064:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3065:                   '<input type="radio" name="autoenroll_run"'.
 3066:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3067:                   '<label><input type="radio" name="autoenroll_run"'.
 3068:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3069:                   '</tr><tr>'.
 3070:                   '<td>'.&mt('Notification messages - sender').
 3071:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3072:                   &mt('username').':&nbsp;'.
 3073:                   '<input type="text" name="sender_uname" value="'.
 3074:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3075:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3076:                   '<tr class="LC_odd_row">'.
 3077:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3078:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3079:                   '<input type="radio" name="autoassign_coowners"'.
 3080:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3081:                   '<label><input type="radio" name="autoassign_coowners"'.
 3082:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3083:                   '</tr><tr>'.
 3084:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3085:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3086:                   '<input type="text" name="autoenroll_failsafe"'.
 3087:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3088:     $$rowtotal += 4;
 3089:     return $datatable;
 3090: }
 3091: 
 3092: sub print_autoupdate {
 3093:     my ($position,$dom,$settings,$rowtotal) = @_;
 3094:     my $datatable;
 3095:     if ($position eq 'top') {
 3096:         my $updateon = ' ';
 3097:         my $updateoff = ' checked="checked" ';
 3098:         my $classlistson = ' ';
 3099:         my $classlistsoff = ' checked="checked" ';
 3100:         if (ref($settings) eq 'HASH') {
 3101:             if ($settings->{'run'} eq '1') {
 3102:                 $updateon = $updateoff;
 3103:                 $updateoff = ' ';
 3104:             }
 3105:             if ($settings->{'classlists'} eq '1') {
 3106:                 $classlistson = $classlistsoff;
 3107:                 $classlistsoff = ' ';
 3108:             }
 3109:         }
 3110:         my %title = (
 3111:                    run => 'Auto-update active?',
 3112:                    classlists => 'Update information in classlists?',
 3113:                     );
 3114:         $datatable = '<tr class="LC_odd_row">'. 
 3115:                   '<td>'.&mt($title{'run'}).'</td>'.
 3116:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3117:                   '<input type="radio" name="autoupdate_run"'.
 3118:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3119:                   '<label><input type="radio" name="autoupdate_run"'.
 3120:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3121:                   '</tr><tr>'.
 3122:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 3123:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3124:                   '<label><input type="radio" name="classlists"'.
 3125:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3126:                   '<label><input type="radio" name="classlists"'.
 3127:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3128:                   '</tr>';
 3129:         $$rowtotal += 2;
 3130:     } elsif ($position eq 'middle') {
 3131:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3132:         my $numinrow = 3;
 3133:         my $locknamesettings;
 3134:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3135:                                      $dom,$numinrow,$othertitle,
 3136:                                     'lockablenames',$rowtotal);
 3137:         $$rowtotal ++;
 3138:     } else {
 3139:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3140:         my @fields = ('lastname','firstname','middlename','generation',
 3141:                       'permanentemail','id');
 3142:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3143:         my $numrows = 0;
 3144:         if (ref($types) eq 'ARRAY') {
 3145:             if (@{$types} > 0) {
 3146:                 $datatable = 
 3147:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3148:                                          \@fields,$types,\$numrows);
 3149:                     $$rowtotal += @{$types}; 
 3150:             }
 3151:         }
 3152:         $datatable .= 
 3153:             &usertype_update_row($settings,{'default' => $othertitle},
 3154:                                  \%fieldtitles,\@fields,['default'],
 3155:                                  \$numrows);
 3156:         $$rowtotal ++;     
 3157:     }
 3158:     return $datatable;
 3159: }
 3160: 
 3161: sub print_autocreate {
 3162:     my ($dom,$settings,$rowtotal) = @_;
 3163:     my (%createon,%createoff,%currhash);
 3164:     my @types = ('xml','req');
 3165:     if (ref($settings) eq 'HASH') {
 3166:         foreach my $item (@types) {
 3167:             $createoff{$item} = ' checked="checked" ';
 3168:             $createon{$item} = ' ';
 3169:             if (exists($settings->{$item})) {
 3170:                 if ($settings->{$item}) {
 3171:                     $createon{$item} = ' checked="checked" ';
 3172:                     $createoff{$item} = ' ';
 3173:                 }
 3174:             }
 3175:         }
 3176:         if ($settings->{'xmldc'} ne '') {
 3177:             $currhash{$settings->{'xmldc'}} = 1;
 3178:         }
 3179:     } else {
 3180:         foreach my $item (@types) {
 3181:             $createoff{$item} = ' checked="checked" ';
 3182:             $createon{$item} = ' ';
 3183:         }
 3184:     }
 3185:     $$rowtotal += 2;
 3186:     my $numinrow = 2;
 3187:     my $datatable='<tr class="LC_odd_row">'.
 3188:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3189:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3190:                   '<input type="radio" name="autocreate_xml"'.
 3191:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3192:                   '<label><input type="radio" name="autocreate_xml"'.
 3193:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3194:                   '</td></tr><tr>'.
 3195:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3196:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3197:                   '<input type="radio" name="autocreate_req"'.
 3198:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3199:                   '<label><input type="radio" name="autocreate_req"'.
 3200:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3201:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3202:                                                    'autocreate_xmldc',%currhash);
 3203:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3204:     if ($numdc > 1) {
 3205:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3206:                       '</td><td class="LC_left_item">';
 3207:     } else {
 3208:         $datatable .= &mt('Course creation processed as:').
 3209:                       '</td><td class="LC_right_item">';
 3210:     }
 3211:     $datatable .= $dctable.'</td></tr>';
 3212:     $$rowtotal += $rows;
 3213:     return $datatable;
 3214: }
 3215: 
 3216: sub print_directorysrch {
 3217:     my ($position,$dom,$settings,$rowtotal) = @_;
 3218:     my $datatable;
 3219:     if ($position eq 'top') {
 3220:         my $instsrchon = ' ';
 3221:         my $instsrchoff = ' checked="checked" ';
 3222:         my ($exacton,$containson,$beginson);
 3223:         my $instlocalon = ' ';
 3224:         my $instlocaloff = ' checked="checked" ';
 3225:         if (ref($settings) eq 'HASH') {
 3226:             if ($settings->{'available'} eq '1') {
 3227:                 $instsrchon = $instsrchoff;
 3228:                 $instsrchoff = ' ';
 3229:             }
 3230:             if ($settings->{'localonly'} eq '1') {
 3231:                 $instlocalon = $instlocaloff;
 3232:                 $instlocaloff = ' ';
 3233:             }
 3234:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3235:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3236:                     if ($type eq 'exact') {
 3237:                         $exacton = ' checked="checked" ';
 3238:                     } elsif ($type eq 'contains') {
 3239:                         $containson = ' checked="checked" ';
 3240:                     } elsif ($type eq 'begins') {
 3241:                         $beginson = ' checked="checked" ';
 3242:                     }
 3243:                 }
 3244:             } else {
 3245:                 if ($settings->{'searchtypes'} eq 'exact') {
 3246:                     $exacton = ' checked="checked" ';
 3247:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3248:                     $containson = ' checked="checked" ';
 3249:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3250:                     $exacton = ' checked="checked" ';
 3251:                     $containson = ' checked="checked" ';
 3252:                 }
 3253:             }
 3254:         }
 3255:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3256:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3257: 
 3258:         my $numinrow = 4;
 3259:         my $cansrchrow = 0;
 3260:         $datatable='<tr class="LC_odd_row">'.
 3261:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3262:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3263:                    '<input type="radio" name="dirsrch_available"'.
 3264:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3265:                    '<label><input type="radio" name="dirsrch_available"'.
 3266:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3267:                    '</tr><tr>'.
 3268:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3269:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3270:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3271:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3272:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3273:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3274:                    '</tr>';
 3275:         $$rowtotal += 2;
 3276:         if (ref($usertypes) eq 'HASH') {
 3277:             if (keys(%{$usertypes}) > 0) {
 3278:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3279:                                              $numinrow,$othertitle,'cansearch',
 3280:                                              $rowtotal);
 3281:                 $cansrchrow = 1;
 3282:             }
 3283:         }
 3284:         if ($cansrchrow) {
 3285:             $$rowtotal ++;
 3286:             $datatable .= '<tr>';
 3287:         } else {
 3288:             $datatable .= '<tr class="LC_odd_row">';
 3289:         }
 3290:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3291:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3292:         foreach my $title (@{$titleorder}) {
 3293:             if (defined($searchtitles->{$title})) {
 3294:                 my $check = ' ';
 3295:                 if (ref($settings) eq 'HASH') {
 3296:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3297:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3298:                             $check = ' checked="checked" ';
 3299:                         }
 3300:                     }
 3301:                 }
 3302:                 $datatable .= '<td class="LC_left_item">'.
 3303:                               '<span class="LC_nobreak"><label>'.
 3304:                               '<input type="checkbox" name="searchby" '.
 3305:                               'value="'.$title.'"'.$check.'/>'.
 3306:                               $searchtitles->{$title}.'</label></span></td>';
 3307:             }
 3308:         }
 3309:         $datatable .= '</tr></table></td></tr>';
 3310:         $$rowtotal ++;
 3311:         if ($cansrchrow) {
 3312:             $datatable .= '<tr class="LC_odd_row">';
 3313:         } else {
 3314:             $datatable .= '<tr>';
 3315:         }
 3316:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3317:                       '<td class="LC_left_item" colspan="2">'.
 3318:                       '<span class="LC_nobreak"><label>'.
 3319:                       '<input type="checkbox" name="searchtypes" '.
 3320:                       $exacton.' value="exact" />'.&mt('Exact match').
 3321:                       '</label>&nbsp;'.
 3322:                       '<label><input type="checkbox" name="searchtypes" '.
 3323:                       $beginson.' value="begins" />'.&mt('Begins with').
 3324:                       '</label>&nbsp;'.
 3325:                       '<label><input type="checkbox" name="searchtypes" '.
 3326:                       $containson.' value="contains" />'.&mt('Contains').
 3327:                       '</label></span></td></tr>';
 3328:         $$rowtotal ++;
 3329:     } else {
 3330:         my $domsrchon = ' checked="checked" ';
 3331:         my $domsrchoff = ' ';
 3332:         my $domlocalon = ' ';
 3333:         my $domlocaloff = ' checked="checked" ';
 3334:         if (ref($settings) eq 'HASH') {
 3335:             if ($settings->{'lclocalonly'} eq '1') {
 3336:                 $domlocalon = $domlocaloff;
 3337:                 $domlocaloff = ' ';
 3338:             }
 3339:             if ($settings->{'lcavailable'} eq '0') {
 3340:                 $domsrchoff = $domsrchon;
 3341:                 $domsrchon = ' ';
 3342:             }
 3343:         }
 3344:         $datatable='<tr class="LC_odd_row">'.
 3345:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3346:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3347:                       '<input type="radio" name="dirsrch_domavailable"'.
 3348:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3349:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3350:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3351:                       '</tr><tr>'.
 3352:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3353:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3354:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3355:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3356:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3357:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3358:                       '</tr>';
 3359:         $$rowtotal += 2;
 3360:     }
 3361:     return $datatable;
 3362: }
 3363: 
 3364: sub print_contacts {
 3365:     my ($position,$dom,$settings,$rowtotal) = @_;
 3366:     my $datatable;
 3367:     my @contacts = ('adminemail','supportemail');
 3368:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3369:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3370:     if ($position eq 'top') {
 3371:         if (ref($settings) eq 'HASH') {
 3372:             foreach my $item (@contacts) {
 3373:                 if (exists($settings->{$item})) {
 3374:                     $to{$item} = $settings->{$item};
 3375:                 }
 3376:             }
 3377:         }
 3378:     } elsif ($position eq 'middle') {
 3379:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3380:                      'updatesmail','idconflictsmail');
 3381:         foreach my $type (@mailings) {
 3382:             $otheremails{$type} = '';
 3383:         }
 3384:     } elsif ($position eq 'lower') {
 3385:         if (ref($settings) eq 'HASH') {
 3386:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3387:                 %lonstatus = %{$settings->{'lonstatus'}};
 3388:             }
 3389:         }
 3390:     } else {
 3391:         @mailings = ('helpdeskmail','otherdomsmail');
 3392:         foreach my $type (@mailings) {
 3393:             $otheremails{$type} = '';
 3394:         }
 3395:         $bccemails{'helpdeskmail'} = '';
 3396:         $bccemails{'otherdomsmail'} = '';
 3397:         $includestr{'helpdeskmail'} = '';
 3398:         $includestr{'otherdomsmail'} = '';
 3399:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3400:     }
 3401:     if (ref($settings) eq 'HASH') {
 3402:         unless (($position eq 'top') || ($position eq 'lower')) {
 3403:             foreach my $type (@mailings) {
 3404:                 if (exists($settings->{$type})) {
 3405:                     if (ref($settings->{$type}) eq 'HASH') {
 3406:                         foreach my $item (@contacts) {
 3407:                             if ($settings->{$type}{$item}) {
 3408:                                 $checked{$type}{$item} = ' checked="checked" ';
 3409:                             }
 3410:                         }
 3411:                         $otheremails{$type} = $settings->{$type}{'others'};
 3412:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3413:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3414:                             if ($settings->{$type}{'include'} ne '') {
 3415:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3416:                                 $includestr{$type} = &unescape($includestr{$type});
 3417:                             }
 3418:                         }
 3419:                     }
 3420:                 } elsif ($type eq 'lonstatusmail') {
 3421:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3422:                 }
 3423:             }
 3424:         }
 3425:         if ($position eq 'bottom') {
 3426:             foreach my $type (@mailings) {
 3427:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3428:                 if ($settings->{$type}{'include'} ne '') {
 3429:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3430:                     $includestr{$type} = &unescape($includestr{$type});
 3431:                 }
 3432:             }
 3433:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3434:                 if (ref($fields) eq 'ARRAY') {
 3435:                     foreach my $field (@{$fields}) {
 3436:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3437:                     }
 3438:                 }
 3439:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3440:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3441:                 } else {
 3442:                     $maxsize = '1.0';
 3443:                 }
 3444:             } else {
 3445:                 if (ref($fields) eq 'ARRAY') {
 3446:                     foreach my $field (@{$fields}) {
 3447:                         $currfield{$field} = 'yes';
 3448:                     }
 3449:                 }
 3450:                 $maxsize = '1.0';
 3451:             }
 3452:         }
 3453:     } else {
 3454:         if ($position eq 'top') {
 3455:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3456:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3457:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3458:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3459:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3460:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3461:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3462:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3463:         } elsif ($position eq 'bottom') {
 3464:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3465:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3466:             if (ref($fields) eq 'ARRAY') {
 3467:                 foreach my $field (@{$fields}) {
 3468:                     $currfield{$field} = 'yes';
 3469:                 }
 3470:             }
 3471:             $maxsize = '1.0';
 3472:         }
 3473:     }
 3474:     my ($titles,$short_titles) = &contact_titles();
 3475:     my $rownum = 0;
 3476:     my $css_class;
 3477:     if ($position eq 'top') {
 3478:         foreach my $item (@contacts) {
 3479:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3480:             $datatable .= '<tr'.$css_class.'>'. 
 3481:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3482:                           '</span></td><td class="LC_right_item">'.
 3483:                           '<input type="text" name="'.$item.'" value="'.
 3484:                           $to{$item}.'" /></td></tr>';
 3485:             $rownum ++;
 3486:         }
 3487:     } elsif ($position eq 'bottom') {
 3488:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3489:         $datatable .= '<tr'.$css_class.'>'.
 3490:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3491:                       &mt('(e-mail, subject, and description always shown)').
 3492:                       '</td><td class="LC_left_item">';
 3493:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3494:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3495:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3496:             foreach my $field (@{$fields}) {
 3497:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3498:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3499:                     $datatable .= ' '.&mt('(logged-in users)');
 3500:                 }
 3501:                 $datatable .='</td><td>';
 3502:                 my $clickaction;
 3503:                 if ($field eq 'screenshot') {
 3504:                     $clickaction = ' onclick="screenshotSize(this);"';
 3505:                 }
 3506:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3507:                     foreach my $option (@{$possoptions->{$field}}) {
 3508:                         my $checked;
 3509:                         if ($currfield{$field} eq $option) {
 3510:                             $checked = ' checked="checked"';
 3511:                         }
 3512:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3513:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3514:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3515:                                       '</label></span>'.('&nbsp;'x2);
 3516:                     }
 3517:                 }
 3518:                 if ($field eq 'screenshot') {
 3519:                     my $display;
 3520:                     if ($currfield{$field} eq 'no') {
 3521:                         $display = ' style="display:none"';
 3522:                     }
 3523:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3524:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3525:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3526:                 }
 3527:                 $datatable .= '</td></tr>';
 3528:             }
 3529:             $datatable .= '</table>';
 3530:         }
 3531:         $datatable .= '</td></tr>'."\n";
 3532:         $rownum ++;
 3533:     }
 3534:     unless (($position eq 'top') || ($position eq 'lower')) {
 3535:         foreach my $type (@mailings) {
 3536:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3537:             $datatable .= '<tr'.$css_class.'>'.
 3538:                           '<td><span class="LC_nobreak">'.
 3539:                           $titles->{$type}.': </span></td>'.
 3540:                           '<td class="LC_left_item">';
 3541:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3542:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3543:             }
 3544:             $datatable .= '<span class="LC_nobreak">';
 3545:             foreach my $item (@contacts) {
 3546:                 $datatable .= '<label>'.
 3547:                               '<input type="checkbox" name="'.$type.'"'.
 3548:                               $checked{$type}{$item}.
 3549:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3550:                               '</label>&nbsp;';
 3551:             }
 3552:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3553:                           '<input type="text" name="'.$type.'_others" '.
 3554:                           'value="'.$otheremails{$type}.'"  />';
 3555:             my %locchecked;
 3556:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3557:                 foreach my $loc ('s','b') {
 3558:                     if ($includeloc{$type} eq $loc) {
 3559:                         $locchecked{$loc} = ' checked="checked"';
 3560:                         last;
 3561:                     }
 3562:                 }
 3563:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3564:                               '<input type="text" name="'.$type.'_bcc" '.
 3565:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3566:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3567:                               &mt('Text automatically added to e-mail:').' '.
 3568:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3569:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3570:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3571:                               ('&nbsp;'x2).
 3572:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3573:                               '</span></fieldset>';
 3574:             }
 3575:             $datatable .= '</td></tr>'."\n";
 3576:             $rownum ++;
 3577:         }
 3578:     }
 3579:     if ($position eq 'middle') {
 3580:         my %choices;
 3581:         my $corelink = &core_link_msu();
 3582:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3583:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3584:                                         $corelink);
 3585:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3586:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3587:         my %defaultchecked = ('reporterrors'  => 'on',
 3588:                               'reportupdates' => 'on',
 3589:                               'reportstatus'  => 'on');
 3590:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3591:                                                    \%choices,$rownum);
 3592:         $datatable .= $reports;
 3593:     } elsif ($position eq 'lower') {
 3594:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3595:         my ($threshold,$sysmail,%excluded,%weights);
 3596:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3597:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3598:             $threshold = $lonstatus{'threshold'};
 3599:         } else {
 3600:             $threshold = $defaults->{'threshold'};
 3601:         }
 3602:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3603:             $sysmail = $lonstatus{'sysmail'};
 3604:         } else {
 3605:             $sysmail = $defaults->{'sysmail'};
 3606:         }
 3607:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3608:             foreach my $type ('E','W','N','U') {
 3609:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3610:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3611:                 } else {
 3612:                     $weights{$type} = $defaults->{$type};
 3613:                 }
 3614:             }
 3615:         } else {
 3616:             foreach my $type ('E','W','N','U') {
 3617:                 $weights{$type} = $defaults->{$type};
 3618:             }
 3619:         }
 3620:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3621:             if (@{$lonstatus{'excluded'}} > 0) {
 3622:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3623:             }
 3624:         }
 3625:         $datatable .= '<tr'.$css_class.'>'.
 3626:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3627:                       $titles->{'errorthreshold'}.
 3628:                       '</span></td><td class="LC_left_item">'.
 3629:                       '<input type="text" name="errorthreshold" value="'.
 3630:                       $threshold.'" size="5" /></td></tr>';
 3631:         $rownum ++;
 3632:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3633:         $datatable .= '<tr'.$css_class.'>'.
 3634:                       '<td class="LC_left_item">'.
 3635:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3636:                       '</span></td><td class="LC_left_item"><table><tr>';
 3637:         foreach my $type ('E','W','N','U') {
 3638:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3639:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3640:                           $weights{$type}.'" size="5" /></td>';
 3641:         }
 3642:         $datatable .= '</tr></table></tr>';
 3643:         $rownum ++;
 3644:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3645:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3646:                       $titles->{'errorexcluded'}.'</td>'.
 3647:                       '<td class="LC_left_item"><table>';
 3648:         my $numinrow = 4;
 3649:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3650:         for (my $i=0; $i<@ids; $i++) {
 3651:             my $rem = $i%($numinrow);
 3652:             if ($rem == 0) {
 3653:                 if ($i > 0) {
 3654:                     $datatable .= '</tr>';
 3655:                 }
 3656:                 $datatable .= '<tr>';
 3657:             }
 3658:             my $check;
 3659:             if ($excluded{$ids[$i]}) {
 3660:                 $check = ' checked="checked" ';
 3661:             }
 3662:             $datatable .= '<td class="LC_left_item">'.
 3663:                           '<span class="LC_nobreak"><label>'.
 3664:                           '<input type="checkbox" name="errorexcluded" '.
 3665:                           'value="'.$ids[$i].'"'.$check.' />'.
 3666:                           $ids[$i].'</label></span></td>';
 3667:         }
 3668:         my $colsleft = $numinrow - @ids%($numinrow);
 3669:         if ($colsleft > 1 ) {
 3670:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3671:                           '&nbsp;</td>';
 3672:         } elsif ($colsleft == 1) {
 3673:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3674:         }
 3675:         $datatable .= '</tr></table></td></tr>';
 3676:         $rownum ++;
 3677:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3678:         $datatable .= '<tr'.$css_class.'>'.
 3679:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3680:                       $titles->{'errorsysmail'}.
 3681:                       '</span></td><td class="LC_left_item">'.
 3682:                       '<input type="text" name="errorsysmail" value="'.
 3683:                       $sysmail.'" size="5" /></td></tr>';
 3684:         $rownum ++;
 3685:     } elsif ($position eq 'bottom') {
 3686:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3687:         my (@posstypes,%usertypeshash);
 3688:         if (ref($types) eq 'ARRAY') {
 3689:             @posstypes = @{$types};
 3690:         }
 3691:         if (@posstypes) {
 3692:             if (ref($usertypes) eq 'HASH') {
 3693:                 %usertypeshash = %{$usertypes};
 3694:             }
 3695:             my @overridden;
 3696:             my $numinrow = 4;
 3697:             if (ref($settings) eq 'HASH') {
 3698:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3699:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3700:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3701:                             push(@overridden,$key);
 3702:                             foreach my $item (@contacts) {
 3703:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3704:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3705:                                 }
 3706:                             }
 3707:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3708:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3709:                             $includeloc{'override_'.$key} = '';
 3710:                             $includestr{'override_'.$key} = '';
 3711:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3712:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3713:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3714:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3715:                             }
 3716:                         }
 3717:                     }
 3718:                 }
 3719:             }
 3720:             my $customclass = 'LC_helpdesk_override';
 3721:             my $optionsprefix = 'LC_options_helpdesk_';
 3722: 
 3723:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3724:  
 3725:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3726:                                          $numinrow,$othertitle,'overrides',
 3727:                                          \$rownum,$onclicktypes,$customclass);
 3728:             $rownum ++;
 3729:             $usertypeshash{'default'} = $othertitle;
 3730:             foreach my $status (@posstypes) {
 3731:                 my $css_class;
 3732:                 if ($rownum%2) {
 3733:                     $css_class = 'LC_odd_row ';
 3734:                 }
 3735:                 $css_class .= $customclass;
 3736:                 my $rowid = $optionsprefix.$status;
 3737:                 my $hidden = 1;
 3738:                 my $currstyle = 'display:none';
 3739:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3740:                     $currstyle = 'display:table-row';
 3741:                     $hidden = 0;
 3742:                 }
 3743:                 my $key = 'override_'.$status;
 3744:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3745:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3746:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3747:                                                   \@contacts,$short_titles);
 3748:                 unless ($hidden) {
 3749:                     $rownum ++;
 3750:                 }
 3751:             }
 3752:         }
 3753:     }
 3754:     $$rowtotal += $rownum;
 3755:     return $datatable;
 3756: }
 3757: 
 3758: sub core_link_msu {
 3759:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3760:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3761: }
 3762: 
 3763: sub overridden_helpdesk {
 3764:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3765:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3766:     my $class = 'LC_left_item';
 3767:     if ($css_class) {
 3768:         $css_class = ' class="'.$css_class.'"';
 3769:     }
 3770:     if ($rowid) {
 3771:         $rowid = ' id="'.$rowid.'"';
 3772:     }
 3773:     if ($rowstyle) {
 3774:         $rowstyle = ' style="'.$rowstyle.'"';
 3775:     }
 3776:     my ($output,$description);
 3777:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3778:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3779:               "<td>$description</td>\n".
 3780:               '<td class="'.$class.'" colspan="2">'.
 3781:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3782:               '<span class="LC_nobreak">';
 3783:     if (ref($contacts) eq 'ARRAY') {
 3784:         foreach my $item (@{$contacts}) {
 3785:             my $check;
 3786:             if (ref($checked) eq 'HASH') {
 3787:                $check = $checked->{$item};
 3788:             }
 3789:             my $title;
 3790:             if (ref($short_titles) eq 'HASH') {
 3791:                 $title = $short_titles->{$item}; 
 3792:             }
 3793:             $output .= '<label>'.
 3794:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3795:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3796:         }
 3797:     }
 3798:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3799:                '<input type="text" name="override_'.$type.'_others" '.
 3800:                'value="'.$otheremails.'"  />';
 3801:     my %locchecked;
 3802:     foreach my $loc ('s','b') {
 3803:         if ($includeloc eq $loc) {
 3804:             $locchecked{$loc} = ' checked="checked"';
 3805:             last;
 3806:         }
 3807:     }
 3808:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3809:                '<input type="text" name="override_'.$type.'_bcc" '.
 3810:                'value="'.$bccemails.'"  /></fieldset>'.
 3811:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3812:                &mt('Text automatically added to e-mail:').' '.
 3813:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3814:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3815:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3816:                ('&nbsp;'x2).
 3817:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3818:                '</span></fieldset>'.
 3819:                '</td></tr>'."\n";
 3820:     return $output;
 3821: }
 3822: 
 3823: sub contacts_javascript {
 3824:     return <<"ENDSCRIPT";
 3825: 
 3826: <script type="text/javascript">
 3827: // <![CDATA[
 3828: 
 3829: function screenshotSize(field) {
 3830:     if (document.getElementById('help_screenshotsize')) {
 3831:         if (field.value == 'no') {
 3832:             document.getElementById('help_screenshotsize').style.display="none";
 3833:         } else {
 3834:             document.getElementById('help_screenshotsize').style.display="";
 3835:         }
 3836:     }
 3837:     return;
 3838: }
 3839: 
 3840: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3841:     if (form.elements[checkbox].length != undefined) {
 3842:         var count = 0;
 3843:         if (docount) {
 3844:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3845:                 if (form.elements[checkbox][i].checked) {
 3846:                     count ++;
 3847:                 }
 3848:             }
 3849:         }
 3850:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3851:             var type = form.elements[checkbox][i].value;
 3852:             if (document.getElementById(prefix+type)) {
 3853:                 if (form.elements[checkbox][i].checked) {
 3854:                     document.getElementById(prefix+type).style.display = 'table-row';
 3855:                     if (count % 2 == 1) {
 3856:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3857:                     } else {
 3858:                         document.getElementById(prefix+type).className = target;
 3859:                     }
 3860:                     count ++;
 3861:                 } else {
 3862:                     document.getElementById(prefix+type).style.display = 'none';
 3863:                 }
 3864:             }
 3865:         }
 3866:     }
 3867:     return;
 3868: }
 3869: 
 3870: 
 3871: // ]]>
 3872: </script>
 3873: 
 3874: ENDSCRIPT
 3875: }
 3876: 
 3877: sub print_helpsettings {
 3878:     my ($position,$dom,$settings,$rowtotal) = @_;
 3879:     my $confname = $dom.'-domainconfig';
 3880:     my $formname = 'display';
 3881:     my ($datatable,$itemcount);
 3882:     if ($position eq 'top') {
 3883:         $itemcount = 1;
 3884:         my (%choices,%defaultchecked,@toggles);
 3885:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3886:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3887:                                      &mt('LON-CAPA bug tracker'),600,500));
 3888:         %defaultchecked = ('submitbugs' => 'on');
 3889:         @toggles = ('submitbugs');
 3890:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3891:                                                      \%choices,$itemcount);
 3892:         $$rowtotal ++;
 3893:     } else {
 3894:         my $css_class;
 3895:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3896:         my (%customroles,%ordered,%current);
 3897:         if (ref($settings) eq 'HASH') {
 3898:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3899:                 %current = %{$settings->{'adhoc'}};
 3900:             }
 3901:         }
 3902:         my $count = 0;
 3903:         foreach my $key (sort(keys(%existing))) {
 3904:             if ($key=~/^rolesdef\_(\w+)$/) {
 3905:                 my $rolename = $1;
 3906:                 my (%privs,$order);
 3907:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3908:                 $customroles{$rolename} = \%privs;
 3909:                 if (ref($current{$rolename}) eq 'HASH') {
 3910:                     $order = $current{$rolename}{'order'};
 3911:                 }
 3912:                 if ($order eq '') {
 3913:                     $order = $count;
 3914:                 }
 3915:                 $ordered{$order} = $rolename;
 3916:                 $count++;
 3917:             }
 3918:         }
 3919:         my $maxnum = scalar(keys(%ordered));
 3920:         my @roles_by_num = ();
 3921:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3922:             push(@roles_by_num,$item);
 3923:         }
 3924:         my $context = 'domprefs';
 3925:         my $crstype = 'Course';
 3926:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3927:         my @accesstypes = ('all','dh','da','none');
 3928:         my ($numstatustypes,@jsarray);
 3929:         if (ref($types) eq 'ARRAY') {
 3930:             if (@{$types} > 0) {
 3931:                 $numstatustypes = scalar(@{$types});
 3932:                 push(@accesstypes,'status');
 3933:                 @jsarray = ('bystatus');
 3934:             }
 3935:         }
 3936:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3937:         if (keys(%domhelpdesk)) {
 3938:             push(@accesstypes,('inc','exc'));
 3939:             push(@jsarray,('notinc','notexc'));
 3940:         }
 3941:         my $hiddenstr = join("','",@jsarray);
 3942:         my $context = 'domprefs';
 3943:         my $crstype = 'Course';
 3944:         my $prefix = 'helproles_';
 3945:         my $add_class = 'LC_hidden';
 3946:         foreach my $num (@roles_by_num) {
 3947:             my $role = $ordered{$num};
 3948:             my ($desc,$access,@statuses);
 3949:             if (ref($current{$role}) eq 'HASH') {
 3950:                 $desc = $current{$role}{'desc'};
 3951:                 $access = $current{$role}{'access'};
 3952:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3953:                     @statuses = @{$current{$role}{'insttypes'}};
 3954:                 }
 3955:             }
 3956:             if ($desc eq '') {
 3957:                 $desc = $role;
 3958:             }
 3959:             my $identifier = 'custhelp'.$num;
 3960:             my %full=();
 3961:             my %levels= (
 3962:                          course => {},
 3963:                          domain => {},
 3964:                          system => {},
 3965:                         );
 3966:             my %levelscurrent=(
 3967:                                course => {},
 3968:                                domain => {},
 3969:                                system => {},
 3970:                               );
 3971:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3972:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3973:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3974:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3975:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 3976:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3977:             for (my $k=0; $k<=$maxnum; $k++) {
 3978:                 my $vpos = $k+1;
 3979:                 my $selstr;
 3980:                 if ($k == $num) {
 3981:                     $selstr = ' selected="selected" ';
 3982:                 }
 3983:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3984:             }
 3985:             $datatable .= '</select>'.('&nbsp;'x2).
 3986:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3987:                           '</td>'.
 3988:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3989:                           &mt('Name shown to users:').
 3990:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3991:                           '</fieldset>'.
 3992:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3993:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3994:                           '<fieldset>'.
 3995:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3996:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3997:                                                                    \%levelscurrent,$identifier,
 3998:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3999:                           '</fieldset></td>';
 4000:             $itemcount ++;
 4001:         }
 4002:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4003:         my $newcust = 'custhelp'.$count;
 4004:         my (%privs,%levelscurrent);
 4005:         my %full=();
 4006:         my %levels= (
 4007:                      course => {},
 4008:                      domain => {},
 4009:                      system => {},
 4010:                     );
 4011:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4012:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4013:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4014:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4015:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4016:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4017:         for (my $k=0; $k<$maxnum+1; $k++) {
 4018:             my $vpos = $k+1;
 4019:             my $selstr;
 4020:             if ($k == $maxnum) {
 4021:                 $selstr = ' selected="selected" ';
 4022:             }
 4023:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4024:         }
 4025:         $datatable .= '</select>&nbsp;'."\n".
 4026:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4027:                       '</label></span></td>'.
 4028:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4029:                       '<span class="LC_nobreak">'.
 4030:                       &mt('Internal name:').
 4031:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4032:                       '</span>'.('&nbsp;'x4).
 4033:                       '<span class="LC_nobreak">'.
 4034:                       &mt('Name shown to users:').
 4035:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4036:                       '</span></fieldset>'.
 4037:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4038:                                              $usertypes,$types,\%domhelpdesk).
 4039:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4040:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4041:                                                                 \@templateroles,$newcust).
 4042:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4043:                                                                \%levelscurrent,$newcust).
 4044:                       '</fieldset>'.
 4045:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4046:                       '</td></tr>';
 4047:         $count ++;
 4048:         $$rowtotal += $count;
 4049:     }
 4050:     return $datatable;
 4051: }
 4052: 
 4053: sub adhocbutton {
 4054:     my ($prefix,$num,$field,$visibility) = @_;
 4055:     my %lt = &Apache::lonlocal::texthash(
 4056:                                           show => 'Show details',
 4057:                                           hide => 'Hide details',
 4058:                                         );
 4059:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4060:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4061:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4062:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4063: }
 4064: 
 4065: sub helpsettings_javascript {
 4066:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4067:     return unless(ref($roles_by_num) eq 'ARRAY');
 4068:     my %html_js_lt = &Apache::lonlocal::texthash(
 4069:                                           show => 'Show details',
 4070:                                           hide => 'Hide details',
 4071:                                         );
 4072:     &html_escape(\%html_js_lt);
 4073:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4074:     return <<"ENDSCRIPT";
 4075: <script type="text/javascript">
 4076: // <![CDATA[
 4077: 
 4078: function reorderHelpRoles(form,item) {
 4079:     var changedVal;
 4080: $jstext
 4081:     var newpos = 'helproles_${total}_pos';
 4082:     var maxh = 1 + $total;
 4083:     var current = new Array();
 4084:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4085:     if (item == newpos) {
 4086:         changedVal = newitemVal;
 4087:     } else {
 4088:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4089:         current[newitemVal] = newpos;
 4090:     }
 4091:     for (var i=0; i<helproles.length; i++) {
 4092:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4093:         if (elementName != item) {
 4094:             if (form.elements[elementName]) {
 4095:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4096:                 current[currVal] = elementName;
 4097:             }
 4098:         }
 4099:     }
 4100:     var oldVal;
 4101:     for (var j=0; j<maxh; j++) {
 4102:         if (current[j] == undefined) {
 4103:             oldVal = j;
 4104:         }
 4105:     }
 4106:     if (oldVal < changedVal) {
 4107:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4108:            var elementName = current[k];
 4109:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4110:         }
 4111:     } else {
 4112:         for (var k=changedVal; k<oldVal; k++) {
 4113:             var elementName = current[k];
 4114:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4115:         }
 4116:     }
 4117:     return;
 4118: }
 4119: 
 4120: function helpdeskAccess(num) {
 4121:     var curraccess = null;
 4122:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4123:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4124:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4125:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4126:             }
 4127:         }
 4128:     }
 4129:     var shown = Array();
 4130:     var hidden = Array();
 4131:     if (curraccess == 'none') {
 4132:         hidden = Array('$hiddenstr');
 4133:     } else {
 4134:         if (curraccess == 'status') {
 4135:             shown = Array('bystatus');
 4136:             hidden = Array('notinc','notexc');
 4137:         } else {
 4138:             if (curraccess == 'exc') {
 4139:                 shown = Array('notexc');
 4140:                 hidden = Array('notinc','bystatus');
 4141:             }
 4142:             if (curraccess == 'inc') {
 4143:                 shown = Array('notinc');
 4144:                 hidden = Array('notexc','bystatus');
 4145:             }
 4146:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4147:                 hidden = Array('notinc','notexc','bystatus');
 4148:             }
 4149:         }
 4150:     }
 4151:     if (hidden.length > 0) {
 4152:         for (var i=0; i<hidden.length; i++) {
 4153:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4154:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4155:             }
 4156:         }
 4157:     }
 4158:     if (shown.length > 0) {
 4159:         for (var i=0; i<shown.length; i++) {
 4160:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4161:                 if (shown[i] == 'privs') {
 4162:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4163:                 } else {
 4164:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4165:                 }
 4166:             }
 4167:         }
 4168:     }
 4169:     return;
 4170: }
 4171: 
 4172: function toggleHelpdeskItem(num,field) {
 4173:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4174:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4175:             document.getElementById('helproles_'+num+'_'+field).className =
 4176:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4177:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4178:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4179:             }
 4180:         } else {
 4181:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4182:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4183:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4184:             }
 4185:         }
 4186:     }
 4187:     return;
 4188: }
 4189: 
 4190: // ]]>
 4191: </script>
 4192: 
 4193: ENDSCRIPT
 4194: }
 4195: 
 4196: sub helpdeskroles_access {
 4197:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4198:         $usertypes,$types,$domhelpdesk) = @_;
 4199:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4200:     my %lt = &Apache::lonlocal::texthash(
 4201:                     'rou'    => 'Role usage',
 4202:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4203:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4204:                     'dh'     => 'All with domain helpdesk role',
 4205:                     'da'     => 'All with domain helpdesk assistant role',
 4206:                     'none'   => 'None',
 4207:                     'status' => 'Determined based on institutional status',
 4208:                     'inc'    => 'Include all, but exclude specific personnel',
 4209:                     'exc'    => 'Exclude all, but include specific personnel',
 4210:                   );
 4211:     my %usecheck = (
 4212:                      all => ' checked="checked"',
 4213:                    );
 4214:     my %displaydiv = (
 4215:                       status => 'none',
 4216:                       inc    => 'none',
 4217:                       exc    => 'none',
 4218:                       priv   => 'block',
 4219:                      );
 4220:     my $output;
 4221:     if (ref($current) eq 'HASH') {
 4222:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4223:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4224:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4225:                 delete($usecheck{'all'});
 4226:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4227:                     my $access = $1;
 4228:                     $displaydiv{$access} = 'inline';
 4229:                 } elsif ($current->{access} eq 'none') {
 4230:                     $displaydiv{'priv'} = 'none';
 4231:                 }
 4232:             }
 4233:         }
 4234:     }
 4235:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4236:               '<p>'.$lt{'whi'}.'</p>';
 4237:     foreach my $access (@{$accesstypes}) {
 4238:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4239:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4240:                    $lt{$access}.'</label>';
 4241:         if ($access eq 'status') {
 4242:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4243:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4244:                                                                  $othertitle,$usertypes,$types).
 4245:                        '</div>';
 4246:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4247:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4248:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4249:                        '</div>';
 4250:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4251:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4252:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4253:                        '</div>';
 4254:         }
 4255:         $output .= '</p>';
 4256:     }
 4257:     $output .= '</fieldset>';
 4258:     return $output;
 4259: }
 4260: 
 4261: sub radiobutton_prefs {
 4262:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4263:         $additional,$align) = @_;
 4264:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4265:                    (ref($choices) eq 'HASH'));
 4266: 
 4267:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4268: 
 4269:     foreach my $item (@{$toggles}) {
 4270:         if ($defaultchecked->{$item} eq 'on') {
 4271:             $checkedon{$item} = ' checked="checked" ';
 4272:             $checkedoff{$item} = ' ';
 4273:         } elsif ($defaultchecked->{$item} eq 'off') {
 4274:             $checkedoff{$item} = ' checked="checked" ';
 4275:             $checkedon{$item} = ' ';
 4276:         }
 4277:     }
 4278:     if (ref($settings) eq 'HASH') {
 4279:         foreach my $item (@{$toggles}) {
 4280:             if ($settings->{$item} eq '1') {
 4281:                 $checkedon{$item} =  ' checked="checked" ';
 4282:                 $checkedoff{$item} = ' ';
 4283:             } elsif ($settings->{$item} eq '0') {
 4284:                 $checkedoff{$item} =  ' checked="checked" ';
 4285:                 $checkedon{$item} = ' ';
 4286:             }
 4287:         }
 4288:     }
 4289:     if ($onclick) {
 4290:         $onclick = ' onclick="'.$onclick.'"';
 4291:     }
 4292:     foreach my $item (@{$toggles}) {
 4293:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4294:         $datatable .=
 4295:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4296:             '<span class="LC_nobreak">'.$choices->{$item}.
 4297:             '</span></td>';
 4298:         if ($align eq 'left') {
 4299:             $datatable .= '<td class="LC_left_item">';
 4300:         } else {
 4301:             $datatable .= '<td class="LC_right_item">';
 4302:         }
 4303:         $datatable .=
 4304:             '<span class="LC_nobreak">'.
 4305:             '<label><input type="radio" name="'.
 4306:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4307:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4308:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4309:             '</span>'.$additional.
 4310:             '</td>'.
 4311:             '</tr>';
 4312:         $itemcount ++;
 4313:     }
 4314:     return ($datatable,$itemcount);
 4315: }
 4316: 
 4317: sub print_ltitools {
 4318:     my ($dom,$settings,$rowtotal) = @_;
 4319:     my $rownum = 0;
 4320:     my $css_class;
 4321:     my $itemcount = 1;
 4322:     my $maxnum = 0;
 4323:     my %ordered;
 4324:     if (ref($settings) eq 'HASH') {
 4325:         foreach my $item (keys(%{$settings})) {
 4326:             if (ref($settings->{$item}) eq 'HASH') {
 4327:                 my $num = $settings->{$item}{'order'};
 4328:                 $ordered{$num} = $item;
 4329:             }
 4330:         }
 4331:     }
 4332:     my $confname = $dom.'-domainconfig';
 4333:     my $switchserver = &check_switchserver($dom,$confname);
 4334:     my $maxnum = scalar(keys(%ordered));
 4335:     my $datatable;
 4336:     my %lt = &ltitools_names();
 4337:     my @courseroles = ('cc','in','ta','ep','st');
 4338:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4339:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4340:     if (keys(%ordered)) {
 4341:         my @items = sort { $a <=> $b } keys(%ordered);
 4342:         for (my $i=0; $i<@items; $i++) {
 4343:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4344:             my $item = $ordered{$items[$i]};
 4345:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4346:             if (ref($settings->{$item}) eq 'HASH') {
 4347:                 $title = $settings->{$item}->{'title'};
 4348:                 $url = $settings->{$item}->{'url'};
 4349:                 $key = $settings->{$item}->{'key'};
 4350:                 $secret = $settings->{$item}->{'secret'};
 4351:                 $lifetime = $settings->{$item}->{'lifetime'};
 4352:                 my $image = $settings->{$item}->{'image'};
 4353:                 if ($image ne '') {
 4354:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4355:                 }
 4356:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4357:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4358:                 } else {
 4359:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4360:                 }
 4361:             }
 4362:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4363:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4364:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4365:             for (my $k=0; $k<=$maxnum; $k++) {
 4366:                 my $vpos = $k+1;
 4367:                 my $selstr;
 4368:                 if ($k == $i) {
 4369:                     $selstr = ' selected="selected" ';
 4370:                 }
 4371:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4372:             }
 4373:             $datatable .= '</select>'.('&nbsp;'x2).
 4374:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4375:                 &mt('Delete?').'</label></span></td>'.
 4376:                 '<td colspan="2">'.
 4377:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4378:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4379:                 ('&nbsp;'x2).
 4380:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4381:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4382:                 ('&nbsp;'x2).
 4383:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4384:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4385:                 ('&nbsp;'x2).
 4386:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4387:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4388:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4389:                 '<br /><br />'.
 4390:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4391:                 ' value="'.$url.'" /></span>'.
 4392:                 ('&nbsp;'x2).
 4393:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4394:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4395:                 ('&nbsp;'x2).
 4396:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4397:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4398:                 ('&nbsp;'x2).
 4399:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4400:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4401:                 '<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>'.
 4402:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4403:                 '</fieldset>'.
 4404:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4405:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4406:             my %currdisp;
 4407:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4408:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4409:                     $currdisp{'window'} = ' checked="checked"';
 4410:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4411:                     $currdisp{'tab'} = ' checked="checked"';
 4412:                 } else {
 4413:                     $currdisp{'iframe'} = ' checked="checked"';
 4414:                 }
 4415:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4416:                     $currdisp{'width'} = $1;
 4417:                 }
 4418:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4419:                      $currdisp{'height'} = $1;
 4420:                 }
 4421:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4422:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4423:             } else {
 4424:                 $currdisp{'iframe'} = ' checked="checked"';
 4425:             }
 4426:             foreach my $disp ('iframe','tab','window') {
 4427:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4428:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4429:             }
 4430:             $datatable .= ('&nbsp;'x4);
 4431:             foreach my $dimen ('width','height') {
 4432:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4433:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4434:                               ('&nbsp;'x2);
 4435:             }
 4436:             $datatable .= '</span><br />'.
 4437:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4438:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4439:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4440:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4441:                           '</textarea></div><div style=""></div><br />';
 4442:             my %units = (
 4443:                           'passback' => 'days',
 4444:                           'roster'   => 'seconds',
 4445:                         );
 4446:             foreach my $extra ('passback','roster') {
 4447:                 my $validsty = 'none';
 4448:                 my $currvalid;
 4449:                 my $checkedon = '';
 4450:                 my $checkedoff = ' checked="checked"';
 4451:                 if ($settings->{$item}->{$extra}) {
 4452:                     $checkedon = $checkedoff;
 4453:                     $checkedoff = '';
 4454:                     $validsty = 'inline-block';
 4455:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4456:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4457:                     }
 4458:                 }
 4459:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4460:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4461:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4462:                               &mt('No').'</label>'.('&nbsp;'x2).
 4463:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4464:                               &mt('Yes').'</label></span></div>'.
 4465:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4466:                               '<span class="LC_nobreak">'.
 4467:                               &mt("at least [_1] $units{$extra} after launch",
 4468:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4469:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4470:             }
 4471:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4472:             if ($imgsrc) {
 4473:                 $datatable .= $imgsrc.
 4474:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4475:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4476:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4477:             } else {
 4478:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4479:             }
 4480:             if ($switchserver) {
 4481:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4482:             } else {
 4483:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4484:             }
 4485:             $datatable .= '</span></fieldset>';
 4486:             my (%checkedfields,%rolemaps,$userincdom);
 4487:             if (ref($settings->{$item}) eq 'HASH') {
 4488:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4489:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4490:                 }
 4491:                 $userincdom = $settings->{$item}->{'incdom'};
 4492:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4493:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4494:                     $checkedfields{'roles'} = 1;
 4495:                 }
 4496:             }
 4497:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4498:                           '<span class="LC_nobreak">';
 4499:             my $userfieldstyle = 'display:none;';
 4500:             my $seluserdom = '';
 4501:             my $unseluserdom = ' selected="selected"';
 4502:             foreach my $field (@fields) {
 4503:                 my ($checked,$onclick,$id,$spacer);
 4504:                 if ($checkedfields{$field}) {
 4505:                     $checked = ' checked="checked"';
 4506:                 }
 4507:                 if ($field eq 'user') {
 4508:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4509:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4510:                     if ($checked) {
 4511:                         $userfieldstyle = 'display:inline-block';
 4512:                         if ($userincdom) {
 4513:                             $seluserdom = $unseluserdom;
 4514:                             $unseluserdom = '';
 4515:                         }
 4516:                     }
 4517:                 } else {
 4518:                     $spacer = ('&nbsp;' x2);
 4519:                 }
 4520:                 $datatable .= '<label>'.
 4521:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4522:                               $lt{$field}.'</label>'.$spacer;
 4523:             }
 4524:             $datatable .= '</span>';
 4525:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4526:                           '<span class="LC_nobreak"> : '.
 4527:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4528:                           '<option value="">'.&mt('Select').'</option>'.
 4529:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4530:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4531:                           '</select></span></div>';
 4532:             $datatable .= '</fieldset>'.
 4533:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4534:             foreach my $role (@courseroles) {
 4535:                 my ($selected,$selectnone);
 4536:                 if (!$rolemaps{$role}) {
 4537:                     $selectnone = ' selected="selected"';
 4538:                 }
 4539:                 $datatable .= '<td style="text-align: center">'. 
 4540:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4541:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4542:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4543:                 foreach my $ltirole (@ltiroles) {
 4544:                     unless ($selectnone) {
 4545:                         if ($rolemaps{$role} eq $ltirole) {
 4546:                             $selected = ' selected="selected"';
 4547:                         } else {
 4548:                             $selected = '';
 4549:                         }
 4550:                     }
 4551:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4552:                 }
 4553:                 $datatable .= '</select></td>';
 4554:             }
 4555:             $datatable .= '</tr></table></fieldset>';
 4556:             my %courseconfig;
 4557:             if (ref($settings->{$item}) eq 'HASH') {
 4558:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4559:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4560:                 }
 4561:             }
 4562:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4563:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4564:                 my $checked;
 4565:                 if ($courseconfig{$item}) {
 4566:                     $checked = ' checked="checked"';
 4567:                 }
 4568:                 $datatable .= '<label>'.
 4569:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4570:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4571:             }
 4572:             $datatable .= '</span></fieldset>'.
 4573:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4574:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4575:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4576:                 my %custom = %{$settings->{$item}->{'custom'}};
 4577:                 if (keys(%custom) > 0) {
 4578:                     foreach my $key (sort(keys(%custom))) {
 4579:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4580:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4581:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4582:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4583:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4584:                     }
 4585:                 }
 4586:             }
 4587:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4588:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4589:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4590:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4591:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4592:             $itemcount ++;
 4593:         }
 4594:     }
 4595:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4596:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4597:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4598:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4599:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4600:     for (my $k=0; $k<$maxnum+1; $k++) {
 4601:         my $vpos = $k+1;
 4602:         my $selstr;
 4603:         if ($k == $maxnum) {
 4604:             $selstr = ' selected="selected" ';
 4605:         }
 4606:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4607:     }
 4608:     $datatable .= '</select>&nbsp;'."\n".
 4609:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4610:                   '<td colspan="2">'.
 4611:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4612:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4613:                   ('&nbsp;'x2).
 4614:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4615:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4616:                   ('&nbsp;'x2).
 4617:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4618:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4619:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4620:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4621:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4622:                   '<br />'.
 4623:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4624:                   ('&nbsp;'x2).
 4625:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4626:                   ('&nbsp;'x2).
 4627:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4628:                   ('&nbsp;'x2).
 4629:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4630:                   '<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".
 4631:                   '</fieldset>'.
 4632:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4633:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4634:     my %defaultdisp;
 4635:     $defaultdisp{'iframe'} = ' checked="checked"';
 4636:     foreach my $disp ('iframe','tab','window') {
 4637:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4638:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4639:     }
 4640:     $datatable .= ('&nbsp;'x4);
 4641:     foreach my $dimen ('width','height') {
 4642:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4643:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4644:                       ('&nbsp;'x2);
 4645:     }
 4646:     $datatable .= '</span><br />'.
 4647:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4648:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4649:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4650:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4651:                   '</div><div style=""></div><br />';
 4652:     my %units = (
 4653:                   'passback' => 'days',
 4654:                   'roster'   => 'seconds',
 4655:                 );
 4656:     my %defaulttimes = (
 4657:                      'passback' => '7',
 4658:                      'roster'   => '300',
 4659:                    );
 4660:     foreach my $extra ('passback','roster') {
 4661:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4662:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4663:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4664:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4665:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4666:                       &mt('Yes').'</label></span></div>'.
 4667:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4668:                       '<span class="LC_nobreak">'.
 4669:                       &mt("at least [_1] $units{$extra} after launch",
 4670:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4671:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4672:     }
 4673:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4674:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4675:     if ($switchserver) {
 4676:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4677:     } else {
 4678:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4679:     }
 4680:     $datatable .= '</span></fieldset>'.
 4681:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4682:                   '<span class="LC_nobreak">';
 4683:     foreach my $field (@fields) {
 4684:         my ($id,$onclick,$spacer);
 4685:         if ($field eq 'user') {
 4686:             $id = ' id="ltitools_user_field_add"';
 4687:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4688:         } else {
 4689:             $spacer = ('&nbsp;' x2);
 4690:         }
 4691:         $datatable .= '<label>'.
 4692:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4693:                       $lt{$field}.'</label>'.$spacer;
 4694:     }
 4695:     $datatable .= '</span>'.
 4696:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4697:                   '<span class="LC_nobreak"> : '.
 4698:                   '<select name="ltitools_userincdom_add">'.
 4699:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4700:                   '<option value="0">'.&mt('username').'</option>'.
 4701:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4702:                   '</select></span></div></fieldset>';
 4703:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4704:     foreach my $role (@courseroles) {
 4705:         my ($checked,$checkednone);
 4706:         $datatable .= '<td style="text-align: center">'.
 4707:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4708:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4709:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4710:         foreach my $ltirole (@ltiroles) {
 4711:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4712:         }
 4713:         $datatable .= '</select></td>';
 4714:     }
 4715:     $datatable .= '</tr></table></fieldset>'.
 4716:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4717:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4718:         $datatable .= '<label>'.
 4719:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4720:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4721:     }
 4722:     $datatable .= '</span></fieldset>'.
 4723:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4724:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4725:                   '<tr><td><span class="LC_nobreak">'.
 4726:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4727:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4728:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4729:                   '</table></fieldset>'."\n".
 4730:                   '</td>'."\n".
 4731:                   '</tr>'."\n";
 4732:     $itemcount ++;
 4733:     return $datatable;
 4734: }
 4735: 
 4736: sub ltitools_names {
 4737:     my %lt = &Apache::lonlocal::texthash(
 4738:                                           'title'          => 'Title',
 4739:                                           'version'        => 'Version',
 4740:                                           'msgtype'        => 'Message Type',
 4741:                                           'sigmethod'      => 'Signature Method',
 4742:                                           'url'            => 'URL',
 4743:                                           'key'            => 'Key',
 4744:                                           'lifetime'       => 'Nonce lifetime (s)',
 4745:                                           'secret'         => 'Secret',
 4746:                                           'icon'           => 'Icon',   
 4747:                                           'user'           => 'User',
 4748:                                           'fullname'       => 'Full Name',
 4749:                                           'firstname'      => 'First Name',
 4750:                                           'lastname'       => 'Last Name',
 4751:                                           'email'          => 'E-mail',
 4752:                                           'roles'          => 'Role',
 4753:                                           'window'         => 'Window',
 4754:                                           'tab'            => 'Tab',
 4755:                                           'iframe'         => 'iFrame',
 4756:                                           'height'         => 'Height',
 4757:                                           'width'          => 'Width',
 4758:                                           'linktext'       => 'Default Link Text',
 4759:                                           'explanation'    => 'Default Explanation',
 4760:                                           'passback'       => 'Tool can return grades:',
 4761:                                           'roster'         => 'Tool can retrieve roster:',
 4762:                                           'crstarget'      => 'Display target',
 4763:                                           'crslabel'       => 'Course label',
 4764:                                           'crstitle'       => 'Course title', 
 4765:                                           'crslinktext'    => 'Link Text',
 4766:                                           'crsexplanation' => 'Explanation',
 4767:                                           'crsappend'      => 'Provider URL',
 4768:                                         );
 4769:     return %lt;
 4770: }
 4771: 
 4772: sub print_lti {
 4773:     my ($dom,$settings,$rowtotal) = @_;
 4774:     my $itemcount = 1;
 4775:     my $maxnum = 0;
 4776:     my $css_class;
 4777:     my %ordered;
 4778:     if (ref($settings) eq 'HASH') {
 4779:         foreach my $item (keys(%{$settings})) {
 4780:             if (ref($settings->{$item}) eq 'HASH') {
 4781:                 my $num = $settings->{$item}{'order'};
 4782:                 $ordered{$num} = $item;
 4783:             }
 4784:         }
 4785:     }
 4786:     my $maxnum = scalar(keys(%ordered));
 4787:     my $datatable;
 4788:     my %lt = &lti_names();
 4789:     if (keys(%ordered)) {
 4790:         my @items = sort { $a <=> $b } keys(%ordered);
 4791:         for (my $i=0; $i<@items; $i++) {
 4792:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4793:             my $item = $ordered{$items[$i]};
 4794:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 4795:             if (ref($settings->{$item}) eq 'HASH') {
 4796:                 $key = $settings->{$item}->{'key'};
 4797:                 $secret = $settings->{$item}->{'secret'};
 4798:                 $lifetime = $settings->{$item}->{'lifetime'};
 4799:                 $consumer = $settings->{$item}->{'consumer'};
 4800:                 $requser = $settings->{$item}->{'requser'};
 4801:                 $current = $settings->{$item};
 4802:             }
 4803:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 4804:             my %checkedrequser = (
 4805:                                    yes => ' checked="checked"',
 4806:                                    no  => '',
 4807:                                  );
 4808:             if (!$requser) {
 4809:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 4810:                 $checkedrequser{'yes'} = '';
 4811:             } 
 4812:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 4813:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4814:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 4815:             for (my $k=0; $k<=$maxnum; $k++) {
 4816:                 my $vpos = $k+1;
 4817:                 my $selstr;
 4818:                 if ($k == $i) {
 4819:                     $selstr = ' selected="selected" ';
 4820:                 }
 4821:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4822:             }
 4823:             $datatable .= '</select>'.('&nbsp;'x2).
 4824:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 4825:                 &mt('Delete?').'</label></span></td>'.
 4826:                 '<td colspan="2">'.
 4827:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4828:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 4829:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 4830:                 ('&nbsp;'x2).
 4831:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 4832:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4833:                 ('&nbsp;'x2).
 4834:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 4835:                 'value="'.$lifetime.'" size="3" /></span>'.
 4836:                 ('&nbsp;'x2).
 4837:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4838:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 4839:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 4840:                 '<br /><br />'.
 4841:                 '<span class="LC_nobreak">'.$lt{'key'}.
 4842:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 4843:                 ('&nbsp;'x2).
 4844:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4845:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 4846:                 '<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>'.
 4847:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 4848:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 4849:             $itemcount ++;
 4850:         }
 4851:     }
 4852:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4853:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 4854:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4855:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 4856:                   '<select name="lti_pos_add"'.$chgstr.'>';
 4857:     for (my $k=0; $k<$maxnum+1; $k++) {
 4858:         my $vpos = $k+1;
 4859:         my $selstr;
 4860:         if ($k == $maxnum) {
 4861:             $selstr = ' selected="selected" ';
 4862:         }
 4863:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4864:     }
 4865:     $datatable .= '</select>&nbsp;'."\n".
 4866:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4867:                   '<td colspan="2">'.
 4868:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4869:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 4870:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 4871:                   ('&nbsp;'x2).
 4872:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 4873:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4874:                   ('&nbsp;'x2).
 4875:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 4876:                   ('&nbsp;'x2).
 4877:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4878:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 4879:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 4880:                   '<br /><br />'.
 4881:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 4882:                   ('&nbsp;'x2).
 4883:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 4884:                   '<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".
 4885:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 4886:                   '</td>'."\n".
 4887:                   '</tr>'."\n";
 4888:     $$rowtotal ++;
 4889:     return $datatable;;
 4890: }
 4891: 
 4892: sub lti_names {
 4893:     my %lt = &Apache::lonlocal::texthash(
 4894:                                           'version'   => 'LTI Version',
 4895:                                           'url'       => 'URL',
 4896:                                           'key'       => 'Key',
 4897:                                           'lifetime'  => 'Nonce lifetime (s)',
 4898:                                           'consumer'  => 'Consumer',
 4899:                                           'secret'    => 'Secret',
 4900:                                           'requser'   => "User's identity sent",
 4901:                                           'email'     => 'Email address',
 4902:                                           'sourcedid' => 'User ID',
 4903:                                           'other'     => 'Other',
 4904:                                           'passback'  => 'Can return grades to Consumer:',
 4905:                                           'roster'    => 'Can retrieve roster from Consumer:',
 4906:                                           'topmenu'   => 'Display LON-CAPA page header',
 4907:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 4908:                                         );
 4909:     return %lt;
 4910: }
 4911: 
 4912: sub lti_options {
 4913:     my ($num,$current,$itemcount,%lt) = @_;
 4914:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield);
 4915:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 4916:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 4917:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 4918:     $checked{'mapcrstype'} = {};
 4919:     $checked{'makeuser'} = {};
 4920:     $checked{'selfenroll'} = {};
 4921:     $checked{'crssec'} = {};
 4922:     $checked{'crssecsrc'} = {};
 4923:     $checked{'lcauth'} = {};
 4924:     $checked{'menuitem'} = {};
 4925:     if ($num eq 'add') {
 4926:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 4927:     }
 4928:     my $userfieldsty = 'none';
 4929:     my $crsfieldsty = 'none';
 4930:     my $crssecfieldsty = 'none';
 4931:     my $secsrcfieldsty = 'none';
 4932:     my $passbacksty = 'none';
 4933:     my $optionsty = 'block';
 4934:     my $lcauthparm;
 4935:     my $lcauthparmstyle = 'display:none';
 4936:     my $lcauthparmtext;
 4937:     my $menusty;
 4938:     my $numinrow = 4;
 4939:     my %menutitles = &ltimenu_titles();
 4940: 
 4941:     if (ref($current) eq 'HASH') {
 4942:         if (!$current->{'requser'}) {
 4943:             $optionsty = 'none';
 4944:         }
 4945:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 4946:             $checked{'mapuser'}{'sourcedid'} = '';
 4947:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 4948:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 4949:             } else {
 4950:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 4951:                 $userfield = $current->{'mapuser'};
 4952:                 $userfieldsty = 'inline-block';
 4953:             }
 4954:         }
 4955:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 4956:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 4957:             if ($current->{'mapcrs'} eq 'context_id') {
 4958:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 4959:             } else {
 4960:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 4961:                 $cidfield = $current->{'mapcrs'};
 4962:                 $crsfieldsty = 'inline-block';
 4963:             }
 4964:         }
 4965:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 4966:             foreach my $type (@{$current->{'mapcrstype'}}) {
 4967:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 4968:             }
 4969:         }
 4970:         if ($current->{'makecrs'}) {
 4971:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 4972:         }
 4973:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 4974:             foreach my $role (@{$current->{'makeuser'}}) {
 4975:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 4976:             }
 4977:         }
 4978:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 4979:             $checked{'lcauth'}{$1} = ' checked="checked"';
 4980:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 4981:                 $lcauthparm = $current->{'lcauthparm'};
 4982:                 $lcauthparmstyle = 'display:table-row'; 
 4983:                 if ($current->{'lcauth'} eq 'localauth') {
 4984:                     $lcauthparmtext = &mt('Local auth argument');
 4985:                 } else {
 4986:                     $lcauthparmtext = &mt('Kerberos domain');
 4987:                 }
 4988:             }
 4989:         }
 4990:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 4991:             foreach my $role (@{$current->{'selfenroll'}}) {
 4992:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 4993:             }
 4994:         }
 4995:         if (ref($current->{'maproles'}) eq 'HASH') {
 4996:             %rolemaps = %{$current->{'maproles'}};
 4997:         }
 4998:         if ($current->{'section'} ne '') {
 4999:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 5000:             $crssecfieldsty = 'inline-block';
 5001:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5002:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5003:             } else {
 5004:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5005:                 $crssecsrc = $current->{'section'};
 5006:                 $secsrcfieldsty = 'inline-block';
 5007:             }
 5008:         } else {
 5009:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5010:         }
 5011:         if ($current->{'topmenu'}) {
 5012:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5013:         } else {
 5014:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5015:         }
 5016:         if ($current->{'inlinemenu'}) {
 5017:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5018:         } else {
 5019:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5020:         }
 5021:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5022:             $menusty = 'inline-block';
 5023:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5024:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5025:                     if (exists($menutitles{$item})) {
 5026:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5027:                     }
 5028:                 }
 5029:             }
 5030:         } else {
 5031:             $menusty = 'none';
 5032:         }
 5033:     } else {
 5034:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5035:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5036:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5037:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 5038:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5039:         $menusty = 'inline-block'; 
 5040:     }
 5041:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 5042:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5043:                                official   => 'Official',
 5044:                                unofficial => 'Unofficial',
 5045:                                community  => 'Community',
 5046:                                textbook   => 'Textbook',
 5047:                                placement  => 'Placement Test',
 5048:                                lti        => 'LTI Provider',
 5049:     );
 5050:     my @authtypes = ('internal','krb4','krb5','localauth');
 5051:     my %shortauth = (
 5052:                      internal => 'int',
 5053:                      krb4 => 'krb4',
 5054:                      krb5 => 'krb5',
 5055:                      localauth  => 'loc'
 5056:                     );
 5057:     my %authnames = &authtype_names();
 5058:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5059:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5060:     my @courseroles = ('cc','in','ta','ep','st');
 5061:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5062:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5063:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5064:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5065:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5066:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5067:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5068:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5069:     foreach my $option ('sourcedid','email','other') {
 5070:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5071:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5072:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5073:     }
 5074:     $output .= '</span></div>'.
 5075:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5076:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5077:                'value="'.$userfield.'" /></div></fieldset>'. 
 5078:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5079:     foreach my $ltirole (@lticourseroles) {
 5080:         my ($selected,$selectnone);
 5081:         if ($rolemaps{$ltirole} eq '') {
 5082:             $selectnone = ' selected="selected"';
 5083:         }
 5084:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5085:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5086:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5087:         foreach my $role (@courseroles) {
 5088:             unless ($selectnone) {
 5089:                 if ($rolemaps{$ltirole} eq $role) {
 5090:                     $selected = ' selected="selected"';
 5091:                 } else {
 5092:                     $selected = '';
 5093:                 }
 5094:             }
 5095:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5096:                        &Apache::lonnet::plaintext($role,'Course').
 5097:                        '</option>';
 5098:         }
 5099:         $output .= '</select></td>';
 5100:     }
 5101:     $output .= '</tr></table></fieldset>'.
 5102:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5103:     foreach my $ltirole (@ltiroles) {
 5104:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5105:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 5106:     }
 5107:     $output .= '</fieldset>'.
 5108:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5109:                '<table>'.
 5110:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5111:                '</table>'.
 5112:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5113:                '<td class="LC_left_item">';
 5114:     foreach my $auth ('lti',@authtypes) {
 5115:         my $authtext;
 5116:         if ($auth eq 'lti') {
 5117:             $authtext = &mt('None');
 5118:         } else {
 5119:             $authtext = $authnames{$shortauth{$auth}};
 5120:         }
 5121:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5122:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5123:                    $authtext.'</label></span> &nbsp;';
 5124:     }
 5125:     $output .= '</td></tr>'.
 5126:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5127:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5128:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5129:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5130:                '</table></fieldset>'.
 5131:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5132:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5133:                &mt('Unique course identifier').':&nbsp;';
 5134:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5135:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5136:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5137:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5138:     }
 5139:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5140:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5141:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5142:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5143:     foreach my $type (@coursetypes) {
 5144:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5145:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5146:                    ('&nbsp;'x2);
 5147:     }
 5148:     $output .= '</span></fieldset>'.
 5149:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5150:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5151:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5152:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5153:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5154:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5155:                '</fieldset>'.
 5156:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5157:     foreach my $lticrsrole (@lticourseroles) {
 5158:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5159:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5160:     }
 5161:     $output .= '</fieldset>'.
 5162:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 5163:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5164:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5165:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5166:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5167:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5168:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5169:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5170:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5171:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5172:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5173:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5174:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5175:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5176:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5177:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5178:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5179:     foreach my $extra ('roster','passback') {
 5180:         my $checkedon = '';
 5181:         my $checkedoff = ' checked="checked"';
 5182:         if ($extra eq 'passback') {
 5183:             $pb1p1chk = ' checked="checked"';
 5184:             $pb1p0chk = '';
 5185:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 5186:         } else {
 5187:             $onclickpb = ''; 
 5188:         }
 5189:         if (ref($current) eq 'HASH') {
 5190:             if (($current->{$extra})) {
 5191:                 $checkedon = $checkedoff;
 5192:                 $checkedoff = '';
 5193:                 if ($extra eq 'passback') {
 5194:                     $passbacksty = 'inline-block';
 5195:                 }
 5196:                 if ($current->{'passbackformat'} eq '1.0') {
 5197:                     $pb1p0chk =  ' checked="checked"';
 5198:                     $pb1p1chk = '';
 5199:                 }
 5200:             }
 5201:         }
 5202:         $output .= $lt{$extra}.'&nbsp;'.
 5203:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 5204:                    &mt('No').'</label>'.('&nbsp;'x2).
 5205:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 5206:                    &mt('Yes').'</label><br />';
 5207:     }
 5208:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 5209:                '<span class="LC_nobreak">'.&mt('Grade format').
 5210:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 5211:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 5212:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 5213:                &mt('Outcomes Extension (1.0)').'</label></span></div></fieldset>'.
 5214:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 5215:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 5216:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 5217:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5218:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 5219:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 5220:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5221:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 5222:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 5223:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5224:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 5225:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 5226:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 5227:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 5228:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 5229:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 5230:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 5231:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 5232:                    ('&nbsp;'x2);
 5233:     }
 5234:     $output .= '</span></div></fieldset>';
 5235: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 5236: #
 5237: #    $output .= '</fieldset>'.
 5238: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 5239:     return $output;
 5240: }
 5241: 
 5242: sub ltimenu_titles {
 5243:     return &Apache::lonlocal::texthash(
 5244:                                         fullname    => 'Full name',
 5245:                                         coursetitle => 'Course title',
 5246:                                         role        => 'Role',
 5247:                                         logout      => 'Logout',
 5248:                                         grades      => 'Grades',
 5249:     );
 5250: }
 5251: 
 5252: sub print_coursedefaults {
 5253:     my ($position,$dom,$settings,$rowtotal) = @_;
 5254:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 5255:     my $itemcount = 1;
 5256:     my %choices =  &Apache::lonlocal::texthash (
 5257:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 5258:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 5259:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 5260:         coursecredits        => 'Credits can be specified for courses',
 5261:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 5262:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 5263:         texengine            => 'Default method to display mathematics',
 5264:         postsubmit           => 'Disable submit button/keypress following student submission',
 5265:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 5266:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 5267:     );
 5268:     my %staticdefaults = (
 5269:                            texengine            => 'MathJax',
 5270:                            anonsurvey_threshold => 10,
 5271:                            uploadquota          => 500,
 5272:                            postsubmit           => 60,
 5273:                            mysqltables          => 172800,
 5274:                          );
 5275:     if ($position eq 'top') {
 5276:         %defaultchecked = (
 5277:                             'canuse_pdfforms' => 'off',
 5278:                             'uselcmath'       => 'on',
 5279:                             'usejsme'         => 'on',
 5280:                             'canclone'        => 'none',
 5281:                           );
 5282:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 5283:         my $deftex = $staticdefaults{'texengine'};
 5284:         if (ref($settings) eq 'HASH') {
 5285:             if ($settings->{'texengine'}) {
 5286:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 5287:                     $deftex = $settings->{'texengine'};
 5288:                 }
 5289:             }
 5290:         }
 5291:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5292:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 5293:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 5294:                        '</span></td><td class="LC_right_item">'.
 5295:                        '<select name="texengine">'."\n";
 5296:         my %texoptions = (
 5297:                             MathJax  => 'MathJax',
 5298:                             mimetex  => &mt('Convert to Images'),
 5299:                             tth      => &mt('TeX to HTML'),
 5300:                          );
 5301:         foreach my $renderer ('MathJax','mimetex','tth') {
 5302:             my $selected = '';
 5303:             if ($renderer eq $deftex) {
 5304:                 $selected = ' selected="selected"';
 5305:             }
 5306:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 5307:         }
 5308:         $mathdisp .= '</select></td></tr>'."\n";
 5309:         $itemcount ++;
 5310:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 5311:                                                      \%choices,$itemcount);
 5312:         $datatable = $mathdisp.$datatable;
 5313:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5314:         $datatable .=
 5315:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5316:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 5317:             '</span></td><td class="LC_left_item">';
 5318:         my $currcanclone = 'none';
 5319:         my $onclick;
 5320:         my @cloneoptions = ('none','domain');
 5321:         my %clonetitles = (
 5322:                              none     => 'No additional course requesters',
 5323:                              domain   => "Any course requester in course's domain",
 5324:                              instcode => 'Course requests for official courses ...',
 5325:                           );
 5326:         my (%codedefaults,@code_order,@posscodes);
 5327:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 5328:                                                     \@code_order) eq 'ok') {
 5329:             if (@code_order > 0) {
 5330:                 push(@cloneoptions,'instcode');
 5331:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 5332:             }
 5333:         }
 5334:         if (ref($settings) eq 'HASH') {
 5335:             if ($settings->{'canclone'}) {
 5336:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 5337:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 5338:                         if (@code_order > 0) {
 5339:                             $currcanclone = 'instcode';
 5340:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 5341:                         }
 5342:                     }
 5343:                 } elsif ($settings->{'canclone'} eq 'domain') {
 5344:                     $currcanclone = $settings->{'canclone'};
 5345:                 }
 5346:             }
 5347:         }
 5348:         foreach my $option (@cloneoptions) {
 5349:             my ($checked,$additional);
 5350:             if ($currcanclone eq $option) {
 5351:                 $checked = ' checked="checked"';
 5352:             }
 5353:             if ($option eq 'instcode') {
 5354:                 if (@code_order) {
 5355:                     my $show = 'none';
 5356:                     if ($checked) {
 5357:                         $show = 'block';
 5358:                     }
 5359:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 5360:                                   &mt('Institutional codes for new and cloned course have identical:').
 5361:                                   '<br />';
 5362:                     foreach my $item (@code_order) {
 5363:                         my $codechk;
 5364:                         if ($checked) {
 5365:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 5366:                                 $codechk = ' checked="checked"';
 5367:                             }
 5368:                         }
 5369:                         $additional .= '<label>'.
 5370:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 5371:                                        $item.'</label>';
 5372:                     }
 5373:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 5374:                 }
 5375:             }
 5376:             $datatable .=
 5377:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 5378:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 5379:                 '</label>&nbsp;'.$additional.'</span><br />';
 5380:         }
 5381:         $datatable .= '</td>'.
 5382:                       '</tr>';
 5383:         $itemcount ++;
 5384:     } else {
 5385:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5386:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 5387:         my $currusecredits = 0;
 5388:         my $postsubmitclient = 1;
 5389:         my @types = ('official','unofficial','community','textbook','placement');
 5390:         if (ref($settings) eq 'HASH') {
 5391:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 5392:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 5393:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 5394:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 5395:                 }
 5396:             }
 5397:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 5398:                 foreach my $type (@types) {
 5399:                     next if ($type eq 'community');
 5400:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 5401:                     if ($defcredits{$type} ne '') {
 5402:                         $currusecredits = 1;
 5403:                     }
 5404:                 }
 5405:             }
 5406:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 5407:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 5408:                     $postsubmitclient = 0;
 5409:                     foreach my $type (@types) {
 5410:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5411:                     }
 5412:                 } else {
 5413:                     foreach my $type (@types) {
 5414:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 5415:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 5416:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 5417:                             } else {
 5418:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5419:                             }
 5420:                         } else {
 5421:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5422:                         }
 5423:                     }
 5424:                 }
 5425:             } else {
 5426:                 foreach my $type (@types) {
 5427:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5428:                 }
 5429:             }
 5430:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 5431:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 5432:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 5433:                 }
 5434:             } else {
 5435:                 foreach my $type (@types) {
 5436:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 5437:                 }
 5438:             }
 5439:         } else {
 5440:             foreach my $type (@types) {
 5441:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5442:             }
 5443:         }
 5444:         if (!$currdefresponder) {
 5445:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 5446:         } elsif ($currdefresponder < 1) {
 5447:             $currdefresponder = 1;
 5448:         }
 5449:         foreach my $type (@types) {
 5450:             if ($curruploadquota{$type} eq '') {
 5451:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 5452:             }
 5453:         }
 5454:         $datatable .=
 5455:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5456:                 $choices{'anonsurvey_threshold'}.
 5457:                 '</span></td>'.
 5458:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 5459:                 '<input type="text" name="anonsurvey_threshold"'.
 5460:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 5461:                 '</td></tr>'."\n";
 5462:         $itemcount ++;
 5463:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5464:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5465:                       $choices{'uploadquota'}.
 5466:                       '</span></td>'.
 5467:                       '<td style="text-align: right" class="LC_right_item">'.
 5468:                       '<table><tr>';
 5469:         foreach my $type (@types) {
 5470:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5471:                            '<input type="text" name="uploadquota_'.$type.'"'.
 5472:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 5473:         }
 5474:         $datatable .= '</tr></table></td></tr>'."\n";
 5475:         $itemcount ++;
 5476:         my $onclick = "toggleDisplay(this.form,'credits');";
 5477:         my $display = 'none';
 5478:         if ($currusecredits) {
 5479:             $display = 'block';
 5480:         }
 5481:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 5482:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 5483:         foreach my $type (@types) {
 5484:             next if ($type eq 'community');
 5485:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5486:                            '<input type="text" name="'.$type.'_credits"'.
 5487:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 5488:         }
 5489:         $additional .= '</tr></table></div>'."\n";
 5490:         %defaultchecked = ('coursecredits' => 'off');
 5491:         @toggles = ('coursecredits');
 5492:         my $current = {
 5493:                         'coursecredits' => $currusecredits,
 5494:                       };
 5495:         (my $table,$itemcount) =
 5496:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5497:                                \%choices,$itemcount,$onclick,$additional,'left');
 5498:         $datatable .= $table;
 5499:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 5500:         my $display = 'none';
 5501:         if ($postsubmitclient) {
 5502:             $display = 'block';
 5503:         }
 5504:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 5505:                       &mt('Number of seconds submit is disabled').'<br />'.
 5506:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 5507:                       '<table><tr>';
 5508:         foreach my $type (@types) {
 5509:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5510:                            '<input type="text" name="'.$type.'_timeout" value="'.
 5511:                            $deftimeout{$type}.'" size="5" /></td>';
 5512:         }
 5513:         $additional .= '</tr></table></div>'."\n";
 5514:         %defaultchecked = ('postsubmit' => 'on');
 5515:         @toggles = ('postsubmit');
 5516:         $current = {
 5517:                        'postsubmit' => $postsubmitclient,
 5518:                    };
 5519:         ($table,$itemcount) =
 5520:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5521:                                \%choices,$itemcount,$onclick,$additional,'left');
 5522:         $datatable .= $table;
 5523:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5524:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5525:                       $choices{'mysqltables'}.
 5526:                       '</span></td>'.
 5527:                       '<td style="text-align: right" class="LC_right_item">'.
 5528:                       '<table><tr>';
 5529:         foreach my $type (@types) {
 5530:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5531:                            '<input type="text" name="mysqltables_'.$type.'"'.
 5532:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 5533:         }
 5534:         $datatable .= '</tr></table></td></tr>'."\n";
 5535:         $itemcount ++;
 5536: 
 5537:     }
 5538:     $$rowtotal += $itemcount;
 5539:     return $datatable;
 5540: }
 5541: 
 5542: sub print_selfenrollment {
 5543:     my ($position,$dom,$settings,$rowtotal) = @_;
 5544:     my ($css_class,$datatable);
 5545:     my $itemcount = 1;
 5546:     my @types = ('official','unofficial','community','textbook','placement');
 5547:     if (($position eq 'top') || ($position eq 'middle')) {
 5548:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 5549:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 5550:         my @rows;
 5551:         my $key;
 5552:         if ($position eq 'top') {
 5553:             $key = 'admin'; 
 5554:             if (ref($rowsref) eq 'ARRAY') {
 5555:                 @rows = @{$rowsref};
 5556:             }
 5557:         } elsif ($position eq 'middle') {
 5558:             $key = 'default';
 5559:             @rows = ('types','registered','approval','limit');
 5560:         }
 5561:         foreach my $row (@rows) {
 5562:             if (defined($titlesref->{$row})) {
 5563:                 $itemcount ++;
 5564:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5565:                 $datatable .= '<tr'.$css_class.'>'.
 5566:                               '<td>'.$titlesref->{$row}.'</td>'.
 5567:                               '<td class="LC_left_item">'.
 5568:                               '<table><tr>';
 5569:                 my (%current,%currentcap);
 5570:                 if (ref($settings) eq 'HASH') {
 5571:                     if (ref($settings->{$key}) eq 'HASH') {
 5572:                         foreach my $type (@types) {
 5573:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5574:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 5575:                             }
 5576:                             if (($row eq 'limit') && ($key eq 'default')) {
 5577:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5578:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 5579:                                 }
 5580:                             }
 5581:                         }
 5582:                     }
 5583:                 }
 5584:                 my %roles = (
 5585:                              '0' => &Apache::lonnet::plaintext('dc'),
 5586:                             ); 
 5587:             
 5588:                 foreach my $type (@types) {
 5589:                     unless (($row eq 'registered') && ($key eq 'default')) {
 5590:                         $datatable .= '<th>'.&mt($type).'</th>';
 5591:                     }
 5592:                 }
 5593:                 unless (($row eq 'registered') && ($key eq 'default')) {
 5594:                     $datatable .= '</tr><tr>';
 5595:                 }
 5596:                 foreach my $type (@types) {
 5597:                     if ($type eq 'community') {
 5598:                         $roles{'1'} = &mt('Community personnel');
 5599:                     } else {
 5600:                         $roles{'1'} = &mt('Course personnel');
 5601:                     }
 5602:                     $datatable .= '<td style="vertical-align: top">';
 5603:                     if ($position eq 'top') {
 5604:                         my %checked;
 5605:                         if ($current{$type} eq '0') {
 5606:                             $checked{'0'} = ' checked="checked"';
 5607:                         } else {
 5608:                             $checked{'1'} = ' checked="checked"';
 5609:                         }
 5610:                         foreach my $role ('1','0') {
 5611:                             $datatable .= '<span class="LC_nobreak"><label>'.
 5612:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 5613:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 5614:                                           $roles{$role}.'</label></span> ';
 5615:                         }
 5616:                     } else {
 5617:                         if ($row eq 'types') {
 5618:                             my %checked;
 5619:                             if ($current{$type} =~ /^(all|dom)$/) {
 5620:                                 $checked{$1} = ' checked="checked"';
 5621:                             } else {
 5622:                                 $checked{''} = ' checked="checked"';
 5623:                             }
 5624:                             foreach my $val ('','dom','all') {
 5625:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5626:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5627:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5628:                             }
 5629:                         } elsif ($row eq 'registered') {
 5630:                             my %checked;
 5631:                             if ($current{$type} eq '1') {
 5632:                                 $checked{'1'} = ' checked="checked"';
 5633:                             } else {
 5634:                                 $checked{'0'} = ' checked="checked"';
 5635:                             }
 5636:                             foreach my $val ('0','1') {
 5637:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5638:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5639:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5640:                             }
 5641:                         } elsif ($row eq 'approval') {
 5642:                             my %checked;
 5643:                             if ($current{$type} =~ /^([12])$/) {
 5644:                                 $checked{$1} = ' checked="checked"';
 5645:                             } else {
 5646:                                 $checked{'0'} = ' checked="checked"';
 5647:                             }
 5648:                             for my $val (0..2) {
 5649:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5650:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5651:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5652:                             }
 5653:                         } elsif ($row eq 'limit') {
 5654:                             my %checked;
 5655:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 5656:                                 $checked{$1} = ' checked="checked"';
 5657:                             } else {
 5658:                                 $checked{'none'} = ' checked="checked"';
 5659:                             }
 5660:                             my $cap;
 5661:                             if ($currentcap{$type} =~ /^\d+$/) {
 5662:                                 $cap = $currentcap{$type};
 5663:                             }
 5664:                             foreach my $val ('none','allstudents','selfenrolled') {
 5665:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5666:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5667:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5668:                             }
 5669:                             $datatable .= '<br />'.
 5670:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 5671:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 5672:                                           '</span>'; 
 5673:                         }
 5674:                     }
 5675:                     $datatable .= '</td>';
 5676:                 }
 5677:                 $datatable .= '</tr>';
 5678:             }
 5679:             $datatable .= '</table></td></tr>';
 5680:         }
 5681:     } elsif ($position eq 'bottom') {
 5682:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 5683:     }
 5684:     $$rowtotal += $itemcount;
 5685:     return $datatable;
 5686: }
 5687: 
 5688: sub print_validation_rows {
 5689:     my ($caller,$dom,$settings,$rowtotal) = @_;
 5690:     my ($itemsref,$namesref,$fieldsref);
 5691:     if ($caller eq 'selfenroll') { 
 5692:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 5693:     } elsif ($caller eq 'requestcourses') {
 5694:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 5695:     }
 5696:     my %currvalidation;
 5697:     if (ref($settings) eq 'HASH') {
 5698:         if (ref($settings->{'validation'}) eq 'HASH') {
 5699:             %currvalidation = %{$settings->{'validation'}};
 5700:         }
 5701:     }
 5702:     my $datatable;
 5703:     my $itemcount = 0;
 5704:     foreach my $item (@{$itemsref}) {
 5705:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5706:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5707:                       $namesref->{$item}.
 5708:                       '</span></td>'.
 5709:                       '<td class="LC_left_item">';
 5710:         if (($item eq 'url') || ($item eq 'button')) {
 5711:             $datatable .= '<span class="LC_nobreak">'.
 5712:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 5713:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 5714:         } elsif ($item eq 'fields') {
 5715:             my @currfields;
 5716:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 5717:                 @currfields = @{$currvalidation{$item}};
 5718:             }
 5719:             foreach my $field (@{$fieldsref}) {
 5720:                 my $check = '';
 5721:                 if (grep(/^\Q$field\E$/,@currfields)) {
 5722:                     $check = ' checked="checked"';
 5723:                 }
 5724:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5725:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 5726:                               ' value="'.$field.'"'.$check.' />'.$field.
 5727:                               '</label></span> ';
 5728:             }
 5729:         } elsif ($item eq 'markup') {
 5730:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 5731:                            $currvalidation{$item}.
 5732:                               '</textarea>';
 5733:         }
 5734:         $datatable .= '</td></tr>'."\n";
 5735:         if (ref($rowtotal)) {
 5736:             $itemcount ++;
 5737:         }
 5738:     }
 5739:     if ($caller eq 'requestcourses') {
 5740:         my %currhash;
 5741:         if (ref($settings) eq 'HASH') {
 5742:             if (ref($settings->{'validation'}) eq 'HASH') {
 5743:                 if ($settings->{'validation'}{'dc'} ne '') {
 5744:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 5745:                 }
 5746:             }
 5747:         }
 5748:         my $numinrow = 2;
 5749:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 5750:                                                        'validationdc',%currhash);
 5751:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5752:         $datatable .= '<tr'.$css_class.'><td>';
 5753:         if ($numdc > 1) {
 5754:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 5755:         } else {
 5756:             $datatable .=  &mt('Course creation processed as: ');
 5757:         }
 5758:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 5759:         $itemcount ++;
 5760:     }
 5761:     if (ref($rowtotal)) {
 5762:         $$rowtotal += $itemcount;
 5763:     }
 5764:     return $datatable;
 5765: }
 5766: 
 5767: sub print_usersessions {
 5768:     my ($position,$dom,$settings,$rowtotal) = @_;
 5769:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 5770:     my (%by_ip,%by_location,@intdoms,@instdoms);
 5771:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5772: 
 5773:     my @alldoms = &Apache::lonnet::all_domains();
 5774:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5775:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5776:     my %altids = &id_for_thisdom(%servers);
 5777:     if ($position eq 'top') {
 5778:         if (keys(%serverhomes) > 1) {
 5779:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 5780:             my $curroffloadnow;
 5781:             if (ref($settings) eq 'HASH') {
 5782:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 5783:                     $curroffloadnow = $settings->{'offloadnow'};
 5784:                 }
 5785:             }
 5786:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 5787:         } else {
 5788:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5789:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 5790:                           '</td></tr>';
 5791:         }
 5792:     } else {
 5793:         my %titles = &usersession_titles();
 5794:         my ($prefix,@types);
 5795:         if ($position eq 'bottom') {
 5796:             $prefix = 'remote';
 5797:             @types = ('version','excludedomain','includedomain');
 5798:         } else {
 5799:             $prefix = 'hosted';
 5800:             @types = ('excludedomain','includedomain');
 5801:         }
 5802:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 5803:     }
 5804:     $$rowtotal += $itemcount;
 5805:     return $datatable;
 5806: }
 5807: 
 5808: sub rules_by_location {
 5809:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 5810:     my ($datatable,$itemcount,$css_class);
 5811:     if (keys(%{$by_location}) == 0) {
 5812:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5813:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 5814:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 5815:                      '</td></tr>';
 5816:         $itemcount = 1;
 5817:     } else {
 5818:         $itemcount = 0;
 5819:         my $numinrow = 5;
 5820:         my (%current,%checkedon,%checkedoff);
 5821:         my @locations = sort(keys(%{$by_location}));
 5822:         foreach my $type (@{$types}) {
 5823:             $checkedon{$type} = '';
 5824:             $checkedoff{$type} = ' checked="checked"';
 5825:         }
 5826:         if (ref($settings) eq 'HASH') {
 5827:             if (ref($settings->{$prefix}) eq 'HASH') {
 5828:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 5829:                     $current{$key} = $settings->{$prefix}{$key};
 5830:                     if ($key eq 'version') {
 5831:                         if ($current{$key} ne '') {
 5832:                             $checkedon{$key} = ' checked="checked"';
 5833:                             $checkedoff{$key} = '';
 5834:                         }
 5835:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 5836:                         $checkedon{$key} = ' checked="checked"';
 5837:                         $checkedoff{$key} = '';
 5838:                     }
 5839:                 }
 5840:             }
 5841:         }
 5842:         foreach my $type (@{$types}) {
 5843:             next if ($type ne 'version' && !@locations);
 5844:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5845:             $datatable .= '<tr'.$css_class.'>
 5846:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 5847:                            <span class="LC_nobreak">&nbsp;
 5848:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 5849:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 5850:             if ($type eq 'version') {
 5851:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 5852:                 my $selector = '<select name="'.$prefix.'_version">';
 5853:                 foreach my $version (@lcversions) {
 5854:                     my $selected = '';
 5855:                     if ($current{'version'} eq $version) {
 5856:                         $selected = ' selected="selected"';
 5857:                     }
 5858:                     $selector .= ' <option value="'.$version.'"'.
 5859:                                  $selected.'>'.$version.'</option>';
 5860:                 }
 5861:                 $selector .= '</select> ';
 5862:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 5863:             } else {
 5864:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 5865:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 5866:                              ' />'.('&nbsp;'x2).
 5867:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 5868:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 5869:                              "\n".
 5870:                              '</div><div><table>';
 5871:                 my $rem;
 5872:                 for (my $i=0; $i<@locations; $i++) {
 5873:                     my ($showloc,$value,$checkedtype);
 5874:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 5875:                         my $ip = $by_location->{$locations[$i]}->[0];
 5876:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5877:                             $value = join(':',@{$by_ip->{$ip}});
 5878:                             $showloc = join(', ',@{$by_ip->{$ip}});
 5879:                             if (ref($current{$type}) eq 'ARRAY') {
 5880:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 5881:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 5882:                                         $checkedtype = ' checked="checked"';
 5883:                                         last;
 5884:                                     }
 5885:                                 }
 5886:                             }
 5887:                         }
 5888:                     }
 5889:                     $rem = $i%($numinrow);
 5890:                     if ($rem == 0) {
 5891:                         if ($i > 0) {
 5892:                             $datatable .= '</tr>';
 5893:                         }
 5894:                         $datatable .= '<tr>';
 5895:                     }
 5896:                     $datatable .= '<td class="LC_left_item">'.
 5897:                                   '<span class="LC_nobreak"><label>'.
 5898:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 5899:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 5900:                                   '</label></span></td>';
 5901:                 }
 5902:                 $rem = @locations%($numinrow);
 5903:                 my $colsleft = $numinrow - $rem;
 5904:                 if ($colsleft > 1 ) {
 5905:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5906:                                   '&nbsp;</td>';
 5907:                 } elsif ($colsleft == 1) {
 5908:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5909:                 }
 5910:                 $datatable .= '</tr></table>';
 5911:             }
 5912:             $datatable .= '</td></tr>';
 5913:             $itemcount ++;
 5914:         }
 5915:     }
 5916:     return ($datatable,$itemcount);
 5917: }
 5918: 
 5919: sub print_ssl {
 5920:     my ($position,$dom,$settings,$rowtotal) = @_;
 5921:     my ($css_class,$datatable);
 5922:     my $itemcount = 1;
 5923:     if ($position eq 'top') {
 5924:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5925:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5926:         my $same_institution;
 5927:         if ($intdom ne '') {
 5928:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 5929:             if (ref($internet_names) eq 'ARRAY') {
 5930:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 5931:                     $same_institution = 1;
 5932:                 }
 5933:             }
 5934:         }
 5935:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5936:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 5937:         if ($same_institution) {
 5938:             my %domservers = &Apache::lonnet::get_servers($dom);
 5939:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 5940:         } else {
 5941:             $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.");
 5942:         }
 5943:         $datatable .= '</td></tr>';
 5944:         $itemcount ++;
 5945:     } else {
 5946:         my %titles = &ssl_titles();
 5947:         my (%by_ip,%by_location,@intdoms,@instdoms);
 5948:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5949:         my @alldoms = &Apache::lonnet::all_domains();
 5950:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5951:         my @domservers = &Apache::lonnet::get_servers($dom);
 5952:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5953:         my %altids = &id_for_thisdom(%servers);
 5954:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 5955:             my $legacy;
 5956:             unless (ref($settings) eq 'HASH') {
 5957:                 my $name;
 5958:                 if ($position eq 'connto') {
 5959:                     $name = 'loncAllowInsecure';
 5960:                 } else {
 5961:                     $name = 'londAllowInsecure';
 5962:                 }
 5963:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 5964:                 my @ids=&Apache::lonnet::current_machine_ids();
 5965:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 5966:                     my %what = (
 5967:                                    $name => 1,
 5968:                                );
 5969:                     my ($result,$returnhash) =
 5970:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 5971:                     if ($result eq 'ok') {
 5972:                         if (ref($returnhash) eq 'HASH') {
 5973:                             $legacy = $returnhash->{$name};
 5974:                         }
 5975:                     }
 5976:                 } else {
 5977:                     $legacy = $Apache::lonnet::perlvar{$name};
 5978:                 }
 5979:             }
 5980:             foreach my $type ('dom','intdom','other') {
 5981:                 my %checked;
 5982:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5983:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 5984:                               '<td class="LC_right_item">';
 5985:                 my $skip; 
 5986:                 if ($type eq 'dom') {
 5987:                     unless (keys(%servers) > 1) {
 5988:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 5989:                         $skip = 1;
 5990:                     }
 5991:                 }
 5992:                 if ($type eq 'intdom') {
 5993:                     unless (@instdoms > 1) {
 5994:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 5995:                         $skip = 1;
 5996:                     } 
 5997:                 } elsif ($type eq 'other') {
 5998:                     if (keys(%by_location) == 0) {
 5999:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 6000:                         $skip = 1;
 6001:                     }
 6002:                 }
 6003:                 unless ($skip) {
 6004:                     $checked{'yes'} = ' checked="checked"'; 
 6005:                     if (ref($settings) eq 'HASH') {
 6006:                         if (ref($settings->{$position}) eq 'HASH') {
 6007:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 6008:                                 $checked{$1} = $checked{'yes'};
 6009:                                 delete($checked{'yes'}); 
 6010:                             }
 6011:                         }
 6012:                     } else {
 6013:                         if ($legacy == 0) {
 6014:                             $checked{'req'} = $checked{'yes'};
 6015:                             delete($checked{'yes'});    
 6016:                         }
 6017:                     }
 6018:                     foreach my $option ('no','yes','req') {
 6019:                         $datatable .= '<span class="LC_nobreak"><label>'.
 6020:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 6021:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 6022:                                       '</label></span>'.('&nbsp;'x2);
 6023:                     }
 6024:                 }
 6025:                 $datatable .= '</td></tr>';
 6026:                 $itemcount ++; 
 6027:             }
 6028:         } else {
 6029:             my $prefix = 'replication';
 6030:             my @types = ('certreq','nocertreq');
 6031:             if (keys(%by_location) == 0) {
 6032:                 $datatable .= '<tr'.$css_class.'><td>'.
 6033:                               &mt('Nothing to set here, as there are no other institutions').
 6034:                               '</td></tr>';
 6035:                 $itemcount ++;
 6036:             } else {
 6037:                 ($datatable,$itemcount) = 
 6038:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6039:             }
 6040:         }
 6041:     }
 6042:     $$rowtotal += $itemcount;
 6043:     return $datatable;
 6044: }
 6045: 
 6046: sub ssl_titles {
 6047:     return &Apache::lonlocal::texthash (
 6048:                dom           => 'LON-CAPA servers/VMs from same domain',
 6049:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 6050:                other         => 'External LON-CAPA servers/VMs',
 6051:                connto        => 'Connections to other servers',
 6052:                connfrom      => 'Connections from other servers',
 6053:                replication   => 'Replicating content to other institutions',
 6054:                certreq       => 'Client certificate required, but specific domains exempt',
 6055:                nocertreq     => 'No client certificate required, except for specific domains',
 6056:                no            => 'SSL not used',
 6057:                yes           => 'SSL Optional (used if available)',
 6058:                req           => 'SSL Required',
 6059:     );
 6060: }
 6061: 
 6062: sub print_trust {
 6063:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 6064:     my ($css_class,$datatable,%checked,%choices);
 6065:     my (%by_ip,%by_location,@intdoms,@instdoms);
 6066:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6067:     my $itemcount = 1;
 6068:     my %titles = &trust_titles();
 6069:     my @types = ('exc','inc');
 6070:     if ($prefix eq 'top') {
 6071:         $prefix = 'content';
 6072:     } elsif ($prefix eq 'bottom') {
 6073:         $prefix = 'msg';
 6074:     }
 6075:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6076:     $$rowtotal += $itemcount;
 6077:     return $datatable;
 6078: }
 6079: 
 6080: sub trust_titles {
 6081:     return &Apache::lonlocal::texthash(
 6082:                content  => "Access to this domain's content by others",
 6083:                shared   => "Access to other domain's content by this domain",
 6084:                enroll   => "Enrollment in this domain's courses by others", 
 6085:                othcoau  => "Co-author roles in this domain for others",
 6086:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 6087:                domroles => "Domain roles in this domain assignable to others",
 6088:                catalog  => "Course Catalog for this domain displayed elsewhere",
 6089:                reqcrs   => "Requests for creation of courses in this domain by others",
 6090:                msg      => "Users in other domains can send messages to this domain",
 6091:                exc      => "Allow all, but exclude specific domains",
 6092:                inc      => "Deny all, but include specific domains",
 6093:            );
 6094: } 
 6095: 
 6096: sub build_location_hashes {
 6097:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 6098:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 6099:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 6100:     my %iphost = &Apache::lonnet::get_iphost();
 6101:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 6102:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 6103:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 6104:         foreach my $id (@{$iphost{$primary_ip}}) {
 6105:             my $intdom = &Apache::lonnet::internet_dom($id);
 6106:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 6107:                 push(@{$intdoms},$intdom);
 6108:             }
 6109:         }
 6110:     }
 6111:     foreach my $ip (keys(%iphost)) {
 6112:         if (ref($iphost{$ip}) eq 'ARRAY') {
 6113:             foreach my $id (@{$iphost{$ip}}) {
 6114:                 my $location = &Apache::lonnet::internet_dom($id);
 6115:                 if ($location) {
 6116:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 6117:                         my $dom = &Apache::lonnet::host_domain($id);
 6118:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 6119:                             push(@{$instdoms},$dom);
 6120:                         }
 6121:                         next;
 6122:                     }
 6123:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6124:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 6125:                             push(@{$by_ip->{$ip}},$location);
 6126:                         }
 6127:                     } else {
 6128:                         $by_ip->{$ip} = [$location];
 6129:                     }
 6130:                 }
 6131:             }
 6132:         }
 6133:     }
 6134:     foreach my $ip (sort(keys(%{$by_ip}))) {
 6135:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6136:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 6137:             my $first = $by_ip->{$ip}->[0];
 6138:             if (ref($by_location->{$first}) eq 'ARRAY') {
 6139:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 6140:                     push(@{$by_location->{$first}},$ip);
 6141:                 }
 6142:             } else {
 6143:                 $by_location->{$first} = [$ip];
 6144:             }
 6145:         }
 6146:     }
 6147:     return;
 6148: }
 6149: 
 6150: sub current_offloads_to {
 6151:     my ($dom,$settings,$servers) = @_;
 6152:     my (%spareid,%otherdomconfigs);
 6153:     if (ref($servers) eq 'HASH') {
 6154:         foreach my $lonhost (sort(keys(%{$servers}))) {
 6155:             my $gotspares;
 6156:             if (ref($settings) eq 'HASH') {
 6157:                 if (ref($settings->{'spares'}) eq 'HASH') {
 6158:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 6159:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 6160:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 6161:                         $gotspares = 1;
 6162:                     }
 6163:                 }
 6164:             }
 6165:             unless ($gotspares) {
 6166:                 my $gotspares;
 6167:                 my $serverhomeID =
 6168:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 6169:                 my $serverhomedom =
 6170:                     &Apache::lonnet::host_domain($serverhomeID);
 6171:                 if ($serverhomedom ne $dom) {
 6172:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 6173:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6174:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6175:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6176:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6177:                                 $gotspares = 1;
 6178:                             }
 6179:                         }
 6180:                     } else {
 6181:                         $otherdomconfigs{$serverhomedom} =
 6182:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 6183:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 6184:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6185:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6186:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 6187:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6188:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6189:                                         $gotspares = 1;
 6190:                                     }
 6191:                                 }
 6192:                             }
 6193:                         }
 6194:                     }
 6195:                 }
 6196:             }
 6197:             unless ($gotspares) {
 6198:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6199:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6200:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6201:                } else {
 6202:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 6203:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 6204:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6205:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6206:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6207:                     } else {
 6208:                         my %what = (
 6209:                              spareid => 1,
 6210:                         );
 6211:                         my ($result,$returnhash) = 
 6212:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 6213:                         if ($result eq 'ok') { 
 6214:                             if (ref($returnhash) eq 'HASH') {
 6215:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 6216:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 6217:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 6218:                                 }
 6219:                             }
 6220:                         }
 6221:                     }
 6222:                 }
 6223:             }
 6224:         }
 6225:     }
 6226:     return %spareid;
 6227: }
 6228: 
 6229: sub spares_row {
 6230:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 6231:     my $css_class;
 6232:     my $numinrow = 4;
 6233:     my $itemcount = 1;
 6234:     my $datatable;
 6235:     my %typetitles = &sparestype_titles();
 6236:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6237:         foreach my $server (sort(keys(%{$servers}))) {
 6238:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 6239:             my ($othercontrol,$serverdom);
 6240:             if ($serverhome ne $server) {
 6241:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 6242:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6243:             } else {
 6244:                 $serverdom = &Apache::lonnet::host_domain($server);
 6245:                 if ($serverdom ne $dom) {
 6246:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6247:                 }
 6248:             }
 6249:             next unless (ref($spareid->{$server}) eq 'HASH');
 6250:             my $checkednow;
 6251:             if (ref($curroffloadnow) eq 'HASH') {
 6252:                 if ($curroffloadnow->{$server}) {
 6253:                     $checkednow = ' checked="checked"';
 6254:                 }
 6255:             }
 6256:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6257:             $datatable .= '<tr'.$css_class.'>
 6258:                            <td rowspan="2">
 6259:                             <span class="LC_nobreak">'.
 6260:                           &mt('[_1] when busy, offloads to:'
 6261:                               ,'<b>'.$server.'</b>').'</span><br />'.
 6262:                           '<span class="LC_nobreak">'."\n".
 6263:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 6264:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 6265:                           "\n";
 6266:             my (%current,%canselect);
 6267:             my @choices = 
 6268:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 6269:             foreach my $type ('primary','default') {
 6270:                 if (ref($spareid->{$server}) eq 'HASH') {
 6271:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 6272:                         my @spares = @{$spareid->{$server}{$type}};
 6273:                         if (@spares > 0) {
 6274:                             if ($othercontrol) {
 6275:                                 $current{$type} = join(', ',@spares);
 6276:                             } else {
 6277:                                 $current{$type} .= '<table>';
 6278:                                 my $numspares = scalar(@spares);
 6279:                                 for (my $i=0;  $i<@spares; $i++) {
 6280:                                     my $rem = $i%($numinrow);
 6281:                                     if ($rem == 0) {
 6282:                                         if ($i > 0) {
 6283:                                             $current{$type} .= '</tr>';
 6284:                                         }
 6285:                                         $current{$type} .= '<tr>';
 6286:                                     }
 6287:                                     $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;'.
 6288:                                                        $spareid->{$server}{$type}[$i].
 6289:                                                        '</label></td>'."\n";
 6290:                                 }
 6291:                                 my $rem = @spares%($numinrow);
 6292:                                 my $colsleft = $numinrow - $rem;
 6293:                                 if ($colsleft > 1 ) {
 6294:                                     $current{$type} .= '<td colspan="'.$colsleft.
 6295:                                                        '" class="LC_left_item">'.
 6296:                                                        '&nbsp;</td>';
 6297:                                 } elsif ($colsleft == 1) {
 6298:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 6299:                                 }
 6300:                                 $current{$type} .= '</tr></table>';
 6301:                             }
 6302:                         }
 6303:                     }
 6304:                     if ($current{$type} eq '') {
 6305:                         $current{$type} = &mt('None specified');
 6306:                     }
 6307:                     if ($othercontrol) {
 6308:                         if ($type eq 'primary') {
 6309:                             $canselect{$type} = $othercontrol;
 6310:                         }
 6311:                     } else {
 6312:                         $canselect{$type} = 
 6313:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 6314:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 6315:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 6316:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 6317:                         if (@choices > 0) {
 6318:                             foreach my $lonhost (@choices) {
 6319:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 6320:                             }
 6321:                         }
 6322:                         $canselect{$type} .= '</select>'."\n";
 6323:                     }
 6324:                 } else {
 6325:                     $current{$type} = &mt('Could not be determined');
 6326:                     if ($type eq 'primary') {
 6327:                         $canselect{$type} =  $othercontrol;
 6328:                     }
 6329:                 }
 6330:                 if ($type eq 'default') {
 6331:                     $datatable .= '<tr'.$css_class.'>';
 6332:                 }
 6333:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 6334:                               '<td>'.$current{$type}.'</td>'."\n".
 6335:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 6336:             }
 6337:             $itemcount ++;
 6338:         }
 6339:     }
 6340:     $$rowtotal += $itemcount;
 6341:     return $datatable;
 6342: }
 6343: 
 6344: sub possible_newspares {
 6345:     my ($server,$currspares,$serverhomes,$altids) = @_;
 6346:     my $serverhostname = &Apache::lonnet::hostname($server);
 6347:     my %excluded;
 6348:     if ($serverhostname ne '') {
 6349:         %excluded = (
 6350:                        $serverhostname => 1,
 6351:                     );
 6352:     }
 6353:     if (ref($currspares) eq 'HASH') {
 6354:         foreach my $type (keys(%{$currspares})) {
 6355:             if (ref($currspares->{$type}) eq 'ARRAY') {
 6356:                 if (@{$currspares->{$type}} > 0) {
 6357:                     foreach my $curr (@{$currspares->{$type}}) {
 6358:                         my $hostname = &Apache::lonnet::hostname($curr);
 6359:                         $excluded{$hostname} = 1;
 6360:                     }
 6361:                 }
 6362:             }
 6363:         }
 6364:     }
 6365:     my @choices;
 6366:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6367:         if (keys(%{$serverhomes}) > 1) {
 6368:             foreach my $name (sort(keys(%{$serverhomes}))) {
 6369:                 unless ($excluded{$name}) {
 6370:                     if (exists($altids->{$serverhomes->{$name}})) {
 6371:                         push(@choices,$altids->{$serverhomes->{$name}});
 6372:                     } else {
 6373:                         push(@choices,$serverhomes->{$name});
 6374:                     }
 6375:                 }
 6376:             }
 6377:         }
 6378:     }
 6379:     return sort(@choices);
 6380: }
 6381: 
 6382: sub print_loadbalancing {
 6383:     my ($dom,$settings,$rowtotal) = @_;
 6384:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6385:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6386:     my $numinrow = 1;
 6387:     my $datatable;
 6388:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6389:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 6390:     if (ref($settings) eq 'HASH') {
 6391:         %existing = %{$settings};
 6392:     }
 6393:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 6394:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 6395:                                   \%currtargets,\%currrules,\%currcookies);
 6396:     } else {
 6397:         return;
 6398:     }
 6399:     my ($othertitle,$usertypes,$types) =
 6400:         &Apache::loncommon::sorted_inst_types($dom);
 6401:     my $rownum = 8;
 6402:     if (ref($types) eq 'ARRAY') {
 6403:         $rownum += scalar(@{$types});
 6404:     }
 6405:     my @css_class = ('LC_odd_row','LC_even_row');
 6406:     my $balnum = 0;
 6407:     my $islast;
 6408:     my (@toshow,$disabledtext);
 6409:     if (keys(%currbalancer) > 0) {
 6410:         @toshow = sort(keys(%currbalancer));
 6411:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 6412:             push(@toshow,'');
 6413:         }
 6414:     } else {
 6415:         @toshow = ('');
 6416:         $disabledtext = &mt('No existing load balancer');
 6417:     }
 6418:     foreach my $lonhost (@toshow) {
 6419:         if ($balnum == scalar(@toshow)-1) {
 6420:             $islast = 1;
 6421:         } else {
 6422:             $islast = 0;
 6423:         }
 6424:         my $cssidx = $balnum%2;
 6425:         my $targets_div_style = 'display: none';
 6426:         my $disabled_div_style = 'display: block';
 6427:         my $homedom_div_style = 'display: none';
 6428:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 6429:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6430:                       '<p>';
 6431:         if ($lonhost eq '') {
 6432:             $datatable .= '<span class="LC_nobreak">';
 6433:             if (keys(%currbalancer) > 0) {
 6434:                 $datatable .= &mt('Add balancer:');
 6435:             } else {
 6436:                 $datatable .= &mt('Enable balancer:');
 6437:             }
 6438:             $datatable .= '&nbsp;'.
 6439:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 6440:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 6441:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 6442:                           '<option value="" selected="selected">'.&mt('None').
 6443:                           '</option>'."\n";
 6444:             foreach my $server (sort(keys(%servers))) {
 6445:                 next if ($currbalancer{$server});
 6446:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 6447:             }
 6448:             $datatable .=
 6449:                 '</select>'."\n".
 6450:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 6451:         } else {
 6452:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 6453:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 6454:                            &mt('Stop balancing').'</label>'.
 6455:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 6456:             $targets_div_style = 'display: block';
 6457:             $disabled_div_style = 'display: none';
 6458:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 6459:                 $homedom_div_style = 'display: block';
 6460:             }
 6461:         }
 6462:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6463:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 6464:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 6465:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 6466:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 6467:         my @sparestypes = ('primary','default');
 6468:         my %typetitles = &sparestype_titles();
 6469:         my %hostherechecked = (
 6470:                                   no => ' checked="checked"',
 6471:                               );
 6472:         my %balcookiechecked = (
 6473:                                   no => ' checked="checked"', 
 6474:                                );
 6475:         foreach my $sparetype (@sparestypes) {
 6476:             my $targettable;
 6477:             for (my $i=0; $i<$numspares; $i++) {
 6478:                 my $checked;
 6479:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 6480:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6481:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6482:                             $checked = ' checked="checked"';
 6483:                         }
 6484:                     }
 6485:                 }
 6486:                 my ($chkboxval,$disabled);
 6487:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 6488:                     $chkboxval = $spares[$i];
 6489:                 }
 6490:                 if (exists($currbalancer{$spares[$i]})) {
 6491:                     $disabled = ' disabled="disabled"';
 6492:                 }
 6493:                 $targettable .=
 6494:                     '<td><span class="LC_nobreak"><label>'.
 6495:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 6496:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 6497:                     '</span></label></span></td>';
 6498:                 my $rem = $i%($numinrow);
 6499:                 if ($rem == 0) {
 6500:                     if (($i > 0) && ($i < $numspares-1)) {
 6501:                         $targettable .= '</tr>';
 6502:                     }
 6503:                     if ($i < $numspares-1) {
 6504:                         $targettable .= '<tr>';
 6505:                     }
 6506:                 }
 6507:             }
 6508:             if ($targettable ne '') {
 6509:                 my $rem = $numspares%($numinrow);
 6510:                 my $colsleft = $numinrow - $rem;
 6511:                 if ($colsleft > 1 ) {
 6512:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6513:                                     '&nbsp;</td>';
 6514:                 } elsif ($colsleft == 1) {
 6515:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 6516:                 }
 6517:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 6518:                                '<table><tr>'.$targettable.'</tr></table><br />';
 6519:             }
 6520:             $hostherechecked{$sparetype} = '';
 6521:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 6522:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6523:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6524:                         $hostherechecked{$sparetype} = ' checked="checked"';
 6525:                         $hostherechecked{'no'} = '';
 6526:                     }
 6527:                 }
 6528:             }
 6529:         }
 6530:         if ($currcookies{$lonhost}) {
 6531:             %balcookiechecked = (
 6532:                                     yes => ' checked="checked"',
 6533:                                 );
 6534:         }
 6535:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 6536:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 6537:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 6538:         foreach my $sparetype (@sparestypes) {
 6539:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 6540:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 6541:                           '</i></label><br />';
 6542:         }
 6543:         $datatable .= &mt('Use balancer cookie').'<br />'.
 6544:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 6545:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 6546:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 6547:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 6548:                       '</div></td></tr>'.
 6549:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 6550:                                            $othertitle,$usertypes,$types,\%servers,
 6551:                                            \%currbalancer,$lonhost,
 6552:                                            $targets_div_style,$homedom_div_style,
 6553:                                            $css_class[$cssidx],$balnum,$islast);
 6554:         $$rowtotal += $rownum;
 6555:         $balnum ++;
 6556:     }
 6557:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 6558:     return $datatable;
 6559: }
 6560: 
 6561: sub get_loadbalancers_config {
 6562:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 6563:     return unless ((ref($servers) eq 'HASH') &&
 6564:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 6565:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 6566:                    (ref($currcookies) eq 'HASH'));
 6567:     if (keys(%{$existing}) > 0) {
 6568:         my $oldlonhost;
 6569:         foreach my $key (sort(keys(%{$existing}))) {
 6570:             if ($key eq 'lonhost') {
 6571:                 $oldlonhost = $existing->{'lonhost'};
 6572:                 $currbalancer->{$oldlonhost} = 1;
 6573:             } elsif ($key eq 'targets') {
 6574:                 if ($oldlonhost) {
 6575:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 6576:                 }
 6577:             } elsif ($key eq 'rules') {
 6578:                 if ($oldlonhost) {
 6579:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 6580:                 }
 6581:             } elsif (ref($existing->{$key}) eq 'HASH') {
 6582:                 $currbalancer->{$key} = 1;
 6583:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 6584:                 $currrules->{$key} = $existing->{$key}{'rules'};
 6585:                 if ($existing->{$key}{'cookie'}) {
 6586:                     $currcookies->{$key} = 1;
 6587:                 }
 6588:             }
 6589:         }
 6590:     } else {
 6591:         my ($balancerref,$targetsref) =
 6592:                 &Apache::lonnet::get_lonbalancer_config($servers);
 6593:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 6594:             foreach my $server (sort(keys(%{$balancerref}))) {
 6595:                 $currbalancer->{$server} = 1;
 6596:                 $currtargets->{$server} = $targetsref->{$server};
 6597:             }
 6598:         }
 6599:     }
 6600:     return;
 6601: }
 6602: 
 6603: sub loadbalancing_rules {
 6604:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 6605:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 6606:         $css_class,$balnum,$islast) = @_;
 6607:     my $output;
 6608:     my $num = 0;
 6609:     my ($alltypes,$othertypes,$titles) =
 6610:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 6611:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 6612:         foreach my $type (@{$alltypes}) {
 6613:             $num ++;
 6614:             my $current;
 6615:             if (ref($currrules) eq 'HASH') {
 6616:                 $current = $currrules->{$type};
 6617:             }
 6618:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6619:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 6620:                     $current = '';
 6621:                 }
 6622:             }
 6623:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 6624:                                              $servers,$currbalancer,$lonhost,$dom,
 6625:                                              $targets_div_style,$homedom_div_style,
 6626:                                              $css_class,$balnum,$num,$islast);
 6627:         }
 6628:     }
 6629:     return $output;
 6630: }
 6631: 
 6632: sub loadbalancing_titles {
 6633:     my ($dom,$intdom,$usertypes,$types) = @_;
 6634:     my %othertypes = (
 6635:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 6636:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 6637:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 6638:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 6639:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 6640:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 6641:                      );
 6642:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 6643:     my @available;
 6644:     if (ref($types) eq 'ARRAY') {
 6645:         @available = @{$types};
 6646:     }
 6647:     unless (grep(/^default$/,@available)) {
 6648:         push(@available,'default');
 6649:     }
 6650:     unshift(@alltypes,@available);
 6651:     my %titles;
 6652:     foreach my $type (@alltypes) {
 6653:         if ($type =~ /^_LC_/) {
 6654:             $titles{$type} = $othertypes{$type};
 6655:         } elsif ($type eq 'default') {
 6656:             $titles{$type} = &mt('All users from [_1]',$dom);
 6657:             if (ref($types) eq 'ARRAY') {
 6658:                 if (@{$types} > 0) {
 6659:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 6660:                 }
 6661:             }
 6662:         } elsif (ref($usertypes) eq 'HASH') {
 6663:             $titles{$type} = $usertypes->{$type};
 6664:         }
 6665:     }
 6666:     return (\@alltypes,\%othertypes,\%titles);
 6667: }
 6668: 
 6669: sub loadbalance_rule_row {
 6670:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 6671:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 6672:     my @rulenames;
 6673:     my %ruletitles = &offloadtype_text();
 6674:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 6675:         @rulenames = ('balancer','offloadedto','specific');
 6676:     } else {
 6677:         @rulenames = ('default','homeserver');
 6678:         if ($type eq '_LC_external') {
 6679:             push(@rulenames,'externalbalancer');
 6680:         } else {
 6681:             push(@rulenames,'specific');
 6682:         }
 6683:         push(@rulenames,'none');
 6684:     }
 6685:     my $style = $targets_div_style;
 6686:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6687:         $style = $homedom_div_style;
 6688:     }
 6689:     my $space;
 6690:     if ($islast && $num == 1) {
 6691:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 6692:     }
 6693:     my $output =
 6694:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 6695:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 6696:         '<td valaign="top">'.$space.
 6697:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 6698:     for (my $i=0; $i<@rulenames; $i++) {
 6699:         my $rule = $rulenames[$i];
 6700:         my ($checked,$extra);
 6701:         if ($rulenames[$i] eq 'default') {
 6702:             $rule = '';
 6703:         }
 6704:         if ($rulenames[$i] eq 'specific') {
 6705:             if (ref($servers) eq 'HASH') {
 6706:                 my $default;
 6707:                 if (($current ne '') && (exists($servers->{$current}))) {
 6708:                     $checked = ' checked="checked"';
 6709:                 }
 6710:                 unless ($checked) {
 6711:                     $default = ' selected="selected"';
 6712:                 }
 6713:                 $extra =
 6714:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6715:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6716:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 6717:                     '<option value=""'.$default.'></option>'."\n";
 6718:                 foreach my $server (sort(keys(%{$servers}))) {
 6719:                     if (ref($currbalancer) eq 'HASH') {
 6720:                         next if (exists($currbalancer->{$server}));
 6721:                     }
 6722:                     my $selected;
 6723:                     if ($server eq $current) {
 6724:                         $selected = ' selected="selected"';
 6725:                     }
 6726:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 6727:                 }
 6728:                 $extra .= '</select>';
 6729:             }
 6730:         } elsif ($rule eq $current) {
 6731:             $checked = ' checked="checked"';
 6732:         }
 6733:         $output .= '<span class="LC_nobreak"><label>'.
 6734:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 6735:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 6736:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 6737:                    ')"'.$checked.' />&nbsp;';
 6738:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 6739:             $output .= $ruletitles{'particular'};
 6740:         } else {
 6741:             $output .= $ruletitles{$rulenames[$i]};
 6742:         }
 6743:         $output .= '</label>'.$extra.'</span><br />'."\n";
 6744:     }
 6745:     $output .= '</div></td></tr>'."\n";
 6746:     return $output;
 6747: }
 6748: 
 6749: sub offloadtype_text {
 6750:     my %ruletitles = &Apache::lonlocal::texthash (
 6751:            'default'          => 'Offloads to default destinations',
 6752:            'homeserver'       => "Offloads to user's home server",
 6753:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 6754:            'specific'         => 'Offloads to specific server',
 6755:            'none'             => 'No offload',
 6756:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 6757:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 6758:            'particular'       => 'Session hosted (after re-auth) on server:',
 6759:     );
 6760:     return %ruletitles;
 6761: }
 6762: 
 6763: sub sparestype_titles {
 6764:     my %typestitles = &Apache::lonlocal::texthash (
 6765:                           'primary' => 'primary',
 6766:                           'default' => 'default',
 6767:                       );
 6768:     return %typestitles;
 6769: }
 6770: 
 6771: sub contact_titles {
 6772:     my %titles = &Apache::lonlocal::texthash (
 6773:                    'supportemail'    => 'Support E-mail address',
 6774:                    'adminemail'      => 'Default Server Admin E-mail address',
 6775:                    'errormail'       => 'Error reports to be e-mailed to',
 6776:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 6777:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 6778:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 6779:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 6780:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 6781:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 6782:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 6783:                    'errorthreshold'  => 'Error/warning threshold for status e-mail',
 6784:                    'errorsysmail'    => 'Error threshold for e-mail to core group',
 6785:                    'errorweights'    => 'Weights used to compute error count',
 6786:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 6787:                  );
 6788:     my %short_titles = &Apache::lonlocal::texthash (
 6789:                            adminemail   => 'Admin E-mail address',
 6790:                            supportemail => 'Support E-mail',
 6791:                        );   
 6792:     return (\%titles,\%short_titles);
 6793: }
 6794: 
 6795: sub helpform_fields {
 6796:     my %titles =  &Apache::lonlocal::texthash (
 6797:                        'username'   => 'Name',
 6798:                        'user'       => 'Username/domain',
 6799:                        'phone'      => 'Phone',
 6800:                        'cc'         => 'Cc e-mail',
 6801:                        'course'     => 'Course Details',
 6802:                        'section'    => 'Sections',
 6803:                        'screenshot' => 'File upload',
 6804:     );
 6805:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 6806:     my %possoptions = (
 6807:                         username     => ['yes','no','req'],
 6808:                         phone        => ['yes','no','req'],
 6809:                         user         => ['yes','no'],
 6810:                         cc           => ['yes','no'],
 6811:                         course       => ['yes','no'],
 6812:                         section      => ['yes','no'],
 6813:                         screenshot   => ['yes','no'],
 6814:                       );
 6815:     my %fieldoptions = &Apache::lonlocal::texthash (
 6816:                          'yes'  => 'Optional',
 6817:                          'req'  => 'Required',
 6818:                          'no'   => "Not shown",
 6819:     );
 6820:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 6821: }
 6822: 
 6823: sub tool_titles {
 6824:     my %titles = &Apache::lonlocal::texthash (
 6825:                      aboutme    => 'Personal web page',
 6826:                      blog       => 'Blog',
 6827:                      webdav     => 'WebDAV',
 6828:                      portfolio  => 'Portfolio',
 6829:                      official   => 'Official courses (with institutional codes)',
 6830:                      unofficial => 'Unofficial courses',
 6831:                      community  => 'Communities',
 6832:                      textbook   => 'Textbook courses',
 6833:                      placement  => 'Placement tests',
 6834:                  );
 6835:     return %titles;
 6836: }
 6837: 
 6838: sub courserequest_titles {
 6839:     my %titles = &Apache::lonlocal::texthash (
 6840:                                    official   => 'Official',
 6841:                                    unofficial => 'Unofficial',
 6842:                                    community  => 'Communities',
 6843:                                    textbook   => 'Textbook',
 6844:                                    placement  => 'Placement tests',
 6845:                                    lti        => 'LTI Provider',
 6846:                                    norequest  => 'Not allowed',
 6847:                                    approval   => 'Approval by DC',
 6848:                                    validate   => 'With validation',
 6849:                                    autolimit  => 'Numerical limit',
 6850:                                    unlimited  => '(blank for unlimited)',
 6851:                  );
 6852:     return %titles;
 6853: }
 6854: 
 6855: sub authorrequest_titles {
 6856:     my %titles = &Apache::lonlocal::texthash (
 6857:                                    norequest  => 'Not allowed',
 6858:                                    approval   => 'Approval by Dom. Coord.',
 6859:                                    automatic  => 'Automatic approval',
 6860:                  );
 6861:     return %titles;
 6862: }
 6863: 
 6864: sub courserequest_conditions {
 6865:     my %conditions = &Apache::lonlocal::texthash (
 6866:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6867:        validate   => '(Processing of request subject to institutional validation).',
 6868:                  );
 6869:     return %conditions;
 6870: }
 6871: 
 6872: 
 6873: sub print_usercreation {
 6874:     my ($position,$dom,$settings,$rowtotal) = @_;
 6875:     my $numinrow = 4;
 6876:     my $datatable;
 6877:     if ($position eq 'top') {
 6878:         $$rowtotal ++;
 6879:         my $rowcount = 0;
 6880:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6881:         if (ref($rules) eq 'HASH') {
 6882:             if (keys(%{$rules}) > 0) {
 6883:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6884:                                                 $ruleorder,$numinrow,$rowcount);
 6885:                 $$rowtotal ++;
 6886:                 $rowcount ++;
 6887:             }
 6888:         }
 6889:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6890:         if (ref($idrules) eq 'HASH') {
 6891:             if (keys(%{$idrules}) > 0) {
 6892:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6893:                                                 $idruleorder,$numinrow,$rowcount);
 6894:                 $$rowtotal ++;
 6895:                 $rowcount ++;
 6896:             }
 6897:         }
 6898:         if ($rowcount == 0) {
 6899:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6900:             $$rowtotal ++;
 6901:             $rowcount ++;
 6902:         }
 6903:     } elsif ($position eq 'middle') {
 6904:         my @creators = ('author','course','requestcrs');
 6905:         my ($rules,$ruleorder) =
 6906:             &Apache::lonnet::inst_userrules($dom,'username');
 6907:         my %lt = &usercreation_types();
 6908:         my %checked;
 6909:         if (ref($settings) eq 'HASH') {
 6910:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6911:                 foreach my $item (@creators) {
 6912:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6913:                 }
 6914:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6915:                 foreach my $item (@creators) {
 6916:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6917:                         $checked{$item} = 'none';
 6918:                     }
 6919:                 }
 6920:             }
 6921:         }
 6922:         my $rownum = 0;
 6923:         foreach my $item (@creators) {
 6924:             $rownum ++;
 6925:             if ($checked{$item} eq '') {
 6926:                 $checked{$item} = 'any';
 6927:             }
 6928:             my $css_class;
 6929:             if ($rownum%2) {
 6930:                 $css_class = '';
 6931:             } else {
 6932:                 $css_class = ' class="LC_odd_row" ';
 6933:             }
 6934:             $datatable .= '<tr'.$css_class.'>'.
 6935:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6936:                          '</span></td><td style="text-align: right">';
 6937:             my @options = ('any');
 6938:             if (ref($rules) eq 'HASH') {
 6939:                 if (keys(%{$rules}) > 0) {
 6940:                     push(@options,('official','unofficial'));
 6941:                 }
 6942:             }
 6943:             push(@options,'none');
 6944:             foreach my $option (@options) {
 6945:                 my $type = 'radio';
 6946:                 my $check = ' ';
 6947:                 if ($checked{$item} eq $option) {
 6948:                     $check = ' checked="checked" ';
 6949:                 } 
 6950:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6951:                               '<input type="'.$type.'" name="can_createuser_'.
 6952:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6953:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6954:             }
 6955:             $datatable .= '</td></tr>';
 6956:         }
 6957:     } else {
 6958:         my @contexts = ('author','course','domain');
 6959:         my @authtypes = ('int','krb4','krb5','loc','lti');
 6960:         my %checked;
 6961:         if (ref($settings) eq 'HASH') {
 6962:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6963:                 foreach my $item (@contexts) {
 6964:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6965:                         foreach my $auth (@authtypes) {
 6966:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6967:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6968:                             }
 6969:                         }
 6970:                     }
 6971:                 }
 6972:             }
 6973:         } else {
 6974:             foreach my $item (@contexts) {
 6975:                 foreach my $auth (@authtypes) {
 6976:                     $checked{$item}{$auth} = ' checked="checked" ';
 6977:                 }
 6978:             }
 6979:         }
 6980:         my %title = &context_names();
 6981:         my %authname = &authtype_names();
 6982:         my $rownum = 0;
 6983:         my $css_class; 
 6984:         foreach my $item (@contexts) {
 6985:             if ($rownum%2) {
 6986:                 $css_class = '';
 6987:             } else {
 6988:                 $css_class = ' class="LC_odd_row" ';
 6989:             }
 6990:             $datatable .=   '<tr'.$css_class.'>'.
 6991:                             '<td>'.$title{$item}.
 6992:                             '</td><td class="LC_left_item">'.
 6993:                             '<span class="LC_nobreak">';
 6994:             foreach my $auth (@authtypes) {
 6995:                 $datatable .= '<label>'. 
 6996:                               '<input type="checkbox" name="'.$item.'_auth" '.
 6997:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 6998:                               $authname{$auth}.'</label>&nbsp;';
 6999:             }
 7000:             $datatable .= '</span></td></tr>';
 7001:             $rownum ++;
 7002:         }
 7003:         $$rowtotal += $rownum;
 7004:     }
 7005:     return $datatable;
 7006: }
 7007: 
 7008: sub print_selfcreation {
 7009:     my ($position,$dom,$settings,$rowtotal) = @_;
 7010:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 7011:         $emaildomain,$datatable);
 7012:     if (ref($settings) eq 'HASH') {
 7013:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 7014:             $createsettings = $settings->{'cancreate'};
 7015:             if (ref($createsettings) eq 'HASH') {
 7016:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 7017:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 7018:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 7019:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 7020:                         @selfcreate = ('email','login','sso');
 7021:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 7022:                         @selfcreate = ($createsettings->{'selfcreate'});
 7023:                     }
 7024:                 }
 7025:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 7026:                     $processing = $createsettings->{'selfcreateprocessing'};
 7027:                 }
 7028:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 7029:                     $emailoptions = $createsettings->{'emailoptions'};
 7030:                 }
 7031:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 7032:                     $emailverified = $createsettings->{'emailverified'};
 7033:                 }
 7034:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 7035:                     $emaildomain = $createsettings->{'emaildomain'};
 7036:                 }
 7037:             }
 7038:         }
 7039:     }
 7040:     my %radiohash;
 7041:     my $numinrow = 4;
 7042:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 7043:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7044:     if ($position eq 'top') {
 7045:         my %choices = &Apache::lonlocal::texthash (
 7046:                                                       cancreate_login      => 'Institutional Login',
 7047:                                                       cancreate_sso        => 'Institutional Single Sign On',
 7048:                                                   );
 7049:         my @toggles = sort(keys(%choices));
 7050:         my %defaultchecked = (
 7051:                                'cancreate_login' => 'off',
 7052:                                'cancreate_sso'   => 'off',
 7053:                              );
 7054:         my ($onclick,$itemcount);
 7055:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7056:                                                      \%choices,$itemcount,$onclick);
 7057:         $$rowtotal += $itemcount;
 7058:         
 7059:         if (ref($usertypes) eq 'HASH') {
 7060:             if (keys(%{$usertypes}) > 0) {
 7061:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 7062:                                              $dom,$numinrow,$othertitle,
 7063:                                              'statustocreate',$rowtotal);
 7064:                 $$rowtotal ++;
 7065:             }
 7066:         }
 7067:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 7068:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7069:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 7070:         my $rem;
 7071:         my $numperrow = 2;
 7072:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 7073:         $datatable .= '<tr'.$css_class.'>'.
 7074:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 7075:                      '<td class="LC_left_item">'."\n".
 7076:                      '<table>'."\n";
 7077:         for (my $i=0; $i<@fields; $i++) {
 7078:             $rem = $i%($numperrow);
 7079:             if ($rem == 0) {
 7080:                 if ($i > 0) {
 7081:                     $datatable .= '</tr>';
 7082:                 }
 7083:                 $datatable .= '<tr>';
 7084:             }
 7085:             my $currval;
 7086:             if (ref($createsettings) eq 'HASH') {
 7087:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 7088:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 7089:                 }
 7090:             }
 7091:             $datatable .= '<td class="LC_left_item">'.
 7092:                           '<span class="LC_nobreak">'.
 7093:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 7094:                           'value="'.$currval.'" size="10" />&nbsp;'.
 7095:                           $fieldtitles{$fields[$i]}.'</span></td>';
 7096:         }
 7097:         my $colsleft = $numperrow - $rem;
 7098:         if ($colsleft > 1 ) {
 7099:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7100:                          '&nbsp;</td>';
 7101:         } elsif ($colsleft == 1) {
 7102:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7103:         }
 7104:         $datatable .= '</tr></table></td></tr>';
 7105:         $$rowtotal ++;
 7106:     } elsif ($position eq 'middle') {
 7107:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 7108:         my @posstypes;
 7109:         if (ref($types) eq 'ARRAY') {
 7110:             @posstypes = @{$types};
 7111:         }
 7112:         unless (grep(/^default$/,@posstypes)) {
 7113:             push(@posstypes,'default');
 7114:         }
 7115:         my %usertypeshash;
 7116:         if (ref($usertypes) eq 'HASH') {
 7117:             %usertypeshash = %{$usertypes};
 7118:         }
 7119:         $usertypeshash{'default'} = $othertitle;
 7120:         foreach my $status (@posstypes) {
 7121:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 7122:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 7123:             $$rowtotal ++;
 7124:         }
 7125:     } else {
 7126:         my %choices = &Apache::lonlocal::texthash (
 7127:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 7128:                                                   );
 7129:         my @toggles = sort(keys(%choices));
 7130:         my %defaultchecked = (
 7131:                                'cancreate_email' => 'off',
 7132:                              );
 7133:         my $customclass = 'LC_selfcreate_email';
 7134:         my $classprefix = 'LC_canmodify_emailusername_';
 7135:         my $optionsprefix = 'LC_options_emailusername_';
 7136:         my $display = 'none';
 7137:         my $rowstyle = 'display:none';
 7138:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 7139:             $display = 'block';
 7140:             $rowstyle = 'display:table-row';
 7141:         }
 7142:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 7143:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7144:                                                      \%choices,$$rowtotal,$onclick);
 7145:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 7146:                                          $rowstyle);
 7147:         $$rowtotal ++;
 7148:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 7149:                                       $rowstyle);
 7150:         $$rowtotal ++;
 7151:         my (@ordered,@posstypes,%usertypeshash);
 7152:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7153:         my ($emailrules,$emailruleorder) =
 7154:             &Apache::lonnet::inst_userrules($dom,'email');
 7155:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7156:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7157:         if (ref($types) eq 'ARRAY') {
 7158:             @posstypes = @{$types};
 7159:         }
 7160:         if (@posstypes) {
 7161:             unless (grep(/^default$/,@posstypes)) {
 7162:                 push(@posstypes,'default');
 7163:             }
 7164:             if (ref($usertypes) eq 'HASH') {
 7165:                 %usertypeshash = %{$usertypes};
 7166:             }
 7167:             my $currassign;
 7168:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 7169:                 $currassign = {
 7170:                                   selfassign => $domdefaults{'inststatusguest'},
 7171:                               };
 7172:                 @ordered = @{$domdefaults{'inststatusguest'}};
 7173:             } else {
 7174:                 $currassign = { selfassign => [] };
 7175:             }
 7176:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 7177:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 7178:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 7179:                                          $numinrow,$othertitle,'selfassign',
 7180:                                          $rowtotal,$onclicktypes,$customclass,
 7181:                                          $rowstyle);
 7182:             $$rowtotal ++;
 7183:             $usertypeshash{'default'} = $othertitle;
 7184:             foreach my $status (@posstypes) {
 7185:                 my $css_class;
 7186:                 if ($$rowtotal%2) {
 7187:                     $css_class = 'LC_odd_row ';
 7188:                 }
 7189:                 $css_class .= $customclass;
 7190:                 my $rowid = $optionsprefix.$status;
 7191:                 my $hidden = 1;
 7192:                 my $currstyle = 'display:none';
 7193:                 if (grep(/^\Q$status\E$/,@ordered)) {
 7194:                     $currstyle = $rowstyle;
 7195:                     $hidden = 0; 
 7196:                 }
 7197:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7198:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 7199:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 7200:                 unless ($hidden) {
 7201:                     $$rowtotal ++;
 7202:                 }
 7203:             }
 7204:         } else {
 7205:             my $css_class;
 7206:             if ($$rowtotal%2) {
 7207:                 $css_class = 'LC_odd_row ';
 7208:             }
 7209:             $css_class .= $customclass;
 7210:             $usertypeshash{'default'} = $othertitle;
 7211:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7212:                                          $emailrules,$emailruleorder,$settings,'default','',
 7213:                                          $othertitle,$css_class,$rowstyle,$intdom);
 7214:             $$rowtotal ++;
 7215:         }
 7216:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7217:         $numinrow = 1;
 7218:         if (@posstypes) {
 7219:             foreach my $status (@posstypes) {
 7220:                 my $rowid = $classprefix.$status;
 7221:                 my $datarowstyle = 'display:none';
 7222:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 7223:                     $datarowstyle = $rowstyle; 
 7224:                 }
 7225:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 7226:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7227:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 7228:                 unless ($datarowstyle eq 'display:none') {
 7229:                     $$rowtotal ++;
 7230:                 }
 7231:             }
 7232:         } else {
 7233:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 7234:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7235:                                                    $infotitles,'',$customclass,$rowstyle);
 7236:         }
 7237:     }
 7238:     return $datatable;
 7239: }
 7240: 
 7241: sub selfcreate_javascript {
 7242:     return <<"ENDSCRIPT";
 7243: 
 7244: <script type="text/javascript">
 7245: // <![CDATA[
 7246: 
 7247: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 7248:     var x = document.getElementsByClassName(target);
 7249:     var insttypes = 0;
 7250:     var insttypeRegExp = new RegExp(prefix);
 7251:     if ((x.length != undefined) && (x.length > 0)) {
 7252:         if (form.elements[radio].length != undefined) {
 7253:             for (var i=0; i<form.elements[radio].length; i++) {
 7254:                 if (form.elements[radio][i].checked) {
 7255:                     if (form.elements[radio][i].value == 1) {
 7256:                         for (var j=0; j<x.length; j++) {
 7257:                             if (x[j].id == 'undefined') {
 7258:                                 x[j].style.display = 'table-row';
 7259:                             } else if (insttypeRegExp.test(x[j].id)) {
 7260:                                 insttypes ++;
 7261:                             } else {
 7262:                                 x[j].style.display = 'table-row';
 7263:                             }
 7264:                         }
 7265:                     } else {
 7266:                         for (var j=0; j<x.length; j++) {
 7267:                             x[j].style.display = 'none';
 7268:                         }
 7269:                     }
 7270:                     break;
 7271:                 }
 7272:             }
 7273:             if (insttypes > 0) {
 7274:                 toggleDataRow(form,checkbox,target,altprefix);
 7275:                 toggleDataRow(form,checkbox,target,prefix,1);
 7276:             }
 7277:         }
 7278:     }
 7279:     return;
 7280: }
 7281: 
 7282: function toggleDataRow(form,checkbox,target,prefix,docount) {
 7283:     if (form.elements[checkbox].length != undefined) {
 7284:         var count = 0;
 7285:         if (docount) {
 7286:             for (var i=0; i<form.elements[checkbox].length; i++) {
 7287:                 if (form.elements[checkbox][i].checked) {
 7288:                     count ++;
 7289:                 }
 7290:             }
 7291:         }
 7292:         for (var i=0; i<form.elements[checkbox].length; i++) {
 7293:             var type = form.elements[checkbox][i].value;
 7294:             if (document.getElementById(prefix+type)) {
 7295:                 if (form.elements[checkbox][i].checked) {
 7296:                     document.getElementById(prefix+type).style.display = 'table-row';
 7297:                     if (count % 2 == 1) {
 7298:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 7299:                     } else {
 7300:                         document.getElementById(prefix+type).className = target;
 7301:                     }
 7302:                     count ++;
 7303:                 } else {
 7304:                     document.getElementById(prefix+type).style.display = 'none';
 7305:                 }
 7306:             }
 7307:         }
 7308:     }
 7309:     return;
 7310: }
 7311: 
 7312: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 7313:     var caller = radio+'_'+status;
 7314:     if (form.elements[caller].length != undefined) {
 7315:         for (var i=0; i<form.elements[caller].length; i++) {
 7316:             if (form.elements[caller][i].checked) {
 7317:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 7318:                     var curr = form.elements[caller][i].value;
 7319:                     if (prefix) {
 7320:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 7321:                     }
 7322:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 7323:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 7324:                     if (curr == 'custom') {
 7325:                         if (prefix) { 
 7326:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 7327:                         }
 7328:                     } else if (curr == 'inst') {
 7329:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 7330:                     } else if (curr == 'noninst') {
 7331:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 7332:                     }
 7333:                     break;
 7334:                 }
 7335:             }
 7336:         }
 7337:     }
 7338: }
 7339: 
 7340: // ]]>
 7341: </script>
 7342: 
 7343: ENDSCRIPT
 7344: }
 7345: 
 7346: sub noninst_users {
 7347:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 7348:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 7349:     my $class = 'LC_left_item';
 7350:     if ($css_class) {
 7351:         $css_class = ' class="'.$css_class.'"'; 
 7352:     }
 7353:     if ($rowid) {
 7354:         $rowid = ' id="'.$rowid.'"';
 7355:     }
 7356:     if ($rowstyle) {
 7357:         $rowstyle = ' style="'.$rowstyle.'"';
 7358:     }
 7359:     my ($output,$description);
 7360:     if ($type eq 'default') {
 7361:         $description = &mt('Requests for: [_1]',$typetitle);
 7362:     } else {
 7363:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 7364:     }
 7365:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 7366:               "<td>$description</td>\n".           
 7367:               '<td class="'.$class.'" colspan="2">'.
 7368:               '<table><tr>';
 7369:     my %headers = &Apache::lonlocal::texthash( 
 7370:               approve  => 'Processing',
 7371:               email    => 'E-mail',
 7372:               username => 'Username',
 7373:     );
 7374:     foreach my $item ('approve','email','username') {
 7375:         $output .= '<th>'.$headers{$item}.'</th>';
 7376:     }
 7377:     $output .= '</tr><tr>';
 7378:     foreach my $item ('approve','email','username') {
 7379:         $output .= '<td style="vertical-align: top">';
 7380:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 7381:         if ($item eq 'approve') {
 7382:             %choices = &Apache::lonlocal::texthash (
 7383:                                                      automatic => 'Automatically approved',
 7384:                                                      approval  => 'Queued for approval',
 7385:                                                    );
 7386:             @options = ('automatic','approval');
 7387:             $hashref = $processing;
 7388:             $defoption = 'automatic';
 7389:             $name = 'cancreate_emailprocess_'.$type;
 7390:         } elsif ($item eq 'email') {
 7391:             %choices = &Apache::lonlocal::texthash (
 7392:                                                      any     => 'Any e-mail',
 7393:                                                      inst    => 'Institutional only',
 7394:                                                      noninst => 'Non-institutional only',
 7395:                                                      custom  => 'Custom restrictions',
 7396:                                                    );
 7397:             @options = ('any','inst','noninst');
 7398:             my $showcustom;
 7399:             if (ref($emailrules) eq 'HASH') {
 7400:                 if (keys(%{$emailrules}) > 0) {
 7401:                     push(@options,'custom');
 7402:                     $showcustom = 'cancreate_emailrule';
 7403:                     if (ref($settings) eq 'HASH') {
 7404:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 7405:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 7406:                                 if (exists($emailrules->{$rule})) {
 7407:                                     $hascustom ++;
 7408:                                 }
 7409:                             }
 7410:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 7411:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 7412:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 7413:                                     if (exists($emailrules->{$rule})) {
 7414:                                         $hascustom ++;
 7415:                                     }
 7416:                                 }
 7417:                             }
 7418:                         }
 7419:                     }
 7420:                 }
 7421:             }
 7422:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 7423:                                                      "'cancreate_emaildomain','$type'".');"';
 7424:             $hashref = $emailoptions;
 7425:             $defoption = 'any';
 7426:             $name = 'cancreate_emailoptions_'.$type;
 7427:         } elsif ($item eq 'username') {
 7428:             %choices = &Apache::lonlocal::texthash (
 7429:                                                      all    => 'Same as e-mail',
 7430:                                                      first  => 'Omit @domain',
 7431:                                                      free   => 'Free to choose',
 7432:                                                    );
 7433:             @options = ('all','first','free');
 7434:             $hashref = $emailverified;
 7435:             $defoption = 'all';
 7436:             $name = 'cancreate_usernameoptions_'.$type;
 7437:         }
 7438:         foreach my $option (@options) {
 7439:             my $checked;
 7440:             if (ref($hashref) eq 'HASH') {
 7441:                 if ($type eq '') {
 7442:                     if (!exists($hashref->{'default'})) {
 7443:                         if ($option eq $defoption) {
 7444:                             $checked = ' checked="checked"';
 7445:                         }
 7446:                     } else {
 7447:                         if ($hashref->{'default'} eq $option) {
 7448:                             $checked = ' checked="checked"';
 7449:                         }
 7450:                     }
 7451:                 } else {
 7452:                     if (!exists($hashref->{$type})) {
 7453:                         if ($option eq $defoption) {
 7454:                             $checked = ' checked="checked"';
 7455:                         }
 7456:                     } else {
 7457:                         if ($hashref->{$type} eq $option) {
 7458:                             $checked = ' checked="checked"';
 7459:                         }
 7460:                     }
 7461:                 }
 7462:             } elsif (($item eq 'email') && ($hascustom)) {
 7463:                 if ($option eq 'custom') {
 7464:                     $checked = ' checked="checked"';
 7465:                 }
 7466:             } elsif ($option eq $defoption) {
 7467:                 $checked = ' checked="checked"';
 7468:             }
 7469:             $output .= '<span class="LC_nobreak"><label>'.
 7470:                        '<input type="radio" name="'.$name.'"'.
 7471:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 7472:                        $choices{$option}.'</label></span><br />';
 7473:             if ($item eq 'email') {
 7474:                 if ($option eq 'custom') {
 7475:                     my $id = 'cancreate_emailrule_'.$type;
 7476:                     my $display = 'none';
 7477:                     if ($checked) {
 7478:                         $display = 'inline';
 7479:                     }
 7480:                     my $numinrow = 2;
 7481:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 7482:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 7483:                                &user_formats_row('email',$settings,$emailrules,
 7484:                                                  $emailruleorder,$numinrow,'',$type);
 7485:                               '</table></fieldset>';
 7486:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 7487:                     my %text = &Apache::lonlocal::texthash (
 7488:                                                              inst    => 'must end:',
 7489:                                                              noninst => 'cannot end:',
 7490:                                                            );
 7491:                     my $value;
 7492:                     if (ref($emaildomain) eq 'HASH') {
 7493:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 7494:                             $value = $emaildomain->{$type}->{$option}; 
 7495:                         }
 7496:                     }
 7497:                     if ($value eq '') {
 7498:                         $value = '@'.$intdom;
 7499:                     }
 7500:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 7501:                     my $display = 'none';
 7502:                     if ($checked) {
 7503:                         $display = 'inline';
 7504:                     }
 7505:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 7506:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 7507:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 7508:                                '</div>';
 7509:                 }
 7510:             }
 7511:         }
 7512:         $output .= '</td>'."\n";
 7513:     }
 7514:     $output .= "</tr></table></td></tr>\n";
 7515:     return $output;
 7516: }
 7517: 
 7518: sub captcha_choice {
 7519:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 7520:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 7521:         $vertext,$currver);
 7522:     my %lt = &captcha_phrases();
 7523:     $keyentry = 'hidden';
 7524:     if ($context eq 'cancreate') {
 7525:         $rowname = &mt('CAPTCHA validation');
 7526:     } elsif ($context eq 'login') {
 7527:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 7528:     }
 7529:     if (ref($settings) eq 'HASH') {
 7530:         if ($settings->{'captcha'}) {
 7531:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 7532:         } else {
 7533:             $checked{'original'} = ' checked="checked"';
 7534:         }
 7535:         if ($settings->{'captcha'} eq 'recaptcha') {
 7536:             $pubtext = $lt{'pub'};
 7537:             $privtext = $lt{'priv'};
 7538:             $keyentry = 'text';
 7539:             $vertext = $lt{'ver'};
 7540:             $currver = $settings->{'recaptchaversion'};
 7541:             if ($currver ne '2') {
 7542:                 $currver = 1;
 7543:             }
 7544:         }
 7545:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 7546:             $currpub = $settings->{'recaptchakeys'}{'public'};
 7547:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 7548:         }
 7549:     } else {
 7550:         $checked{'original'} = ' checked="checked"';
 7551:     }
 7552:     my $css_class;
 7553:     if ($itemcount%2) {
 7554:         $css_class = 'LC_odd_row';
 7555:     }
 7556:     if ($customcss) {
 7557:         $css_class .= " $customcss";
 7558:     }
 7559:     $css_class =~ s/^\s+//;
 7560:     if ($css_class) {
 7561:         $css_class = ' class="'.$css_class.'"';
 7562:     }
 7563:     if ($rowstyle) {
 7564:         $css_class .= ' style="'.$rowstyle.'"';
 7565:     }
 7566:     my $output = '<tr'.$css_class.'>'.
 7567:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 7568:                  '<table><tr><td>'."\n";
 7569:     foreach my $option ('original','recaptcha','notused') {
 7570:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 7571:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 7572:                    $lt{$option}.'</label></span>';
 7573:         unless ($option eq 'notused') {
 7574:             $output .= ('&nbsp;'x2)."\n";
 7575:         }
 7576:     }
 7577: #
 7578: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 7579: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 7580: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 7581: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 7582: #
 7583:     $output .= '</td></tr>'."\n".
 7584:                '<tr><td class="LC_zero_height">'."\n".
 7585:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 7586:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 7587:                $currpub.'" size="40" /></span><br />'."\n".
 7588:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 7589:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 7590:                $currpriv.'" size="40" /></span><br />'.
 7591:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 7592:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 7593:                $currver.'" size="3" /></span><br />'.
 7594:                '</td></tr></table>'."\n".
 7595:                '</td></tr>';
 7596:     return $output;
 7597: }
 7598: 
 7599: sub user_formats_row {
 7600:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 7601:     my $output;
 7602:     my %text = (
 7603:                    'username' => 'new usernames',
 7604:                    'id'       => 'IDs',
 7605:                );
 7606:     unless ($type eq 'email') {
 7607:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7608:         $output = '<tr '.$css_class.'>'.
 7609:                   '<td><span class="LC_nobreak">'.
 7610:                   &mt("Format rules to check for $text{$type}: ").
 7611:                   '</td><td class="LC_left_item" colspan="2"><table>';
 7612:     }
 7613:     my $rem;
 7614:     if (ref($ruleorder) eq 'ARRAY') {
 7615:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 7616:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 7617:                 my $rem = $i%($numinrow);
 7618:                 if ($rem == 0) {
 7619:                     if ($i > 0) {
 7620:                         $output .= '</tr>';
 7621:                     }
 7622:                     $output .= '<tr>';
 7623:                 }
 7624:                 my $check = ' ';
 7625:                 if (ref($settings) eq 'HASH') {
 7626:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 7627:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 7628:                             $check = ' checked="checked" ';
 7629:                         }
 7630:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 7631:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 7632:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 7633:                                 $check = ' checked="checked" ';
 7634:                             }
 7635:                         }
 7636:                     }
 7637:                 }
 7638:                 my $name = $type.'_rule';
 7639:                 if ($type eq 'email') {
 7640:                     $name .= '_'.$status;
 7641:                 }
 7642:                 $output .= '<td class="LC_left_item">'.
 7643:                            '<span class="LC_nobreak"><label>'.
 7644:                            '<input type="checkbox" name="'.$name.'" '.
 7645:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 7646:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 7647:             }
 7648:         }
 7649:         $rem = @{$ruleorder}%($numinrow);
 7650:     }
 7651:     my $colsleft;
 7652:     if ($rem) {
 7653:         $colsleft = $numinrow - $rem;
 7654:     }
 7655:     if ($colsleft > 1 ) {
 7656:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7657:                    '&nbsp;</td>';
 7658:     } elsif ($colsleft == 1) {
 7659:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7660:     }
 7661:     $output .= '</tr></table>';
 7662:     unless ($type eq 'email') {
 7663:         $output .= '</td></tr>';
 7664:     }
 7665:     return $output;
 7666: }
 7667: 
 7668: sub usercreation_types {
 7669:     my %lt = &Apache::lonlocal::texthash (
 7670:                     author     => 'When adding a co-author',
 7671:                     course     => 'When adding a user to a course',
 7672:                     requestcrs => 'When requesting a course',
 7673:                     any        => 'Any',
 7674:                     official   => 'Institutional only ',
 7675:                     unofficial => 'Non-institutional only',
 7676:                     none       => 'None',
 7677:     );
 7678:     return %lt;
 7679: }
 7680: 
 7681: sub selfcreation_types {
 7682:     my %lt = &Apache::lonlocal::texthash (
 7683:                     selfcreate => 'User creates own account',
 7684:                     any        => 'Any',
 7685:                     official   => 'Institutional only ',
 7686:                     unofficial => 'Non-institutional only',
 7687:                     email      => 'E-mail address',
 7688:                     login      => 'Institutional Login',
 7689:                     sso        => 'SSO',
 7690:              );
 7691: }
 7692: 
 7693: sub authtype_names {
 7694:     my %lt = &Apache::lonlocal::texthash(
 7695:                       int    => 'Internal',
 7696:                       krb4   => 'Kerberos 4',
 7697:                       krb5   => 'Kerberos 5',
 7698:                       loc    => 'Local',
 7699:                       lti    => 'LTI',
 7700:                   );
 7701:     return %lt;
 7702: }
 7703: 
 7704: sub context_names {
 7705:     my %context_title = &Apache::lonlocal::texthash(
 7706:        author => 'Creating users when an Author',
 7707:        course => 'Creating users when in a course',
 7708:        domain => 'Creating users when a Domain Coordinator',
 7709:     );
 7710:     return %context_title;
 7711: }
 7712: 
 7713: sub print_usermodification {
 7714:     my ($position,$dom,$settings,$rowtotal) = @_;
 7715:     my $numinrow = 4;
 7716:     my ($context,$datatable,$rowcount);
 7717:     if ($position eq 'top') {
 7718:         $rowcount = 0;
 7719:         $context = 'author'; 
 7720:         foreach my $role ('ca','aa') {
 7721:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7722:                                                    $numinrow,$rowcount);
 7723:             $$rowtotal ++;
 7724:             $rowcount ++;
 7725:         }
 7726:     } elsif ($position eq 'bottom') {
 7727:         $context = 'course';
 7728:         $rowcount = 0;
 7729:         foreach my $role ('st','ep','ta','in','cr') {
 7730:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7731:                                                    $numinrow,$rowcount);
 7732:             $$rowtotal ++;
 7733:             $rowcount ++;
 7734:         }
 7735:     }
 7736:     return $datatable;
 7737: }
 7738: 
 7739: sub print_defaults {
 7740:     my ($position,$dom,$settings,$rowtotal) = @_;
 7741:     my $rownum = 0;
 7742:     my ($datatable,$css_class,$titles);
 7743:     unless ($position eq 'bottom') {
 7744:         $titles = &defaults_titles($dom);
 7745:     }
 7746:     if ($position eq 'top') {
 7747:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 7748:                      'datelocale_def','portal_def');
 7749:         my %defaults;
 7750:         if (ref($settings) eq 'HASH') {
 7751:             %defaults = %{$settings};
 7752:         } else {
 7753:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7754:             foreach my $item (@items) {
 7755:                 $defaults{$item} = $domdefaults{$item};
 7756:             }
 7757:         }
 7758:         foreach my $item (@items) {
 7759:             if ($rownum%2) {
 7760:                 $css_class = '';
 7761:             } else {
 7762:                 $css_class = ' class="LC_odd_row" ';
 7763:             }
 7764:             $datatable .= '<tr'.$css_class.'>'.
 7765:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7766:                           '</span></td><td class="LC_right_item" colspan="3">';
 7767:             if ($item eq 'auth_def') {
 7768:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 7769:                 my %shortauth = (
 7770:                                  internal => 'int',
 7771:                                  krb4 => 'krb4',
 7772:                                  krb5 => 'krb5',
 7773:                                  localauth  => 'loc',
 7774:                                  lti => 'lti',
 7775:                                 );
 7776:                 my %authnames = &authtype_names();
 7777:                 foreach my $auth (@authtypes) {
 7778:                     my $checked = ' ';
 7779:                     if ($defaults{$item} eq $auth) {
 7780:                         $checked = ' checked="checked" ';
 7781:                     }
 7782:                     $datatable .= '<label><input type="radio" name="'.$item.
 7783:                                   '" value="'.$auth.'"'.$checked.'/>'.
 7784:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 7785:                 }
 7786:             } elsif ($item eq 'timezone_def') {
 7787:                 my $includeempty = 1;
 7788:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 7789:             } elsif ($item eq 'datelocale_def') {
 7790:                 my $includeempty = 1;
 7791:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 7792:             } elsif ($item eq 'lang_def') {
 7793:                 my $includeempty = 1;
 7794:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 7795:             } else {
 7796:                 my $size;
 7797:                 if ($item eq 'portal_def') {
 7798:                     $size = ' size="25"';
 7799:                 }
 7800:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7801:                               $defaults{$item}.'"'.$size.' />';
 7802:             }
 7803:             $datatable .= '</td></tr>';
 7804:             $rownum ++;
 7805:         }
 7806:     } elsif ($position eq 'middle') {
 7807:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7808:         my %defaults;
 7809:         if (ref($settings) eq 'HASH') {
 7810:             %defaults = %{$settings};
 7811:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7812:                 $defaults{'intauth_cost'} = 10;
 7813:             }
 7814:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7815:                 $defaults{'intauth_check'} = 0;
 7816:             }
 7817:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7818:                 $defaults{'intauth_switch'} = 0;
 7819:             }
 7820:         } else {
 7821:             %defaults = (
 7822:                           'intauth_cost'   => 10,
 7823:                           'intauth_check'  => 0,
 7824:                           'intauth_switch' => 0,
 7825:                         );
 7826:         }
 7827:         foreach my $item (@items) {
 7828:             if ($rownum%2) {
 7829:                 $css_class = '';
 7830:             } else {
 7831:                 $css_class = ' class="LC_odd_row" ';
 7832:             }
 7833:             $datatable .= '<tr'.$css_class.'>'.
 7834:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7835:                           '</span></td><td class="LC_left_item" colspan="3">';
 7836:             if ($item eq 'intauth_switch') {
 7837:                 my @options = (0,1,2);
 7838:                 my %optiondesc = &Apache::lonlocal::texthash (
 7839:                                    0 => 'No',
 7840:                                    1 => 'Yes',
 7841:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7842:                                  );
 7843:                 $datatable .= '<table width="100%">';
 7844:                 foreach my $option (@options) {
 7845:                     my $checked = ' ';
 7846:                     if ($defaults{$item} eq $option) {
 7847:                         $checked = ' checked="checked"';
 7848:                     }
 7849:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7850:                                   '<label><input type="radio" name="'.$item.
 7851:                                   '" value="'.$option.'"'.$checked.' />'.
 7852:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7853:                 }
 7854:                 $datatable .= '</table>';
 7855:             } elsif ($item eq 'intauth_check') {
 7856:                 my @options = (0,1,2);
 7857:                 my %optiondesc = &Apache::lonlocal::texthash (
 7858:                                    0 => 'No',
 7859:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7860:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7861:                                  );
 7862:                 $datatable .= '<table width="100%">';
 7863:                 foreach my $option (@options) {
 7864:                     my $checked = ' ';
 7865:                     my $onclick;
 7866:                     if ($defaults{$item} eq $option) {
 7867:                         $checked = ' checked="checked"';
 7868:                     }
 7869:                     if ($option == 2) {
 7870:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7871:                     }
 7872:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7873:                                   '<label><input type="radio" name="'.$item.
 7874:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7875:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7876:                 }
 7877:                 $datatable .= '</table>';
 7878:             } else {
 7879:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7880:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 7881:             }
 7882:             $datatable .= '</td></tr>';
 7883:             $rownum ++;
 7884:         }
 7885:     } else {
 7886:         my %defaults;
 7887:         if (ref($settings) eq 'HASH') {
 7888:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7889:                 my $maxnum = @{$settings->{'inststatusorder'}};
 7890:                 for (my $i=0; $i<$maxnum; $i++) {
 7891:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 7892:                     my $item = $settings->{'inststatusorder'}->[$i];
 7893:                     my $title = $settings->{'inststatustypes'}->{$item};
 7894:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 7895:                     $datatable .= '<tr'.$css_class.'>'.
 7896:                                   '<td><span class="LC_nobreak">'.
 7897:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 7898:                     for (my $k=0; $k<=$maxnum; $k++) {
 7899:                         my $vpos = $k+1;
 7900:                         my $selstr;
 7901:                         if ($k == $i) {
 7902:                             $selstr = ' selected="selected" ';
 7903:                         }
 7904:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7905:                     }
 7906:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 7907:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 7908:                                   &mt('delete').'</span></td>'.
 7909:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 7910:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 7911:                                   '</span></td></tr>';
 7912:                 }
 7913:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 7914:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 7915:                 $datatable .= '<tr '.$css_class.'>'.
 7916:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 7917:                 for (my $k=0; $k<=$maxnum; $k++) {
 7918:                     my $vpos = $k+1;
 7919:                     my $selstr;
 7920:                     if ($k == $maxnum) {
 7921:                         $selstr = ' selected="selected" ';
 7922:                     }
 7923:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7924:                 }
 7925:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 7926:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 7927:                               '&nbsp;'.&mt('(new)').
 7928:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 7929:                               &mt('Name displayed:').
 7930:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 7931:                               '</tr>'."\n";
 7932:                 $rownum ++;
 7933:             }
 7934:         }
 7935:     }
 7936:     $$rowtotal += $rownum;
 7937:     return $datatable;
 7938: }
 7939: 
 7940: sub get_languages_hash {
 7941:     my %langchoices;
 7942:     foreach my $id (&Apache::loncommon::languageids()) {
 7943:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7944:         if ($code ne '') {
 7945:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7946:         }
 7947:     }
 7948:     return %langchoices;
 7949: }
 7950: 
 7951: sub defaults_titles {
 7952:     my ($dom) = @_;
 7953:     my %titles = &Apache::lonlocal::texthash (
 7954:                    'auth_def'      => 'Default authentication type',
 7955:                    'auth_arg_def'  => 'Default authentication argument',
 7956:                    'lang_def'      => 'Default language',
 7957:                    'timezone_def'  => 'Default timezone',
 7958:                    'datelocale_def' => 'Default locale for dates',
 7959:                    'portal_def'     => 'Portal/Default URL',
 7960:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7961:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7962:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7963:                  );
 7964:     if ($dom) {
 7965:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7966:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7967:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7968:         $protocol = 'http' if ($protocol ne 'https');
 7969:         if ($uint_dom) {
 7970:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7971:                                          $uint_dom);
 7972:         }
 7973:     }
 7974:     return (\%titles);
 7975: }
 7976: 
 7977: sub print_scantronformat {
 7978:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 7979:     my $itemcount = 1;
 7980:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 7981:         %confhash);
 7982:     my $switchserver = &check_switchserver($dom,$confname);
 7983:     my %lt = &Apache::lonlocal::texthash (
 7984:                 default => 'Default bubblesheet format file error',
 7985:                 custom  => 'Custom bubblesheet format file error',
 7986:              );
 7987:     my %scantronfiles = (
 7988:         default => 'default.tab',
 7989:         custom => 'custom.tab',
 7990:     );
 7991:     foreach my $key (keys(%scantronfiles)) {
 7992:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 7993:                               .$scantronfiles{$key};
 7994:     }
 7995:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 7996:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 7997:         if (!$switchserver) {
 7998:             my $servadm = $r->dir_config('lonAdmEMail');
 7999:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 8000:             if ($configuserok eq 'ok') {
 8001:                 if ($author_ok eq 'ok') {
 8002:                     my %legacyfile = (
 8003:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 8004:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 8005:                     );
 8006:                     my %md5chk;
 8007:                     foreach my $type (keys(%legacyfile)) {
 8008:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 8009:                         chomp($md5chk{$type});
 8010:                     }
 8011:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 8012:                         foreach my $type (keys(%legacyfile)) {
 8013:                             ($scantronurls{$type},my $error) = 
 8014:                                 &legacy_scantronformat($r,$dom,$confname,
 8015:                                                  $type,$legacyfile{$type},
 8016:                                                  $scantronurls{$type},
 8017:                                                  $scantronfiles{$type});
 8018:                             if ($error ne '') {
 8019:                                 $error{$type} = $error;
 8020:                             }
 8021:                         }
 8022:                         if (keys(%error) == 0) {
 8023:                             $is_custom = 1;
 8024:                             $confhash{'scantron'}{'scantronformat'} = 
 8025:                                 $scantronurls{'custom'};
 8026:                             my $putresult = 
 8027:                                 &Apache::lonnet::put_dom('configuration',
 8028:                                                          \%confhash,$dom);
 8029:                             if ($putresult ne 'ok') {
 8030:                                 $error{'custom'} = 
 8031:                                     '<span class="LC_error">'.
 8032:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8033:                             }
 8034:                         }
 8035:                     } else {
 8036:                         ($scantronurls{'default'},my $error) =
 8037:                             &legacy_scantronformat($r,$dom,$confname,
 8038:                                           'default',$legacyfile{'default'},
 8039:                                           $scantronurls{'default'},
 8040:                                           $scantronfiles{'default'});
 8041:                         if ($error eq '') {
 8042:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 8043:                             my $putresult =
 8044:                                 &Apache::lonnet::put_dom('configuration',
 8045:                                                          \%confhash,$dom);
 8046:                             if ($putresult ne 'ok') {
 8047:                                 $error{'default'} =
 8048:                                     '<span class="LC_error">'.
 8049:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8050:                             }
 8051:                         } else {
 8052:                             $error{'default'} = $error;
 8053:                         }
 8054:                     }
 8055:                 }
 8056:             }
 8057:         } else {
 8058:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 8059:         }
 8060:     }
 8061:     if (ref($settings) eq 'HASH') {
 8062:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 8063:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 8064:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 8065:                 $scantronurl = '';
 8066:             } else {
 8067:                 $scantronurl = $settings->{'scantronformat'};
 8068:             }
 8069:             $is_custom = 1;
 8070:         } else {
 8071:             $scantronurl = $scantronurls{'default'};
 8072:         }
 8073:     } else {
 8074:         if ($is_custom) {
 8075:             $scantronurl = $scantronurls{'custom'};
 8076:         } else {
 8077:             $scantronurl = $scantronurls{'default'};
 8078:         }
 8079:     }
 8080:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8081:     $datatable .= '<tr'.$css_class.'>';
 8082:     if (!$is_custom) {
 8083:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 8084:                       '<span class="LC_nobreak">';
 8085:         if ($scantronurl) {
 8086:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 8087:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 8088:         } else {
 8089:             $datatable = &mt('File unavailable for display');
 8090:         }
 8091:         $datatable .= '</span></td>';
 8092:         if (keys(%error) == 0) { 
 8093:             $datatable .= '<td style="vertical-align: bottom">';
 8094:             if (!$switchserver) {
 8095:                 $datatable .= &mt('Upload:').'<br />';
 8096:             }
 8097:         } else {
 8098:             my $errorstr;
 8099:             foreach my $key (sort(keys(%error))) {
 8100:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8101:             }
 8102:             $datatable .= '<td>'.$errorstr;
 8103:         }
 8104:     } else {
 8105:         if (keys(%error) > 0) {
 8106:             my $errorstr;
 8107:             foreach my $key (sort(keys(%error))) {
 8108:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8109:             } 
 8110:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 8111:         } elsif ($scantronurl) {
 8112:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 8113:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8114:             $datatable .= '<td><span class="LC_nobreak">'.
 8115:                           $link.
 8116:                           '<label><input type="checkbox" name="scantronformat_del"'.
 8117:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 8118:                           '<td><span class="LC_nobreak">&nbsp;'.
 8119:                           &mt('Replace:').'</span><br />';
 8120:         }
 8121:     }
 8122:     if (keys(%error) == 0) {
 8123:         if ($switchserver) {
 8124:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 8125:         } else {
 8126:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8127:                          '<input type="file" name="scantronformat" /></span>';
 8128:         }
 8129:     }
 8130:     $datatable .= '</td></tr>';
 8131:     $$rowtotal ++;
 8132:     return $datatable;
 8133: }
 8134: 
 8135: sub legacy_scantronformat {
 8136:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 8137:     my ($url,$error);
 8138:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 8139:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 8140:         (my $result,$url) =
 8141:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 8142:                          '','',$newfile);
 8143:         if ($result ne 'ok') {
 8144:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 8145:         }
 8146:     }
 8147:     return ($url,$error);
 8148: }
 8149: 
 8150: sub print_coursecategories {
 8151:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 8152:     my $datatable;
 8153:     if ($position eq 'top') {
 8154:         my (%checked);
 8155:         my @catitems = ('unauth','auth');
 8156:         my @cattypes = ('std','domonly','codesrch','none');
 8157:         $checked{'unauth'} = 'std';
 8158:         $checked{'auth'} = 'std';
 8159:         if (ref($settings) eq 'HASH') {
 8160:             foreach my $type (@cattypes) {
 8161:                 if ($type eq $settings->{'unauth'}) {
 8162:                     $checked{'unauth'} = $type;
 8163:                 }
 8164:                 if ($type eq $settings->{'auth'}) {
 8165:                     $checked{'auth'} = $type;
 8166:                 }
 8167:             }
 8168:         }
 8169:         my %lt = &Apache::lonlocal::texthash (
 8170:                                                unauth   => 'Catalog type for unauthenticated users',
 8171:                                                auth     => 'Catalog type for authenticated users',
 8172:                                                none     => 'No catalog',
 8173:                                                std      => 'Standard catalog',
 8174:                                                domonly  => 'Domain-only catalog',
 8175:                                                codesrch => "Code search form",
 8176:                                              );
 8177:        my $itemcount = 0;
 8178:        foreach my $item (@catitems) {
 8179:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8180:            $datatable .= '<tr '.$css_class.'>'.
 8181:                          '<td>'.$lt{$item}.'</td>'.
 8182:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 8183:            foreach my $type (@cattypes) {
 8184:                my $ischecked;
 8185:                if ($checked{$item} eq $type) {
 8186:                    $ischecked=' checked="checked"';
 8187:                }
 8188:                $datatable .= '<label>'.
 8189:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 8190:                              ' />'.$lt{$type}.'</label>&nbsp;';
 8191:            }
 8192:            $datatable .= '</span></td></tr>';
 8193:            $itemcount ++;
 8194:         }
 8195:         $$rowtotal += $itemcount;
 8196:     } elsif ($position eq 'middle') {
 8197:         my $toggle_cats_crs = ' ';
 8198:         my $toggle_cats_dom = ' checked="checked" ';
 8199:         my $can_cat_crs = ' ';
 8200:         my $can_cat_dom = ' checked="checked" ';
 8201:         my $toggle_catscomm_comm = ' ';
 8202:         my $toggle_catscomm_dom = ' checked="checked" ';
 8203:         my $can_catcomm_comm = ' ';
 8204:         my $can_catcomm_dom = ' checked="checked" ';
 8205:         my $toggle_catsplace_place = ' ';
 8206:         my $toggle_catsplace_dom = ' checked="checked" ';
 8207:         my $can_catplace_place = ' ';
 8208:         my $can_catplace_dom = ' checked="checked" ';
 8209: 
 8210:         if (ref($settings) eq 'HASH') {
 8211:             if ($settings->{'togglecats'} eq 'crs') {
 8212:                 $toggle_cats_crs = $toggle_cats_dom;
 8213:                 $toggle_cats_dom = ' ';
 8214:             }
 8215:             if ($settings->{'categorize'} eq 'crs') {
 8216:                 $can_cat_crs = $can_cat_dom;
 8217:                 $can_cat_dom = ' ';
 8218:             }
 8219:             if ($settings->{'togglecatscomm'} eq 'comm') {
 8220:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 8221:                 $toggle_catscomm_dom = ' ';
 8222:             }
 8223:             if ($settings->{'categorizecomm'} eq 'comm') {
 8224:                 $can_catcomm_comm = $can_catcomm_dom;
 8225:                 $can_catcomm_dom = ' ';
 8226:             }
 8227:             if ($settings->{'togglecatsplace'} eq 'place') {
 8228:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 8229:                 $toggle_catsplace_dom = ' ';
 8230:             }
 8231:             if ($settings->{'categorizeplace'} eq 'place') {
 8232:                 $can_catplace_place = $can_catplace_dom;
 8233:                 $can_catplace_dom = ' ';
 8234:             }
 8235:         }
 8236:         my %title = &Apache::lonlocal::texthash (
 8237:                      togglecats      => 'Show/Hide a course in catalog',
 8238:                      togglecatscomm  => 'Show/Hide a community in catalog',
 8239:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 8240:                      categorize      => 'Assign a category to a course',
 8241:                      categorizecomm  => 'Assign a category to a community',
 8242:                      categorizeplace => 'Assign a category to a placement test',
 8243:                     );
 8244:         my %level = &Apache::lonlocal::texthash (
 8245:                      dom   => 'Set in Domain',
 8246:                      crs   => 'Set in Course',
 8247:                      comm  => 'Set in Community',
 8248:                      place => 'Set in Placement Test',
 8249:                     );
 8250:         $datatable = '<tr class="LC_odd_row">'.
 8251:                   '<td>'.$title{'togglecats'}.'</td>'.
 8252:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8253:                   '<input type="radio" name="togglecats"'.
 8254:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8255:                   '<label><input type="radio" name="togglecats"'.
 8256:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8257:                   '</tr><tr>'.
 8258:                   '<td>'.$title{'categorize'}.'</td>'.
 8259:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8260:                   '<label><input type="radio" name="categorize"'.
 8261:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8262:                   '<label><input type="radio" name="categorize"'.
 8263:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8264:                   '</tr><tr class="LC_odd_row">'.
 8265:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 8266:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8267:                   '<input type="radio" name="togglecatscomm"'.
 8268:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8269:                   '<label><input type="radio" name="togglecatscomm"'.
 8270:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8271:                   '</tr><tr>'.
 8272:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 8273:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8274:                   '<label><input type="radio" name="categorizecomm"'.
 8275:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8276:                   '<label><input type="radio" name="categorizecomm"'.
 8277:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8278:                   '</tr><tr class="LC_odd_row">'.
 8279:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 8280:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8281:                   '<input type="radio" name="togglecatsplace"'.
 8282:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8283:                   '<label><input type="radio" name="togglecatscomm"'.
 8284:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 8285:                   '</tr><tr>'.
 8286:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 8287:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8288:                   '<label><input type="radio" name="categorizeplace"'.
 8289:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8290:                   '<label><input type="radio" name="categorizeplace"'.
 8291:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 8292:                   '</tr>';
 8293:         $$rowtotal += 6;
 8294:     } else {
 8295:         my $css_class;
 8296:         my $itemcount = 1;
 8297:         my $cathash; 
 8298:         if (ref($settings) eq 'HASH') {
 8299:             $cathash = $settings->{'cats'};
 8300:         }
 8301:         if (ref($cathash) eq 'HASH') {
 8302:             my (@cats,@trails,%allitems,%idx,@jsarray);
 8303:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 8304:                                                    \%allitems,\%idx,\@jsarray);
 8305:             my $maxdepth = scalar(@cats);
 8306:             my $colattrib = '';
 8307:             if ($maxdepth > 2) {
 8308:                 $colattrib = ' colspan="2" ';
 8309:             }
 8310:             my @path;
 8311:             if (@cats > 0) {
 8312:                 if (ref($cats[0]) eq 'ARRAY') {
 8313:                     my $numtop = @{$cats[0]};
 8314:                     my $maxnum = $numtop;
 8315:                     my %default_names = (
 8316:                           instcode    => &mt('Official courses'),
 8317:                           communities => &mt('Communities'),
 8318:                           placement   => &mt('Placement Tests'),
 8319:                     );
 8320: 
 8321:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 8322:                         ($cathash->{'instcode::0'} eq '') ||
 8323:                         (!grep(/^communities$/,@{$cats[0]})) || 
 8324:                         ($cathash->{'communities::0'} eq '') ||
 8325:                         (!grep(/^placement$/,@{$cats[0]})) ||
 8326:                         ($cathash->{'placement::0'} eq '')) {
 8327:                         $maxnum ++;
 8328:                     }
 8329:                     my $lastidx;
 8330:                     for (my $i=0; $i<$numtop; $i++) {
 8331:                         my $parent = $cats[0][$i];
 8332:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8333:                         my $item = &escape($parent).'::0';
 8334:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 8335:                         $lastidx = $idx{$item};
 8336:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8337:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 8338:                         for (my $k=0; $k<=$maxnum; $k++) {
 8339:                             my $vpos = $k+1;
 8340:                             my $selstr;
 8341:                             if ($k == $i) {
 8342:                                 $selstr = ' selected="selected" ';
 8343:                             }
 8344:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8345:                         }
 8346:                         $datatable .= '</select></span></td><td>';
 8347:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 8348:                             $datatable .=  '<span class="LC_nobreak">'
 8349:                                            .$default_names{$parent}.'</span>';
 8350:                             if ($parent eq 'instcode') {
 8351:                                 $datatable .= '<br /><span class="LC_nobreak">('
 8352:                                               .&mt('with institutional codes')
 8353:                                               .')</span></td><td'.$colattrib.'>';
 8354:                             } else {
 8355:                                 $datatable .= '<table><tr><td>';
 8356:                             }
 8357:                             $datatable .= '<span class="LC_nobreak">'
 8358:                                           .'<label><input type="radio" name="'
 8359:                                           .$parent.'" value="1" checked="checked" />'
 8360:                                           .&mt('Display').'</label>';
 8361:                             if ($parent eq 'instcode') {
 8362:                                 $datatable .= '&nbsp;';
 8363:                             } else {
 8364:                                 $datatable .= '</span></td></tr><tr><td>'
 8365:                                               .'<span class="LC_nobreak">';
 8366:                             }
 8367:                             $datatable .= '<label><input type="radio" name="'
 8368:                                           .$parent.'" value="0" />'
 8369:                                           .&mt('Do not display').'</label></span>';
 8370:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 8371:                                 $datatable .= '</td></tr></table>';
 8372:                             }
 8373:                             $datatable .= '</td>';
 8374:                         } else {
 8375:                             $datatable .= $parent
 8376:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 8377:                                           .'<input type="checkbox" name="deletecategory" '
 8378:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 8379:                         }
 8380:                         my $depth = 1;
 8381:                         push(@path,$parent);
 8382:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 8383:                         pop(@path);
 8384:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 8385:                         $itemcount ++;
 8386:                     }
 8387:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8388:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 8389:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 8390:                     for (my $k=0; $k<=$maxnum; $k++) {
 8391:                         my $vpos = $k+1;
 8392:                         my $selstr;
 8393:                         if ($k == $numtop) {
 8394:                             $selstr = ' selected="selected" ';
 8395:                         }
 8396:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8397:                     }
 8398:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 8399:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 8400:                                   .'</tr>'."\n";
 8401:                     $itemcount ++;
 8402:                     foreach my $default ('instcode','communities','placement') {
 8403:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 8404:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8405:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 8406:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 8407:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 8408:                             for (my $k=0; $k<=$maxnum; $k++) {
 8409:                                 my $vpos = $k+1;
 8410:                                 my $selstr;
 8411:                                 if ($k == $maxnum) {
 8412:                                     $selstr = ' selected="selected" ';
 8413:                                 }
 8414:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8415:                             }
 8416:                             $datatable .= '</select></span></td>'.
 8417:                                           '<td><span class="LC_nobreak">'.
 8418:                                           $default_names{$default}.'</span>';
 8419:                             if ($default eq 'instcode') {
 8420:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 8421:                                               .&mt('with institutional codes').')</span>';
 8422:                             }
 8423:                             $datatable .= '</td>'
 8424:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 8425:                                           .&mt('Display').'</label>&nbsp;'
 8426:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 8427:                                           .&mt('Do not display').'</label></span></td></tr>';
 8428:                         }
 8429:                     }
 8430:                 }
 8431:             } else {
 8432:                 $datatable .= &initialize_categories($itemcount);
 8433:             }
 8434:         } else {
 8435:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 8436:                           .&initialize_categories($itemcount);
 8437:         }
 8438:         $$rowtotal += $itemcount;
 8439:     }
 8440:     return $datatable;
 8441: }
 8442: 
 8443: sub print_serverstatuses {
 8444:     my ($dom,$settings,$rowtotal) = @_;
 8445:     my $datatable;
 8446:     my @pages = &serverstatus_pages();
 8447:     my (%namedaccess,%machineaccess);
 8448:     foreach my $type (@pages) {
 8449:         $namedaccess{$type} = '';
 8450:         $machineaccess{$type}= '';
 8451:     }
 8452:     if (ref($settings) eq 'HASH') {
 8453:         foreach my $type (@pages) {
 8454:             if (exists($settings->{$type})) {
 8455:                 if (ref($settings->{$type}) eq 'HASH') {
 8456:                     foreach my $key (keys(%{$settings->{$type}})) {
 8457:                         if ($key eq 'namedusers') {
 8458:                             $namedaccess{$type} = $settings->{$type}->{$key};
 8459:                         } elsif ($key eq 'machines') {
 8460:                             $machineaccess{$type} = $settings->{$type}->{$key};
 8461:                         }
 8462:                     }
 8463:                 }
 8464:             }
 8465:         }
 8466:     }
 8467:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8468:     my $rownum = 0;
 8469:     my $css_class;
 8470:     foreach my $type (@pages) {
 8471:         $rownum ++;
 8472:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 8473:         $datatable .= '<tr'.$css_class.'>'.
 8474:                       '<td><span class="LC_nobreak">'.
 8475:                       $titles->{$type}.'</span></td>'.
 8476:                       '<td class="LC_left_item">'.
 8477:                       '<input type="text" name="'.$type.'_namedusers" '.
 8478:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 8479:                       '<td class="LC_right_item">'.
 8480:                       '<span class="LC_nobreak">'.
 8481:                       '<input type="text" name="'.$type.'_machines" '.
 8482:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 8483:                       '</span></td></tr>'."\n";
 8484:     }
 8485:     $$rowtotal += $rownum;
 8486:     return $datatable;
 8487: }
 8488: 
 8489: sub serverstatus_pages {
 8490:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 8491:             'checksums','clusterstatus','certstatus','metadata_keywords',
 8492:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 8493:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 8494: }
 8495: 
 8496: sub defaults_javascript {
 8497:     my ($settings) = @_;
 8498:     my $intauthcheck = &mt('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.');
 8499:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 8500:     &js_escape(\$intauthcheck);
 8501:     &js_escape(\$intauthcost);
 8502:     my $intauthjs = <<"ENDSCRIPT";
 8503: 
 8504: function warnIntAuth(field) {
 8505:     if (field.name == 'intauth_check') {
 8506:         if (field.value == '2') {
 8507:             alert('$intauthcheck');
 8508:         }
 8509:     }
 8510:     if (field.name == 'intauth_cost') {
 8511:         field.value.replace(/\s/g,'');
 8512:         if (field.value != '') {
 8513:             var regexdigit=/^\\d+\$/;
 8514:             if (!regexdigit.test(field.value)) {
 8515:                 alert('$intauthcost');
 8516:             }
 8517:         }
 8518:     }
 8519:     return;
 8520: }
 8521: 
 8522: ENDSCRIPT
 8523: 
 8524:     if (ref($settings) ne 'HASH') {
 8525:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8526:     }
 8527:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 8528:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 8529:         if ($maxnum eq '') {
 8530:             $maxnum = 0;
 8531:         }
 8532:         $maxnum ++;
 8533:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 8534:         return <<"ENDSCRIPT";
 8535: <script type="text/javascript">
 8536: // <![CDATA[
 8537: function reorderTypes(form,caller) {
 8538:     var changedVal;
 8539: $jstext 
 8540:     var newpos = 'addinststatus_pos';
 8541:     var current = new Array;
 8542:     var maxh = $maxnum;
 8543:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8544:     var oldVal;
 8545:     if (caller == newpos) {
 8546:         changedVal = newitemVal;
 8547:     } else {
 8548:         var curritem = 'inststatus_pos_'+caller;
 8549:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 8550:         current[newitemVal] = newpos;
 8551:     }
 8552:     for (var i=0; i<inststatuses.length; i++) {
 8553:         if (inststatuses[i] != caller) {
 8554:             var elementName = 'inststatus_pos_'+inststatuses[i];
 8555:             if (form.elements[elementName]) {
 8556:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8557:                 current[currVal] = elementName;
 8558:             }
 8559:         }
 8560:     }
 8561:     for (var j=0; j<maxh; j++) {
 8562:         if (current[j] == undefined) {
 8563:             oldVal = j;
 8564:         }
 8565:     }
 8566:     if (oldVal < changedVal) {
 8567:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8568:            var elementName = current[k];
 8569:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8570:         }
 8571:     } else {
 8572:         for (var k=changedVal; k<oldVal; k++) {
 8573:             var elementName = current[k];
 8574:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8575:         }
 8576:     }
 8577:     return;
 8578: }
 8579: 
 8580: $intauthjs
 8581: 
 8582: // ]]>
 8583: </script>
 8584: 
 8585: ENDSCRIPT
 8586:     } else {
 8587:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8588:     }
 8589: }
 8590: 
 8591: sub coursecategories_javascript {
 8592:     my ($settings) = @_;
 8593:     my ($output,$jstext,$cathash);
 8594:     if (ref($settings) eq 'HASH') {
 8595:         $cathash = $settings->{'cats'};
 8596:     }
 8597:     if (ref($cathash) eq 'HASH') {
 8598:         my (@cats,@jsarray,%idx);
 8599:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 8600:         if (@jsarray > 0) {
 8601:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 8602:             for (my $i=0; $i<@jsarray; $i++) {
 8603:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 8604:                     my $catstr = join('","',@{$jsarray[$i]});
 8605:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 8606:                 }
 8607:             }
 8608:         }
 8609:     } else {
 8610:         $jstext  = '    var categories = Array(1);'."\n".
 8611:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 8612:     }
 8613:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 8614:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 8615:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 8616:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 8617:     &js_escape(\$instcode_reserved);
 8618:     &js_escape(\$communities_reserved);
 8619:     &js_escape(\$placement_reserved);
 8620:     &js_escape(\$choose_again);
 8621:     $output = <<"ENDSCRIPT";
 8622: <script type="text/javascript">
 8623: // <![CDATA[
 8624: function reorderCats(form,parent,item,idx) {
 8625:     var changedVal;
 8626: $jstext
 8627:     var newpos = 'addcategory_pos';
 8628:     if (parent == '') {
 8629:         var has_instcode = 0;
 8630:         var maxtop = categories[idx].length;
 8631:         for (var j=0; j<maxtop; j++) {
 8632:             if (categories[idx][j] == 'instcode::0') {
 8633:                 has_instcode == 1;
 8634:             }
 8635:         }
 8636:         if (has_instcode == 0) {
 8637:             categories[idx][maxtop] = 'instcode_pos';
 8638:         }
 8639:     } else {
 8640:         newpos += '_'+parent;
 8641:     }
 8642:     var maxh = 1 + categories[idx].length;
 8643:     var current = new Array;
 8644:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8645:     if (item == newpos) {
 8646:         changedVal = newitemVal;
 8647:     } else {
 8648:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 8649:         current[newitemVal] = newpos;
 8650:     }
 8651:     for (var i=0; i<categories[idx].length; i++) {
 8652:         var elementName = categories[idx][i];
 8653:         if (elementName != item) {
 8654:             if (form.elements[elementName]) {
 8655:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8656:                 current[currVal] = elementName;
 8657:             }
 8658:         }
 8659:     }
 8660:     var oldVal;
 8661:     for (var j=0; j<maxh; j++) {
 8662:         if (current[j] == undefined) {
 8663:             oldVal = j;
 8664:         }
 8665:     }
 8666:     if (oldVal < changedVal) {
 8667:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8668:            var elementName = current[k];
 8669:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8670:         }
 8671:     } else {
 8672:         for (var k=changedVal; k<oldVal; k++) {
 8673:             var elementName = current[k];
 8674:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8675:         }
 8676:     }
 8677:     return;
 8678: }
 8679: 
 8680: function categoryCheck(form) {
 8681:     if (form.elements['addcategory_name'].value == 'instcode') {
 8682:         alert('$instcode_reserved\\n$choose_again');
 8683:         return false;
 8684:     }
 8685:     if (form.elements['addcategory_name'].value == 'communities') {
 8686:         alert('$communities_reserved\\n$choose_again');
 8687:         return false;
 8688:     }
 8689:     if (form.elements['addcategory_name'].value == 'placement') {
 8690:         alert('$placement_reserved\\n$choose_again');
 8691:         return false;
 8692:     }
 8693:     return true;
 8694: }
 8695: 
 8696: // ]]>
 8697: </script>
 8698: 
 8699: ENDSCRIPT
 8700:     return $output;
 8701: }
 8702: 
 8703: sub initialize_categories {
 8704:     my ($itemcount) = @_;
 8705:     my ($datatable,$css_class,$chgstr);
 8706:     my %default_names = (
 8707:                       instcode    => 'Official courses (with institutional codes)',
 8708:                       communities => 'Communities',
 8709:                       placement   => 'Placement Tests',
 8710:                         );
 8711:     my %selnum = (
 8712:                    instcode    => '0',
 8713:                    communities => '1',
 8714:                    placement   => '2',
 8715:                  );
 8716:     my %selected;
 8717:     foreach my $default ('instcode','communities','placement') {
 8718:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8719:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 8720:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
 8721:         $selected{$selnum{$default}} = ' selected="selected"';
 8722:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8723:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
 8724:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
 8725:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
 8726:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
 8727:                      .'<option value="3">4</option></select>&nbsp;'
 8728:                      .$default_names{$default}
 8729:                      .'</span></td><td><span class="LC_nobreak">'
 8730:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 8731:                      .&mt('Display').'</label>&nbsp;<label>'
 8732:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 8733:                  .'</label></span></td></tr>';
 8734:         $itemcount ++;
 8735:     }
 8736:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8737:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 8738:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8739:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 8740:                   .'<option value="0">1</option>'
 8741:                   .'<option value="1">2</option>'
 8742:                   .'<option value="2">3</option>'
 8743:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
 8744:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 8745:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 8746:                   .'</td></tr>';
 8747:     return $datatable;
 8748: }
 8749: 
 8750: sub build_category_rows {
 8751:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 8752:     my ($text,$name,$item,$chgstr);
 8753:     if (ref($cats) eq 'ARRAY') {
 8754:         my $maxdepth = scalar(@{$cats});
 8755:         if (ref($cats->[$depth]) eq 'HASH') {
 8756:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8757:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8758:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8759:                 $text .= '<td><table class="LC_data_table">';
 8760:                 my ($idxnum,$parent_name,$parent_item);
 8761:                 my $higher = $depth - 1;
 8762:                 if ($higher == 0) {
 8763:                     $parent_name = &escape($parent).'::'.$higher;
 8764:                 } else {
 8765:                     if (ref($path) eq 'ARRAY') {
 8766:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8767:                     }
 8768:                 }
 8769:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8770:                 for (my $j=0; $j<=$numchildren; $j++) {
 8771:                     if ($j < $numchildren) {
 8772:                         $name = $cats->[$depth]{$parent}[$j];
 8773:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8774:                         $idxnum = $idx->{$item};
 8775:                     } else {
 8776:                         $name = $parent_name;
 8777:                         $item = $parent_item;
 8778:                     }
 8779:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8780:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8781:                     for (my $i=0; $i<=$numchildren; $i++) {
 8782:                         my $vpos = $i+1;
 8783:                         my $selstr;
 8784:                         if ($j == $i) {
 8785:                             $selstr = ' selected="selected" ';
 8786:                         }
 8787:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8788:                     }
 8789:                     $text .= '</select>&nbsp;';
 8790:                     if ($j < $numchildren) {
 8791:                         my $deeper = $depth+1;
 8792:                         $text .= $name.'&nbsp;'
 8793:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8794:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8795:                         if(ref($path) eq 'ARRAY') {
 8796:                             push(@{$path},$name);
 8797:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8798:                             pop(@{$path});
 8799:                         }
 8800:                     } else {
 8801:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 8802:                         if ($j == $numchildren) {
 8803:                             $text .= $name;
 8804:                         } else {
 8805:                             $text .= $item;
 8806:                         }
 8807:                         $text .= '" value="" />';
 8808:                     }
 8809:                     $text .= '</td></tr>';
 8810:                 }
 8811:                 $text .= '</table></td>';
 8812:             } else {
 8813:                 my $higher = $depth-1;
 8814:                 if ($higher == 0) {
 8815:                     $name = &escape($parent).'::'.$higher;
 8816:                 } else {
 8817:                     if (ref($path) eq 'ARRAY') {
 8818:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8819:                     }
 8820:                 }
 8821:                 my $colspan;
 8822:                 if ($parent ne 'instcode') {
 8823:                     $colspan = $maxdepth - $depth - 1;
 8824:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 8825:                 }
 8826:             }
 8827:         }
 8828:     }
 8829:     return $text;
 8830: }
 8831: 
 8832: sub modifiable_userdata_row {
 8833:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 8834:         $rowid,$customcss,$rowstyle) = @_;
 8835:     my ($role,$rolename,$statustype);
 8836:     $role = $item;
 8837:     if ($context eq 'cancreate') {
 8838:         if ($item =~ /^(emailusername)_(.+)$/) {
 8839:             $role = $1;
 8840:             $statustype = $2;
 8841:             if (ref($usertypes) eq 'HASH') {
 8842:                 if ($usertypes->{$statustype}) {
 8843:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 8844:                 } else {
 8845:                     $rolename = &mt('Data provided by user');
 8846:                 }
 8847:             }
 8848:         }
 8849:     } elsif ($context eq 'selfcreate') {
 8850:         if (ref($usertypes) eq 'HASH') {
 8851:             $rolename = $usertypes->{$role};
 8852:         } else {
 8853:             $rolename = $role;
 8854:         }
 8855:     } elsif ($context eq 'lti') {
 8856:         $rolename = &mt('Institutional data used (if available)');
 8857:     } else {
 8858:         if ($role eq 'cr') {
 8859:             $rolename = &mt('Custom role');
 8860:         } else {
 8861:             $rolename = &Apache::lonnet::plaintext($role);
 8862:         }
 8863:     }
 8864:     my (@fields,%fieldtitles);
 8865:     if (ref($fieldsref) eq 'ARRAY') {
 8866:         @fields = @{$fieldsref};
 8867:     } else {
 8868:         @fields = ('lastname','firstname','middlename','generation',
 8869:                    'permanentemail','id');
 8870:     }
 8871:     if ((ref($titlesref) eq 'HASH')) {
 8872:         %fieldtitles = %{$titlesref};
 8873:     } else {
 8874:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8875:     }
 8876:     my $output;
 8877:     my $css_class;
 8878:     if ($rowcount%2) {
 8879:         $css_class = 'LC_odd_row';
 8880:     }
 8881:     if ($customcss) {
 8882:         $css_class .= " $customcss";
 8883:     }
 8884:     $css_class =~ s/^\s+//;
 8885:     if ($css_class) {
 8886:         $css_class = ' class="'.$css_class.'"';
 8887:     }
 8888:     if ($rowstyle) {
 8889:         $css_class .= ' style="'.$rowstyle.'"';
 8890:     }
 8891:     if ($rowid) {
 8892:         $rowid = ' id="'.$rowid.'"';
 8893:     }
 8894:     $output = '<tr '.$css_class.$rowid.'>'.
 8895:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 8896:               '<td class="LC_left_item" colspan="2"><table>';
 8897:     my $rem;
 8898:     my %checks;
 8899:     my %current;
 8900:     if (ref($settings) eq 'HASH') {
 8901:         my $hashref;
 8902:         if ($context eq 'lti') {
 8903:             if (ref($settings) eq 'HASH') {
 8904:                 $hashref = $settings->{'instdata'};
 8905:             }
 8906:         } elsif (ref($settings->{$context}) eq 'HASH') {
 8907:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 8908:                 $hashref = $settings->{'lti_instdata'};
 8909:             }
 8910:             if ($role eq 'emailusername') {
 8911:                 if ($statustype) {
 8912:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 8913:                         $hashref = $settings->{$context}->{$role}->{$statustype};
 8914:                     }
 8915:                 }
 8916:             }
 8917:         }
 8918:         if (ref($hashref) eq 'HASH') { 
 8919:             foreach my $field (@fields) {
 8920:                 if ($hashref->{$field}) {
 8921:                     if ($role eq 'emailusername') {
 8922:                         $checks{$field} = $hashref->{$field};
 8923:                     } else {
 8924:                         $checks{$field} = ' checked="checked" ';
 8925:                     }
 8926:                 }
 8927:             }
 8928:         }
 8929:     }
 8930:  
 8931:     my $total = scalar(@fields);
 8932:     for (my $i=0; $i<$total; $i++) {
 8933:         $rem = $i%($numinrow);
 8934:         if ($rem == 0) {
 8935:             if ($i > 0) {
 8936:                 $output .= '</tr>';
 8937:             }
 8938:             $output .= '<tr>';
 8939:         }
 8940:         my $check = ' ';
 8941:         unless ($role eq 'emailusername') {
 8942:             if (exists($checks{$fields[$i]})) {
 8943:                 $check = $checks{$fields[$i]}
 8944:             } elsif ($context ne 'lti') {
 8945:                 if ($role eq 'st') {
 8946:                     if (ref($settings) ne 'HASH') {
 8947:                         $check = ' checked="checked" '; 
 8948:                     }
 8949:                 }
 8950:             }
 8951:         }
 8952:         $output .= '<td class="LC_left_item">'.
 8953:                    '<span class="LC_nobreak">';
 8954:         my $prefix = 'canmodify';
 8955:         if ($role eq 'emailusername') {
 8956:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 8957:                 $checks{$fields[$i]} = 'omit';
 8958:             }
 8959:             foreach my $option ('required','optional','omit') {
 8960:                 my $checked='';
 8961:                 if ($checks{$fields[$i]} eq $option) {
 8962:                     $checked='checked="checked" ';
 8963:                 }
 8964:                 $output .= '<label>'.
 8965:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 8966:                            &mt($option).'</label>'.('&nbsp;' x2);
 8967:             }
 8968:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 8969:         } else {
 8970:             if ($context eq 'lti') {
 8971:                 $prefix = 'lti';
 8972:             }
 8973:             $output .= '<label>'.
 8974:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
 8975:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 8976:                        '</label>';
 8977:         }
 8978:         $output .= '</span></td>';
 8979:     }
 8980:     $rem = $total%$numinrow;
 8981:     my $colsleft;
 8982:     if ($rem) {
 8983:         $colsleft = $numinrow - $rem;
 8984:     }
 8985:     if ($colsleft > 1) {
 8986:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8987:                    '&nbsp;</td>';
 8988:     } elsif ($colsleft == 1) {
 8989:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8990:     }
 8991:     $output .= '</tr></table></td></tr>';
 8992:     return $output;
 8993: }
 8994: 
 8995: sub insttypes_row {
 8996:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 8997:         $customcss,$rowstyle) = @_;
 8998:     my %lt = &Apache::lonlocal::texthash (
 8999:                       cansearch => 'Users allowed to search',
 9000:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 9001:                       lockablenames => 'User preference to lock name',
 9002:                       selfassign    => 'Self-reportable affiliations',
 9003:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 9004:              );
 9005:     my $showdom;
 9006:     if ($context eq 'cansearch') {
 9007:         $showdom = ' ('.$dom.')';
 9008:     }
 9009:     my $class = 'LC_left_item';
 9010:     if ($context eq 'statustocreate') {
 9011:         $class = 'LC_right_item';
 9012:     }
 9013:     my $css_class;
 9014:     if ($$rowtotal%2) {
 9015:         $css_class = 'LC_odd_row';
 9016:     }
 9017:     if ($customcss) {
 9018:         $css_class .= ' '.$customcss;
 9019:     }
 9020:     $css_class =~ s/^\s+//;
 9021:     if ($css_class) {
 9022:         $css_class = ' class="'.$css_class.'"';
 9023:     }
 9024:     if ($rowstyle) {
 9025:         $css_class .= ' style="'.$rowstyle.'"';
 9026:     }
 9027:     if ($onclick) {
 9028:         $onclick = 'onclick="'.$onclick.'" ';
 9029:     }
 9030:     my $output = '<tr'.$css_class.'>'.
 9031:                  '<td>'.$lt{$context}.$showdom.
 9032:                  '</td><td class="'.$class.'" colspan="2"><table>';
 9033:     my $rem;
 9034:     if (ref($types) eq 'ARRAY') {
 9035:         for (my $i=0; $i<@{$types}; $i++) {
 9036:             if (defined($usertypes->{$types->[$i]})) {
 9037:                 my $rem = $i%($numinrow);
 9038:                 if ($rem == 0) {
 9039:                     if ($i > 0) {
 9040:                         $output .= '</tr>';
 9041:                     }
 9042:                     $output .= '<tr>';
 9043:                 }
 9044:                 my $check = ' ';
 9045:                 if (ref($settings) eq 'HASH') {
 9046:                     if (ref($settings->{$context}) eq 'ARRAY') {
 9047:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 9048:                             $check = ' checked="checked" ';
 9049:                         }
 9050:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 9051:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 9052:                             $check = ' checked="checked" ';
 9053:                         }
 9054:                     } elsif ($context eq 'statustocreate') {
 9055:                         $check = ' checked="checked" ';
 9056:                     }
 9057:                 }
 9058:                 $output .= '<td class="LC_left_item">'.
 9059:                            '<span class="LC_nobreak"><label>'.
 9060:                            '<input type="checkbox" name="'.$context.'" '.
 9061:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 9062:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 9063:             }
 9064:         }
 9065:         $rem = @{$types}%($numinrow);
 9066:     }
 9067:     my $colsleft = $numinrow - $rem;
 9068:     if ($context eq 'overrides') {
 9069:         if ($colsleft > 1) {
 9070:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9071:         } else {
 9072:             $output .= '<td class="LC_left_item">';
 9073:         }
 9074:         $output .= '&nbsp;';  
 9075:     } else {
 9076:         if ($rem == 0) {
 9077:             $output .= '<tr>';
 9078:         }
 9079:         if ($colsleft > 1) {
 9080:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9081:         } else {
 9082:             $output .= '<td class="LC_left_item">';
 9083:         }
 9084:         my $defcheck = ' ';
 9085:         if (ref($settings) eq 'HASH') {  
 9086:             if (ref($settings->{$context}) eq 'ARRAY') {
 9087:                 if (grep(/^default$/,@{$settings->{$context}})) {
 9088:                     $defcheck = ' checked="checked" ';
 9089:                 }
 9090:             } elsif ($context eq 'statustocreate') {
 9091:                 $defcheck = ' checked="checked" ';
 9092:             }
 9093:         }
 9094:         $output .= '<span class="LC_nobreak"><label>'.
 9095:                    '<input type="checkbox" name="'.$context.'" '.
 9096:                    'value="default"'.$defcheck.$onclick.' />'.
 9097:                    $othertitle.'</label></span>';
 9098:     }
 9099:     $output .= '</td></tr></table></td></tr>';
 9100:     return $output;
 9101: }
 9102: 
 9103: sub sorted_searchtitles {
 9104:     my %searchtitles = &Apache::lonlocal::texthash(
 9105:                          'uname' => 'username',
 9106:                          'lastname' => 'last name',
 9107:                          'lastfirst' => 'last name, first name',
 9108:                      );
 9109:     my @titleorder = ('uname','lastname','lastfirst');
 9110:     return (\%searchtitles,\@titleorder);
 9111: }
 9112: 
 9113: sub sorted_searchtypes {
 9114:     my %srchtypes_desc = (
 9115:                            exact    => 'is exact match',
 9116:                            contains => 'contains ..',
 9117:                            begins   => 'begins with ..',
 9118:                          );
 9119:     my @srchtypeorder = ('exact','begins','contains');
 9120:     return (\%srchtypes_desc,\@srchtypeorder);
 9121: }
 9122: 
 9123: sub usertype_update_row {
 9124:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 9125:     my $datatable;
 9126:     my $numinrow = 4;
 9127:     foreach my $type (@{$types}) {
 9128:         if (defined($usertypes->{$type})) {
 9129:             $$rownums ++;
 9130:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 9131:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 9132:                           '</td><td class="LC_left_item"><table>';
 9133:             for (my $i=0; $i<@{$fields}; $i++) {
 9134:                 my $rem = $i%($numinrow);
 9135:                 if ($rem == 0) {
 9136:                     if ($i > 0) {
 9137:                         $datatable .= '</tr>';
 9138:                     }
 9139:                     $datatable .= '<tr>';
 9140:                 }
 9141:                 my $check = ' ';
 9142:                 if (ref($settings) eq 'HASH') {
 9143:                     if (ref($settings->{'fields'}) eq 'HASH') {
 9144:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 9145:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 9146:                                 $check = ' checked="checked" ';
 9147:                             }
 9148:                         }
 9149:                     }
 9150:                 }
 9151: 
 9152:                 if ($i == @{$fields}-1) {
 9153:                     my $colsleft = $numinrow - $rem;
 9154:                     if ($colsleft > 1) {
 9155:                         $datatable .= '<td colspan="'.$colsleft.'">';
 9156:                     } else {
 9157:                         $datatable .= '<td>';
 9158:                     }
 9159:                 } else {
 9160:                     $datatable .= '<td>';
 9161:                 }
 9162:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9163:                               '<input type="checkbox" name="updateable_'.$type.
 9164:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 9165:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 9166:             }
 9167:             $datatable .= '</tr></table></td></tr>';
 9168:         }
 9169:     }
 9170:     return $datatable;
 9171: }
 9172: 
 9173: sub modify_login {
 9174:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9175:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 9176:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 9177:     %title = ( coursecatalog => 'Display course catalog',
 9178:                adminmail => 'Display administrator E-mail address',
 9179:                helpdesk  => 'Display "Contact Helpdesk" link',
 9180:                newuser => 'Link for visitors to create a user account',
 9181:                loginheader => 'Log-in box header');
 9182:     @offon = ('off','on');
 9183:     if (ref($domconfig{login}) eq 'HASH') {
 9184:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 9185:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 9186:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 9187:             }
 9188:         }
 9189:     }
 9190:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 9191:                                            \%domconfig,\%loginhash);
 9192:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9193:     foreach my $item (@toggles) {
 9194:         $loginhash{login}{$item} = $env{'form.'.$item};
 9195:     }
 9196:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 9197:     if (ref($colchanges{'login'}) eq 'HASH') {  
 9198:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 9199:                                          \%loginhash);
 9200:     }
 9201: 
 9202:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9203:     my %domservers = &Apache::lonnet::get_servers($dom);
 9204:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 9205:     if (keys(%servers) > 1) {
 9206:         foreach my $lonhost (keys(%servers)) {
 9207:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 9208:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 9209:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 9210:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 9211:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 9212:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9213:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9214:                         $changes{'loginvia'}{$lonhost} = 1;
 9215:                     } else {
 9216:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 9217:                         $changes{'loginvia'}{$lonhost} = 1;
 9218:                     }
 9219:                 } else {
 9220:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9221:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9222:                         $changes{'loginvia'}{$lonhost} = 1;
 9223:                     }
 9224:                 }
 9225:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 9226:                     foreach my $item (@loginvia_attribs) {
 9227:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 9228:                     }
 9229:                 } else {
 9230:                     foreach my $item (@loginvia_attribs) {
 9231:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9232:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9233:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 9234:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9235:                                 $new = '/';
 9236:                             }
 9237:                         }
 9238:                         if (($item eq 'custompath') && 
 9239:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9240:                             $new = '';
 9241:                         }
 9242:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 9243:                             $changes{'loginvia'}{$lonhost} = 1;
 9244:                         }
 9245:                         if ($item eq 'exempt') {
 9246:                             $new = &check_exempt_addresses($new);
 9247:                         }
 9248:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9249:                     }
 9250:                 }
 9251:             } else {
 9252:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9253:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9254:                     $changes{'loginvia'}{$lonhost} = 1;
 9255:                     foreach my $item (@loginvia_attribs) {
 9256:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9257:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9258:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9259:                                 $new = '/';
 9260:                             }
 9261:                         }
 9262:                         if (($item eq 'custompath') && 
 9263:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9264:                             $new = '';
 9265:                         }
 9266:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9267:                     }
 9268:                 }
 9269:             }
 9270:         }
 9271:     }
 9272: 
 9273:     my $servadm = $r->dir_config('lonAdmEMail');
 9274:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 9275:     if (ref($domconfig{'login'}) eq 'HASH') {
 9276:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 9277:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 9278:                 if ($lang eq 'nolang') {
 9279:                     push(@currlangs,$lang);
 9280:                 } elsif (defined($langchoices{$lang})) {
 9281:                     push(@currlangs,$lang);
 9282:                 } else {
 9283:                     next;
 9284:                 }
 9285:             }
 9286:         }
 9287:     }
 9288:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 9289:     if (@currlangs > 0) {
 9290:         foreach my $lang (@currlangs) {
 9291:             if (grep(/^\Q$lang\E$/,@delurls)) {
 9292:                 $changes{'helpurl'}{$lang} = 1;
 9293:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 9294:                 $changes{'helpurl'}{$lang} = 1;
 9295:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 9296:                 push(@newlangs,$lang);
 9297:             } else {
 9298:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9299:             }
 9300:         }
 9301:     }
 9302:     unless (grep(/^nolang$/,@currlangs)) {
 9303:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 9304:             $changes{'helpurl'}{'nolang'} = 1;
 9305:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 9306:             push(@newlangs,'nolang');
 9307:         }
 9308:     }
 9309:     if ($env{'form.loginhelpurl_add_lang'}) {
 9310:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 9311:             ($env{'form.loginhelpurl_add_file.filename'})) {
 9312:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 9313:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 9314:         }
 9315:     }
 9316:     if ((@newlangs > 0) || ($addedfile)) {
 9317:         my $error;
 9318:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9319:         if ($configuserok eq 'ok') {
 9320:             if ($switchserver) {
 9321:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 9322:             } elsif ($author_ok eq 'ok') {
 9323:                 my @allnew = @newlangs;
 9324:                 if ($addedfile ne '') {
 9325:                     push(@allnew,$addedfile);
 9326:                 }
 9327:                 foreach my $lang (@allnew) {
 9328:                     my $formelem = 'loginhelpurl_'.$lang;
 9329:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 9330:                         $formelem = 'loginhelpurl_add_file';
 9331:                     }
 9332:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9333:                                                                "help/$lang",'','',$newfile{$lang});
 9334:                     if ($result eq 'ok') {
 9335:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 9336:                         $changes{'helpurl'}{$lang} = 1;
 9337:                     } else {
 9338:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 9339:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9340:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 9341:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 9342:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9343:                         }
 9344:                     }
 9345:                 }
 9346:             } else {
 9347:                 $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);
 9348:             }
 9349:         } else {
 9350:             $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);
 9351:         }
 9352:         if ($error) {
 9353:             &Apache::lonnet::logthis($error);
 9354:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9355:         }
 9356:     }
 9357: 
 9358:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 9359:     if (ref($domconfig{'login'}) eq 'HASH') {
 9360:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 9361:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 9362:                 if ($domservers{$lonhost}) {
 9363:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9364:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 9365:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 9366:                     }
 9367:                 }
 9368:             }
 9369:         }
 9370:     }
 9371:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 9372:     foreach my $lonhost (sort(keys(%domservers))) {
 9373:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9374:             $changes{'headtag'}{$lonhost} = 1;
 9375:         } else {
 9376:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 9377:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 9378:             }
 9379:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 9380:                 push(@newhosts,$lonhost);
 9381:             } elsif ($currheadtagurls{$lonhost}) {
 9382:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 9383:                 if ($currexempt{$lonhost}) {
 9384:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 9385:                         $changes{'headtag'}{$lonhost} = 1;
 9386:                     }
 9387:                 } elsif ($possexempt{$lonhost}) {
 9388:                     $changes{'headtag'}{$lonhost} = 1;
 9389:                 }
 9390:                 if ($possexempt{$lonhost}) {
 9391:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9392:                 }
 9393:             }
 9394:         }
 9395:     }
 9396:     if (@newhosts) {
 9397:         my $error;
 9398:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9399:         if ($configuserok eq 'ok') {
 9400:             if ($switchserver) {
 9401:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 9402:             } elsif ($author_ok eq 'ok') {
 9403:                 foreach my $lonhost (@newhosts) {
 9404:                     my $formelem = 'loginheadtag_'.$lonhost;
 9405:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9406:                                                                           "login/headtag/$lonhost",'','',
 9407:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 9408:                     if ($result eq 'ok') {
 9409:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 9410:                         $changes{'headtag'}{$lonhost} = 1;
 9411:                         if ($possexempt{$lonhost}) {
 9412:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9413:                         }
 9414:                     } else {
 9415:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 9416:                                            $newheadtagurls{$lonhost},$result);
 9417:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9418:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 9419:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 9420:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 9421:                         }
 9422:                     }
 9423:                 }
 9424:             } else {
 9425:                 $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);
 9426:             }
 9427:         } else {
 9428:             $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);
 9429:         }
 9430:         if ($error) {
 9431:             &Apache::lonnet::logthis($error);
 9432:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9433:         }
 9434:     }
 9435:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 9436: 
 9437:     my $defaulthelpfile = '/adm/loginproblems.html';
 9438:     my $defaulttext = &mt('Default in use');
 9439: 
 9440:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 9441:                                              $dom);
 9442:     if ($putresult eq 'ok') {
 9443:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9444:         my %defaultchecked = (
 9445:                     'coursecatalog' => 'on',
 9446:                     'helpdesk'      => 'on',
 9447:                     'adminmail'     => 'off',
 9448:                     'newuser'       => 'off',
 9449:         );
 9450:         if (ref($domconfig{'login'}) eq 'HASH') {
 9451:             foreach my $item (@toggles) {
 9452:                 if ($defaultchecked{$item} eq 'on') { 
 9453:                     if (($domconfig{'login'}{$item} eq '0') &&
 9454:                         ($env{'form.'.$item} eq '1')) {
 9455:                         $changes{$item} = 1;
 9456:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9457:                               $domconfig{'login'}{$item} eq '1') &&
 9458:                              ($env{'form.'.$item} eq '0')) {
 9459:                         $changes{$item} = 1;
 9460:                     }
 9461:                 } elsif ($defaultchecked{$item} eq 'off') {
 9462:                     if (($domconfig{'login'}{$item} eq '1') &&
 9463:                         ($env{'form.'.$item} eq '0')) {
 9464:                         $changes{$item} = 1;
 9465:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9466:                               $domconfig{'login'}{$item} eq '0') &&
 9467:                              ($env{'form.'.$item} eq '1')) {
 9468:                         $changes{$item} = 1;
 9469:                     }
 9470:                 }
 9471:             }
 9472:         }
 9473:         if (keys(%changes) > 0 || $colchgtext) {
 9474:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9475:             if (ref($lastactref) eq 'HASH') {
 9476:                 $lastactref->{'domainconfig'} = 1;
 9477:             }
 9478:             $resulttext = &mt('Changes made:').'<ul>';
 9479:             foreach my $item (sort(keys(%changes))) {
 9480:                 if ($item eq 'loginvia') {
 9481:                     if (ref($changes{$item}) eq 'HASH') {
 9482:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 9483:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9484:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 9485:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 9486:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 9487:                                     $protocol = 'http' if ($protocol ne 'https');
 9488:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 9489: 
 9490:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 9491:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 9492:                                     } else {
 9493:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 9494:                                     }
 9495:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 9496:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 9497:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 9498:                                     }
 9499:                                     $resulttext .= '</li>';
 9500:                                 } else {
 9501:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 9502:                                 }
 9503:                             } else {
 9504:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 9505:                             }
 9506:                         }
 9507:                         $resulttext .= '</ul></li>';
 9508:                     }
 9509:                 } elsif ($item eq 'helpurl') {
 9510:                     if (ref($changes{$item}) eq 'HASH') {
 9511:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 9512:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 9513:                                 my ($chg,$link);
 9514:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 9515:                                 if ($lang eq 'nolang') {
 9516:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 9517:                                 } else {
 9518:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 9519:                                 }
 9520:                                 $resulttext .= '<li>'.$chg.'</li>';
 9521:                             } else {
 9522:                                 my $chg;
 9523:                                 if ($lang eq 'nolang') {
 9524:                                     $chg = &mt('custom log-in help file for no preferred language');
 9525:                                 } else {
 9526:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 9527:                                 }
 9528:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 9529:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 9530:                                                       '?inhibitmenu=yes',$chg,600,500).
 9531:                                                '</li>';
 9532:                             }
 9533:                         }
 9534:                     }
 9535:                 } elsif ($item eq 'headtag') {
 9536:                     if (ref($changes{$item}) eq 'HASH') {
 9537:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9538:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9539:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 9540:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9541:                                 $resulttext .= '<li><a href="'.
 9542:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 9543:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 9544:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 9545:                                 if ($possexempt{$lonhost}) {
 9546:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 9547:                                 } else {
 9548:                                     $resulttext .= &mt('included for any client IP');
 9549:                                 }
 9550:                                 $resulttext .= '</li>';
 9551:                             }
 9552:                         }
 9553:                     }
 9554:                 } elsif ($item eq 'captcha') {
 9555:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9556:                         my $chgtxt;
 9557:                         if ($loginhash{'login'}{$item} eq 'notused') {
 9558:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 9559:                         } else {
 9560:                             my %captchas = &captcha_phrases();
 9561:                             if ($captchas{$loginhash{'login'}{$item}}) {
 9562:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 9563:                             } else {
 9564:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 9565:                             }
 9566:                         }
 9567:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9568:                     }
 9569:                 } elsif ($item eq 'recaptchakeys') {
 9570:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9571:                         my ($privkey,$pubkey);
 9572:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 9573:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 9574:                             $privkey = $loginhash{'login'}{$item}{'private'};
 9575:                         }
 9576:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 9577:                         if (!$pubkey) {
 9578:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 9579:                         } else {
 9580:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9581:                         }
 9582:                         if (!$privkey) {
 9583:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 9584:                         } else {
 9585:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 9586:                         }
 9587:                         $chgtxt .= '</ul>';
 9588:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9589:                     }
 9590:                 } elsif ($item eq 'recaptchaversion') {
 9591:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9592:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 9593:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 9594:                                            '</li>';
 9595:                         }
 9596:                     }
 9597:                 } else {
 9598:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 9599:                 }
 9600:             }
 9601:             $resulttext .= $colchgtext.'</ul>';
 9602:         } else {
 9603:             $resulttext = &mt('No changes made to log-in page settings');
 9604:         }
 9605:     } else {
 9606:         $resulttext = '<span class="LC_error">'.
 9607: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9608:     }
 9609:     if ($errors) {
 9610:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9611:                        $errors.'</ul>';
 9612:     }
 9613:     return $resulttext;
 9614: }
 9615: 
 9616: sub check_exempt_addresses {
 9617:     my ($iplist) = @_;
 9618:     $iplist =~ s/^\s+//;
 9619:     $iplist =~ s/\s+$//;
 9620:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 9621:     my (@okips,$new);
 9622:     foreach my $ip (@poss_ips) {
 9623:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 9624:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 9625:                 push(@okips,$ip);
 9626:             }
 9627:         }
 9628:     }
 9629:     if (@okips > 0) {
 9630:         $new = join(',',@okips);
 9631:     } else {
 9632:         $new = '';
 9633:     }
 9634:     return $new;
 9635: }
 9636: 
 9637: sub color_font_choices {
 9638:     my %choices =
 9639:         &Apache::lonlocal::texthash (
 9640:             img => "Header",
 9641:             bgs => "Background colors",
 9642:             links => "Link colors",
 9643:             images => "Images",
 9644:             font => "Font color",
 9645:             fontmenu => "Font menu",
 9646:             pgbg => "Page",
 9647:             tabbg => "Header",
 9648:             sidebg => "Border",
 9649:             link => "Link",
 9650:             alink => "Active link",
 9651:             vlink => "Visited link",
 9652:         );
 9653:     return %choices;
 9654: }
 9655: 
 9656: sub modify_rolecolors {
 9657:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 9658:     my ($resulttext,%rolehash);
 9659:     $rolehash{'rolecolors'} = {};
 9660:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 9661:         if ($domconfig{'rolecolors'} eq '') {
 9662:             $domconfig{'rolecolors'} = {};
 9663:         }
 9664:     }
 9665:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 9666:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 9667:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 9668:                                              $dom);
 9669:     if ($putresult eq 'ok') {
 9670:         if (keys(%changes) > 0) {
 9671:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9672:             if (ref($lastactref) eq 'HASH') {
 9673:                 $lastactref->{'domainconfig'} = 1;
 9674:             }
 9675:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 9676:                                              $rolehash{'rolecolors'});
 9677:         } else {
 9678:             $resulttext = &mt('No changes made to default color schemes');
 9679:         }
 9680:     } else {
 9681:         $resulttext = '<span class="LC_error">'.
 9682: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9683:     }
 9684:     if ($errors) {
 9685:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9686:                        $errors.'</ul>';
 9687:     }
 9688:     return $resulttext;
 9689: }
 9690: 
 9691: sub modify_colors {
 9692:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 9693:     my (%changes,%choices);
 9694:     my @bgs;
 9695:     my @links = ('link','alink','vlink');
 9696:     my @logintext;
 9697:     my @images;
 9698:     my $servadm = $r->dir_config('lonAdmEMail');
 9699:     my $errors;
 9700:     my %defaults;
 9701:     foreach my $role (@{$roles}) {
 9702:         if ($role eq 'login') {
 9703:             %choices = &login_choices();
 9704:             @logintext = ('textcol','bgcol');
 9705:         } else {
 9706:             %choices = &color_font_choices();
 9707:         }
 9708:         if ($role eq 'login') {
 9709:             @images = ('img','logo','domlogo','login');
 9710:             @bgs = ('pgbg','mainbg','sidebg');
 9711:         } else {
 9712:             @images = ('img');
 9713:             @bgs = ('pgbg','tabbg','sidebg');
 9714:         }
 9715:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 9716:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 9717:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 9718:         }
 9719:         if ($role eq 'login') {
 9720:             foreach my $item (@logintext) {
 9721:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9722:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9723:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9724:                 }
 9725:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 9726:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9727:                 }
 9728:             }
 9729:         } else {
 9730:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 9731:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 9732:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 9733:             }
 9734:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 9735:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 9736:             }
 9737:         }
 9738:         foreach my $item (@bgs) {
 9739:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9740:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9741:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9742:             }
 9743:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 9744:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9745:             }
 9746:         }
 9747:         foreach my $item (@links) {
 9748:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9749:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9750:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9751:             }
 9752:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 9753:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9754:             }
 9755:         }
 9756:         my ($configuserok,$author_ok,$switchserver) = 
 9757:             &config_check($dom,$confname,$servadm);
 9758:         my ($width,$height) = &thumb_dimensions();
 9759:         if (ref($domconfig->{$role}) ne 'HASH') {
 9760:             $domconfig->{$role} = {};
 9761:         }
 9762:         foreach my $img (@images) {
 9763:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 9764:                 if (defined($env{'form.login_showlogo_'.$img})) {
 9765:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 9766:                 } else { 
 9767:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 9768:                 }
 9769:             } 
 9770: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 9771: 		 && !defined($domconfig->{$role}{$img})
 9772: 		 && !$env{'form.'.$role.'_del_'.$img}
 9773: 		 && $env{'form.'.$role.'_import_'.$img}) {
 9774: 		# import the old configured image from the .tab setting
 9775: 		# if they haven't provided a new one 
 9776: 		$domconfig->{$role}{$img} = 
 9777: 		    $env{'form.'.$role.'_import_'.$img};
 9778: 	    }
 9779:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 9780:                 my $error;
 9781:                 if ($configuserok eq 'ok') {
 9782:                     if ($switchserver) {
 9783:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 9784:                     } else {
 9785:                         if ($author_ok eq 'ok') {
 9786:                             my ($result,$logourl) = 
 9787:                                 &publishlogo($r,'upload',$role.'_'.$img,
 9788:                                            $dom,$confname,$img,$width,$height);
 9789:                             if ($result eq 'ok') {
 9790:                                 $confhash->{$role}{$img} = $logourl;
 9791:                                 $changes{$role}{'images'}{$img} = 1;
 9792:                             } else {
 9793:                                 $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);
 9794:                             }
 9795:                         } else {
 9796:                             $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);
 9797:                         }
 9798:                     }
 9799:                 } else {
 9800:                     $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);
 9801:                 }
 9802:                 if ($error) {
 9803:                     &Apache::lonnet::logthis($error);
 9804:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9805:                 }
 9806:             } elsif ($domconfig->{$role}{$img} ne '') {
 9807:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 9808:                     my $error;
 9809:                     if ($configuserok eq 'ok') {
 9810: # is confname an author?
 9811:                         if ($switchserver eq '') {
 9812:                             if ($author_ok eq 'ok') {
 9813:                                 my ($result,$logourl) = 
 9814:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 9815:                                             $dom,$confname,$img,$width,$height);
 9816:                                 if ($result eq 'ok') {
 9817:                                     $confhash->{$role}{$img} = $logourl;
 9818: 				    $changes{$role}{'images'}{$img} = 1;
 9819:                                 }
 9820:                             }
 9821:                         }
 9822:                     }
 9823:                 }
 9824:             }
 9825:         }
 9826:         if (ref($domconfig) eq 'HASH') {
 9827:             if (ref($domconfig->{$role}) eq 'HASH') {
 9828:                 foreach my $img (@images) {
 9829:                     if ($domconfig->{$role}{$img} ne '') {
 9830:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9831:                             $confhash->{$role}{$img} = '';
 9832:                             $changes{$role}{'images'}{$img} = 1;
 9833:                         } else {
 9834:                             if ($confhash->{$role}{$img} eq '') {
 9835:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 9836:                             }
 9837:                         }
 9838:                     } else {
 9839:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9840:                             $confhash->{$role}{$img} = '';
 9841:                             $changes{$role}{'images'}{$img} = 1;
 9842:                         } 
 9843:                     }
 9844:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 9845:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 9846:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 9847:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 9848:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 9849:                             }
 9850:                         } else {
 9851:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9852:                                 $changes{$role}{'showlogo'}{$img} = 1;
 9853:                             }
 9854:                         }
 9855:                     }
 9856:                 }
 9857:                 if ($domconfig->{$role}{'font'} ne '') {
 9858:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 9859:                         $changes{$role}{'font'} = 1;
 9860:                     }
 9861:                 } else {
 9862:                     if ($confhash->{$role}{'font'}) {
 9863:                         $changes{$role}{'font'} = 1;
 9864:                     }
 9865:                 }
 9866:                 if ($role ne 'login') {
 9867:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 9868:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 9869:                             $changes{$role}{'fontmenu'} = 1;
 9870:                         }
 9871:                     } else {
 9872:                         if ($confhash->{$role}{'fontmenu'}) {
 9873:                             $changes{$role}{'fontmenu'} = 1;
 9874:                         }
 9875:                     }
 9876:                 }
 9877:                 foreach my $item (@bgs) {
 9878:                     if ($domconfig->{$role}{$item} ne '') {
 9879:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9880:                             $changes{$role}{'bgs'}{$item} = 1;
 9881:                         } 
 9882:                     } else {
 9883:                         if ($confhash->{$role}{$item}) {
 9884:                             $changes{$role}{'bgs'}{$item} = 1;
 9885:                         }
 9886:                     }
 9887:                 }
 9888:                 foreach my $item (@links) {
 9889:                     if ($domconfig->{$role}{$item} ne '') {
 9890:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9891:                             $changes{$role}{'links'}{$item} = 1;
 9892:                         }
 9893:                     } else {
 9894:                         if ($confhash->{$role}{$item}) {
 9895:                             $changes{$role}{'links'}{$item} = 1;
 9896:                         }
 9897:                     }
 9898:                 }
 9899:                 foreach my $item (@logintext) {
 9900:                     if ($domconfig->{$role}{$item} ne '') {
 9901:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9902:                             $changes{$role}{'logintext'}{$item} = 1;
 9903:                         }
 9904:                     } else {
 9905:                         if ($confhash->{$role}{$item}) {
 9906:                             $changes{$role}{'logintext'}{$item} = 1;
 9907:                         }
 9908:                     }
 9909:                 }
 9910:             } else {
 9911:                 &default_change_checker($role,\@images,\@links,\@bgs,
 9912:                                         \@logintext,$confhash,\%changes); 
 9913:             }
 9914:         } else {
 9915:             &default_change_checker($role,\@images,\@links,\@bgs,
 9916:                                     \@logintext,$confhash,\%changes); 
 9917:         }
 9918:     }
 9919:     return ($errors,%changes);
 9920: }
 9921: 
 9922: sub config_check {
 9923:     my ($dom,$confname,$servadm) = @_;
 9924:     my ($configuserok,$author_ok,$switchserver,%currroles);
 9925:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 9926:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 9927:                                                    $confname,$servadm);
 9928:     if ($configuserok eq 'ok') {
 9929:         $switchserver = &check_switchserver($dom,$confname);
 9930:         if ($switchserver eq '') {
 9931:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 9932:         }
 9933:     }
 9934:     return ($configuserok,$author_ok,$switchserver);
 9935: }
 9936: 
 9937: sub default_change_checker {
 9938:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 9939:     foreach my $item (@{$links}) {
 9940:         if ($confhash->{$role}{$item}) {
 9941:             $changes->{$role}{'links'}{$item} = 1;
 9942:         }
 9943:     }
 9944:     foreach my $item (@{$bgs}) {
 9945:         if ($confhash->{$role}{$item}) {
 9946:             $changes->{$role}{'bgs'}{$item} = 1;
 9947:         }
 9948:     }
 9949:     foreach my $item (@{$logintext}) {
 9950:         if ($confhash->{$role}{$item}) {
 9951:             $changes->{$role}{'logintext'}{$item} = 1;
 9952:         }
 9953:     }
 9954:     foreach my $img (@{$images}) {
 9955:         if ($env{'form.'.$role.'_del_'.$img}) {
 9956:             $confhash->{$role}{$img} = '';
 9957:             $changes->{$role}{'images'}{$img} = 1;
 9958:         }
 9959:         if ($role eq 'login') {
 9960:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9961:                 $changes->{$role}{'showlogo'}{$img} = 1;
 9962:             }
 9963:         }
 9964:     }
 9965:     if ($confhash->{$role}{'font'}) {
 9966:         $changes->{$role}{'font'} = 1;
 9967:     }
 9968: }
 9969: 
 9970: sub display_colorchgs {
 9971:     my ($dom,$changes,$roles,$confhash) = @_;
 9972:     my (%choices,$resulttext);
 9973:     if (!grep(/^login$/,@{$roles})) {
 9974:         $resulttext = &mt('Changes made:').'<br />';
 9975:     }
 9976:     foreach my $role (@{$roles}) {
 9977:         if ($role eq 'login') {
 9978:             %choices = &login_choices();
 9979:         } else {
 9980:             %choices = &color_font_choices();
 9981:         }
 9982:         if (ref($changes->{$role}) eq 'HASH') {
 9983:             if ($role ne 'login') {
 9984:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 9985:             }
 9986:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 9987:                 if ($role ne 'login') {
 9988:                     $resulttext .= '<ul>';
 9989:                 }
 9990:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 9991:                     if ($role ne 'login') {
 9992:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 9993:                     }
 9994:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 9995:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 9996:                             if ($confhash->{$role}{$key}{$item}) {
 9997:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 9998:                             } else {
 9999:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
10000:                             }
10001:                         } elsif ($confhash->{$role}{$item} eq '') {
10002:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
10003:                         } else {
10004:                             my $newitem = $confhash->{$role}{$item};
10005:                             if ($key eq 'images') {
10006:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
10007:                             }
10008:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
10009:                         }
10010:                     }
10011:                     if ($role ne 'login') {
10012:                         $resulttext .= '</ul></li>';
10013:                     }
10014:                 } else {
10015:                     if ($confhash->{$role}{$key} eq '') {
10016:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
10017:                     } else {
10018:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
10019:                     }
10020:                 }
10021:                 if ($role ne 'login') {
10022:                     $resulttext .= '</ul>';
10023:                 }
10024:             }
10025:         }
10026:     }
10027:     return $resulttext;
10028: }
10029: 
10030: sub thumb_dimensions {
10031:     return ('200','50');
10032: }
10033: 
10034: sub check_dimensions {
10035:     my ($inputfile) = @_;
10036:     my ($fullwidth,$fullheight);
10037:     if ($inputfile =~ m|^[/\w.\-]+$|) {
10038:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
10039:             my $imageinfo = <PIPE>;
10040:             if (!close(PIPE)) {
10041:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
10042:             }
10043:             chomp($imageinfo);
10044:             my ($fullsize) = 
10045:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
10046:             if ($fullsize) {
10047:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
10048:             }
10049:         }
10050:     }
10051:     return ($fullwidth,$fullheight);
10052: }
10053: 
10054: sub check_configuser {
10055:     my ($uhome,$dom,$confname,$servadm) = @_;
10056:     my ($configuserok,%currroles);
10057:     if ($uhome eq 'no_host') {
10058:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
10059:         my $configpass = &LONCAPA::Enrollment::create_password();
10060:         $configuserok = 
10061:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
10062:                              $configpass,'','','','','',undef,$servadm);
10063:     } else {
10064:         $configuserok = 'ok';
10065:         %currroles = 
10066:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
10067:     }
10068:     return ($configuserok,%currroles);
10069: }
10070: 
10071: sub check_authorstatus {
10072:     my ($dom,$confname,%currroles) = @_;
10073:     my $author_ok;
10074:     if (!$currroles{':'.$dom.':au'}) {
10075:         my $start = time;
10076:         my $end = 0;
10077:         $author_ok = 
10078:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
10079:                                         'au',$end,$start,'','','domconfig');
10080:     } else {
10081:         $author_ok = 'ok';
10082:     }
10083:     return $author_ok;
10084: }
10085: 
10086: sub publishlogo {
10087:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
10088:     my ($output,$fname,$logourl,$madethumb);
10089:     if ($action eq 'upload') {
10090:         $fname=$env{'form.'.$formname.'.filename'};
10091:         chop($env{'form.'.$formname});
10092:     } else {
10093:         ($fname) = ($formname =~ /([^\/]+)$/);
10094:     }
10095:     if ($savefileas ne '') {
10096:         $fname = $savefileas;
10097:     }
10098:     $fname=&Apache::lonnet::clean_filename($fname);
10099: # See if there is anything left
10100:     unless ($fname) { return ('error: no uploaded file'); }
10101:     $fname="$subdir/$fname";
10102:     my $docroot=$r->dir_config('lonDocRoot');
10103:     my $filepath="$docroot/priv";
10104:     my $relpath = "$dom/$confname";
10105:     my ($fnamepath,$file,$fetchthumb);
10106:     $file=$fname;
10107:     if ($fname=~m|/|) {
10108:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
10109:     }
10110:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
10111:     my $count;
10112:     for ($count=5;$count<=$#parts;$count++) {
10113:         $filepath.="/$parts[$count]";
10114:         if ((-e $filepath)!=1) {
10115:             mkdir($filepath,02770);
10116:         }
10117:     }
10118:     # Check for bad extension and disallow upload
10119:     if ($file=~/\.(\w+)$/ &&
10120:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
10121:         $output = 
10122:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
10123:     } elsif ($file=~/\.(\w+)$/ &&
10124:         !defined(&Apache::loncommon::fileembstyle($1))) {
10125:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
10126:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
10127:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
10128:     } elsif (-d "$filepath/$file") {
10129:         $output = &mt('Filename is a directory name - rename the file and re-upload');
10130:     } else {
10131:         my $source = $filepath.'/'.$file;
10132:         my $logfile;
10133:         if (!open($logfile,">>",$source.'.log')) {
10134:             return (&mt('No write permission to Authoring Space'));
10135:         }
10136:         print $logfile
10137: "\n================= Publish ".localtime()." ================\n".
10138: $env{'user.name'}.':'.$env{'user.domain'}."\n";
10139: # Save the file
10140:         if (!open(FH,">",$source)) {
10141:             &Apache::lonnet::logthis('Failed to create '.$source);
10142:             return (&mt('Failed to create file'));
10143:         }
10144:         if ($action eq 'upload') {
10145:             if (!print FH ($env{'form.'.$formname})) {
10146:                 &Apache::lonnet::logthis('Failed to write to '.$source);
10147:                 return (&mt('Failed to write file'));
10148:             }
10149:         } else {
10150:             my $original = &Apache::lonnet::filelocation('',$formname);
10151:             if(!copy($original,$source)) {
10152:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
10153:                 return (&mt('Failed to write file'));
10154:             }
10155:         }
10156:         close(FH);
10157:         chmod(0660, $source); # Permissions to rw-rw---.
10158: 
10159:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
10160:         my $copyfile=$targetdir.'/'.$file;
10161: 
10162:         my @parts=split(/\//,$targetdir);
10163:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
10164:         for (my $count=5;$count<=$#parts;$count++) {
10165:             $path.="/$parts[$count]";
10166:             if (!-e $path) {
10167:                 print $logfile "\nCreating directory ".$path;
10168:                 mkdir($path,02770);
10169:             }
10170:         }
10171:         my $versionresult;
10172:         if (-e $copyfile) {
10173:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
10174:         } else {
10175:             $versionresult = 'ok';
10176:         }
10177:         if ($versionresult eq 'ok') {
10178:             if (copy($source,$copyfile)) {
10179:                 print $logfile "\nCopied original source to ".$copyfile."\n";
10180:                 $output = 'ok';
10181:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
10182:                 push(@{$modified_urls},[$copyfile,$source]);
10183:                 my $metaoutput = 
10184:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
10185:                 unless ($registered_cleanup) {
10186:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10187:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10188:                     $registered_cleanup=1;
10189:                 }
10190:             } else {
10191:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
10192:                 $output = &mt('Failed to copy file to RES space').", $!";
10193:             }
10194:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
10195:                 my $inputfile = $filepath.'/'.$file;
10196:                 my $outfile = $filepath.'/'.'tn-'.$file;
10197:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
10198:                 if ($fullwidth ne '' && $fullheight ne '') { 
10199:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
10200:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
10201:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
10202:                         system({$args[0]} @args);
10203:                         chmod(0660, $filepath.'/tn-'.$file);
10204:                         if (-e $outfile) {
10205:                             my $copyfile=$targetdir.'/tn-'.$file;
10206:                             if (copy($outfile,$copyfile)) {
10207:                                 print $logfile "\nCopied source to ".$copyfile."\n";
10208:                                 my $thumb_metaoutput = 
10209:                                     &write_metadata($dom,$confname,$formname,
10210:                                                     $targetdir,'tn-'.$file,$logfile);
10211:                                 push(@{$modified_urls},[$copyfile,$outfile]);
10212:                                 unless ($registered_cleanup) {
10213:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10214:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10215:                                     $registered_cleanup=1;
10216:                                 }
10217:                                 $madethumb = 1;
10218:                             } else {
10219:                                 print $logfile "\nUnable to write ".$copyfile.
10220:                                                ':'.$!."\n";
10221:                             }
10222:                         }
10223:                     }
10224:                 }
10225:             }
10226:         } else {
10227:             $output = $versionresult;
10228:         }
10229:     }
10230:     return ($output,$logourl,$madethumb);
10231: }
10232: 
10233: sub logo_versioning {
10234:     my ($targetdir,$file,$logfile) = @_;
10235:     my $target = $targetdir.'/'.$file;
10236:     my ($maxversion,$fn,$extn,$output);
10237:     $maxversion = 0;
10238:     if ($file =~ /^(.+)\.(\w+)$/) {
10239:         $fn=$1;
10240:         $extn=$2;
10241:     }
10242:     opendir(DIR,$targetdir);
10243:     while (my $filename=readdir(DIR)) {
10244:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
10245:             $maxversion=($1>$maxversion)?$1:$maxversion;
10246:         }
10247:     }
10248:     $maxversion++;
10249:     print $logfile "\nCreating old version ".$maxversion."\n";
10250:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
10251:     if (copy($target,$copyfile)) {
10252:         print $logfile "Copied old target to ".$copyfile."\n";
10253:         $copyfile=$copyfile.'.meta';
10254:         if (copy($target.'.meta',$copyfile)) {
10255:             print $logfile "Copied old target metadata to ".$copyfile."\n";
10256:             $output = 'ok';
10257:         } else {
10258:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
10259:             $output = &mt('Failed to copy old meta').", $!, ";
10260:         }
10261:     } else {
10262:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
10263:         $output = &mt('Failed to copy old target').", $!, ";
10264:     }
10265:     return $output;
10266: }
10267: 
10268: sub write_metadata {
10269:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
10270:     my (%metadatafields,%metadatakeys,$output);
10271:     $metadatafields{'title'}=$formname;
10272:     $metadatafields{'creationdate'}=time;
10273:     $metadatafields{'lastrevisiondate'}=time;
10274:     $metadatafields{'copyright'}='public';
10275:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
10276:                                          $env{'user.domain'};
10277:     $metadatafields{'authorspace'}=$confname.':'.$dom;
10278:     $metadatafields{'domain'}=$dom;
10279:     {
10280:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
10281:         my $mfh;
10282:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
10283:             foreach (sort(keys(%metadatafields))) {
10284:                 unless ($_=~/\./) {
10285:                     my $unikey=$_;
10286:                     $unikey=~/^([A-Za-z]+)/;
10287:                     my $tag=$1;
10288:                     $tag=~tr/A-Z/a-z/;
10289:                     print $mfh "\n\<$tag";
10290:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
10291:                         my $value=$metadatafields{$unikey.'.'.$_};
10292:                         $value=~s/\"/\'\'/g;
10293:                         print $mfh ' '.$_.'="'.$value.'"';
10294:                     }
10295:                     print $mfh '>'.
10296:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
10297:                             .'</'.$tag.'>';
10298:                 }
10299:             }
10300:             $output = 'ok';
10301:             print $logfile "\nWrote metadata";
10302:             close($mfh);
10303:         } else {
10304:             print $logfile "\nFailed to open metadata file";
10305:             $output = &mt('Could not write metadata');
10306:         }
10307:     }
10308:     return $output;
10309: }
10310: 
10311: sub notifysubscribed {
10312:     foreach my $targetsource (@{$modified_urls}){
10313:         next unless (ref($targetsource) eq 'ARRAY');
10314:         my ($target,$source)=@{$targetsource};
10315:         if ($source ne '') {
10316:             if (open(my $logfh,">>",$source.'.log')) {
10317:                 print $logfh "\nCleanup phase: Notifications\n";
10318:                 my @subscribed=&subscribed_hosts($target);
10319:                 foreach my $subhost (@subscribed) {
10320:                     print $logfh "\nNotifying host ".$subhost.':';
10321:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
10322:                     print $logfh $reply;
10323:                 }
10324:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
10325:                 foreach my $subhost (@subscribedmeta) {
10326:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
10327:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
10328:                                                         $subhost);
10329:                     print $logfh $reply;
10330:                 }
10331:                 print $logfh "\n============ Done ============\n";
10332:                 close($logfh);
10333:             }
10334:         }
10335:     }
10336:     return OK;
10337: }
10338: 
10339: sub subscribed_hosts {
10340:     my ($target) = @_;
10341:     my @subscribed;
10342:     if (open(my $fh,"<","$target.subscription")) {
10343:         while (my $subline=<$fh>) {
10344:             if ($subline =~ /^($match_lonid):/) {
10345:                 my $host = $1;
10346:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
10347:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
10348:                         push(@subscribed,$host);
10349:                     }
10350:                 }
10351:             }
10352:         }
10353:     }
10354:     return @subscribed;
10355: }
10356: 
10357: sub check_switchserver {
10358:     my ($dom,$confname) = @_;
10359:     my ($allowed,$switchserver);
10360:     my $home = &Apache::lonnet::homeserver($confname,$dom);
10361:     if ($home eq 'no_host') {
10362:         $home = &Apache::lonnet::domain($dom,'primary');
10363:     }
10364:     my @ids=&Apache::lonnet::current_machine_ids();
10365:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
10366:     if (!$allowed) {
10367: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
10368:     }
10369:     return $switchserver;
10370: }
10371: 
10372: sub modify_quotas {
10373:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10374:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
10375:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
10376:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
10377:         $validationfieldsref);
10378:     if ($action eq 'quotas') {
10379:         $context = 'tools'; 
10380:     } else {
10381:         $context = $action;
10382:     }
10383:     if ($context eq 'requestcourses') {
10384:         @usertools = ('official','unofficial','community','textbook','placement','lti');
10385:         @options =('norequest','approval','validate','autolimit');
10386:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
10387:         %titles = &courserequest_titles();
10388:         $toolregexp = join('|',@usertools);
10389:         %conditions = &courserequest_conditions();
10390:         $confname = $dom.'-domainconfig';
10391:         my $servadm = $r->dir_config('lonAdmEMail');
10392:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10393:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
10394:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
10395:     } elsif ($context eq 'requestauthor') {
10396:         @usertools = ('author');
10397:         %titles = &authorrequest_titles();
10398:     } else {
10399:         @usertools = ('aboutme','blog','webdav','portfolio');
10400:         %titles = &tool_titles();
10401:     }
10402:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10403:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10404:     foreach my $key (keys(%env)) {
10405:         if ($context eq 'requestcourses') {
10406:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
10407:                 my $item = $1;
10408:                 my $type = $2;
10409:                 if ($type =~ /^limit_(.+)/) {
10410:                     $limithash{$item}{$1} = $env{$key};
10411:                 } else {
10412:                     $confhash{$item}{$type} = $env{$key};
10413:                 }
10414:             }
10415:         } elsif ($context eq 'requestauthor') {
10416:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
10417:                 $confhash{$1} = $env{$key};
10418:             }
10419:         } else {
10420:             if ($key =~ /^form\.quota_(.+)$/) {
10421:                 $confhash{'defaultquota'}{$1} = $env{$key};
10422:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
10423:                 $confhash{'authorquota'}{$1} = $env{$key};
10424:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
10425:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
10426:             }
10427:         }
10428:     }
10429:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10430:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
10431:         @approvalnotify = sort(@approvalnotify);
10432:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
10433:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
10434:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
10435:         foreach my $type (@hasuniquecode) {
10436:             if (grep(/^\Q$type\E$/,@crstypes)) {
10437:                 $confhash{'uniquecode'}{$type} = 1;
10438:             }
10439:         }
10440:         my (%newbook,%allpos);
10441:         if ($context eq 'requestcourses') {
10442:             foreach my $type ('textbooks','templates') {
10443:                 @{$allpos{$type}} = (); 
10444:                 my $invalid;
10445:                 if ($type eq 'textbooks') {
10446:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
10447:                 } else {
10448:                     $invalid = &mt('Invalid LON-CAPA course for template');
10449:                 }
10450:                 if ($env{'form.'.$type.'_addbook'}) {
10451:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
10452:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
10453:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
10454:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
10455:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10456:                         } else {
10457:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
10458:                             my $position = $env{'form.'.$type.'_addbook_pos'};
10459:                             $position =~ s/\D+//g;
10460:                             if ($position ne '') {
10461:                                 $allpos{$type}[$position] = $newbook{$type};
10462:                             }
10463:                         }
10464:                     } else {
10465:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10466:                     }
10467:                 }
10468:             } 
10469:         }
10470:         if (ref($domconfig{$action}) eq 'HASH') {
10471:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
10472:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
10473:                     $changes{'notify'}{'approval'} = 1;
10474:                 }
10475:             } else {
10476:                 if ($confhash{'notify'}{'approval'}) {
10477:                     $changes{'notify'}{'approval'} = 1;
10478:                 }
10479:             }
10480:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
10481:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
10482:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
10483:                         unless ($confhash{'uniquecode'}{$crstype}) {
10484:                             $changes{'uniquecode'} = 1;
10485:                         }
10486:                     }
10487:                     unless ($changes{'uniquecode'}) {
10488:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
10489:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
10490:                                 $changes{'uniquecode'} = 1;
10491:                             }
10492:                         }
10493:                     }
10494:                } else {
10495:                    $changes{'uniquecode'} = 1;
10496:                }
10497:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
10498:                 $changes{'uniquecode'} = 1;
10499:             }
10500:             if ($context eq 'requestcourses') {
10501:                 foreach my $type ('textbooks','templates') {
10502:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
10503:                         my %deletions;
10504:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
10505:                         if (@todelete) {
10506:                             map { $deletions{$_} = 1; } @todelete;
10507:                         }
10508:                         my %imgdeletions;
10509:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
10510:                         if (@todeleteimages) {
10511:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
10512:                         }
10513:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
10514:                         for (my $i=0; $i<=$maxnum; $i++) {
10515:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
10516:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
10517:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
10518:                                 if ($deletions{$key}) {
10519:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
10520:                                         #FIXME need to obsolete item in RES space
10521:                                     }
10522:                                     next;
10523:                                 } else {
10524:                                     my $newpos = $env{'form.'.$itemid};
10525:                                     $newpos =~ s/\D+//g;
10526:                                     foreach my $item ('subject','title','publisher','author') {
10527:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10528:                                                  ($type eq 'templates'));
10529:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
10530:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
10531:                                             $changes{$type}{$key} = 1;
10532:                                         }
10533:                                     }
10534:                                     $allpos{$type}[$newpos] = $key;
10535:                                 }
10536:                                 if ($imgdeletions{$key}) {
10537:                                     $changes{$type}{$key} = 1;
10538:                                     #FIXME need to obsolete item in RES space
10539:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
10540:                                     my ($cdom,$cnum) = split(/_/,$key);
10541:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10542:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10543:                                     } else {
10544:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
10545:                                                                                       $cdom,$cnum,$type,$configuserok,
10546:                                                                                       $switchserver,$author_ok);
10547:                                         if ($imgurl) {
10548:                                             $confhash{$type}{$key}{'image'} = $imgurl;
10549:                                             $changes{$type}{$key} = 1; 
10550:                                         }
10551:                                         if ($error) {
10552:                                             &Apache::lonnet::logthis($error);
10553:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10554:                                         }
10555:                                     }
10556:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
10557:                                     $confhash{$type}{$key}{'image'} = 
10558:                                         $domconfig{$action}{$type}{$key}{'image'};
10559:                                 }
10560:                             }
10561:                         }
10562:                     }
10563:                 }
10564:             }
10565:         } else {
10566:             if ($confhash{'notify'}{'approval'}) {
10567:                 $changes{'notify'}{'approval'} = 1;
10568:             }
10569:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
10570:                 $changes{'uniquecode'} = 1;
10571:             }
10572:         }
10573:         if ($context eq 'requestcourses') {
10574:             foreach my $type ('textbooks','templates') {
10575:                 if ($newbook{$type}) {
10576:                     $changes{$type}{$newbook{$type}} = 1;
10577:                     foreach my $item ('subject','title','publisher','author') {
10578:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10579:                                  ($type eq 'template'));
10580:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
10581:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
10582:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
10583:                         }
10584:                     }
10585:                     if ($type eq 'textbooks') {
10586:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
10587:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
10588:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10589:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10590:                             } else {
10591:                                 my ($imageurl,$error) =
10592:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
10593:                                                             $configuserok,$switchserver,$author_ok);
10594:                                 if ($imageurl) {
10595:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
10596:                                 }
10597:                                 if ($error) {
10598:                                     &Apache::lonnet::logthis($error);
10599:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10600:                                 }
10601:                             }
10602:                         }
10603:                     }
10604:                 }
10605:                 if (@{$allpos{$type}} > 0) {
10606:                     my $idx = 0;
10607:                     foreach my $item (@{$allpos{$type}}) {
10608:                         if ($item ne '') {
10609:                             $confhash{$type}{$item}{'order'} = $idx;
10610:                             if (ref($domconfig{$action}) eq 'HASH') {
10611:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
10612:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
10613:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
10614:                                             $changes{$type}{$item} = 1;
10615:                                         }
10616:                                     }
10617:                                 }
10618:                             }
10619:                             $idx ++;
10620:                         }
10621:                     }
10622:                 }
10623:             }
10624:             if (ref($validationitemsref) eq 'ARRAY') {
10625:                 foreach my $item (@{$validationitemsref}) {
10626:                     if ($item eq 'fields') {
10627:                         my @changed;
10628:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
10629:                         if (@{$confhash{'validation'}{$item}} > 0) {
10630:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
10631:                         }
10632:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10633:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10634:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
10635:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
10636:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
10637:                                 } else {
10638:                                     @changed = @{$confhash{'validation'}{$item}};
10639:                                 }
10640:                             } else {
10641:                                 @changed = @{$confhash{'validation'}{$item}};
10642:                             }
10643:                         } else {
10644:                             @changed = @{$confhash{'validation'}{$item}};
10645:                         }
10646:                         if (@changed) {
10647:                             if ($confhash{'validation'}{$item}) {
10648:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
10649:                             } else {
10650:                                 $changes{'validation'}{$item} = &mt('None');
10651:                             }
10652:                         }
10653:                     } else {
10654:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
10655:                         if ($item eq 'markup') {
10656:                             if ($env{'form.requestcourses_validation_'.$item}) {
10657:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10658:                             }
10659:                         }
10660:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10661:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10662:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
10663:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10664:                                 }
10665:                             } else {
10666:                                 if ($confhash{'validation'}{$item} ne '') {
10667:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10668:                                 }
10669:                             }
10670:                         } else {
10671:                             if ($confhash{'validation'}{$item} ne '') {
10672:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10673:                             }
10674:                         }
10675:                     }
10676:                 }
10677:             }
10678:             if ($env{'form.validationdc'}) {
10679:                 my $newval = $env{'form.validationdc'};
10680:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10681:                 if (exists($domcoords{$newval})) {
10682:                     $confhash{'validation'}{'dc'} = $newval;
10683:                 }
10684:             }
10685:             if (ref($confhash{'validation'}) eq 'HASH') {
10686:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10687:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10688:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10689:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
10690:                                 if ($confhash{'validation'}{'dc'} eq '') {
10691:                                     $changes{'validation'}{'dc'} = &mt('None');
10692:                                 } else {
10693:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10694:                                 }
10695:                             }
10696:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
10697:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10698:                         }
10699:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
10700:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10701:                     }
10702:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
10703:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10704:                 }
10705:             } else {
10706:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10707:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10708:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10709:                             $changes{'validation'}{'dc'} = &mt('None');
10710:                         }
10711:                     }
10712:                 }
10713:             }
10714:         }
10715:     } else {
10716:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
10717:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
10718:     }
10719:     foreach my $item (@usertools) {
10720:         foreach my $type (@{$types},'default','_LC_adv') {
10721:             my $unset; 
10722:             if ($context eq 'requestcourses') {
10723:                 $unset = '0';
10724:                 if ($type eq '_LC_adv') {
10725:                     $unset = '';
10726:                 }
10727:                 if ($confhash{$item}{$type} eq 'autolimit') {
10728:                     $confhash{$item}{$type} .= '=';
10729:                     unless ($limithash{$item}{$type} =~ /\D/) {
10730:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
10731:                     }
10732:                 }
10733:             } elsif ($context eq 'requestauthor') {
10734:                 $unset = '0';
10735:                 if ($type eq '_LC_adv') {
10736:                     $unset = '';
10737:                 }
10738:             } else {
10739:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
10740:                     $confhash{$item}{$type} = 1;
10741:                 } else {
10742:                     $confhash{$item}{$type} = 0;
10743:                 }
10744:             }
10745:             if (ref($domconfig{$action}) eq 'HASH') {
10746:                 if ($action eq 'requestauthor') {
10747:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
10748:                         $changes{$type} = 1;
10749:                     }
10750:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
10751:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
10752:                         $changes{$item}{$type} = 1;
10753:                     }
10754:                 } else {
10755:                     if ($context eq 'requestcourses') {
10756:                         if ($confhash{$item}{$type} ne $unset) {
10757:                             $changes{$item}{$type} = 1;
10758:                         }
10759:                     } else {
10760:                         if (!$confhash{$item}{$type}) {
10761:                             $changes{$item}{$type} = 1;
10762:                         }
10763:                     }
10764:                 }
10765:             } else {
10766:                 if ($context eq 'requestcourses') {
10767:                     if ($confhash{$item}{$type} ne $unset) {
10768:                         $changes{$item}{$type} = 1;
10769:                     }
10770:                 } elsif ($context eq 'requestauthor') {
10771:                     if ($confhash{$type} ne $unset) {
10772:                         $changes{$type} = 1;
10773:                     }
10774:                 } else {
10775:                     if (!$confhash{$item}{$type}) {
10776:                         $changes{$item}{$type} = 1;
10777:                     }
10778:                 }
10779:             }
10780:         }
10781:     }
10782:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10783:         if (ref($domconfig{'quotas'}) eq 'HASH') {
10784:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10785:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
10786:                     if (exists($confhash{'defaultquota'}{$key})) {
10787:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
10788:                             $changes{'defaultquota'}{$key} = 1;
10789:                         }
10790:                     } else {
10791:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
10792:                     }
10793:                 }
10794:             } else {
10795:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
10796:                     if (exists($confhash{'defaultquota'}{$key})) {
10797:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
10798:                             $changes{'defaultquota'}{$key} = 1;
10799:                         }
10800:                     } else {
10801:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
10802:                     }
10803:                 }
10804:             }
10805:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10806:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
10807:                     if (exists($confhash{'authorquota'}{$key})) {
10808:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
10809:                             $changes{'authorquota'}{$key} = 1;
10810:                         }
10811:                     } else {
10812:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
10813:                     }
10814:                 }
10815:             }
10816:         }
10817:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
10818:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
10819:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10820:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10821:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
10822:                             $changes{'defaultquota'}{$key} = 1;
10823:                         }
10824:                     } else {
10825:                         if (!exists($domconfig{'quotas'}{$key})) {
10826:                             $changes{'defaultquota'}{$key} = 1;
10827:                         }
10828:                     }
10829:                 } else {
10830:                     $changes{'defaultquota'}{$key} = 1;
10831:                 }
10832:             }
10833:         }
10834:         if (ref($confhash{'authorquota'}) eq 'HASH') {
10835:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
10836:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10837:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10838:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
10839:                             $changes{'authorquota'}{$key} = 1;
10840:                         }
10841:                     } else {
10842:                         $changes{'authorquota'}{$key} = 1;
10843:                     }
10844:                 } else {
10845:                     $changes{'authorquota'}{$key} = 1;
10846:                 }
10847:             }
10848:         }
10849:     }
10850: 
10851:     if ($context eq 'requestauthor') {
10852:         $domdefaults{'requestauthor'} = \%confhash;
10853:     } else {
10854:         foreach my $key (keys(%confhash)) {
10855:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
10856:                 $domdefaults{$key} = $confhash{$key};
10857:             }
10858:         }
10859:     }
10860: 
10861:     my %quotahash = (
10862:                       $action => { %confhash }
10863:                     );
10864:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
10865:                                              $dom);
10866:     if ($putresult eq 'ok') {
10867:         if (keys(%changes) > 0) {
10868:             my $cachetime = 24*60*60;
10869:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10870:             if (ref($lastactref) eq 'HASH') {
10871:                 $lastactref->{'domdefaults'} = 1;
10872:             }
10873:             $resulttext = &mt('Changes made:').'<ul>';
10874:             unless (($context eq 'requestcourses') ||
10875:                     ($context eq 'requestauthor')) {
10876:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
10877:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
10878:                     foreach my $type (@{$types},'default') {
10879:                         if (defined($changes{'defaultquota'}{$type})) {
10880:                             my $typetitle = $usertypes->{$type};
10881:                             if ($type eq 'default') {
10882:                                 $typetitle = $othertitle;
10883:                             }
10884:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
10885:                         }
10886:                     }
10887:                     $resulttext .= '</ul></li>';
10888:                 }
10889:                 if (ref($changes{'authorquota'}) eq 'HASH') {
10890:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
10891:                     foreach my $type (@{$types},'default') {
10892:                         if (defined($changes{'authorquota'}{$type})) {
10893:                             my $typetitle = $usertypes->{$type};
10894:                             if ($type eq 'default') {
10895:                                 $typetitle = $othertitle;
10896:                             }
10897:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
10898:                         }
10899:                     }
10900:                     $resulttext .= '</ul></li>';
10901:                 }
10902:             }
10903:             my %newenv;
10904:             foreach my $item (@usertools) {
10905:                 my (%haschgs,%inconf);
10906:                 if ($context eq 'requestauthor') {
10907:                     %haschgs = %changes;
10908:                     %inconf = %confhash;
10909:                 } else {
10910:                     if (ref($changes{$item}) eq 'HASH') {
10911:                         %haschgs = %{$changes{$item}};
10912:                     }
10913:                     if (ref($confhash{$item}) eq 'HASH') {
10914:                         %inconf = %{$confhash{$item}};
10915:                     }
10916:                 }
10917:                 if (keys(%haschgs) > 0) {
10918:                     my $newacc = 
10919:                         &Apache::lonnet::usertools_access($env{'user.name'},
10920:                                                           $env{'user.domain'},
10921:                                                           $item,'reload',$context);
10922:                     if (($context eq 'requestcourses') ||
10923:                         ($context eq 'requestauthor')) {
10924:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
10925:                             $newenv{'environment.canrequest.'.$item} = $newacc;
10926:                         }
10927:                     } else {
10928:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
10929:                             $newenv{'environment.availabletools.'.$item} = $newacc;
10930:                         }
10931:                     }
10932:                     unless ($context eq 'requestauthor') {
10933:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
10934:                     }
10935:                     foreach my $type (@{$types},'default','_LC_adv') {
10936:                         if ($haschgs{$type}) {
10937:                             my $typetitle = $usertypes->{$type};
10938:                             if ($type eq 'default') {
10939:                                 $typetitle = $othertitle;
10940:                             } elsif ($type eq '_LC_adv') {
10941:                                 $typetitle = 'LON-CAPA Advanced Users'; 
10942:                             }
10943:                             if ($inconf{$type}) {
10944:                                 if ($context eq 'requestcourses') {
10945:                                     my $cond;
10946:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
10947:                                         if ($1 eq '') {
10948:                                             $cond = &mt('(Automatic processing of any request).');
10949:                                         } else {
10950:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
10951:                                         }
10952:                                     } else { 
10953:                                         $cond = $conditions{$inconf{$type}};
10954:                                     }
10955:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
10956:                                 } elsif ($context eq 'requestauthor') {
10957:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
10958:                                                              $titles{$inconf{$type}},$typetitle);
10959: 
10960:                                 } else {
10961:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
10962:                                 }
10963:                             } else {
10964:                                 if ($type eq '_LC_adv') {
10965:                                     if ($inconf{$type} eq '0') {
10966:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10967:                                     } else { 
10968:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
10969:                                     }
10970:                                 } else {
10971:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10972:                                 }
10973:                             }
10974:                         }
10975:                     }
10976:                     unless ($context eq 'requestauthor') {
10977:                         $resulttext .= '</ul></li>';
10978:                     }
10979:                 }
10980:             }
10981:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
10982:                 if (ref($changes{'notify'}) eq 'HASH') {
10983:                     if ($changes{'notify'}{'approval'}) {
10984:                         if (ref($confhash{'notify'}) eq 'HASH') {
10985:                             if ($confhash{'notify'}{'approval'}) {
10986:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
10987:                             } else {
10988:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
10989:                             }
10990:                         }
10991:                     }
10992:                 }
10993:             }
10994:             if ($action eq 'requestcourses') {
10995:                 my @offon = ('off','on');
10996:                 if ($changes{'uniquecode'}) {
10997:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
10998:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
10999:                         $resulttext .= '<li>'.
11000:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
11001:                                        '</li>';
11002:                     } else {
11003:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
11004:                                        '</li>';
11005:                     }
11006:                 }
11007:                 foreach my $type ('textbooks','templates') {
11008:                     if (ref($changes{$type}) eq 'HASH') {
11009:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
11010:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
11011:                             my %coursehash = &Apache::lonnet::coursedescription($key);
11012:                             my $coursetitle = $coursehash{'description'};
11013:                             my $position = $confhash{$type}{$key}{'order'} + 1;
11014:                             $resulttext .= '<li>';
11015:                             foreach my $item ('subject','title','publisher','author') {
11016:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
11017:                                          ($type eq 'templates'));
11018:                                 my $name = $item.':';
11019:                                 $name =~ s/^(\w)/\U$1/;
11020:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
11021:                             }
11022:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
11023:                             if ($type eq 'textbooks') {
11024:                                 if ($confhash{$type}{$key}{'image'}) {
11025:                                     $resulttext .= ' '.&mt('Image: [_1]',
11026:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
11027:                                                    ' alt="Textbook cover" />').'<br />';
11028:                                 }
11029:                             }
11030:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
11031:                         }
11032:                         $resulttext .= '</ul></li>';
11033:                     }
11034:                 }
11035:                 if (ref($changes{'validation'}) eq 'HASH') {
11036:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
11037:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
11038:                         foreach my $item (@{$validationitemsref}) {
11039:                             if (exists($changes{'validation'}{$item})) {
11040:                                 if ($item eq 'markup') {
11041:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11042:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
11043:                                 } else {
11044:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11045:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
11046:                                 }
11047:                             }
11048:                         }
11049:                         if (exists($changes{'validation'}{'dc'})) {
11050:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
11051:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
11052:                         }
11053:                     }
11054:                 }
11055:             }
11056:             $resulttext .= '</ul>';
11057:             if (keys(%newenv)) {
11058:                 &Apache::lonnet::appenv(\%newenv);
11059:             }
11060:         } else {
11061:             if ($context eq 'requestcourses') {
11062:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
11063:             } elsif ($context eq 'requestauthor') {
11064:                 $resulttext = &mt('No changes made to rights to request author space.');
11065:             } else {
11066:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
11067:             }
11068:         }
11069:     } else {
11070:         $resulttext = '<span class="LC_error">'.
11071: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11072:     }
11073:     if ($errors) {
11074:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
11075:                        '<ul>'.$errors.'</ul></p>';
11076:     }
11077:     return $resulttext;
11078: }
11079: 
11080: sub process_textbook_image {
11081:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
11082:     my $filename = $env{'form.'.$caller.'.filename'};
11083:     my ($error,$url);
11084:     my ($width,$height) = (50,50);
11085:     if ($configuserok eq 'ok') {
11086:         if ($switchserver) {
11087:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
11088:                          $switchserver);
11089:         } elsif ($author_ok eq 'ok') {
11090:             my ($result,$imageurl) =
11091:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11092:                              "$type/$cdom/$cnum/cover",$width,$height);
11093:             if ($result eq 'ok') {
11094:                 $url = $imageurl;
11095:             } else {
11096:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11097:             }
11098:         } else {
11099:             $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);
11100:         }
11101:     } else {
11102:         $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);
11103:     }
11104:     return ($url,$error);
11105: }
11106: 
11107: sub modify_ltitools {
11108:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11109:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11110:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11111:     my $confname = $dom.'-domainconfig';
11112:     my $servadm = $r->dir_config('lonAdmEMail');
11113:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11114:     my (%posslti,%possfield);
11115:     my @courseroles = ('cc','in','ta','ep','st');
11116:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
11117:     map { $posslti{$_} = 1; } @ltiroles;
11118:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
11119:     map { $possfield{$_} = 1; } @allfields;
11120:     my %lt = &ltitools_names(); 
11121:     if ($env{'form.ltitools_add'}) {
11122:         my $title = $env{'form.ltitools_add_title'};
11123:         $title =~ s/(`)/'/g;
11124:         ($newid,my $error) = &get_ltitools_id($dom,$title);
11125:         if ($newid) {
11126:             my $position = $env{'form.ltitools_add_pos'};
11127:             $position =~ s/\D+//g;
11128:             if ($position ne '') {
11129:                 $allpos[$position] = $newid;
11130:             }
11131:             $changes{$newid} = 1;
11132:             foreach my $item ('title','url','key','secret','lifetime') {
11133:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
11134:                 if ($item eq 'lifetime') {
11135:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
11136:                 }
11137:                 if ($env{'form.ltitools_add_'.$item}) {
11138:                     if (($item eq 'key') || ($item eq 'secret')) {
11139:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11140:                     } else {
11141:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11142:                     }
11143:                 }
11144:             }
11145:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
11146:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
11147:             }
11148:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
11149:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
11150:             }
11151:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
11152:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
11153:             } else {
11154:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
11155:             }
11156:             foreach my $item ('width','height','linktext','explanation') {
11157:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
11158:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
11159:                 if (($item eq 'width') || ($item eq 'height')) {
11160:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
11161:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
11162:                     }
11163:                 } else {
11164:                     if ($env{'form.ltitools_add_'.$item} ne '') {
11165:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
11166:                     }
11167:                 }
11168:             }
11169:             if ($env{'form.ltitools_add_target'} eq 'window') {
11170:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11171:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
11172:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11173:             } else {
11174:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
11175:             }
11176:             foreach my $item ('passback','roster') {
11177:                 if ($env{'form.ltitools_'.$item.'_add'}) {
11178:                     $confhash{$newid}{$item} = 1;
11179:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
11180:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
11181:                         $lifetime =~ s/^\s+|\s+$//g;
11182:                         if ($lifetime =~ /^\d+\.?\d*$/) {
11183:                             $confhash{$newid}{$item.'valid'} = $lifetime;
11184:                         }
11185:                     }
11186:                 }
11187:             }
11188:             if ($env{'form.ltitools_add_image.filename'} ne '') {
11189:                 my ($imageurl,$error) =
11190:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
11191:                                             $configuserok,$switchserver,$author_ok);
11192:                 if ($imageurl) {
11193:                     $confhash{$newid}{'image'} = $imageurl;
11194:                 }
11195:                 if ($error) {
11196:                     &Apache::lonnet::logthis($error);
11197:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11198:                 }
11199:             }
11200:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
11201:             foreach my $field (@fields) {
11202:                 if ($possfield{$field}) {
11203:                     if ($field eq 'roles') {
11204:                         foreach my $role (@courseroles) {
11205:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
11206:                             if (($choice ne '') && ($posslti{$choice})) {
11207:                                 $confhash{$newid}{'roles'}{$role} = $choice;
11208:                                 if ($role eq 'cc') {
11209:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
11210:                                 }
11211:                             }
11212:                         }
11213:                     } else {
11214:                         $confhash{$newid}{'fields'}{$field} = 1;
11215:                     }
11216:                 }
11217:             }
11218:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
11219:                 if ($confhash{$newid}{'fields'}{'user'}) {
11220:                     if ($env{'form.ltitools_userincdom_add'}) {
11221:                         $confhash{$newid}{'incdom'} = 1;
11222:                     }
11223:                 }
11224:             }
11225:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
11226:             foreach my $item (@courseconfig) {
11227:                 $confhash{$newid}{'crsconf'}{$item} = 1;
11228:             }
11229:             if ($env{'form.ltitools_add_custom'}) {
11230:                 my $name = $env{'form.ltitools_add_custom_name'};
11231:                 my $value = $env{'form.ltitools_add_custom_value'};
11232:                 $value =~ s/(`)/'/g;
11233:                 $name =~ s/(`)/'/g;
11234:                 $confhash{$newid}{'custom'}{$name} = $value;
11235:             }
11236:         } else {
11237:             my $error = &mt('Failed to acquire unique ID for new external tool');   
11238:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11239:         }
11240:     }
11241:     if (ref($domconfig{$action}) eq 'HASH') {
11242:         my %deletions;
11243:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
11244:         if (@todelete) {
11245:             map { $deletions{$_} = 1; } @todelete;
11246:         }
11247:         my %customadds;
11248:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
11249:         if (@newcustom) {
11250:             map { $customadds{$_} = 1; } @newcustom;
11251:         } 
11252:         my %imgdeletions;
11253:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
11254:         if (@todeleteimages) {
11255:             map { $imgdeletions{$_} = 1; } @todeleteimages;
11256:         }
11257:         my $maxnum = $env{'form.ltitools_maxnum'};
11258:         for (my $i=0; $i<=$maxnum; $i++) {
11259:             my $itemid = $env{'form.ltitools_id_'.$i};
11260:             $itemid =~ s/\D+//g;
11261:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11262:                 if ($deletions{$itemid}) {
11263:                     if ($domconfig{$action}{$itemid}{'image'}) {
11264:                         #FIXME need to obsolete item in RES space
11265:                     }
11266:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
11267:                     next;
11268:                 } else {
11269:                     my $newpos = $env{'form.ltitools_'.$itemid};
11270:                     $newpos =~ s/\D+//g;
11271:                     foreach my $item ('title','url','lifetime') {
11272:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11273:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
11274:                             $changes{$itemid} = 1;
11275:                         }
11276:                     }
11277:                     foreach my $item ('key','secret') {
11278:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11279:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
11280:                             $changes{$itemid} = 1;
11281:                         }
11282:                     }
11283:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
11284:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
11285:                     }
11286:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
11287:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
11288:                     }
11289:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
11290:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
11291:                     } else {
11292:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
11293:                     }
11294:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
11295:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
11296:                             $changes{$itemid} = 1;
11297:                         }
11298:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
11299:                         $changes{$itemid} = 1;
11300:                     }
11301:                     foreach my $size ('width','height') {
11302:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
11303:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
11304:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
11305:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
11306:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11307:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
11308:                                     $changes{$itemid} = 1;
11309:                                 }
11310:                             } else {
11311:                                 $changes{$itemid} = 1;
11312:                             }
11313:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11314:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
11315:                                 $changes{$itemid} = 1;
11316:                             }
11317:                         }
11318:                     }
11319:                     foreach my $item ('linktext','explanation') {
11320:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
11321:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
11322:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
11323:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11324:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11325:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
11326:                                     $changes{$itemid} = 1;
11327:                                 }
11328:                             } else {
11329:                                 $changes{$itemid} = 1;
11330:                             }
11331:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11332:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
11333:                                 $changes{$itemid} = 1;
11334:                             }
11335:                         }
11336:                     }
11337:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
11338:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11339:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
11340:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11341:                     } else {
11342:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
11343:                     }
11344:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11345:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
11346:                             $changes{$itemid} = 1;
11347:                         }
11348:                     } else {
11349:                         $changes{$itemid} = 1;
11350:                     }
11351:                     foreach my $extra ('passback','roster') {
11352:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
11353:                             $confhash{$itemid}{$extra} = 1;
11354:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
11355:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
11356:                                 $lifetime =~ s/^\s+|\s+$//g;
11357:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
11358:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
11359:                                 }
11360:                             }
11361:                         }
11362:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
11363:                             $changes{$itemid} = 1;
11364:                         }
11365:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
11366:                             $changes{$itemid} = 1;
11367:                         }
11368:                     }
11369:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
11370:                     foreach my $item ('label','title','target','linktext','explanation','append') {
11371:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
11372:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
11373:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
11374:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
11375:                                     $changes{$itemid} = 1;
11376:                                 }
11377:                             } else {
11378:                                 $changes{$itemid} = 1;
11379:                             }
11380:                         }
11381:                     }
11382:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
11383:                     foreach my $field (@fields) {
11384:                         if ($possfield{$field}) {
11385:                             if ($field eq 'roles') {
11386:                                 foreach my $role (@courseroles) {
11387:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
11388:                                     if (($choice ne '') && ($posslti{$choice})) {
11389:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
11390:                                         if ($role eq 'cc') {
11391:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
11392:                                         }
11393:                                     }
11394:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
11395:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
11396:                                             $changes{$itemid} = 1;
11397:                                         }
11398:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
11399:                                         $changes{$itemid} = 1;
11400:                                     }
11401:                                 }
11402:                             } else {
11403:                                 $confhash{$itemid}{'fields'}{$field} = 1;
11404:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
11405:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
11406:                                         $changes{$itemid} = 1;
11407:                                     }
11408:                                 } else {
11409:                                     $changes{$itemid} = 1;
11410:                                 }
11411:                             }
11412:                         }
11413:                     }
11414:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11415:                         if ($confhash{$itemid}{'fields'}{'user'}) {
11416:                             if ($env{'form.ltitools_userincdom_'.$i}) {
11417:                                 $confhash{$itemid}{'incdom'} = 1;
11418:                             }
11419:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
11420:                                 $changes{$itemid} = 1;
11421:                             }
11422:                         }
11423:                     }
11424:                     $allpos[$newpos] = $itemid;
11425:                 }
11426:                 if ($imgdeletions{$itemid}) {
11427:                     $changes{$itemid} = 1;
11428:                     #FIXME need to obsolete item in RES space
11429:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
11430:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
11431:                                                                  $itemid,$configuserok,$switchserver,
11432:                                                                  $author_ok);
11433:                     if ($imgurl) {
11434:                         $confhash{$itemid}{'image'} = $imgurl;
11435:                         $changes{$itemid} = 1;
11436:                     }
11437:                     if ($error) {
11438:                         &Apache::lonnet::logthis($error);
11439:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11440:                     }
11441:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
11442:                     $confhash{$itemid}{'image'} =
11443:                        $domconfig{$action}{$itemid}{'image'};
11444:                 }
11445:                 if ($customadds{$i}) {
11446:                     my $name = $env{'form.ltitools_custom_name_'.$i};
11447:                     $name =~ s/(`)/'/g;
11448:                     $name =~ s/^\s+//;
11449:                     $name =~ s/\s+$//;
11450:                     my $value = $env{'form.ltitools_custom_value_'.$i};
11451:                     $value =~ s/(`)/'/g;
11452:                     $value =~ s/^\s+//;
11453:                     $value =~ s/\s+$//;
11454:                     if ($name ne '') {
11455:                         $confhash{$itemid}{'custom'}{$name} = $value;
11456:                         $changes{$itemid} = 1;
11457:                     }
11458:                 }
11459:                 my %customdels;
11460:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
11461:                 if (@customdeletions) {
11462:                     $changes{$itemid} = 1;
11463:                 }
11464:                 map { $customdels{$_} = 1; } @customdeletions;
11465:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
11466:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
11467:                         unless ($customdels{$key}) {
11468:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
11469:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
11470:                             }
11471:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
11472:                                 $changes{$itemid} = 1;
11473:                             }
11474:                         }
11475:                     }
11476:                 }
11477:                 unless ($changes{$itemid}) {
11478:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
11479:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
11480:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
11481:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
11482:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
11483:                                         $changes{$itemid} = 1;
11484:                                         last;
11485:                                     }
11486:                                 }
11487:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
11488:                                 $changes{$itemid} = 1;
11489:                             }
11490:                         }
11491:                         last if ($changes{$itemid});
11492:                     }
11493:                 }
11494:             }
11495:         }
11496:     }
11497:     if (@allpos > 0) {
11498:         my $idx = 0;
11499:         foreach my $itemid (@allpos) {
11500:             if ($itemid ne '') {
11501:                 $confhash{$itemid}{'order'} = $idx;
11502:                 if (ref($domconfig{$action}) eq 'HASH') {
11503:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11504:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
11505:                             $changes{$itemid} = 1;
11506:                         }
11507:                     }
11508:                 }
11509:                 $idx ++;
11510:             }
11511:         }
11512:     }
11513:     my %ltitoolshash = (
11514:                           $action => { %confhash }
11515:                        );
11516:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
11517:                                              $dom);
11518:     if ($putresult eq 'ok') {
11519:         my %ltienchash = (
11520:                              $action => { %encconfig }
11521:                          );
11522:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
11523:         if (keys(%changes) > 0) {
11524:             my $cachetime = 24*60*60;
11525:             my %ltiall = %confhash;
11526:             foreach my $id (keys(%ltiall)) {
11527:                 if (ref($encconfig{$id}) eq 'HASH') {
11528:                     foreach my $item ('key','secret') {
11529:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
11530:                     }
11531:                 }
11532:             }
11533:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
11534:             if (ref($lastactref) eq 'HASH') {
11535:                 $lastactref->{'ltitools'} = 1;
11536:             }
11537:             $resulttext = &mt('Changes made:').'<ul>';
11538:             my %bynum;
11539:             foreach my $itemid (sort(keys(%changes))) {
11540:                 my $position = $confhash{$itemid}{'order'};
11541:                 $bynum{$position} = $itemid;
11542:             }
11543:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
11544:                 my $itemid = $bynum{$pos}; 
11545:                 if (ref($confhash{$itemid}) ne 'HASH') {
11546:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
11547:                 } else {
11548:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
11549:                     if ($confhash{$itemid}{'image'}) {
11550:                         $resulttext .= '&nbsp;'.
11551:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
11552:                                        ' alt="'.&mt('Tool Provider icon').'" />';
11553:                     }
11554:                     $resulttext .= '</li><ul>';
11555:                     my $position = $pos + 1;
11556:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
11557:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
11558:                         if ($confhash{$itemid}{$item} ne '') {
11559:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
11560:                         }
11561:                     }
11562:                     if ($encconfig{$itemid}{'key'} ne '') {
11563:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
11564:                     }
11565:                     if ($encconfig{$itemid}{'secret'} ne '') {
11566:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
11567:                         my $num = length($encconfig{$itemid}{'secret'});
11568:                         $resulttext .= ('*'x$num).'</li>';
11569:                     }
11570:                     $resulttext .= '<li>'.&mt('Configurable in course:');
11571:                     my @possconfig = ('label','title','target','linktext','explanation','append');
11572:                     my $numconfig = 0; 
11573:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
11574:                         foreach my $item (@possconfig) {
11575:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
11576:                                 $numconfig ++;
11577:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
11578:                             }
11579:                         }
11580:                     }
11581:                     if (!$numconfig) {
11582:                         $resulttext .= &mt('None');
11583:                     }
11584:                     $resulttext .= '</li>';
11585:                     foreach my $item ('passback','roster') {
11586:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
11587:                         if ($confhash{$itemid}{$item}) {
11588:                             $resulttext .= &mt('Yes');
11589:                             if ($confhash{$itemid}{$item.'valid'}) {
11590:                                 if ($item eq 'passback') {
11591:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
11592:                                                            $confhash{$itemid}{$item.'valid'});
11593:                                 } else {
11594:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
11595:                                                            $confhash{$itemid}{$item.'valid'});
11596:                                 }
11597:                             }
11598:                         } else {
11599:                             $resulttext .= &mt('No');
11600:                         }
11601:                         $resulttext .= '</li>';
11602:                     }
11603:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
11604:                         my $displaylist;
11605:                         if ($confhash{$itemid}{'display'}{'target'}) {
11606:                             $displaylist = &mt('Display target').':&nbsp;'.
11607:                                            $confhash{$itemid}{'display'}{'target'}.',';
11608:                         }
11609:                         foreach my $size ('width','height') { 
11610:                             if ($confhash{$itemid}{'display'}{$size}) {
11611:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
11612:                                                 $confhash{$itemid}{'display'}{$size}.',';
11613:                             }
11614:                         }
11615:                         if ($displaylist) {
11616:                             $displaylist =~ s/,$//;
11617:                             $resulttext .= '<li>'.$displaylist.'</li>';
11618:                         }
11619:                         foreach my $item ('linktext','explanation') {
11620:                             if ($confhash{$itemid}{'display'}{$item}) {
11621:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
11622:                             }
11623:                         }
11624:                     }
11625:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11626:                         my $fieldlist;
11627:                         foreach my $field (@allfields) {
11628:                             if ($confhash{$itemid}{'fields'}{$field}) {
11629:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
11630:                             }
11631:                         }
11632:                         if ($fieldlist) {
11633:                             $fieldlist =~ s/,$//;
11634:                             if ($confhash{$itemid}{'fields'}{'user'}) {
11635:                                 if ($confhash{$itemid}{'incdom'}) {
11636:                                     $fieldlist .= ' ('.&mt('username:domain').')';
11637:                                 } else {
11638:                                     $fieldlist .= ' ('.&mt('username').')';
11639:                                 }
11640:                             }
11641:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
11642:                         }
11643:                     }
11644:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
11645:                         my $rolemaps;
11646:                         foreach my $role (@courseroles) {
11647:                             if ($confhash{$itemid}{'roles'}{$role}) {
11648:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
11649:                                              $confhash{$itemid}{'roles'}{$role}.',';
11650:                             }
11651:                         }
11652:                         if ($rolemaps) {
11653:                             $rolemaps =~ s/,$//; 
11654:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
11655:                         }
11656:                     }
11657:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
11658:                         my $customlist;
11659:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
11660:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
11661:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
11662:                             } 
11663:                         }
11664:                         if ($customlist) {
11665:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
11666:                         }
11667:                     } 
11668:                     $resulttext .= '</ul></li>';
11669:                 }
11670:             }
11671:             $resulttext .= '</ul>';
11672:         } else {
11673:             $resulttext = &mt('No changes made.');
11674:         }
11675:     } else {
11676:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11677:     }
11678:     if ($errors) {
11679:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11680:                        $errors.'</ul>';
11681:     }
11682:     return $resulttext;
11683: }
11684: 
11685: sub process_ltitools_image {
11686:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
11687:     my $filename = $env{'form.'.$caller.'.filename'};
11688:     my ($error,$url);
11689:     my ($width,$height) = (21,21);
11690:     if ($configuserok eq 'ok') {
11691:         if ($switchserver) {
11692:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
11693:                          $switchserver);
11694:         } elsif ($author_ok eq 'ok') {
11695:             my ($result,$imageurl,$madethumb) =
11696:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11697:                              "ltitools/$itemid/icon",$width,$height);
11698:             if ($result eq 'ok') {
11699:                 if ($madethumb) {
11700:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
11701:                     my $imagethumb = "$path/tn-".$imagefile;
11702:                     $url = $imagethumb;
11703:                 } else {
11704:                     $url = $imageurl;
11705:                 }
11706:             } else {
11707:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11708:             }
11709:         } else {
11710:             $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);
11711:         }
11712:     } else {
11713:         $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);
11714:     }
11715:     return ($url,$error);
11716: }
11717: 
11718: sub get_ltitools_id {
11719:     my ($cdom,$title) = @_;
11720:     # get lock on ltitools db
11721:     my $lockhash = {
11722:                       lock => $env{'user.name'}.
11723:                               ':'.$env{'user.domain'},
11724:                    };
11725:     my $tries = 0;
11726:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11727:     my ($id,$error);
11728:  
11729:     while (($gotlock ne 'ok') && ($tries<10)) {
11730:         $tries ++;
11731:         sleep (0.1);
11732:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11733:     }
11734:     if ($gotlock eq 'ok') {
11735:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
11736:         if ($currids{'lock'}) {
11737:             delete($currids{'lock'});
11738:             if (keys(%currids)) {
11739:                 my @curr = sort { $a <=> $b } keys(%currids);
11740:                 if ($curr[-1] =~ /^\d+$/) {
11741:                     $id = 1 + $curr[-1];
11742:                 }
11743:             } else {
11744:                 $id = 1;
11745:             }
11746:             if ($id) {
11747:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
11748:                     $error = 'nostore';
11749:                 }
11750:             } else {
11751:                 $error = 'nonumber';
11752:             }
11753:         }
11754:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
11755:     } else {
11756:         $error = 'nolock';
11757:     }
11758:     return ($id,$error);
11759: }
11760: 
11761: sub modify_lti {
11762:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11763:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11764:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11765:     my (%posslti,%posslticrs,%posscrstype);
11766:     my @courseroles = ('cc','in','ta','ep','st');
11767:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
11768:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
11769:     my @coursetypes = ('official','unofficial','community','textbook','placement');
11770:     my %coursetypetitles = &Apache::lonlocal::texthash (
11771:                                official   => 'Official',
11772:                                unofficial => 'Unofficial',
11773:                                community  => 'Community',
11774:                                textbook   => 'Textbook',
11775:                                placement  => 'Placement Test',
11776:     );
11777:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11778:     my %lt = &lti_names();
11779:     map { $posslti{$_} = 1; } @ltiroles;
11780:     map { $posslticrs{$_} = 1; } @lticourseroles;
11781:     map { $posscrstype{$_} = 1; } @coursetypes;
11782: 
11783:     my %menutitles = &ltimenu_titles();
11784: 
11785:     my (@items,%deletions,%itemids);
11786:     if ($env{'form.lti_add'}) {
11787:         my $consumer = $env{'form.lti_consumer_add'};
11788:         $consumer =~ s/(`)/'/g;
11789:         ($newid,my $error) = &get_lti_id($dom,$consumer);
11790:         if ($newid) {
11791:             $itemids{'add'} = $newid;
11792:             push(@items,'add');
11793:             $changes{$newid} = 1;
11794:         } else {
11795:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
11796:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11797:         }
11798:     }
11799:     if (ref($domconfig{$action}) eq 'HASH') {
11800:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
11801:         if (@todelete) {
11802:             map { $deletions{$_} = 1; } @todelete;
11803:         }
11804:         my $maxnum = $env{'form.lti_maxnum'};
11805:         for (my $i=0; $i<=$maxnum; $i++) {
11806:             my $itemid = $env{'form.lti_id_'.$i};
11807:             $itemid =~ s/\D+//g;
11808:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11809:                 if ($deletions{$itemid}) {
11810:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
11811:                 } else {
11812:                    push(@items,$i);
11813:                    $itemids{$i} = $itemid;
11814:                 }
11815:             }
11816:         }
11817:     }
11818:     foreach my $idx (@items) {
11819:         my $itemid = $itemids{$idx};
11820:         next unless ($itemid);
11821:         my $position = $env{'form.lti_pos_'.$idx};
11822:         $position =~ s/\D+//g;
11823:         if ($position ne '') {
11824:             $allpos[$position] = $itemid;
11825:         }
11826:         foreach my $item ('consumer','key','secret','lifetime','requser') {
11827:             my $formitem = 'form.lti_'.$item.'_'.$idx;
11828:             $env{$formitem} =~ s/(`)/'/g;
11829:             if ($item eq 'lifetime') {
11830:                 $env{$formitem} =~ s/[^\d.]//g;
11831:             }
11832:             if ($env{$formitem} ne '') {
11833:                 if (($item eq 'key') || ($item eq 'secret')) {
11834:                     $encconfig{$itemid}{$item} = $env{$formitem};
11835:                 } else {
11836:                     $confhash{$itemid}{$item} = $env{$formitem};
11837:                     unless (($idx eq 'add') || ($changes{$itemid})) {
11838:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
11839:                             $changes{$itemid} = 1;
11840:                         }
11841:                     }
11842:                 }
11843:             }
11844:         }
11845:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
11846:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
11847:         }
11848:         if ($confhash{$itemid}{'requser'}) {
11849:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
11850:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
11851:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
11852:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
11853:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
11854:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
11855:                 $mapuser =~ s/(`)/'/g;
11856:                 $mapuser =~ s/^\s+|\s+$//g; 
11857:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
11858:             }
11859:             foreach my $ltirole (@lticourseroles) {
11860:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
11861:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
11862:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
11863:                 }
11864:             }
11865:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
11866:             my @makeuser;
11867:             foreach my $ltirole (sort(@possmakeuser)) {
11868:                 if ($posslti{$ltirole}) {
11869:                     push(@makeuser,$ltirole);
11870:                 }
11871:             }
11872:             $confhash{$itemid}{'makeuser'} = \@makeuser;
11873:             if (@makeuser) {
11874:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
11875:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
11876:                     $confhash{$itemid}{'lcauth'} = $lcauth;
11877:                     if ($lcauth ne 'internal') {
11878:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
11879:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
11880:                         $lcauthparm =~ s/`//g;
11881:                         if ($lcauthparm ne '') {
11882:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
11883:                         }
11884:                     }
11885:                 } else {
11886:                     $confhash{$itemid}{'lcauth'} = 'lti';
11887:                 }
11888:             }
11889:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
11890:             if (@possinstdata) {
11891:                 foreach my $field (@possinstdata) {
11892:                     if (exists($fieldtitles{$field})) {
11893:                         push(@{$confhash{$itemid}{'instdata'}});
11894:                     }
11895:                 }
11896:             }
11897:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
11898:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
11899:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
11900:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
11901:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
11902:                 $mapcrs =~ s/(`)/'/g;
11903:                 $mapcrs =~ s/^\s+|\s+$//g;
11904:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
11905:             }
11906:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
11907:             my @crstypes;
11908:             foreach my $type (sort(@posstypes)) {
11909:                 if ($posscrstype{$type}) {
11910:                     push(@crstypes,$type);
11911:                 }
11912:             }
11913:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
11914:             if ($env{'form.lti_makecrs_'.$idx}) {
11915:                 $confhash{$itemid}{'makecrs'} = 1;
11916:             }
11917:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
11918:             my @selfenroll;
11919:             foreach my $type (sort(@possenroll)) {
11920:                 if ($posslticrs{$type}) {
11921:                     push(@selfenroll,$type);
11922:                 }
11923:             }
11924:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
11925:             if ($env{'form.lti_crssec_'.$idx}) {
11926:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
11927:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
11928:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
11929:                     my $section = $env{'form.lti_customsection_'.$idx};
11930:                     $section =~ s/(`)/'/g;
11931:                     $section =~ s/^\s+|\s+$//g;
11932:                     if ($section ne '') {
11933:                         $confhash{$itemid}{'section'} = $section;
11934:                     }
11935:                 }
11936:             }
11937:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
11938:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
11939:                     $confhash{$itemid}{$field} = 1;
11940:                 }
11941:             }
11942:             if ($env{'form.lti_passback_'.$idx}) {
11943:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
11944:                     $confhash{$itemid}{'passbackformat'} = '1.0';
11945:                 } else {
11946:                     $confhash{$itemid}{'passbackformat'} = '1.1';
11947:                 }
11948:             }
11949:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
11950:                 $confhash{$itemid}{lcmenu} = [];
11951:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
11952:                 foreach my $field (@possmenu) {
11953:                     if (exists($menutitles{$field})) {
11954:                         if ($field eq 'grades') {
11955:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
11956:                         }
11957:                         push(@{$confhash{$itemid}{lcmenu}},$field);
11958:                     }
11959:                 }
11960:             }
11961:             unless (($idx eq 'add') || ($changes{$itemid})) {
11962:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu') {
11963:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
11964:                         $changes{$itemid} = 1;
11965:                     }
11966:                 }
11967:                 unless ($changes{$itemid}) {
11968:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
11969:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
11970:                             $changes{$itemid} = 1;
11971:                         }
11972:                     }
11973:                 }
11974:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
11975:                     unless ($changes{$itemid}) {
11976:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
11977:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
11978:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
11979:                                                                                $confhash{$itemid}{$field});
11980:                                 if (@diffs) {
11981:                                     $changes{$itemid} = 1;
11982:                                 }
11983:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
11984:                                 $changes{$itemid} = 1;
11985:                             }
11986:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
11987:                             if (@{$confhash{$itemid}{$field}} > 0) {
11988:                                 $changes{$itemid} = 1;
11989:                             }
11990:                         }
11991:                     }
11992:                 }
11993:                 unless ($changes{$itemid}) {
11994:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
11995:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
11996:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
11997:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
11998:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
11999:                                     $changes{$itemid} = 1;
12000:                                     last;
12001:                                 }
12002:                             }
12003:                             unless ($changes{$itemid}) {
12004:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
12005:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
12006:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
12007:                                         $changes{$itemid} = 1;
12008:                                         last;
12009:                                     }
12010:                                 }
12011:                             }
12012:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
12013:                             $changes{$itemid} = 1;
12014:                         }
12015:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12016:                         unless ($changes{$itemid}) {
12017:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
12018:                                 $changes{$itemid} = 1;
12019:                             }
12020:                         }
12021:                     }
12022:                 }
12023:             }
12024:         }
12025:     }
12026:     if (@allpos > 0) {
12027:         my $idx = 0;
12028:         foreach my $itemid (@allpos) {
12029:             if ($itemid ne '') {
12030:                 $confhash{$itemid}{'order'} = $idx;
12031:                 if (ref($domconfig{$action}) eq 'HASH') {
12032:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12033:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
12034:                             $changes{$itemid} = 1;
12035:                         }
12036:                     }
12037:                 }
12038:                 $idx ++;
12039:             }
12040:         }
12041:     }
12042:     my %ltihash = (
12043:                           $action => { %confhash }
12044:                        );
12045:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
12046:                                              $dom);
12047:     if ($putresult eq 'ok') {
12048:         my %ltienchash = (
12049:                              $action => { %encconfig }
12050:                          );
12051:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
12052:         if (keys(%changes) > 0) {
12053:             my $cachetime = 24*60*60;
12054:             my %ltiall = %confhash;
12055:             foreach my $id (keys(%ltiall)) {
12056:                 if (ref($encconfig{$id}) eq 'HASH') {
12057:                     foreach my $item ('key','secret') {
12058:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
12059:                     }
12060:                 }
12061:             }
12062:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
12063:             if (ref($lastactref) eq 'HASH') {
12064:                 $lastactref->{'lti'} = 1;
12065:             }
12066:             $resulttext = &mt('Changes made:').'<ul>';
12067:             my %bynum;
12068:             foreach my $itemid (sort(keys(%changes))) {
12069:                 my $position = $confhash{$itemid}{'order'};
12070:                 $bynum{$position} = $itemid;
12071:             }
12072:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12073:                 my $itemid = $bynum{$pos};
12074:                 if (ref($confhash{$itemid}) ne 'HASH') {
12075:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12076:                 } else {
12077:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
12078:                     my $position = $pos + 1;
12079:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12080:                     foreach my $item ('version','lifetime') {
12081:                         if ($confhash{$itemid}{$item} ne '') {
12082:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
12083:                         }
12084:                     }
12085:                     if ($encconfig{$itemid}{'key'} ne '') {
12086:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
12087:                     }
12088:                     if ($encconfig{$itemid}{'secret'} ne '') {
12089:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
12090:                         my $num = length($encconfig{$itemid}{'secret'});
12091:                         $resulttext .= ('*'x$num).'</li>';
12092:                     }
12093:                     if ($confhash{$itemid}{'requser'}) {
12094:                         if ($confhash{$itemid}{'mapuser'}) {
12095:                             my $shownmapuser;
12096:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
12097:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
12098:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
12099:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
12100:                             } else {
12101:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
12102:                             }
12103:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
12104:                         }
12105:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12106:                             my $rolemaps;
12107:                             foreach my $role (@ltiroles) {
12108:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
12109:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
12110:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
12111:                                                                             'Course').',';
12112:                                 }
12113:                             }
12114:                             if ($rolemaps) {
12115:                                 $rolemaps =~ s/,$//;
12116:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
12117:                             }
12118:                         }
12119:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
12120:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
12121:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
12122:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
12123:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
12124:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
12125:                                 } else {
12126:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
12127:                                                        $confhash{$itemid}{'lcauth'});
12128:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
12129:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
12130:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
12131:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
12132:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
12133:                                         }
12134:                                     } else {
12135:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
12136:                                     }
12137:                                 }
12138:                                 $resulttext .= '</li>';
12139:                             } else {
12140:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
12141:                             }
12142:                         }
12143:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
12144:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
12145:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
12146:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
12147:                             } else {
12148:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
12149:                             }
12150:                         }
12151:                         if ($confhash{$itemid}{'mapcrs'}) {
12152:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
12153:                         }
12154:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
12155:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
12156:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
12157:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
12158:                                                '</li>';
12159:                             } else {
12160:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
12161:                             }
12162:                         }
12163:                         if ($confhash{$itemid}{'makecrs'}) {
12164:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
12165:                         } else {
12166:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
12167:                         }
12168:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
12169:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
12170:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
12171:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
12172:                                                '</li>';
12173:                             } else {
12174:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
12175:                             }
12176:                         }
12177:                         if ($confhash{$itemid}{'section'}) {
12178:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
12179:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
12180:                                                      ' (course_section_sourcedid)'.'</li>';  
12181:                             } else {
12182:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
12183:                                                       $confhash{$itemid}{'section'}.'</li>';
12184:                             }
12185:                         } else {
12186:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
12187:                         }
12188:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
12189:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
12190:                             if ($confhash{$itemid}{$item}) {
12191:                                 $resulttext .= &mt('Yes');
12192:                                 if ($item eq 'passback') {
12193:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
12194:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
12195:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
12196:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
12197:                                     }
12198:                                 }
12199:                             } else {
12200:                                 $resulttext .= &mt('No');
12201:                             }
12202:                             $resulttext .= '</li>';
12203:                         }
12204:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
12205:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
12206:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
12207:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
12208:                             } else {
12209:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
12210:                             }
12211:                         }
12212:                     }
12213:                     $resulttext .= '</ul></li>';
12214:                 }
12215:             }
12216:             $resulttext .= '</ul>';
12217:         } else {
12218:             $resulttext = &mt('No changes made.');
12219:         }
12220:     } else {
12221:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12222:     }
12223:     if ($errors) {
12224:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12225:                        $errors.'</ul>';
12226:     }
12227:     return $resulttext;
12228: }
12229: 
12230: sub get_lti_id {
12231:     my ($domain,$consumer) = @_;
12232:     # get lock on lti db
12233:     my $lockhash = {
12234:                       lock => $env{'user.name'}.
12235:                               ':'.$env{'user.domain'},
12236:                    };
12237:     my $tries = 0;
12238:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12239:     my ($id,$error);
12240: 
12241:     while (($gotlock ne 'ok') && ($tries<10)) {
12242:         $tries ++;
12243:         sleep (0.1);
12244:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12245:     }
12246:     if ($gotlock eq 'ok') {
12247:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
12248:         if ($currids{'lock'}) {
12249:             delete($currids{'lock'});
12250:             if (keys(%currids)) {
12251:                 my @curr = sort { $a <=> $b } keys(%currids);
12252:                 if ($curr[-1] =~ /^\d+$/) {
12253:                     $id = 1 + $curr[-1];
12254:                 }
12255:             } else {
12256:                 $id = 1;
12257:             }
12258:             if ($id) {
12259:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
12260:                     $error = 'nostore';
12261:                 }
12262:             } else {
12263:                 $error = 'nonumber';
12264:             }
12265:         }
12266:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
12267:     } else {
12268:         $error = 'nolock';
12269:     }
12270:     return ($id,$error);
12271: }
12272: 
12273: sub modify_autoenroll {
12274:     my ($dom,$lastactref,%domconfig) = @_;
12275:     my ($resulttext,%changes);
12276:     my %currautoenroll;
12277:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
12278:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
12279:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
12280:         }
12281:     }
12282:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
12283:     my %title = ( run => 'Auto-enrollment active',
12284:                   sender => 'Sender for notification messages',
12285:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
12286:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
12287:     my @offon = ('off','on');
12288:     my $sender_uname = $env{'form.sender_uname'};
12289:     my $sender_domain = $env{'form.sender_domain'};
12290:     if ($sender_domain eq '') {
12291:         $sender_uname = '';
12292:     } elsif ($sender_uname eq '') {
12293:         $sender_domain = '';
12294:     }
12295:     my $coowners = $env{'form.autoassign_coowners'};
12296:     my $failsafe = $env{'form.autoenroll_failsafe'};
12297:     $failsafe =~ s{^\s+|\s+$}{}g;
12298:     if ($failsafe =~ /\D/) {
12299:         undef($failsafe);
12300:     }
12301:     my %autoenrollhash =  (
12302:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
12303:                                        'sender_uname' => $sender_uname,
12304:                                        'sender_domain' => $sender_domain,
12305:                                        'co-owners' => $coowners,
12306:                                        'autofailsafe' => $failsafe,
12307:                                 }
12308:                      );
12309:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
12310:                                              $dom);
12311:     if ($putresult eq 'ok') {
12312:         if (exists($currautoenroll{'run'})) {
12313:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
12314:                  $changes{'run'} = 1;
12315:              }
12316:         } elsif ($autorun) {
12317:             if ($env{'form.autoenroll_run'} ne '1') {
12318:                  $changes{'run'} = 1;
12319:             }
12320:         }
12321:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
12322:             $changes{'sender'} = 1;
12323:         }
12324:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
12325:             $changes{'sender'} = 1;
12326:         }
12327:         if ($currautoenroll{'co-owners'} ne '') {
12328:             if ($currautoenroll{'co-owners'} ne $coowners) {
12329:                 $changes{'coowners'} = 1;
12330:             }
12331:         } elsif ($coowners) {
12332:             $changes{'coowners'} = 1;
12333:         }
12334:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
12335:             $changes{'autofailsafe'} = 1;
12336:         }
12337:         if (keys(%changes) > 0) {
12338:             $resulttext = &mt('Changes made:').'<ul>';
12339:             if ($changes{'run'}) {
12340:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
12341:             }
12342:             if ($changes{'sender'}) {
12343:                 if ($sender_uname eq '' || $sender_domain eq '') {
12344:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
12345:                 } else {
12346:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
12347:                 }
12348:             }
12349:             if ($changes{'coowners'}) {
12350:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
12351:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12352:                 if (ref($lastactref) eq 'HASH') {
12353:                     $lastactref->{'domainconfig'} = 1;
12354:                 }
12355:             }
12356:             if ($changes{'autofailsafe'}) {
12357:                 if ($failsafe ne '') {
12358:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
12359:                 } else {
12360:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
12361:                 }
12362:                 &Apache::lonnet::get_domain_defaults($dom,1);
12363:                 if (ref($lastactref) eq 'HASH') {
12364:                     $lastactref->{'domdefaults'} = 1;
12365:                 }
12366:             }
12367:             $resulttext .= '</ul>';
12368:         } else {
12369:             $resulttext = &mt('No changes made to auto-enrollment settings');
12370:         }
12371:     } else {
12372:         $resulttext = '<span class="LC_error">'.
12373: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12374:     }
12375:     return $resulttext;
12376: }
12377: 
12378: sub modify_autoupdate {
12379:     my ($dom,%domconfig) = @_;
12380:     my ($resulttext,%currautoupdate,%fields,%changes);
12381:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
12382:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
12383:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
12384:         }
12385:     }
12386:     my @offon = ('off','on');
12387:     my %title = &Apache::lonlocal::texthash (
12388:                    run => 'Auto-update:',
12389:                    classlists => 'Updates to user information in classlists?'
12390:                 );
12391:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12392:     my %fieldtitles = &Apache::lonlocal::texthash (
12393:                         id => 'Student/Employee ID',
12394:                         permanentemail => 'E-mail address',
12395:                         lastname => 'Last Name',
12396:                         firstname => 'First Name',
12397:                         middlename => 'Middle Name',
12398:                         generation => 'Generation',
12399:                       );
12400:     $othertitle = &mt('All users');
12401:     if (keys(%{$usertypes}) >  0) {
12402:         $othertitle = &mt('Other users');
12403:     }
12404:     foreach my $key (keys(%env)) {
12405:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
12406:             my ($usertype,$item) = ($1,$2);
12407:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
12408:                 if ($usertype eq 'default') {   
12409:                     push(@{$fields{$1}},$2);
12410:                 } elsif (ref($types) eq 'ARRAY') {
12411:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
12412:                         push(@{$fields{$1}},$2);
12413:                     }
12414:                 }
12415:             }
12416:         }
12417:     }
12418:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
12419:     @lockablenames = sort(@lockablenames);
12420:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
12421:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12422:         if (@changed) {
12423:             $changes{'lockablenames'} = 1;
12424:         }
12425:     } else {
12426:         if (@lockablenames) {
12427:             $changes{'lockablenames'} = 1;
12428:         }
12429:     }
12430:     my %updatehash = (
12431:                       autoupdate => { run => $env{'form.autoupdate_run'},
12432:                                       classlists => $env{'form.classlists'},
12433:                                       fields => {%fields},
12434:                                       lockablenames => \@lockablenames,
12435:                                     }
12436:                      );
12437:     foreach my $key (keys(%currautoupdate)) {
12438:         if (($key eq 'run') || ($key eq 'classlists')) {
12439:             if (exists($updatehash{autoupdate}{$key})) {
12440:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
12441:                     $changes{$key} = 1;
12442:                 }
12443:             }
12444:         } elsif ($key eq 'fields') {
12445:             if (ref($currautoupdate{$key}) eq 'HASH') {
12446:                 foreach my $item (@{$types},'default') {
12447:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
12448:                         my $change = 0;
12449:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
12450:                             if (!exists($fields{$item})) {
12451:                                 $change = 1;
12452:                                 last;
12453:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
12454:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
12455:                                     $change = 1;
12456:                                     last;
12457:                                 }
12458:                             }
12459:                         }
12460:                         if ($change) {
12461:                             push(@{$changes{$key}},$item);
12462:                         }
12463:                     } 
12464:                 }
12465:             }
12466:         } elsif ($key eq 'lockablenames') {
12467:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
12468:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12469:                 if (@changed) {
12470:                     $changes{'lockablenames'} = 1;
12471:                 }
12472:             } else {
12473:                 if (@lockablenames) {
12474:                     $changes{'lockablenames'} = 1;
12475:                 }
12476:             }
12477:         }
12478:     }
12479:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
12480:         if (@lockablenames) {
12481:             $changes{'lockablenames'} = 1;
12482:         }
12483:     }
12484:     foreach my $item (@{$types},'default') {
12485:         if (defined($fields{$item})) {
12486:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
12487:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
12488:                     my $change = 0;
12489:                     if (ref($fields{$item}) eq 'ARRAY') {
12490:                         foreach my $type (@{$fields{$item}}) {
12491:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
12492:                                 $change = 1;
12493:                                 last;
12494:                             }
12495:                         }
12496:                     }
12497:                     if ($change) {
12498:                         push(@{$changes{'fields'}},$item);
12499:                     }
12500:                 } else {
12501:                     push(@{$changes{'fields'}},$item);
12502:                 }
12503:             } else {
12504:                 push(@{$changes{'fields'}},$item);
12505:             }
12506:         }
12507:     }
12508:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
12509:                                              $dom);
12510:     if ($putresult eq 'ok') {
12511:         if (keys(%changes) > 0) {
12512:             $resulttext = &mt('Changes made:').'<ul>';
12513:             foreach my $key (sort(keys(%changes))) {
12514:                 if ($key eq 'lockablenames') {
12515:                     $resulttext .= '<li>';
12516:                     if (@lockablenames) {
12517:                         $usertypes->{'default'} = $othertitle;
12518:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
12519:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
12520:                     } else {
12521:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
12522:                     }
12523:                     $resulttext .= '</li>';
12524:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
12525:                     foreach my $item (@{$changes{$key}}) {
12526:                         my @newvalues;
12527:                         foreach my $type (@{$fields{$item}}) {
12528:                             push(@newvalues,$fieldtitles{$type});
12529:                         }
12530:                         my $newvaluestr;
12531:                         if (@newvalues > 0) {
12532:                             $newvaluestr = join(', ',@newvalues);
12533:                         } else {
12534:                             $newvaluestr = &mt('none');
12535:                         }
12536:                         if ($item eq 'default') {
12537:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
12538:                         } else {
12539:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
12540:                         }
12541:                     }
12542:                 } else {
12543:                     my $newvalue;
12544:                     if ($key eq 'run') {
12545:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
12546:                     } else {
12547:                         $newvalue = $offon[$env{'form.'.$key}];
12548:                     }
12549:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
12550:                 }
12551:             }
12552:             $resulttext .= '</ul>';
12553:         } else {
12554:             $resulttext = &mt('No changes made to autoupdates');
12555:         }
12556:     } else {
12557:         $resulttext = '<span class="LC_error">'.
12558: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12559:     }
12560:     return $resulttext;
12561: }
12562: 
12563: sub modify_autocreate {
12564:     my ($dom,%domconfig) = @_;
12565:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
12566:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
12567:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
12568:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
12569:         }
12570:     }
12571:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
12572:                  req => 'Auto-creation of validated requests for official courses',
12573:                  xmldc => 'Identity of course creator of courses from XML files',
12574:                );
12575:     my @types = ('xml','req');
12576:     foreach my $item (@types) {
12577:         $newvals{$item} = $env{'form.autocreate_'.$item};
12578:         $newvals{$item} =~ s/\D//g;
12579:         $newvals{$item} = 0 if ($newvals{$item} eq '');
12580:     }
12581:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
12582:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12583:     unless (exists($domcoords{$newvals{'xmldc'}})) {
12584:         $newvals{'xmldc'} = '';
12585:     } 
12586:     %autocreatehash =  (
12587:                         autocreate => { xml => $newvals{'xml'},
12588:                                         req => $newvals{'req'},
12589:                                       }
12590:                        );
12591:     if ($newvals{'xmldc'} ne '') {
12592:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
12593:     }
12594:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
12595:                                              $dom);
12596:     if ($putresult eq 'ok') {
12597:         my @items = @types;
12598:         if ($newvals{'xml'}) {
12599:             push(@items,'xmldc');
12600:         }
12601:         foreach my $item (@items) {
12602:             if (exists($currautocreate{$item})) {
12603:                 if ($currautocreate{$item} ne $newvals{$item}) {
12604:                     $changes{$item} = 1;
12605:                 }
12606:             } elsif ($newvals{$item}) {
12607:                 $changes{$item} = 1;
12608:             }
12609:         }
12610:         if (keys(%changes) > 0) {
12611:             my @offon = ('off','on'); 
12612:             $resulttext = &mt('Changes made:').'<ul>';
12613:             foreach my $item (@types) {
12614:                 if ($changes{$item}) {
12615:                     my $newtxt = $offon[$newvals{$item}];
12616:                     $resulttext .= '<li>'.
12617:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
12618:                                        '<b>','</b>').
12619:                                    '</li>';
12620:                 }
12621:             }
12622:             if ($changes{'xmldc'}) {
12623:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
12624:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
12625:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
12626:             }
12627:             $resulttext .= '</ul>';
12628:         } else {
12629:             $resulttext = &mt('No changes made to auto-creation settings');
12630:         }
12631:     } else {
12632:         $resulttext = '<span class="LC_error">'.
12633:             &mt('An error occurred: [_1]',$putresult).'</span>';
12634:     }
12635:     return $resulttext;
12636: }
12637: 
12638: sub modify_directorysrch {
12639:     my ($dom,$lastactref,%domconfig) = @_;
12640:     my ($resulttext,%changes);
12641:     my %currdirsrch;
12642:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
12643:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
12644:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
12645:         }
12646:     }
12647:     my %title = ( available => 'Institutional directory search available',
12648:                   localonly => 'Other domains can search institution',
12649:                   lcavailable => 'LON-CAPA directory search available',
12650:                   lclocalonly => 'Other domains can search LON-CAPA domain',
12651:                   searchby => 'Search types',
12652:                   searchtypes => 'Search latitude');
12653:     my @offon = ('off','on');
12654:     my @otherdoms = ('Yes','No');
12655: 
12656:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
12657:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
12658:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
12659: 
12660:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12661:     if (keys(%{$usertypes}) == 0) {
12662:         @cansearch = ('default');
12663:     } else {
12664:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
12665:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
12666:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
12667:                     push(@{$changes{'cansearch'}},$type);
12668:                 }
12669:             }
12670:             foreach my $type (@cansearch) {
12671:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
12672:                     push(@{$changes{'cansearch'}},$type);
12673:                 }
12674:             }
12675:         } else {
12676:             push(@{$changes{'cansearch'}},@cansearch);
12677:         }
12678:     }
12679: 
12680:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
12681:         foreach my $by (@{$currdirsrch{'searchby'}}) {
12682:             if (!grep(/^\Q$by\E$/,@searchby)) {
12683:                 push(@{$changes{'searchby'}},$by);
12684:             }
12685:         }
12686:         foreach my $by (@searchby) {
12687:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
12688:                 push(@{$changes{'searchby'}},$by);
12689:             }
12690:         }
12691:     } else {
12692:         push(@{$changes{'searchby'}},@searchby);
12693:     }
12694: 
12695:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
12696:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
12697:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
12698:                 push(@{$changes{'searchtypes'}},$type);
12699:             }
12700:         }
12701:         foreach my $type (@searchtypes) {
12702:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
12703:                 push(@{$changes{'searchtypes'}},$type);
12704:             }
12705:         }
12706:     } else {
12707:         if (exists($currdirsrch{'searchtypes'})) {
12708:             foreach my $type (@searchtypes) {  
12709:                 if ($type ne $currdirsrch{'searchtypes'}) { 
12710:                     push(@{$changes{'searchtypes'}},$type);
12711:                 }
12712:             }
12713:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
12714:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
12715:             }   
12716:         } else {
12717:             push(@{$changes{'searchtypes'}},@searchtypes); 
12718:         }
12719:     }
12720: 
12721:     my %dirsrch_hash =  (
12722:             directorysrch => { available => $env{'form.dirsrch_available'},
12723:                                cansearch => \@cansearch,
12724:                                localonly => $env{'form.dirsrch_instlocalonly'},
12725:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
12726:                                lcavailable => $env{'form.dirsrch_domavailable'},
12727:                                searchby => \@searchby,
12728:                                searchtypes => \@searchtypes,
12729:                              }
12730:             );
12731:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
12732:                                              $dom);
12733:     if ($putresult eq 'ok') {
12734:         if (exists($currdirsrch{'available'})) {
12735:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
12736:                  $changes{'available'} = 1;
12737:              }
12738:         } else {
12739:             if ($env{'form.dirsrch_available'} eq '1') {
12740:                 $changes{'available'} = 1;
12741:             }
12742:         }
12743:         if (exists($currdirsrch{'lcavailable'})) {
12744:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
12745:                 $changes{'lcavailable'} = 1;
12746:             }
12747:         } else {
12748:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
12749:                 $changes{'lcavailable'} = 1;
12750:             }
12751:         }
12752:         if (exists($currdirsrch{'localonly'})) {
12753:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
12754:                 $changes{'localonly'} = 1;
12755:             }
12756:         } else {
12757:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
12758:                 $changes{'localonly'} = 1;
12759:             }
12760:         }
12761:         if (exists($currdirsrch{'lclocalonly'})) {
12762:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
12763:                 $changes{'lclocalonly'} = 1;
12764:             }
12765:         } else {
12766:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
12767:                 $changes{'lclocalonly'} = 1;
12768:             }
12769:         }
12770:         if (keys(%changes) > 0) {
12771:             $resulttext = &mt('Changes made:').'<ul>';
12772:             if ($changes{'available'}) {
12773:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
12774:             }
12775:             if ($changes{'lcavailable'}) {
12776:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
12777:             }
12778:             if ($changes{'localonly'}) {
12779:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
12780:             }
12781:             if ($changes{'lclocalonly'}) {
12782:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
12783:             }
12784:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
12785:                 my $chgtext;
12786:                 if (ref($usertypes) eq 'HASH') {
12787:                     if (keys(%{$usertypes}) > 0) {
12788:                         foreach my $type (@{$types}) {
12789:                             if (grep(/^\Q$type\E$/,@cansearch)) {
12790:                                 $chgtext .= $usertypes->{$type}.'; ';
12791:                             }
12792:                         }
12793:                         if (grep(/^default$/,@cansearch)) {
12794:                             $chgtext .= $othertitle;
12795:                         } else {
12796:                             $chgtext =~ s/\; $//;
12797:                         }
12798:                         $resulttext .=
12799:                             '<li>'.
12800:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
12801:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
12802:                             '</li>';
12803:                     }
12804:                 }
12805:             }
12806:             if (ref($changes{'searchby'}) eq 'ARRAY') {
12807:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
12808:                 my $chgtext;
12809:                 foreach my $type (@{$titleorder}) {
12810:                     if (grep(/^\Q$type\E$/,@searchby)) {
12811:                         if (defined($searchtitles->{$type})) {
12812:                             $chgtext .= $searchtitles->{$type}.'; ';
12813:                         }
12814:                     }
12815:                 }
12816:                 $chgtext =~ s/\; $//;
12817:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
12818:             }
12819:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
12820:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
12821:                 my $chgtext;
12822:                 foreach my $type (@{$srchtypeorder}) {
12823:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
12824:                         if (defined($srchtypes_desc->{$type})) {
12825:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
12826:                         }
12827:                     }
12828:                 }
12829:                 $chgtext =~ s/\; $//;
12830:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
12831:             }
12832:             $resulttext .= '</ul>';
12833:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
12834:             if (ref($lastactref) eq 'HASH') {
12835:                 $lastactref->{'directorysrch'} = 1;
12836:             }
12837:         } else {
12838:             $resulttext = &mt('No changes made to directory search settings');
12839:         }
12840:     } else {
12841:         $resulttext = '<span class="LC_error">'.
12842:                       &mt('An error occurred: [_1]',$putresult).'</span>';
12843:     }
12844:     return $resulttext;
12845: }
12846: 
12847: sub modify_contacts {
12848:     my ($dom,$lastactref,%domconfig) = @_;
12849:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
12850:     if (ref($domconfig{'contacts'}) eq 'HASH') {
12851:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
12852:             $currsetting{$key} = $domconfig{'contacts'}{$key};
12853:         }
12854:     }
12855:     my (%others,%to,%bcc,%includestr,%includeloc);
12856:     my @contacts = ('supportemail','adminemail');
12857:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
12858:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
12859:     my @toggles = ('reporterrors','reportupdates','reportstatus');
12860:     my @lonstatus = ('threshold','sysmail','weights','excluded');
12861:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
12862:     foreach my $type (@mailings) {
12863:         @{$newsetting{$type}} = 
12864:             &Apache::loncommon::get_env_multiple('form.'.$type);
12865:         foreach my $item (@contacts) {
12866:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
12867:                 $contacts_hash{contacts}{$type}{$item} = 1;
12868:             } else {
12869:                 $contacts_hash{contacts}{$type}{$item} = 0;
12870:             }
12871:         }
12872:         $others{$type} = $env{'form.'.$type.'_others'};
12873:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
12874:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12875:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
12876:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
12877:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
12878:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
12879:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
12880:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
12881:             }
12882:         }
12883:     }
12884:     foreach my $item (@contacts) {
12885:         $to{$item} = $env{'form.'.$item};
12886:         $contacts_hash{'contacts'}{$item} = $to{$item};
12887:     }
12888:     foreach my $item (@toggles) {
12889:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
12890:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
12891:         }
12892:     }
12893:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
12894:     foreach my $item (@lonstatus) {
12895:         if ($item eq 'excluded') {
12896:             my (%serverhomes,@excluded);
12897:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
12898:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
12899:             if (@possexcluded) {
12900:                 foreach my $id (sort(@possexcluded)) {
12901:                     if ($serverhomes{$id}) {
12902:                         push(@excluded,$id);
12903:                     }
12904:                 }
12905:             }
12906:             if (@excluded) {
12907:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
12908:             }
12909:         } elsif ($item eq 'weights') {
12910:             foreach my $type ('E','W','N') {
12911:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
12912:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
12913:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
12914:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
12915:                             $env{'form.error'.$item.'_'.$type};
12916:                     }
12917:                 }
12918:             }
12919:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
12920:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
12921:             if ($env{'form.error'.$item} =~ /^\d+$/) {
12922:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
12923:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
12924:                 }
12925:             }
12926:         }
12927:     }
12928:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
12929:         foreach my $field (@{$fields}) {
12930:             if (ref($possoptions->{$field}) eq 'ARRAY') {
12931:                 my $value = $env{'form.helpform_'.$field};
12932:                 $value =~ s/^\s+|\s+$//g;
12933:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
12934:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
12935:                     if ($field eq 'screenshot') {
12936:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
12937:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
12938:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
12939:                         }
12940:                     }
12941:                 }
12942:             }
12943:         }
12944:     }
12945:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12946:     my (@statuses,%usertypeshash,@overrides);
12947:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
12948:         @statuses = @{$types};
12949:         if (ref($usertypes) eq 'HASH') {
12950:             %usertypeshash = %{$usertypes};
12951:         }
12952:     }
12953:     if (@statuses) {
12954:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
12955:         foreach my $type (@possoverrides) {
12956:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
12957:                 push(@overrides,$type);
12958:             }
12959:         }
12960:         if (@overrides) {
12961:             foreach my $type (@overrides) {
12962:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
12963:                 foreach my $item (@contacts) {
12964:                     if (grep(/^\Q$item\E$/,@standard)) {
12965:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
12966:                         $newsetting{'override_'.$type}{$item} = 1;
12967:                     } else {
12968:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
12969:                         $newsetting{'override_'.$type}{$item} = 0;
12970:                     }
12971:                 }
12972:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
12973:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
12974:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
12975:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
12976:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
12977:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
12978:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
12979:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
12980:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
12981:                 }
12982:             }     
12983:         }
12984:     }
12985:     if (keys(%currsetting) > 0) {
12986:         foreach my $item (@contacts) {
12987:             if ($to{$item} ne $currsetting{$item}) {
12988:                 $changes{$item} = 1;
12989:             }
12990:         }
12991:         foreach my $type (@mailings) {
12992:             foreach my $item (@contacts) {
12993:                 if (ref($currsetting{$type}) eq 'HASH') {
12994:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
12995:                         push(@{$changes{$type}},$item);
12996:                     }
12997:                 } else {
12998:                     push(@{$changes{$type}},@{$newsetting{$type}});
12999:                 }
13000:             }
13001:             if ($others{$type} ne $currsetting{$type}{'others'}) {
13002:                 push(@{$changes{$type}},'others');
13003:             }
13004:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13005:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
13006:                     push(@{$changes{$type}},'bcc'); 
13007:                 }
13008:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
13009:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
13010:                     push(@{$changes{$type}},'include');
13011:                 }
13012:             }
13013:         }
13014:         if (ref($fields) eq 'ARRAY') {
13015:             if (ref($currsetting{'helpform'}) eq 'HASH') {
13016:                 foreach my $field (@{$fields}) {
13017:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
13018:                         push(@{$changes{'helpform'}},$field);
13019:                     }
13020:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13021:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
13022:                             push(@{$changes{'helpform'}},'maxsize');
13023:                         }
13024:                     }
13025:                 }
13026:             } else {
13027:                 foreach my $field (@{$fields}) {
13028:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13029:                         push(@{$changes{'helpform'}},$field);
13030:                     }
13031:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13032:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13033:                             push(@{$changes{'helpform'}},'maxsize');
13034:                         }
13035:                     }
13036:                 }
13037:             }
13038:         }
13039:         if (@statuses) {
13040:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
13041:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
13042:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
13043:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
13044:                             foreach my $item (@contacts,'bcc','others','include') {
13045:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
13046:                                     push(@{$changes{'overrides'}},$key);
13047:                                     last;
13048:                                 }
13049:                             }
13050:                         } else {
13051:                             push(@{$changes{'overrides'}},$key);
13052:                         }
13053:                     }
13054:                 }
13055:                 foreach my $key (@overrides) {
13056:                     unless (exists($currsetting{'overrides'}{$key})) {
13057:                         push(@{$changes{'overrides'}},$key);
13058:                     }
13059:                 }
13060:             } else {
13061:                 foreach my $key (@overrides) {
13062:                     push(@{$changes{'overrides'}},$key); 
13063:                 }
13064:             }
13065:         }
13066:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
13067:             foreach my $key ('excluded','weights','threshold','sysmail') {
13068:                 if ($key eq 'excluded') {
13069:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13070:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
13071:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13072:                             (@{$currsetting{'lonstatus'}{$key}})) {
13073:                             my @diffs =
13074:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
13075:                                                                    $currsetting{'lonstatus'}{$key});
13076:                             if (@diffs) {
13077:                                 push(@{$changes{'lonstatus'}},$key);
13078:                             }
13079:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
13080:                             push(@{$changes{'lonstatus'}},$key);
13081:                         }
13082:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13083:                              (@{$currsetting{'lonstatus'}{$key}})) {
13084:                         push(@{$changes{'lonstatus'}},$key);
13085:                     }
13086:                 } elsif ($key eq 'weights') {
13087:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13088:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
13089:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13090:                             foreach my $type ('E','W','N','U') {
13091:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
13092:                                         $currsetting{'lonstatus'}{$key}{$type}) {
13093:                                     push(@{$changes{'lonstatus'}},$key);
13094:                                     last;
13095:                                 }
13096:                             }
13097:                         } else {
13098:                             foreach my $type ('E','W','N','U') {
13099:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
13100:                                     push(@{$changes{'lonstatus'}},$key);
13101:                                     last;
13102:                                 }
13103:                             }
13104:                         }
13105:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13106:                         foreach my $type ('E','W','N','U') {
13107:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
13108:                                 push(@{$changes{'lonstatus'}},$key);
13109:                                 last;
13110:                             }
13111:                         }
13112:                     }
13113:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
13114:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13115:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13116:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
13117:                                 push(@{$changes{'lonstatus'}},$key);
13118:                             }
13119:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
13120:                             push(@{$changes{'lonstatus'}},$key);
13121:                         }
13122:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13123:                         push(@{$changes{'lonstatus'}},$key);
13124:                     }
13125:                 }
13126:             }
13127:         } else {
13128:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13129:                 foreach my $key ('excluded','weights','threshold','sysmail') {
13130:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13131:                         push(@{$changes{'lonstatus'}},$key);
13132:                     }
13133:                 }
13134:             }
13135:         }
13136:     } else {
13137:         my %default;
13138:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
13139:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
13140:         $default{'errormail'} = 'adminemail';
13141:         $default{'packagesmail'} = 'adminemail';
13142:         $default{'helpdeskmail'} = 'supportemail';
13143:         $default{'otherdomsmail'} = 'supportemail';
13144:         $default{'lonstatusmail'} = 'adminemail';
13145:         $default{'requestsmail'} = 'adminemail';
13146:         $default{'updatesmail'} = 'adminemail';
13147:         foreach my $item (@contacts) {
13148:            if ($to{$item} ne $default{$item}) {
13149:                $changes{$item} = 1;
13150:            }
13151:         }
13152:         foreach my $type (@mailings) {
13153:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
13154:                 push(@{$changes{$type}},@{$newsetting{$type}});
13155:             }
13156:             if ($others{$type} ne '') {
13157:                 push(@{$changes{$type}},'others');
13158:             }
13159:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13160:                 if ($bcc{$type} ne '') {
13161:                     push(@{$changes{$type}},'bcc');
13162:                 }
13163:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
13164:                     push(@{$changes{$type}},'include');
13165:                 }
13166:             }
13167:         }
13168:         if (ref($fields) eq 'ARRAY') {
13169:             foreach my $field (@{$fields}) {
13170:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13171:                     push(@{$changes{'helpform'}},$field);
13172:                 }
13173:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13174:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13175:                         push(@{$changes{'helpform'}},'maxsize');
13176:                     }
13177:                 }
13178:             }
13179:         }
13180:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13181:             foreach my $key ('excluded','weights','threshold','sysmail') {
13182:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13183:                     push(@{$changes{'lonstatus'}},$key);
13184:                 }
13185:             }
13186:         }
13187:     }
13188:     foreach my $item (@toggles) {
13189:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
13190:             $changes{$item} = 1;
13191:         } elsif ((!$env{'form.'.$item}) &&
13192:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
13193:             $changes{$item} = 1;
13194:         }
13195:     }
13196:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
13197:                                              $dom);
13198:     if ($putresult eq 'ok') {
13199:         if (keys(%changes) > 0) {
13200:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13201:             if (ref($lastactref) eq 'HASH') {
13202:                 $lastactref->{'domainconfig'} = 1;
13203:             }
13204:             my ($titles,$short_titles)  = &contact_titles();
13205:             $resulttext = &mt('Changes made:').'<ul>';
13206:             foreach my $item (@contacts) {
13207:                 if ($changes{$item}) {
13208:                     $resulttext .= '<li>'.$titles->{$item}.
13209:                                     &mt(' set to: ').
13210:                                     '<span class="LC_cusr_emph">'.
13211:                                     $to{$item}.'</span></li>';
13212:                 }
13213:             }
13214:             foreach my $type (@mailings) {
13215:                 if (ref($changes{$type}) eq 'ARRAY') {
13216:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13217:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
13218:                     } else {
13219:                         $resulttext .= '<li>'.$titles->{$type}.': ';
13220:                     }
13221:                     my @text;
13222:                     foreach my $item (@{$newsetting{$type}}) {
13223:                         push(@text,$short_titles->{$item});
13224:                     }
13225:                     if ($others{$type} ne '') {
13226:                         push(@text,$others{$type});
13227:                     }
13228:                     if (@text) {
13229:                         $resulttext .= '<span class="LC_cusr_emph">'.
13230:                                        join(', ',@text).'</span>';
13231:                     }
13232:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13233:                         if ($bcc{$type} ne '') {
13234:                             my $bcctext;
13235:                             if (@text) {
13236:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
13237:                             } else {
13238:                                 $bcctext = '(Bcc)';
13239:                             }
13240:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
13241:                         } elsif (!@text) {
13242:                             $resulttext .= &mt('No one');
13243:                         }   
13244:                         if ($includestr{$type} ne '') {
13245:                             if ($includeloc{$type} eq 'b') {
13246:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
13247:                             } elsif ($includeloc{$type} eq 's') {
13248:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
13249:                             }
13250:                         }
13251:                     } elsif (!@text) {
13252:                         $resulttext .= &mt('No recipients');
13253:                     }
13254:                     $resulttext .= '</li>';
13255:                 }
13256:             }
13257:             if (ref($changes{'overrides'}) eq 'ARRAY') {
13258:                 my @deletions;
13259:                 foreach my $type (@{$changes{'overrides'}}) {
13260:                     if ($usertypeshash{$type}) {
13261:                         if (grep(/^\Q$type\E/,@overrides)) {
13262:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
13263:                                                       $usertypeshash{$type}).'<ul><li>';
13264:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
13265:                                 my @text;
13266:                                 foreach my $item (@contacts) {
13267:                                     if ($newsetting{'override_'.$type}{$item}) { 
13268:                                         push(@text,$short_titles->{$item});
13269:                                     }
13270:                                 }
13271:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
13272:                                     push(@text,$newsetting{'override_'.$type}{'others'});
13273:                                 }
13274:   
13275:                                 if (@text) {
13276:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
13277:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
13278:                                 }
13279:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
13280:                                     my $bcctext;
13281:                                     if (@text) {
13282:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
13283:                                     } else {
13284:                                         $bcctext = '(Bcc)';
13285:                                     }
13286:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
13287:                                 } elsif (!@text) {
13288:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
13289:                                 }
13290:                                 $resulttext .= '</li>';
13291:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
13292:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
13293:                                     if ($loc eq 'b') {
13294:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
13295:                                     } elsif ($loc eq 's') {
13296:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
13297:                                     }
13298:                                 }
13299:                             }
13300:                             $resulttext .= '</li></ul></li>';
13301:                         } else {
13302:                             push(@deletions,$usertypeshash{$type});
13303:                         }
13304:                     }
13305:                 }
13306:                 if (@deletions) {
13307:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
13308:                                               join(', ',@deletions)).'</li>';
13309:                 }
13310:             }
13311:             my @offon = ('off','on');
13312:             my $corelink = &core_link_msu();
13313:             if ($changes{'reporterrors'}) {
13314:                 $resulttext .= '<li>'.
13315:                                &mt('E-mail error reports to [_1] set to "'.
13316:                                    $offon[$env{'form.reporterrors'}].'".',
13317:                                    $corelink).
13318:                                '</li>';
13319:             }
13320:             if ($changes{'reportupdates'}) {
13321:                 $resulttext .= '<li>'.
13322:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
13323:                                     $offon[$env{'form.reportupdates'}].'".',
13324:                                     $corelink).
13325:                                 '</li>';
13326:             }
13327:             if ($changes{'reportstatus'}) {
13328:                 $resulttext .= '<li>'.
13329:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
13330:                                     $offon[$env{'form.reportstatus'}].'".',
13331:                                     $corelink).
13332:                                 '</li>';
13333:             }
13334:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
13335:                 $resulttext .= '<li>'.
13336:                                &mt('Nightly status check e-mail settings').':<ul>';
13337:                 my (%defval,%use_def,%shown);
13338:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
13339:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
13340:                 $defval{'weights'} =
13341:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
13342:                 $defval{'excluded'} = &mt('None');
13343:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
13344:                     foreach my $item ('threshold','sysmail','weights','excluded') {
13345:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
13346:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
13347:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
13348:                             } elsif ($item eq 'weights') {
13349:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
13350:                                     foreach my $type ('E','W','N','U') {
13351:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
13352:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
13353:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
13354:                                         } else {
13355:                                             $shown{$item} .= $lonstatus_defs->{$type};
13356:                                         }
13357:                                         $shown{$item} .= ', ';
13358:                                     }
13359:                                     $shown{$item} =~ s/, $//;
13360:                                 } else {
13361:                                     $shown{$item} = $defval{$item};
13362:                                 }
13363:                             } elsif ($item eq 'excluded') {
13364:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
13365:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
13366:                                 } else {
13367:                                     $shown{$item} = $defval{$item};
13368:                                 }
13369:                             }
13370:                         } else {
13371:                             $shown{$item} = $defval{$item};
13372:                         }
13373:                     }
13374:                 } else {
13375:                     foreach my $item ('threshold','weights','excluded','sysmail') {
13376:                         $shown{$item} = $defval{$item};
13377:                     }
13378:                 }
13379:                 foreach my $item ('threshold','weights','excluded','sysmail') {
13380:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
13381:                                           $shown{$item}).'</li>';
13382:                 }
13383:                 $resulttext .= '</ul></li>';
13384:             }
13385:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
13386:                 my (@optional,@required,@unused,$maxsizechg);
13387:                 foreach my $field (@{$changes{'helpform'}}) {
13388:                     if ($field eq 'maxsize') {
13389:                         $maxsizechg = 1;
13390:                         next;
13391:                     }
13392:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
13393:                         push(@optional,$field);
13394:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
13395:                         push(@unused,$field);
13396:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
13397:                         push(@required,$field);
13398:                     }
13399:                 }
13400:                 if (@optional) {
13401:                     $resulttext .= '<li>'.
13402:                                    &mt('Help form fields changed to "Optional": [_1].',
13403:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
13404:                                    '</li>';
13405:                 }
13406:                 if (@required) {
13407:                     $resulttext .= '<li>'.
13408:                                    &mt('Help form fields changed to "Required": [_1].',
13409:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
13410:                                    '</li>';
13411:                 }
13412:                 if (@unused) {
13413:                     $resulttext .= '<li>'.
13414:                                    &mt('Help form fields changed to "Not shown": [_1].',
13415:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
13416:                                    '</li>';
13417:                 }
13418:                 if ($maxsizechg) {
13419:                     $resulttext .= '<li>'.
13420:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
13421:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
13422:                                    '</li>';
13423:                 }
13424:             }
13425:             $resulttext .= '</ul>';
13426:         } else {
13427:             $resulttext = &mt('No changes made to contacts and form settings');
13428:         }
13429:     } else {
13430:         $resulttext = '<span class="LC_error">'.
13431:             &mt('An error occurred: [_1].',$putresult).'</span>';
13432:     }
13433:     return $resulttext;
13434: }
13435: 
13436: sub modify_usercreation {
13437:     my ($dom,%domconfig) = @_;
13438:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
13439:     my $warningmsg;
13440:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13441:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13442:             if ($key eq 'cancreate') {
13443:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13444:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13445:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
13446:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13447:                         } else {
13448:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13449:                         }
13450:                     }
13451:                 }
13452:             } elsif ($key eq 'email_rule') {
13453:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13454:             } else {
13455:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13456:             }
13457:         }
13458:     }
13459:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
13460:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
13461:     my @contexts = ('author','course','requestcrs');
13462:     foreach my $item(@contexts) {
13463:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
13464:     }
13465:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13466:         foreach my $item (@contexts) {
13467:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
13468:                 push(@{$changes{'cancreate'}},$item);
13469:             }
13470:         }
13471:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
13472:         foreach my $item (@contexts) {
13473:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
13474:                 if ($cancreate{$item} ne 'any') {
13475:                     push(@{$changes{'cancreate'}},$item);
13476:                 }
13477:             } else {
13478:                 if ($cancreate{$item} ne 'none') {
13479:                     push(@{$changes{'cancreate'}},$item);
13480:                 }
13481:             }
13482:         }
13483:     } else {
13484:         foreach my $item (@contexts)  {
13485:             push(@{$changes{'cancreate'}},$item);
13486:         }
13487:     }
13488: 
13489:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
13490:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
13491:             if (!grep(/^\Q$type\E$/,@username_rule)) {
13492:                 push(@{$changes{'username_rule'}},$type);
13493:             }
13494:         }
13495:         foreach my $type (@username_rule) {
13496:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
13497:                 push(@{$changes{'username_rule'}},$type);
13498:             }
13499:         }
13500:     } else {
13501:         push(@{$changes{'username_rule'}},@username_rule);
13502:     }
13503: 
13504:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
13505:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
13506:             if (!grep(/^\Q$type\E$/,@id_rule)) {
13507:                 push(@{$changes{'id_rule'}},$type);
13508:             }
13509:         }
13510:         foreach my $type (@id_rule) {
13511:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
13512:                 push(@{$changes{'id_rule'}},$type);
13513:             }
13514:         }
13515:     } else {
13516:         push(@{$changes{'id_rule'}},@id_rule);
13517:     }
13518: 
13519:     my @authen_contexts = ('author','course','domain');
13520:     my @authtypes = ('int','krb4','krb5','loc','lti');
13521:     my %authhash;
13522:     foreach my $item (@authen_contexts) {
13523:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
13524:         foreach my $auth (@authtypes) {
13525:             if (grep(/^\Q$auth\E$/,@authallowed)) {
13526:                 $authhash{$item}{$auth} = 1;
13527:             } else {
13528:                 $authhash{$item}{$auth} = 0;
13529:             }
13530:         }
13531:     }
13532:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
13533:         foreach my $item (@authen_contexts) {
13534:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
13535:                 foreach my $auth (@authtypes) {
13536:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
13537:                         push(@{$changes{'authtypes'}},$item);
13538:                         last;
13539:                     }
13540:                 }
13541:             }
13542:         }
13543:     } else {
13544:         foreach my $item (@authen_contexts) {
13545:             push(@{$changes{'authtypes'}},$item);
13546:         }
13547:     }
13548: 
13549:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
13550:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
13551:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
13552:     $save_usercreate{'id_rule'} = \@id_rule;
13553:     $save_usercreate{'username_rule'} = \@username_rule,
13554:     $save_usercreate{'authtypes'} = \%authhash;
13555: 
13556:     my %usercreation_hash =  (
13557:         usercreation     => \%save_usercreate,
13558:     );
13559: 
13560:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
13561:                                              $dom);
13562: 
13563:     if ($putresult eq 'ok') {
13564:         if (keys(%changes) > 0) {
13565:             $resulttext = &mt('Changes made:').'<ul>';
13566:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13567:                 my %lt = &usercreation_types();
13568:                 foreach my $type (@{$changes{'cancreate'}}) {
13569:                     my $chgtext = $lt{$type}.', ';
13570:                     if ($cancreate{$type} eq 'none') {
13571:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
13572:                     } elsif ($cancreate{$type} eq 'any') {
13573:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
13574:                     } elsif ($cancreate{$type} eq 'official') {
13575:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
13576:                     } elsif ($cancreate{$type} eq 'unofficial') {
13577:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
13578:                     }
13579:                     $resulttext .= '<li>'.$chgtext.'</li>';
13580:                 }
13581:             }
13582:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
13583:                 my ($rules,$ruleorder) = 
13584:                     &Apache::lonnet::inst_userrules($dom,'username');
13585:                 my $chgtext = '<ul>';
13586:                 foreach my $type (@username_rule) {
13587:                     if (ref($rules->{$type}) eq 'HASH') {
13588:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
13589:                     }
13590:                 }
13591:                 $chgtext .= '</ul>';
13592:                 if (@username_rule > 0) {
13593:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
13594:                 } else {
13595:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
13596:                 }
13597:             }
13598:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
13599:                 my ($idrules,$idruleorder) = 
13600:                     &Apache::lonnet::inst_userrules($dom,'id');
13601:                 my $chgtext = '<ul>';
13602:                 foreach my $type (@id_rule) {
13603:                     if (ref($idrules->{$type}) eq 'HASH') {
13604:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
13605:                     }
13606:                 }
13607:                 $chgtext .= '</ul>';
13608:                 if (@id_rule > 0) {
13609:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
13610:                 } else {
13611:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
13612:                 }
13613:             }
13614:             my %authname = &authtype_names();
13615:             my %context_title = &context_names();
13616:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
13617:                 my $chgtext = '<ul>';
13618:                 foreach my $type (@{$changes{'authtypes'}}) {
13619:                     my @allowed;
13620:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
13621:                     foreach my $auth (@authtypes) {
13622:                         if ($authhash{$type}{$auth}) {
13623:                             push(@allowed,$authname{$auth});
13624:                         }
13625:                     }
13626:                     if (@allowed > 0) {
13627:                         $chgtext .= join(', ',@allowed).'</li>';
13628:                     } else {
13629:                         $chgtext .= &mt('none').'</li>';
13630:                     }
13631:                 }
13632:                 $chgtext .= '</ul>';
13633:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
13634:                 $resulttext .= '</li>';
13635:             }
13636:             $resulttext .= '</ul>';
13637:         } else {
13638:             $resulttext = &mt('No changes made to user creation settings');
13639:         }
13640:     } else {
13641:         $resulttext = '<span class="LC_error">'.
13642:             &mt('An error occurred: [_1]',$putresult).'</span>';
13643:     }
13644:     if ($warningmsg ne '') {
13645:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13646:     }
13647:     return $resulttext;
13648: }
13649: 
13650: sub modify_selfcreation {
13651:     my ($dom,$lastactref,%domconfig) = @_;
13652:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
13653:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
13654:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13655:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
13656:     if (ref($typesref) eq 'ARRAY') {
13657:         @types = @{$typesref};
13658:     }
13659:     if (ref($usertypesref) eq 'HASH') {
13660:         %usertypes = %{$usertypesref};
13661:     }
13662:     $usertypes{'default'} = $othertitle;
13663: #
13664: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
13665: #
13666:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13667:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13668:             if ($key eq 'cancreate') {
13669:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13670:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13671:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
13672:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
13673:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
13674:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
13675:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
13676:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
13677:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13678:                         } else {
13679:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13680:                         }
13681:                     }
13682:                 }
13683:             } elsif ($key eq 'email_rule') {
13684:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13685:             } else {
13686:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13687:             }
13688:         }
13689:     }
13690: #
13691: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
13692: #
13693:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13694:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13695:             if ($key eq 'selfcreate') {
13696:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
13697:             } else {
13698:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
13699:             }
13700:         }
13701:     }
13702: #
13703: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
13704: #
13705:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13706:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
13707:             if ($key eq 'inststatusguest') {
13708:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
13709:             } else {
13710:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
13711:             }
13712:         }
13713:     }
13714: 
13715:     my @contexts = ('selfcreate');
13716:     @{$cancreate{'selfcreate'}} = ();
13717:     %{$cancreate{'emailusername'}} = ();
13718:     if (@types) {
13719:         @{$cancreate{'statustocreate'}} = ();
13720:     }
13721:     %{$cancreate{'selfcreateprocessing'}} = ();
13722:     %{$cancreate{'shibenv'}} = ();
13723:     %{$cancreate{'emailverified'}} = ();
13724:     %{$cancreate{'emailoptions'}} = ();
13725:     %{$cancreate{'emaildomain'}} = ();
13726:     my %selfcreatetypes = (
13727:                              sso   => 'users authenticated by institutional single sign on',
13728:                              login => 'users authenticated by institutional log-in',
13729:                              email => 'users verified by e-mail',
13730:                           );
13731: #
13732: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
13733: # is permitted.
13734: #
13735:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
13736: 
13737:     my (@statuses,%email_rule);
13738:     foreach my $item ('login','sso','email') {
13739:         if ($item eq 'email') {
13740:             if ($env{'form.cancreate_email'}) {
13741:                 if (@types) {
13742:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
13743:                     foreach my $status (@poss_statuses) {
13744:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
13745:                             push(@statuses,$status);
13746:                         }
13747:                     }
13748:                     $save_inststatus{'inststatusguest'} = \@statuses;
13749:                 } else {
13750:                     push(@statuses,'default');
13751:                 }
13752:                 if (@statuses) {
13753:                     my %curr_rule;
13754:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
13755:                         foreach my $type (@statuses) {
13756:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
13757:                         }
13758:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
13759:                         foreach my $type (@statuses) {
13760:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
13761:                         }
13762:                     }
13763:                     push(@{$cancreate{'selfcreate'}},'email');
13764:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
13765:                     my %curremaildom;
13766:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
13767:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
13768:                     }
13769:                     foreach my $type (@statuses) {
13770:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
13771:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
13772:                         }
13773:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
13774:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
13775:                         }
13776:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
13777: #
13778: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
13779: #
13780:                             my $chosen = $1;
13781:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
13782:                                 my $emaildom;
13783:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
13784:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
13785:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
13786:                                     if (ref($curremaildom{$type}) eq 'HASH') {
13787:                                         if (exists($curremaildom{$type}{$chosen})) {
13788:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
13789:                                                 push(@{$changes{'cancreate'}},'emaildomain');
13790:                                             }
13791:                                         } elsif ($emaildom ne '') {
13792:                                             push(@{$changes{'cancreate'}},'emaildomain');
13793:                                         }
13794:                                     } elsif ($emaildom ne '') {
13795:                                         push(@{$changes{'cancreate'}},'emaildomain');
13796:                                     } 
13797:                                 }
13798:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13799:                             } elsif ($chosen eq 'custom') {
13800:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
13801:                                 $email_rule{$type} = [];
13802:                                 if (ref($emailrules) eq 'HASH') {
13803:                                     foreach my $rule (@possemail_rules) {
13804:                                         if (exists($emailrules->{$rule})) {
13805:                                             push(@{$email_rule{$type}},$rule);
13806:                                         }
13807:                                     }
13808:                                 }
13809:                                 if (@{$email_rule{$type}}) {
13810:                                     $cancreate{'emailoptions'}{$type} = 'custom';
13811:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
13812:                                         if (@{$curr_rule{$type}} > 0) {
13813:                                             foreach my $rule (@{$curr_rule{$type}}) {
13814:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
13815:                                                     push(@{$changes{'email_rule'}},$type);
13816:                                                 }
13817:                                             }
13818:                                         }
13819:                                         foreach my $type (@{$email_rule{$type}}) {
13820:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
13821:                                                 push(@{$changes{'email_rule'}},$type);
13822:                                             }
13823:                                         }
13824:                                     } else {
13825:                                         push(@{$changes{'email_rule'}},$type);
13826:                                     }
13827:                                 }
13828:                             } else {
13829:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13830:                             }
13831:                         }
13832:                     }
13833:                     if (@types) {
13834:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13835:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
13836:                             if (@changed) {
13837:                                 push(@{$changes{'inststatus'}},'inststatusguest');
13838:                             }
13839:                         } else {
13840:                             push(@{$changes{'inststatus'}},'inststatusguest');
13841:                         }
13842:                     }
13843:                 } else {
13844:                     delete($env{'form.cancreate_email'});
13845:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13846:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13847:                             push(@{$changes{'inststatus'}},'inststatusguest');
13848:                         }
13849:                     }
13850:                 }
13851:             } else {
13852:                 $save_inststatus{'inststatusguest'} = [];
13853:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13854:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13855:                         push(@{$changes{'inststatus'}},'inststatusguest');
13856:                     }
13857:                 }
13858:             }
13859:         } else {
13860:             if ($env{'form.cancreate_'.$item}) {
13861:                 push(@{$cancreate{'selfcreate'}},$item);
13862:             }
13863:         }
13864:     }
13865:     my (%userinfo,%savecaptcha);
13866:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
13867: #
13868: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
13869: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
13870: #
13871: 
13872:     if ($env{'form.cancreate_email'}) {
13873:         push(@contexts,'emailusername');
13874:         if (@statuses) {
13875:             foreach my $type (@statuses) {
13876:                 if (ref($infofields) eq 'ARRAY') {
13877:                     foreach my $field (@{$infofields}) {
13878:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
13879:                             $cancreate{'emailusername'}{$type}{$field} = $1;
13880:                         }
13881:                     }
13882:                 }
13883:             }
13884:         }
13885: #
13886: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
13887: # queued requests for self-creation of account verified by e-mail.
13888: #
13889: 
13890:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
13891:         @approvalnotify = sort(@approvalnotify);
13892:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
13893:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13894:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
13895:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
13896:                     push(@{$changes{'cancreate'}},'notify');
13897:                 }
13898:             } else {
13899:                 if ($cancreate{'notify'}{'approval'}) {
13900:                     push(@{$changes{'cancreate'}},'notify');
13901:                 }
13902:             }
13903:         } elsif ($cancreate{'notify'}{'approval'}) {
13904:             push(@{$changes{'cancreate'}},'notify');
13905:         }
13906: 
13907:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
13908:     }
13909: #  
13910: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
13911: # institutional log-in.
13912: #
13913:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
13914:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
13915:                ($domdefaults{'auth_def'} eq 'localauth'))) {
13916:             $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.').' '.
13917:                           &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.');
13918:         }
13919:     }
13920:     my @fields = ('lastname','firstname','middlename','generation',
13921:                   'permanentemail','id');
13922:     my @shibfields = (@fields,'inststatus');
13923:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13924: #
13925: # Where usernames may created for institutional log-in and/or institutional single sign on:
13926: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
13927: # may self-create accounts 
13928: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
13929: # which the user may supply, if institutional data is unavailable.
13930: #
13931:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
13932:         if (@types) {
13933:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
13934:             push(@contexts,'statustocreate');
13935:             foreach my $type (@types) {
13936:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
13937:                 foreach my $field (@fields) {
13938:                     if (grep(/^\Q$field\E$/,@modifiable)) {
13939:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
13940:                     } else {
13941:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
13942:                     }
13943:                 }
13944:             }
13945:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
13946:                 foreach my $type (@types) {
13947:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
13948:                         foreach my $field (@fields) {
13949:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
13950:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
13951:                                 push(@{$changes{'selfcreate'}},$type);
13952:                                 last;
13953:                             }
13954:                         }
13955:                     }
13956:                 }
13957:             } else {
13958:                 foreach my $type (@types) {
13959:                     push(@{$changes{'selfcreate'}},$type);
13960:                 }
13961:             }
13962:         }
13963:         foreach my $field (@shibfields) {
13964:             if ($env{'form.shibenv_'.$field} ne '') {
13965:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
13966:             }
13967:         }
13968:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13969:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
13970:                 foreach my $field (@shibfields) {
13971:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
13972:                         push(@{$changes{'cancreate'}},'shibenv');
13973:                     }
13974:                 }
13975:             } else {
13976:                 foreach my $field (@shibfields) {
13977:                     if ($env{'form.shibenv_'.$field}) {
13978:                         push(@{$changes{'cancreate'}},'shibenv');
13979:                         last;
13980:                     }
13981:                 }
13982:             }
13983:         }
13984:     }
13985:     foreach my $item (@contexts) {
13986:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
13987:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
13988:                 if (ref($cancreate{$item}) eq 'ARRAY') {
13989:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
13990:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13991:                             push(@{$changes{'cancreate'}},$item);
13992:                         }
13993:                     }
13994:                 }
13995:             }
13996:             if (ref($cancreate{$item}) eq 'ARRAY') {
13997:                 foreach my $type (@{$cancreate{$item}}) {
13998:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
13999:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14000:                             push(@{$changes{'cancreate'}},$item);
14001:                         }
14002:                     }
14003:                 }
14004:             }
14005:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
14006:             if (ref($cancreate{$item}) eq 'HASH') {
14007:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
14008:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
14009:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
14010:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
14011:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14012:                                     push(@{$changes{'cancreate'}},$item);
14013:                                 }
14014:                             }
14015:                         }
14016:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14017:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
14018:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14019:                                 push(@{$changes{'cancreate'}},$item);
14020:                             }
14021:                         }
14022:                     }
14023:                 }
14024:                 foreach my $type (keys(%{$cancreate{$item}})) {
14025:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
14026:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
14027:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
14028:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
14029:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14030:                                         push(@{$changes{'cancreate'}},$item);
14031:                                     }
14032:                                 }
14033:                             } else {
14034:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14035:                                     push(@{$changes{'cancreate'}},$item);
14036:                                 }
14037:                             }
14038:                         }
14039:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14040:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
14041:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14042:                                 push(@{$changes{'cancreate'}},$item);
14043:                             }
14044:                         }
14045:                     }
14046:                 }
14047:             }
14048:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
14049:             if (ref($cancreate{$item}) eq 'ARRAY') {
14050:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
14051:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14052:                         push(@{$changes{'cancreate'}},$item);
14053:                     }
14054:                 }
14055:             }
14056:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14057:             if (ref($cancreate{$item}) eq 'HASH') {
14058:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14059:                     push(@{$changes{'cancreate'}},$item);
14060:                 }
14061:             }
14062:         } elsif ($item eq 'emailusername') {
14063:             if (ref($cancreate{$item}) eq 'HASH') {
14064:                 foreach my $type (keys(%{$cancreate{$item}})) {
14065:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
14066:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
14067:                             if ($cancreate{$item}{$type}{$field}) {
14068:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14069:                                     push(@{$changes{'cancreate'}},$item);
14070:                                 }
14071:                                 last;
14072:                             }
14073:                         }
14074:                     }
14075:                 }
14076:             }
14077:         }
14078:     }
14079: #
14080: # Populate %save_usercreate hash with updates to self-creation configuration.
14081: #
14082:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
14083:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
14084:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
14085:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
14086:     if (ref($cancreate{'notify'}) eq 'HASH') {
14087:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
14088:     }
14089:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
14090:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
14091:     }
14092:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
14093:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
14094:     }
14095:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
14096:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
14097:     }
14098:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
14099:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
14100:     }
14101:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
14102:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
14103:     }
14104:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
14105:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
14106:     }
14107:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
14108:     $save_usercreate{'email_rule'} = \%email_rule;
14109: 
14110:     my %userconfig_hash = (
14111:             usercreation     => \%save_usercreate,
14112:             usermodification => \%save_usermodify,
14113:             inststatus       => \%save_inststatus,
14114:     );
14115: 
14116:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
14117:                                              $dom);
14118: #
14119: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
14120: #
14121:     if ($putresult eq 'ok') {
14122:         if (keys(%changes) > 0) {
14123:             $resulttext = &mt('Changes made:').'<ul>';
14124:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
14125:                 my %lt = &selfcreation_types();
14126:                 foreach my $type (@{$changes{'cancreate'}}) {
14127:                     my $chgtext = '';
14128:                     if ($type eq 'selfcreate') {
14129:                         if (@{$cancreate{$type}} == 0) {
14130:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
14131:                         } else {
14132:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
14133:                                         '<ul>';
14134:                             foreach my $case (@{$cancreate{$type}}) {
14135:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
14136:                             }
14137:                             $chgtext .= '</ul>';
14138:                             if (ref($cancreate{$type}) eq 'ARRAY') {
14139:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
14140:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
14141:                                         if (@{$cancreate{'statustocreate'}} == 0) {
14142:                                             $chgtext .= '<span class="LC_warning">'.
14143:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
14144:                                                         '</span><br />';
14145:                                         }
14146:                                     }
14147:                                 }
14148:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
14149:                                     if (!@statuses) {
14150:                                         $chgtext .= '<span class="LC_warning">'.
14151:                                                     &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.").
14152:                                                     '</span><br />';
14153: 
14154:                                     }
14155:                                 }
14156:                             }
14157:                         }
14158:                     } elsif ($type eq 'shibenv') {
14159:                         if (keys(%{$cancreate{$type}}) == 0) {
14160:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
14161:                         } else {
14162:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
14163:                                         '<ul>';
14164:                             foreach my $field (@shibfields) {
14165:                                 next if ($cancreate{$type}{$field} eq '');
14166:                                 if ($field eq 'inststatus') {
14167:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
14168:                                 } else {
14169:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
14170:                                 }
14171:                             }
14172:                             $chgtext .= '</ul>';
14173:                         }
14174:                     } elsif ($type eq 'statustocreate') {
14175:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
14176:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
14177:                             if (@{$cancreate{'selfcreate'}} > 0) {
14178:                                 if (@{$cancreate{'statustocreate'}} == 0) {
14179:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
14180:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
14181:                                         $chgtext .= '<br />'.
14182:                                                     '<span class="LC_warning">'.
14183:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
14184:                                                     '</span>';
14185:                                     }
14186:                                 } elsif (keys(%usertypes) > 0) {
14187:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
14188:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
14189:                                     } else {
14190:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
14191:                                     }
14192:                                     $chgtext .= '<ul>';
14193:                                     foreach my $case (@{$cancreate{$type}}) {
14194:                                         if ($case eq 'default') {
14195:                                             $chgtext .= '<li>'.$othertitle.'</li>';
14196:                                         } else {
14197:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
14198:                                         }
14199:                                     }
14200:                                     $chgtext .= '</ul>';
14201:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
14202:                                         $chgtext .= '<span class="LC_warning">'.
14203:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
14204:                                                     '</span>';
14205:                                     }
14206:                                 }
14207:                             } else {
14208:                                 if (@{$cancreate{$type}} == 0) {
14209:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
14210:                                 } else {
14211:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
14212:                                 }
14213:                             }
14214:                             $chgtext .= '<br />';
14215:                         }
14216:                     } elsif ($type eq 'selfcreateprocessing') {
14217:                         my %choices = &Apache::lonlocal::texthash (
14218:                                                                     automatic => 'Automatic approval',
14219:                                                                     approval  => 'Queued for approval',
14220:                                                                   );
14221:                         if (@types) {
14222:                             if (@statuses) {
14223:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
14224:                                             '<ul>';
14225:                                 foreach my $status (@statuses) {
14226:                                     if ($status eq 'default') {
14227:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
14228:                                     } else {
14229:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
14230:                                     }
14231:                                 }
14232:                                 $chgtext .= '</ul>';
14233:                             }
14234:                         } else {
14235:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
14236:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
14237:                         }
14238:                     } elsif ($type eq 'emailverified') {
14239:                         my %options = &Apache::lonlocal::texthash (
14240:                                                                     all   => 'Same as e-mail',
14241:                                                                     first => 'Omit @domain',
14242:                                                                     free  => 'Free to choose',
14243:                                                                   );
14244:                         if (@types) {
14245:                             if (@statuses) {
14246:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
14247:                                             '<ul>';
14248:                                 foreach my $status (@statuses) {
14249:                                     if ($type eq 'default') {
14250:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14251:                                     } else {
14252:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14253:                                     }
14254:                                 }
14255:                                 $chgtext .= '</ul>';
14256:                             }
14257:                         } else {
14258:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
14259:                                             $options{$cancreate{'emailverified'}{'default'}});
14260:                         }
14261:                     } elsif ($type eq 'emailoptions') {
14262:                         my %options = &Apache::lonlocal::texthash (
14263:                                                                     any     => 'Any e-mail',
14264:                                                                     inst    => 'Institutional only',
14265:                                                                     noninst => 'Non-institutional only',
14266:                                                                     custom  => 'Custom restrictions',
14267:                                                                   );
14268:                         if (@types) {
14269:                             if (@statuses) {
14270:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
14271:                                             '<ul>';
14272:                                 foreach my $status (@statuses) {
14273:                                     if ($type eq 'default') {
14274:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14275:                                     } else {
14276:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14277:                                     }
14278:                                 }
14279:                                 $chgtext .= '</ul>';
14280:                             }
14281:                         } else {
14282:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
14283:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
14284:                             } else {
14285:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
14286:                                                 $options{$cancreate{'emailoptions'}{'default'}});
14287:                             }
14288:                         }
14289:                     } elsif ($type eq 'emaildomain') {
14290:                         my $output;
14291:                         if (@statuses) {
14292:                             foreach my $type (@statuses) {
14293:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
14294:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
14295:                                         if ($type eq 'default') {
14296:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14297:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14298:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14299:                                             } else {
14300:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
14301:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
14302:                                             }
14303:                                         } else {
14304:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14305:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14306:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14307:                                             } else {
14308:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
14309:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
14310:                                             }
14311:                                         }
14312:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
14313:                                         if ($type eq 'default') {
14314:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14315:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14316:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14317:                                             } else {
14318:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
14319:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
14320:                                             }
14321:                                         } else {
14322:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14323:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14324:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14325:                                             } else {
14326:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
14327:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
14328:                                             }
14329:                                         }
14330:                                     }
14331:                                 }
14332:                             }
14333:                         }
14334:                         if ($output ne '') {
14335:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
14336:                                         '<ul>'.$output.'</ul>';
14337:                         }
14338:                     } elsif ($type eq 'captcha') {
14339:                         if ($savecaptcha{$type} eq 'notused') {
14340:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
14341:                         } else {
14342:                             my %captchas = &captcha_phrases();
14343:                             if ($captchas{$savecaptcha{$type}}) {
14344:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
14345:                             } else {
14346:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
14347:                             }
14348:                         }
14349:                     } elsif ($type eq 'recaptchakeys') {
14350:                         my ($privkey,$pubkey);
14351:                         if (ref($savecaptcha{$type}) eq 'HASH') {
14352:                             $pubkey = $savecaptcha{$type}{'public'};
14353:                             $privkey = $savecaptcha{$type}{'private'};
14354:                         }
14355:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
14356:                         if (!$pubkey) {
14357:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
14358:                         } else {
14359:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
14360:                         }
14361:                         if (!$privkey) {
14362:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
14363:                         } else {
14364:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
14365:                         }
14366:                         $chgtext .= '</ul>';
14367:                     } elsif ($type eq 'recaptchaversion') {
14368:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
14369:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
14370:                         }
14371:                     } elsif ($type eq 'emailusername') {
14372:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
14373:                             if (@statuses) {
14374:                                 foreach my $type (@statuses) {
14375:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
14376:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
14377:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
14378:                                                     '<ul>';
14379:                                             foreach my $field (@{$infofields}) {
14380:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
14381:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
14382:                                                 }
14383:                                             }
14384:                                             $chgtext .= '</ul>';
14385:                                         } else {
14386:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14387:                                         }
14388:                                     } else {
14389:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14390:                                     }
14391:                                 }
14392:                             }
14393:                         }
14394:                     } elsif ($type eq 'notify') {
14395:                         my $numapprove = 0;
14396:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
14397:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
14398:                                 if ($cancreate{'notify'}{'approval'}) {
14399:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
14400:                                     $numapprove ++;
14401:                                 }
14402:                             }
14403:                         }
14404:                         unless ($numapprove) {
14405:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
14406:                         }
14407:                     }
14408:                     if ($chgtext) {
14409:                         $resulttext .= '<li>'.$chgtext.'</li>';
14410:                     }
14411:                 }
14412:             }
14413:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
14414:                 my ($emailrules,$emailruleorder) =
14415:                     &Apache::lonnet::inst_userrules($dom,'email');
14416:                 foreach my $type (@{$changes{'email_rule'}}) {
14417:                     if (ref($email_rule{$type}) eq 'ARRAY') {
14418:                         my $chgtext = '<ul>';
14419:                         foreach my $rule (@{$email_rule{$type}}) {
14420:                             if (ref($emailrules->{$rule}) eq 'HASH') {
14421:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
14422:                             }
14423:                         }
14424:                         $chgtext .= '</ul>';
14425:                         my $typename;
14426:                         if (@types) {
14427:                             if ($type eq 'default') {
14428:                                 $typename = $othertitle;
14429:                             } else {
14430:                                 $typename = $usertypes{$type};
14431:                             } 
14432:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
14433:                         }
14434:                         if (@{$email_rule{$type}} > 0) {
14435:                             $resulttext .= '<li>'.
14436:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
14437:                                                $usertypes{$type}).
14438:                                            $chgtext.
14439:                                            '</li>';
14440:                         } else {
14441:                             $resulttext .= '<li>'.
14442:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
14443:                                            '</li>'.
14444:                                            &mt('(Affiliation: [_1])',$typename);
14445:                         }
14446:                     }
14447:                 }
14448:             }
14449:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
14450:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
14451:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
14452:                         my $chgtext = '<ul>';
14453:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
14454:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
14455:                         }
14456:                         $chgtext .= '</ul>';
14457:                         $resulttext .= '<li>'.
14458:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
14459:                                           $chgtext.
14460:                                        '</li>';
14461:                     } else {
14462:                         $resulttext .= '<li>'.
14463:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
14464:                                        '</li>';
14465:                     }
14466:                 }
14467:             }
14468:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
14469:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
14470:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14471:                 foreach my $type (@{$changes{'selfcreate'}}) {
14472:                     my $typename = $type;
14473:                     if (keys(%usertypes) > 0) {
14474:                         if ($usertypes{$type} ne '') {
14475:                             $typename = $usertypes{$type};
14476:                         }
14477:                     }
14478:                     my @modifiable;
14479:                     $resulttext .= '<li>'.
14480:                                     &mt('Self-creation of account by users with status: [_1]',
14481:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
14482:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
14483:                     foreach my $field (@fields) {
14484:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
14485:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
14486:                         }
14487:                     }
14488:                     if (@modifiable > 0) {
14489:                         $resulttext .= join(', ',@modifiable);
14490:                     } else {
14491:                         $resulttext .= &mt('none');
14492:                     }
14493:                     $resulttext .= '</li>';
14494:                 }
14495:                 $resulttext .= '</ul></li>';
14496:             }
14497:             $resulttext .= '</ul>';
14498:             my $cachetime = 24*60*60;
14499:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
14500:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14501:             if (ref($lastactref) eq 'HASH') {
14502:                 $lastactref->{'domdefaults'} = 1;
14503:             }
14504:         } else {
14505:             $resulttext = &mt('No changes made to self-creation settings');
14506:         }
14507:     } else {
14508:         $resulttext = '<span class="LC_error">'.
14509:             &mt('An error occurred: [_1]',$putresult).'</span>';
14510:     }
14511:     if ($warningmsg ne '') {
14512:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
14513:     }
14514:     return $resulttext;
14515: }
14516: 
14517: sub process_captcha {
14518:     my ($container,$changes,$newsettings,$current) = @_;
14519:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
14520:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
14521:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
14522:         $newsettings->{'captcha'} = 'original';
14523:     }
14524:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
14525:         if ($container eq 'cancreate') {
14526:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14527:                 push(@{$changes->{'cancreate'}},'captcha');
14528:             } elsif (!defined($changes->{'cancreate'})) {
14529:                 $changes->{'cancreate'} = ['captcha'];
14530:             }
14531:         } else {
14532:             $changes->{'captcha'} = 1;
14533:         }
14534:     }
14535:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
14536:     if ($newsettings->{'captcha'} eq 'recaptcha') {
14537:         $newpub = $env{'form.'.$container.'_recaptchapub'};
14538:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
14539:         $newpub =~ s/[^\w\-]//g;
14540:         $newpriv =~ s/[^\w\-]//g;
14541:         $newsettings->{'recaptchakeys'} = {
14542:                                              public  => $newpub,
14543:                                              private => $newpriv,
14544:                                           };
14545:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
14546:         $newversion =~ s/\D//g;
14547:         if ($newversion ne '2') {
14548:             $newversion = 1;
14549:         }
14550:         $newsettings->{'recaptchaversion'} = $newversion;
14551:     }
14552:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
14553:         $currpub = $current->{'recaptchakeys'}{'public'};
14554:         $currpriv = $current->{'recaptchakeys'}{'private'};
14555:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
14556:             $newsettings->{'recaptchakeys'} = {
14557:                                                  public  => '',
14558:                                                  private => '',
14559:                                               }
14560:         }
14561:     }
14562:     if ($current->{'captcha'} eq 'recaptcha') {
14563:         $currversion = $current->{'recaptchaversion'};
14564:         if ($currversion ne '2') {
14565:             $currversion = 1;
14566:         }
14567:     }
14568:     if ($currversion ne $newversion) {
14569:         if ($container eq 'cancreate') {
14570:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14571:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
14572:             } elsif (!defined($changes->{'cancreate'})) {
14573:                 $changes->{'cancreate'} = ['recaptchaversion'];
14574:             }
14575:         } else {
14576:             $changes->{'recaptchaversion'} = 1;
14577:         }
14578:     }
14579:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
14580:         if ($container eq 'cancreate') {
14581:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14582:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
14583:             } elsif (!defined($changes->{'cancreate'})) {
14584:                 $changes->{'cancreate'} = ['recaptchakeys'];
14585:             }
14586:         } else {
14587:             $changes->{'recaptchakeys'} = 1;
14588:         }
14589:     }
14590:     return;
14591: }
14592: 
14593: sub modify_usermodification {
14594:     my ($dom,%domconfig) = @_;
14595:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
14596:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
14597:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
14598:             if ($key eq 'selfcreate') {
14599:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
14600:             } else {  
14601:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
14602:             }
14603:         }
14604:     }
14605:     my @contexts = ('author','course');
14606:     my %context_title = (
14607:                            author => 'In author context',
14608:                            course => 'In course context',
14609:                         );
14610:     my @fields = ('lastname','firstname','middlename','generation',
14611:                   'permanentemail','id');
14612:     my %roles = (
14613:                   author => ['ca','aa'],
14614:                   course => ['st','ep','ta','in','cr'],
14615:                 );
14616:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14617:     foreach my $context (@contexts) {
14618:         foreach my $role (@{$roles{$context}}) {
14619:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
14620:             foreach my $item (@fields) {
14621:                 if (grep(/^\Q$item\E$/,@modifiable)) {
14622:                     $modifyhash{$context}{$role}{$item} = 1;
14623:                 } else {
14624:                     $modifyhash{$context}{$role}{$item} = 0;
14625:                 }
14626:             }
14627:         }
14628:         if (ref($curr_usermodification{$context}) eq 'HASH') {
14629:             foreach my $role (@{$roles{$context}}) {
14630:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
14631:                     foreach my $field (@fields) {
14632:                         if ($modifyhash{$context}{$role}{$field} ne 
14633:                                 $curr_usermodification{$context}{$role}{$field}) {
14634:                             push(@{$changes{$context}},$role);
14635:                             last;
14636:                         }
14637:                     }
14638:                 }
14639:             }
14640:         } else {
14641:             foreach my $context (@contexts) {
14642:                 foreach my $role (@{$roles{$context}}) {
14643:                     push(@{$changes{$context}},$role);
14644:                 }
14645:             }
14646:         }
14647:     }
14648:     my %usermodification_hash =  (
14649:                                    usermodification => \%modifyhash,
14650:                                  );
14651:     my $putresult = &Apache::lonnet::put_dom('configuration',
14652:                                              \%usermodification_hash,$dom);
14653:     if ($putresult eq 'ok') {
14654:         if (keys(%changes) > 0) {
14655:             $resulttext = &mt('Changes made: ').'<ul>';
14656:             foreach my $context (@contexts) {
14657:                 if (ref($changes{$context}) eq 'ARRAY') {
14658:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
14659:                     if (ref($changes{$context}) eq 'ARRAY') {
14660:                         foreach my $role (@{$changes{$context}}) {
14661:                             my $rolename;
14662:                             if ($role eq 'cr') {
14663:                                 $rolename = &mt('Custom');
14664:                             } else {
14665:                                 $rolename = &Apache::lonnet::plaintext($role);
14666:                             }
14667:                             my @modifiable;
14668:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
14669:                             foreach my $field (@fields) {
14670:                                 if ($modifyhash{$context}{$role}{$field}) {
14671:                                     push(@modifiable,$fieldtitles{$field});
14672:                                 }
14673:                             }
14674:                             if (@modifiable > 0) {
14675:                                 $resulttext .= join(', ',@modifiable);
14676:                             } else {
14677:                                 $resulttext .= &mt('none'); 
14678:                             }
14679:                             $resulttext .= '</li>';
14680:                         }
14681:                         $resulttext .= '</ul></li>';
14682:                     }
14683:                 }
14684:             }
14685:             $resulttext .= '</ul>';
14686:         } else {
14687:             $resulttext = &mt('No changes made to user modification settings');
14688:         }
14689:     } else {
14690:         $resulttext = '<span class="LC_error">'.
14691:             &mt('An error occurred: [_1]',$putresult).'</span>';
14692:     }
14693:     return $resulttext;
14694: }
14695: 
14696: sub modify_defaults {
14697:     my ($dom,$lastactref,%domconfig) = @_;
14698:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
14699:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14700:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
14701:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
14702:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
14703:     foreach my $item (@items) {
14704:         $newvalues{$item} = $env{'form.'.$item};
14705:         if ($item eq 'auth_def') {
14706:             if ($newvalues{$item} ne '') {
14707:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
14708:                     push(@errors,$item);
14709:                 }
14710:             }
14711:         } elsif ($item eq 'lang_def') {
14712:             if ($newvalues{$item} ne '') {
14713:                 if ($newvalues{$item} =~ /^(\w+)/) {
14714:                     my $langcode = $1;
14715:                     if ($langcode ne 'x_chef') {
14716:                         if (code2language($langcode) eq '') {
14717:                             push(@errors,$item);
14718:                         }
14719:                     }
14720:                 } else {
14721:                     push(@errors,$item);
14722:                 }
14723:             }
14724:         } elsif ($item eq 'timezone_def') {
14725:             if ($newvalues{$item} ne '') {
14726:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
14727:                     push(@errors,$item);   
14728:                 }
14729:             }
14730:         } elsif ($item eq 'datelocale_def') {
14731:             if ($newvalues{$item} ne '') {
14732:                 my @datelocale_ids = DateTime::Locale->ids();
14733:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
14734:                     push(@errors,$item);
14735:                 }
14736:             }
14737:         } elsif ($item eq 'portal_def') {
14738:             if ($newvalues{$item} ne '') {
14739:                 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])\/?$/) {
14740:                     push(@errors,$item);
14741:                 }
14742:             }
14743:         } elsif ($item eq 'intauth_cost') {
14744:             if ($newvalues{$item} ne '') {
14745:                 if ($newvalues{$item} =~ /\D/) {
14746:                     push(@errors,$item);
14747:                 }
14748:             }
14749:         } elsif ($item eq 'intauth_check') {
14750:             if ($newvalues{$item} ne '') {
14751:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14752:                     push(@errors,$item);
14753:                 }
14754:             }
14755:         } elsif ($item eq 'intauth_switch') {
14756:             if ($newvalues{$item} ne '') {
14757:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14758:                     push(@errors,$item);
14759:                 }
14760:             }
14761:         }
14762:         if (grep(/^\Q$item\E$/,@errors)) {
14763:             $newvalues{$item} = $domdefaults{$item};
14764:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
14765:             $changes{$item} = 1;
14766:         }
14767:         $domdefaults{$item} = $newvalues{$item};
14768:     }
14769:     my %defaults_hash = (
14770:                          defaults => \%newvalues,
14771:                         );
14772:     my $title = &defaults_titles();
14773: 
14774:     my $currinststatus;
14775:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
14776:         $currinststatus = $domconfig{'inststatus'};
14777:     } else {
14778:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14779:         $currinststatus = {
14780:                              inststatustypes => $usertypes,
14781:                              inststatusorder => $types,
14782:                              inststatusguest => [],
14783:                           };
14784:     }
14785:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
14786:     my @allpos;
14787:     my %alltypes;
14788:     my @inststatusguest;
14789:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
14790:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
14791:             unless (grep(/^\Q$type\E$/,@todelete)) {
14792:                 push(@inststatusguest,$type);
14793:             }
14794:         }
14795:     }
14796:     my ($currtitles,$currorder);
14797:     if (ref($currinststatus) eq 'HASH') {
14798:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
14799:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
14800:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
14801:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
14802:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
14803:                     }
14804:                 }
14805:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
14806:                     my $position = $env{'form.inststatus_pos_'.$type};
14807:                     $position =~ s/\D+//g;
14808:                     $allpos[$position] = $type;
14809:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
14810:                     $alltypes{$type} =~ s/`//g;
14811:                 }
14812:             }
14813:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
14814:             $currtitles =~ s/,$//;
14815:         }
14816:     }
14817:     if ($env{'form.addinststatus'}) {
14818:         my $newtype = $env{'form.addinststatus'};
14819:         $newtype =~ s/\W//g;
14820:         unless (exists($alltypes{$newtype})) {
14821:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
14822:             $alltypes{$newtype} =~ s/`//g; 
14823:             my $position = $env{'form.addinststatus_pos'};
14824:             $position =~ s/\D+//g;
14825:             if ($position ne '') {
14826:                 $allpos[$position] = $newtype;
14827:             }
14828:         }
14829:     }
14830:     my @orderedstatus;
14831:     foreach my $type (@allpos) {
14832:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
14833:             push(@orderedstatus,$type);
14834:         }
14835:     }
14836:     foreach my $type (keys(%alltypes)) {
14837:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
14838:             delete($alltypes{$type});
14839:         }
14840:     }
14841:     $defaults_hash{'inststatus'} = {
14842:                                      inststatustypes => \%alltypes,
14843:                                      inststatusorder => \@orderedstatus,
14844:                                      inststatusguest => \@inststatusguest,
14845:                                    };
14846:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
14847:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
14848:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
14849:         }
14850:     }
14851:     if ($currorder ne join(',',@orderedstatus)) {
14852:         $changes{'inststatus'}{'inststatusorder'} = 1;
14853:     }
14854:     my $newtitles;
14855:     foreach my $item (@orderedstatus) {
14856:         $newtitles .= $alltypes{$item}.',';
14857:     }
14858:     $newtitles =~ s/,$//;
14859:     if ($currtitles ne $newtitles) {
14860:         $changes{'inststatus'}{'inststatustypes'} = 1;
14861:     }
14862:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
14863:                                              $dom);
14864:     if ($putresult eq 'ok') {
14865:         if (keys(%changes) > 0) {
14866:             $resulttext = &mt('Changes made:').'<ul>';
14867:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
14868:             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";
14869:             foreach my $item (sort(keys(%changes))) {
14870:                 if ($item eq 'inststatus') {
14871:                     if (ref($changes{'inststatus'}) eq 'HASH') {
14872:                         if (@orderedstatus) {
14873:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
14874:                             foreach my $type (@orderedstatus) { 
14875:                                 $resulttext .= $alltypes{$type}.', ';
14876:                             }
14877:                             $resulttext =~ s/, $//;
14878:                             $resulttext .= '</li>';
14879:                         } else {
14880:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
14881:                         }
14882:                     }
14883:                 } else {
14884:                     my $value = $env{'form.'.$item};
14885:                     if ($value eq '') {
14886:                         $value = &mt('none');
14887:                     } elsif ($item eq 'auth_def') {
14888:                         my %authnames = &authtype_names();
14889:                         my %shortauth = (
14890:                                           internal   => 'int',
14891:                                           krb4       => 'krb4',
14892:                                           krb5       => 'krb5',
14893:                                           localauth  => 'loc',
14894:                                           lti        => 'lti',
14895:                         );
14896:                         $value = $authnames{$shortauth{$value}};
14897:                     } elsif ($item eq 'intauth_switch') {
14898:                         my %optiondesc = &Apache::lonlocal::texthash (
14899:                                             0 => 'No',
14900:                                             1 => 'Yes',
14901:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
14902:                                          );
14903:                         if ($value =~ /^(0|1|2)$/) {
14904:                             $value = $optiondesc{$value};
14905:                         } else {
14906:                             $value = &mt('none -- defaults to No');
14907:                         }
14908:                     } elsif ($item eq 'intauth_check') {
14909:                         my %optiondesc = &Apache::lonlocal::texthash (
14910:                                              0 => 'No',
14911:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
14912:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
14913:                                          );
14914:                         if ($value =~ /^(0|1|2)$/) {
14915:                             $value = $optiondesc{$value};
14916:                         } else {
14917:                             $value = &mt('none -- defaults to No');
14918:                         }
14919:                     }
14920:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
14921:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
14922:                 }
14923:             }
14924:             $resulttext .= '</ul>';
14925:             $mailmsgtext .= "\n";
14926:             my $cachetime = 24*60*60;
14927:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14928:             if (ref($lastactref) eq 'HASH') {
14929:                 $lastactref->{'domdefaults'} = 1;
14930:             }
14931:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
14932:                 my $notify = 1;
14933:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
14934:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
14935:                         $notify = 0;
14936:                     }
14937:                 }
14938:                 if ($notify) {
14939:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
14940:                                                "LON-CAPA Domain Settings Change - $dom",
14941:                                                $mailmsgtext);
14942:                 }
14943:             }
14944:         } else {
14945:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
14946:         }
14947:     } else {
14948:         $resulttext = '<span class="LC_error">'.
14949:             &mt('An error occurred: [_1]',$putresult).'</span>';
14950:     }
14951:     if (@errors > 0) {
14952:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
14953:         foreach my $item (@errors) {
14954:             $resulttext .= ' "'.$title->{$item}.'",';
14955:         }
14956:         $resulttext =~ s/,$//;
14957:     }
14958:     return $resulttext;
14959: }
14960: 
14961: sub modify_scantron {
14962:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14963:     my ($resulttext,%confhash,%changes,$errors);
14964:     my $custom = 'custom.tab';
14965:     my $default = 'default.tab';
14966:     my $servadm = $r->dir_config('lonAdmEMail');
14967:     my ($configuserok,$author_ok,$switchserver) = 
14968:         &config_check($dom,$confname,$servadm);
14969:     if ($env{'form.scantronformat.filename'} ne '') {
14970:         my $error;
14971:         if ($configuserok eq 'ok') {
14972:             if ($switchserver) {
14973:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
14974:             } else {
14975:                 if ($author_ok eq 'ok') {
14976:                     my ($result,$scantronurl) =
14977:                         &publishlogo($r,'upload','scantronformat',$dom,
14978:                                      $confname,'scantron','','',$custom);
14979:                     if ($result eq 'ok') {
14980:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
14981:                         $changes{'scantronformat'} = 1;
14982:                     } else {
14983:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
14984:                     }
14985:                 } else {
14986:                     $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);
14987:                 }
14988:             }
14989:         } else {
14990:             $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);
14991:         }
14992:         if ($error) {
14993:             &Apache::lonnet::logthis($error);
14994:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14995:         }
14996:     }
14997:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14998:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
14999:             if ($env{'form.scantronformat_del'}) {
15000:                 $confhash{'scantron'}{'scantronformat'} = '';
15001:                 $changes{'scantronformat'} = 1;
15002:             }
15003:         }
15004:     }
15005:     if (keys(%confhash) > 0) {
15006:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
15007:                                                  $dom);
15008:         if ($putresult eq 'ok') {
15009:             if (keys(%changes) > 0) {
15010:                 if (ref($confhash{'scantron'}) eq 'HASH') {
15011:                     $resulttext = &mt('Changes made:').'<ul>';
15012:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
15013:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
15014:                     } else {
15015:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
15016:                     }
15017:                     $resulttext .= '</ul>';
15018:                 } else {
15019:                     $resulttext = &mt('Changes made to bubblesheet format file.');
15020:                 }
15021:                 $resulttext .= '</ul>';
15022:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15023:                 if (ref($lastactref) eq 'HASH') {
15024:                     $lastactref->{'domainconfig'} = 1;
15025:                 }
15026:             } else {
15027:                 $resulttext = &mt('No changes made to bubblesheet format file');
15028:             }
15029:         } else {
15030:             $resulttext = '<span class="LC_error">'.
15031:                 &mt('An error occurred: [_1]',$putresult).'</span>';
15032:         }
15033:     } else {
15034:         $resulttext = &mt('No changes made to bubblesheet format file'); 
15035:     }
15036:     if ($errors) {
15037:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15038:                        $errors.'</ul>';
15039:     }
15040:     return $resulttext;
15041: }
15042: 
15043: sub modify_coursecategories {
15044:     my ($dom,$lastactref,%domconfig) = @_;
15045:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
15046:         $cathash);
15047:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
15048:     my @catitems = ('unauth','auth');
15049:     my @cattypes = ('std','domonly','codesrch','none');
15050:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
15051:         $cathash = $domconfig{'coursecategories'}{'cats'};
15052:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
15053:             $changes{'togglecats'} = 1;
15054:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
15055:         }
15056:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
15057:             $changes{'categorize'} = 1;
15058:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
15059:         }
15060:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
15061:             $changes{'togglecatscomm'} = 1;
15062:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
15063:         }
15064:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
15065:             $changes{'categorizecomm'} = 1;
15066:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
15067: 
15068:         }
15069:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
15070:             $changes{'togglecatsplace'} = 1;
15071:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
15072:         }
15073:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
15074:             $changes{'categorizeplace'} = 1;
15075:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
15076:         }
15077:         foreach my $item (@catitems) {
15078:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
15079:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
15080:                     $changes{$item} = 1;
15081:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
15082:                 }
15083:             }
15084:         }
15085:     } else {
15086:         $changes{'togglecats'} = 1;
15087:         $changes{'categorize'} = 1;
15088:         $changes{'togglecatscomm'} = 1;
15089:         $changes{'categorizecomm'} = 1;
15090:         $changes{'togglecatsplace'} = 1;
15091:         $changes{'categorizeplace'} = 1;
15092:         $domconfig{'coursecategories'} = {
15093:                                              togglecats => $env{'form.togglecats'},
15094:                                              categorize => $env{'form.categorize'},
15095:                                              togglecatscomm => $env{'form.togglecatscomm'},
15096:                                              categorizecomm => $env{'form.categorizecomm'},
15097:                                              togglecatsplace => $env{'form.togglecatsplace'},
15098:                                              categorizeplace => $env{'form.categorizeplace'},
15099:                                          };
15100:         foreach my $item (@catitems) {
15101:             if ($env{'form.coursecat_'.$item} ne 'std') {
15102:                 $changes{$item} = 1;
15103:             }
15104:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
15105:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
15106:             }
15107:         }
15108:     }
15109:     if (ref($cathash) eq 'HASH') {
15110:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
15111:             push (@deletecategory,'instcode::0');
15112:         }
15113:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
15114:             push(@deletecategory,'communities::0');
15115:         }
15116:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
15117:             push(@deletecategory,'placement::0');
15118:         }
15119:     }
15120:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
15121:     if (ref($cathash) eq 'HASH') {
15122:         if (@deletecategory > 0) {
15123:             #FIXME Need to remove category from all courses using a deleted category 
15124:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
15125:             foreach my $item (@deletecategory) {
15126:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
15127:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
15128:                     $deletions{$item} = 1;
15129:                     &recurse_cat_deletes($item,$cathash,\%deletions);
15130:                 }
15131:             }
15132:         }
15133:         foreach my $item (keys(%{$cathash})) {
15134:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15135:             if ($cathash->{$item} ne $env{'form.'.$item}) {
15136:                 $reorderings{$item} = 1;
15137:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
15138:             }
15139:             if ($env{'form.addcategory_name_'.$item} ne '') {
15140:                 my $newcat = $env{'form.addcategory_name_'.$item};
15141:                 my $newdepth = $depth+1;
15142:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
15143:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
15144:                 $adds{$newitem} = 1; 
15145:             }
15146:             if ($env{'form.subcat_'.$item} ne '') {
15147:                 my $newcat = $env{'form.subcat_'.$item};
15148:                 my $newdepth = $depth+1;
15149:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
15150:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
15151:                 $adds{$newitem} = 1;
15152:             }
15153:         }
15154:     }
15155:     if ($env{'form.instcode'} eq '1') {
15156:         if (ref($cathash) eq 'HASH') {
15157:             my $newitem = 'instcode::0';
15158:             if ($cathash->{$newitem} eq '') {  
15159:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
15160:                 $adds{$newitem} = 1;
15161:             }
15162:         } else {
15163:             my $newitem = 'instcode::0';
15164:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
15165:             $adds{$newitem} = 1;
15166:         }
15167:     }
15168:     if ($env{'form.communities'} eq '1') {
15169:         if (ref($cathash) eq 'HASH') {
15170:             my $newitem = 'communities::0';
15171:             if ($cathash->{$newitem} eq '') {
15172:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
15173:                 $adds{$newitem} = 1;
15174:             }
15175:         } else {
15176:             my $newitem = 'communities::0';
15177:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
15178:             $adds{$newitem} = 1;
15179:         }
15180:     }
15181:     if ($env{'form.placement'} eq '1') {
15182:         if (ref($cathash) eq 'HASH') {
15183:             my $newitem = 'placement::0';
15184:             if ($cathash->{$newitem} eq '') {
15185:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
15186:                 $adds{$newitem} = 1;
15187:             }
15188:         } else {
15189:             my $newitem = 'placement::0';
15190:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
15191:             $adds{$newitem} = 1;
15192:         }
15193:     }
15194:     if ($env{'form.addcategory_name'} ne '') {
15195:         if (($env{'form.addcategory_name'} ne 'instcode') &&
15196:             ($env{'form.addcategory_name'} ne 'communities') &&
15197:             ($env{'form.addcategory_name'} ne 'placement')) {
15198:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
15199:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
15200:             $adds{$newitem} = 1;
15201:         }
15202:     }
15203:     my $putresult;
15204:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15205:         if (keys(%deletions) > 0) {
15206:             foreach my $key (keys(%deletions)) {
15207:                 if ($predelallitems{$key} ne '') {
15208:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
15209:                 }
15210:             }
15211:         }
15212:         my (@chkcats,@chktrails,%chkallitems);
15213:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
15214:         if (ref($chkcats[0]) eq 'ARRAY') {
15215:             my $depth = 0;
15216:             my $chg = 0;
15217:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
15218:                 my $name = $chkcats[0][$i];
15219:                 my $item;
15220:                 if ($name eq '') {
15221:                     $chg ++;
15222:                 } else {
15223:                     $item = &escape($name).'::0';
15224:                     if ($chg) {
15225:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
15226:                     }
15227:                     $depth ++; 
15228:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
15229:                     $depth --;
15230:                 }
15231:             }
15232:         }
15233:     }
15234:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15235:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
15236:         if ($putresult eq 'ok') {
15237:             my %title = (
15238:                          togglecats     => 'Show/Hide a course in catalog',
15239:                          categorize     => 'Assign a category to a course',
15240:                          togglecatscomm => 'Show/Hide a community in catalog',
15241:                          categorizecomm => 'Assign a category to a community',
15242:                         );
15243:             my %level = (
15244:                          dom  => 'set in Domain ("Modify Course/Community")',
15245:                          crs  => 'set in Course ("Course Configuration")',
15246:                          comm => 'set in Community ("Community Configuration")',
15247:                          none     => 'No catalog',
15248:                          std      => 'Standard catalog',
15249:                          domonly  => 'Domain-only catalog',
15250:                          codesrch => 'Code search form',
15251:                         );
15252:             $resulttext = &mt('Changes made:').'<ul>';
15253:             if ($changes{'togglecats'}) {
15254:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
15255:             }
15256:             if ($changes{'categorize'}) {
15257:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
15258:             }
15259:             if ($changes{'togglecatscomm'}) {
15260:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
15261:             }
15262:             if ($changes{'categorizecomm'}) {
15263:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
15264:             }
15265:             if ($changes{'unauth'}) {
15266:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
15267:             }
15268:             if ($changes{'auth'}) {
15269:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
15270:             }
15271:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15272:                 my $cathash;
15273:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
15274:                     $cathash = $domconfig{'coursecategories'}{'cats'};
15275:                 } else {
15276:                     $cathash = {};
15277:                 } 
15278:                 my (@cats,@trails,%allitems);
15279:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
15280:                 if (keys(%deletions) > 0) {
15281:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
15282:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
15283:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
15284:                     }
15285:                     $resulttext .= '</ul></li>';
15286:                 }
15287:                 if (keys(%reorderings) > 0) {
15288:                     my %sort_by_trail;
15289:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
15290:                     foreach my $key (keys(%reorderings)) {
15291:                         if ($allitems{$key} ne '') {
15292:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15293:                         }
15294:                     }
15295:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15296:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15297:                     }
15298:                     $resulttext .= '</ul></li>';
15299:                 }
15300:                 if (keys(%adds) > 0) {
15301:                     my %sort_by_trail;
15302:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
15303:                     foreach my $key (keys(%adds)) {
15304:                         if ($allitems{$key} ne '') {
15305:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15306:                         }
15307:                     }
15308:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15309:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15310:                     }
15311:                     $resulttext .= '</ul></li>';
15312:                 }
15313:             }
15314:             $resulttext .= '</ul>';
15315:             if ($changes{'unauth'} || $changes{'auth'}) {
15316:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15317:                 if ($changes{'auth'}) {
15318:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
15319:                 }
15320:                 if ($changes{'unauth'}) {
15321:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
15322:                 }
15323:                 my $cachetime = 24*60*60;
15324:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15325:                 if (ref($lastactref) eq 'HASH') {
15326:                     $lastactref->{'domdefaults'} = 1;
15327:                 }
15328:             }
15329:         } else {
15330:             $resulttext = '<span class="LC_error">'.
15331:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15332:         }
15333:     } else {
15334:         $resulttext = &mt('No changes made to course and community categories');
15335:     }
15336:     return $resulttext;
15337: }
15338: 
15339: sub modify_serverstatuses {
15340:     my ($dom,%domconfig) = @_;
15341:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
15342:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
15343:         %currserverstatus = %{$domconfig{'serverstatuses'}};
15344:     }
15345:     my @pages = &serverstatus_pages();
15346:     foreach my $type (@pages) {
15347:         $newserverstatus{$type}{'namedusers'} = '';
15348:         $newserverstatus{$type}{'machines'} = '';
15349:         if (defined($env{'form.'.$type.'_namedusers'})) {
15350:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
15351:             my @okusers;
15352:             foreach my $user (@users) {
15353:                 my ($uname,$udom) = split(/:/,$user);
15354:                 if (($udom =~ /^$match_domain$/) &&   
15355:                     (&Apache::lonnet::domain($udom)) &&
15356:                     ($uname =~ /^$match_username$/)) {
15357:                     if (!grep(/^\Q$user\E/,@okusers)) {
15358:                         push(@okusers,$user);
15359:                     }
15360:                 }
15361:             }
15362:             if (@okusers > 0) {
15363:                  @okusers = sort(@okusers);
15364:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
15365:             }
15366:         }
15367:         if (defined($env{'form.'.$type.'_machines'})) {
15368:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
15369:             my @okmachines;
15370:             foreach my $ip (@machines) {
15371:                 my @parts = split(/\./,$ip);
15372:                 next if (@parts < 4);
15373:                 my $badip = 0;
15374:                 for (my $i=0; $i<4; $i++) {
15375:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
15376:                         $badip = 1;
15377:                         last;
15378:                     }
15379:                 }
15380:                 if (!$badip) {
15381:                     push(@okmachines,$ip);     
15382:                 }
15383:             }
15384:             @okmachines = sort(@okmachines);
15385:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
15386:         }
15387:     }
15388:     my %serverstatushash =  (
15389:                                 serverstatuses => \%newserverstatus,
15390:                             );
15391:     foreach my $type (@pages) {
15392:         foreach my $setting ('namedusers','machines') {
15393:             my (@current,@new);
15394:             if (ref($currserverstatus{$type}) eq 'HASH') {
15395:                 if ($currserverstatus{$type}{$setting} ne '') { 
15396:                     @current = split(/,/,$currserverstatus{$type}{$setting});
15397:                 }
15398:             }
15399:             if ($newserverstatus{$type}{$setting} ne '') {
15400:                 @new = split(/,/,$newserverstatus{$type}{$setting});
15401:             }
15402:             if (@current > 0) {
15403:                 if (@new > 0) {
15404:                     foreach my $item (@current) {
15405:                         if (!grep(/^\Q$item\E$/,@new)) {
15406:                             $changes{$type}{$setting} = 1;
15407:                             last;
15408:                         }
15409:                     }
15410:                     foreach my $item (@new) {
15411:                         if (!grep(/^\Q$item\E$/,@current)) {
15412:                             $changes{$type}{$setting} = 1;
15413:                             last;
15414:                         }
15415:                     }
15416:                 } else {
15417:                     $changes{$type}{$setting} = 1;
15418:                 }
15419:             } elsif (@new > 0) {
15420:                 $changes{$type}{$setting} = 1;
15421:             }
15422:         }
15423:     }
15424:     if (keys(%changes) > 0) {
15425:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
15426:         my $putresult = &Apache::lonnet::put_dom('configuration',
15427:                                                  \%serverstatushash,$dom);
15428:         if ($putresult eq 'ok') {
15429:             $resulttext .= &mt('Changes made:').'<ul>';
15430:             foreach my $type (@pages) {
15431:                 if (ref($changes{$type}) eq 'HASH') {
15432:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
15433:                     if ($changes{$type}{'namedusers'}) {
15434:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
15435:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
15436:                         } else {
15437:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
15438:                         }
15439:                     }
15440:                     if ($changes{$type}{'machines'}) {
15441:                         if ($newserverstatus{$type}{'machines'} eq '') {
15442:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
15443:                         } else {
15444:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
15445:                         }
15446: 
15447:                     }
15448:                     $resulttext .= '</ul></li>';
15449:                 }
15450:             }
15451:             $resulttext .= '</ul>';
15452:         } else {
15453:             $resulttext = '<span class="LC_error">'.
15454:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
15455: 
15456:         }
15457:     } else {
15458:         $resulttext = &mt('No changes made to access to server status pages');
15459:     }
15460:     return $resulttext;
15461: }
15462: 
15463: sub modify_helpsettings {
15464:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15465:     my ($resulttext,$errors,%changes,%helphash);
15466:     my %defaultchecked = ('submitbugs' => 'on');
15467:     my @offon = ('off','on');
15468:     my @toggles = ('submitbugs');
15469:     my %current = ('submitbugs' => '',
15470:                    'adhoc'      => {},
15471:                   );
15472:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
15473:         %current = %{$domconfig{'helpsettings'}};
15474:     }
15475:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15476:     foreach my $item (@toggles) {
15477:         if ($defaultchecked{$item} eq 'on') { 
15478:             if ($current{$item} eq '') {
15479:                 if ($env{'form.'.$item} eq '0') {
15480:                     $changes{$item} = 1;
15481:                 }
15482:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15483:                 $changes{$item} = 1;
15484:             }
15485:         } elsif ($defaultchecked{$item} eq 'off') {
15486:             if ($current{$item} eq '') {
15487:                 if ($env{'form.'.$item} eq '1') {
15488:                     $changes{$item} = 1;
15489:                 }
15490:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15491:                 $changes{$item} = 1;
15492:             }
15493:         }
15494:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
15495:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
15496:         }
15497:     }
15498:     my $maxnum = $env{'form.helproles_maxnum'};
15499:     my $confname = $dom.'-domainconfig';
15500:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
15501:     my (@allpos,%newsettings,%changedprivs,$newrole);
15502:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15503:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
15504:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
15505:     my %lt = &Apache::lonlocal::texthash(
15506:                     s      => 'system',
15507:                     d      => 'domain',
15508:                     order  => 'Display order',
15509:                     access => 'Role usage',
15510:                     all    => 'All with domain helpdesk or helpdesk assistant role',
15511:                     dh     => 'All with domain helpdesk role',
15512:                     da     => 'All with domain helpdesk assistant role',
15513:                     none   => 'None',
15514:                     status => 'Determined based on institutional status',
15515:                     inc    => 'Include all, but exclude specific personnel',
15516:                     exc    => 'Exclude all, but include specific personnel',
15517:     );
15518:     for (my $num=0; $num<=$maxnum; $num++) {
15519:         my ($prefix,$identifier,$rolename,%curr);
15520:         if ($num == $maxnum) {
15521:             next unless ($env{'form.newcusthelp'} == $maxnum);
15522:             $identifier = 'custhelp'.$num;
15523:             $prefix = 'helproles_'.$num;
15524:             $rolename = $env{'form.custhelpname'.$num};
15525:             $rolename=~s/[^A-Za-z0-9]//gs;
15526:             next if ($rolename eq '');
15527:             next if (exists($existing{'rolesdef_'.$rolename}));
15528:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15529:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15530:                                                      $newprivs{'c'},$confname,$dom);
15531:             if ($result ne 'ok') {
15532:                 $errors .= '<li><span class="LC_error">'.
15533:                            &mt('An error occurred storing the new custom role: [_1]',
15534:                            $result).'</span></li>';
15535:                 next;
15536:             } else {
15537:                 $changedprivs{$rolename} = \%newprivs;
15538:                 $newrole = $rolename;
15539:             }
15540:         } else {
15541:             $prefix = 'helproles_'.$num;
15542:             $rolename = $env{'form.'.$prefix};
15543:             next if ($rolename eq '');
15544:             next unless (exists($existing{'rolesdef_'.$rolename}));
15545:             $identifier = 'custhelp'.$num;
15546:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15547:             my %currprivs;
15548:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
15549:                 split(/\_/,$existing{'rolesdef_'.$rolename});
15550:             foreach my $level ('c','d','s') {
15551:                 if ($newprivs{$level} ne $currprivs{$level}) {
15552:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15553:                                                              $newprivs{'c'},$confname,$dom);
15554:                     if ($result ne 'ok') {
15555:                         $errors .= '<li><span class="LC_error">'.
15556:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
15557:                                        $rolename,$result).'</span></li>';
15558:                     } else {
15559:                         $changedprivs{$rolename} = \%newprivs;
15560:                     }
15561:                     last;
15562:                 }
15563:             }
15564:             if (ref($current{'adhoc'}) eq 'HASH') {
15565:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15566:                     %curr = %{$current{'adhoc'}{$rolename}};
15567:                 }
15568:             }
15569:         }
15570:         my $newpos = $env{'form.'.$prefix.'_pos'};
15571:         $newpos =~ s/\D+//g;
15572:         $allpos[$newpos] = $rolename;
15573:         my $newdesc = $env{'form.'.$prefix.'_desc'};
15574:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
15575:         if ($curr{'desc'}) {
15576:             if ($curr{'desc'} ne $newdesc) {
15577:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
15578:                 $newsettings{$rolename}{'desc'} = $newdesc;
15579:             }
15580:         } elsif ($newdesc ne '') {
15581:             $changes{'customrole'}{$rolename}{'desc'} = 1;
15582:             $newsettings{$rolename}{'desc'} = $newdesc;
15583:         }
15584:         my $access = $env{'form.'.$prefix.'_access'};
15585:         if (grep(/^\Q$access\E$/,@accesstypes)) {
15586:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
15587:             if ($access eq 'status') {
15588:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
15589:                 if (scalar(@statuses) == 0) {
15590:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
15591:                 } else {
15592:                     my (@shownstatus,$numtypes);
15593:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15594:                     if (ref($types) eq 'ARRAY') {
15595:                         $numtypes = scalar(@{$types});
15596:                         foreach my $type (sort(@statuses)) {
15597:                             if ($type eq 'default') {
15598:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15599:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
15600:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15601:                                 push(@shownstatus,$usertypes->{$type});
15602:                             }
15603:                         }
15604:                     }
15605:                     if (grep(/^default$/,@statuses)) {
15606:                         push(@shownstatus,$othertitle);
15607:                     }
15608:                     if (scalar(@shownstatus) == 1+$numtypes) {
15609:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
15610:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
15611:                     } else {
15612:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
15613:                         if (ref($curr{'status'}) eq 'ARRAY') {
15614:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15615:                             if (@diffs) {
15616:                                 $changes{'customrole'}{$rolename}{$access} = 1;
15617:                             }
15618:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15619:                             $changes{'customrole'}{$rolename}{$access} = 1;
15620:                         }
15621:                     }
15622:                 }
15623:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
15624:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
15625:                 my @newspecstaff;
15626:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15627:                 foreach my $person (sort(@personnel)) {
15628:                     if ($domhelpdesk{$person}) {
15629:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
15630:                     }
15631:                 }
15632:                 if (ref($curr{$access}) eq 'ARRAY') {
15633:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15634:                     if (@diffs) {
15635:                         $changes{'customrole'}{$rolename}{$access} = 1;
15636:                     }
15637:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15638:                     $changes{'customrole'}{$rolename}{$access} = 1;
15639:                 }
15640:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15641:                     my ($uname,$udom) = split(/:/,$person);
15642:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
15643:                 }
15644:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
15645:             }
15646:         } else {
15647:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
15648:         }
15649:         unless ($curr{'access'} eq $access) {
15650:             $changes{'customrole'}{$rolename}{'access'} = 1;
15651:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
15652:         }
15653:     }
15654:     if (@allpos > 0) {
15655:         my $idx = 0;
15656:         foreach my $rolename (@allpos) {
15657:             if ($rolename ne '') {
15658:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
15659:                 if (ref($current{'adhoc'}) eq 'HASH') {
15660:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15661:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
15662:                             $changes{'customrole'}{$rolename}{'order'} = 1;
15663:                             $newsettings{$rolename}{'order'} = $idx+1;
15664:                         }
15665:                     }
15666:                 }
15667:                 $idx ++;
15668:             }
15669:         }
15670:     }
15671:     my $putresult;
15672:     if (keys(%changes) > 0) {
15673:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
15674:         if ($putresult eq 'ok') {
15675:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
15676:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
15677:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
15678:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
15679:                 }
15680:             }
15681:             my $cachetime = 24*60*60;
15682:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15683:             if (ref($lastactref) eq 'HASH') {
15684:                 $lastactref->{'domdefaults'} = 1;
15685:             }
15686:         } else {
15687:             $errors .= '<li><span class="LC_error">'.
15688:                        &mt('An error occurred storing the settings: [_1]',
15689:                            $putresult).'</span></li>';
15690:         }
15691:     }
15692:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
15693:         $resulttext = &mt('Changes made:').'<ul>';
15694:         my (%shownprivs,@levelorder);
15695:         @levelorder = ('c','d','s');
15696:         if ((keys(%changes)) && ($putresult eq 'ok')) {
15697:             foreach my $item (sort(keys(%changes))) {
15698:                 if ($item eq 'submitbugs') {
15699:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
15700:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
15701:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
15702:                 } elsif ($item eq 'customrole') {
15703:                     if (ref($changes{'customrole'}) eq 'HASH') {
15704:                         my @keyorder = ('order','desc','access','status','exc','inc');
15705:                         my %keytext = &Apache::lonlocal::texthash(
15706:                                                                    order  => 'Order',
15707:                                                                    desc   => 'Role description',
15708:                                                                    access => 'Role usage',
15709:                                                                    status => 'Allowed institutional types',
15710:                                                                    exc    => 'Allowed personnel',
15711:                                                                    inc    => 'Disallowed personnel',
15712:                         );
15713:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
15714:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
15715:                                 if ($role eq $newrole) {
15716:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
15717:                                                               $role).'<ul>';
15718:                                 } else {
15719:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15720:                                                               $role).'<ul>';
15721:                                 }
15722:                                 foreach my $key (@keyorder) {
15723:                                     if ($changes{'customrole'}{$role}{$key}) {
15724:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
15725:                                                                   $keytext{$key},$newsettings{$role}{$key}).
15726:                                                        '</li>';
15727:                                     }
15728:                                 }
15729:                                 if (ref($changedprivs{$role}) eq 'HASH') {
15730:                                     $shownprivs{$role} = 1;
15731:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
15732:                                     foreach my $level (@levelorder) {
15733:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15734:                                             next if ($item eq '');
15735:                                             my ($priv) = split(/\&/,$item,2);
15736:                                             if (&Apache::lonnet::plaintext($priv)) {
15737:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15738:                                                 unless ($level eq 'c') {
15739:                                                     $resulttext .= ' ('.$lt{$level}.')';
15740:                                                 }
15741:                                                 $resulttext .= '</li>';
15742:                                             }
15743:                                         }
15744:                                     }
15745:                                     $resulttext .= '</ul>';
15746:                                 }
15747:                                 $resulttext .= '</ul></li>';
15748:                             }
15749:                         }
15750:                     }
15751:                 }
15752:             }
15753:         }
15754:         if (keys(%changedprivs)) {
15755:             foreach my $role (sort(keys(%changedprivs))) {
15756:                 unless ($shownprivs{$role}) {
15757:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15758:                                               $role).'<ul>'.
15759:                                    '<li>'.&mt('Privileges set to :').'<ul>';
15760:                     foreach my $level (@levelorder) {
15761:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15762:                             next if ($item eq '');
15763:                             my ($priv) = split(/\&/,$item,2);
15764:                             if (&Apache::lonnet::plaintext($priv)) {
15765:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15766:                                 unless ($level eq 'c') {
15767:                                     $resulttext .= ' ('.$lt{$level}.')';
15768:                                 }
15769:                                 $resulttext .= '</li>';
15770:                             }
15771:                         }
15772:                     }
15773:                     $resulttext .= '</ul></li></ul></li>';
15774:                 }
15775:             }
15776:         }
15777:         $resulttext .= '</ul>';
15778:     } else {
15779:         $resulttext = &mt('No changes made to help settings');
15780:     }
15781:     if ($errors) {
15782:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
15783:                                     $errors.'</ul>';
15784:     }
15785:     return $resulttext;
15786: }
15787: 
15788: sub modify_coursedefaults {
15789:     my ($dom,$lastactref,%domconfig) = @_;
15790:     my ($resulttext,$errors,%changes,%defaultshash);
15791:     my %defaultchecked = (
15792:                            'canuse_pdfforms' => 'off',
15793:                            'uselcmath'       => 'on',
15794:                            'usejsme'         => 'on'
15795:                          );
15796:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
15797:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
15798:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
15799:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
15800:                    'mysqltables_textbook','mysqltables_placement');
15801:     my @types = ('official','unofficial','community','textbook','placement');
15802:     my %staticdefaults = (
15803:                            anonsurvey_threshold => 10,
15804:                            uploadquota          => 500,
15805:                            postsubmit           => 60,
15806:                            mysqltables          => 172800,
15807:                          );
15808:     my %texoptions = (
15809:                         MathJax  => 'MathJax',
15810:                         mimetex  => &mt('Convert to Images'),
15811:                         tth      => &mt('TeX to HTML'),
15812:                      );
15813:     $defaultshash{'coursedefaults'} = {};
15814: 
15815:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
15816:         if ($domconfig{'coursedefaults'} eq '') {
15817:             $domconfig{'coursedefaults'} = {};
15818:         }
15819:     }
15820: 
15821:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
15822:         foreach my $item (@toggles) {
15823:             if ($defaultchecked{$item} eq 'on') {
15824:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15825:                     ($env{'form.'.$item} eq '0')) {
15826:                     $changes{$item} = 1;
15827:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15828:                     $changes{$item} = 1;
15829:                 }
15830:             } elsif ($defaultchecked{$item} eq 'off') {
15831:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15832:                     ($env{'form.'.$item} eq '1')) {
15833:                     $changes{$item} = 1;
15834:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15835:                     $changes{$item} = 1;
15836:                 }
15837:             }
15838:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
15839:         }
15840:         foreach my $item (@numbers) {
15841:             my ($currdef,$newdef);
15842:             $newdef = $env{'form.'.$item};
15843:             if ($item eq 'anonsurvey_threshold') {
15844:                 $currdef = $domconfig{'coursedefaults'}{$item};
15845:                 $newdef =~ s/\D//g;
15846:                 if ($newdef eq '' || $newdef < 1) {
15847:                     $newdef = 1;
15848:                 }
15849:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
15850:             } else {
15851:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
15852:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
15853:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
15854:                 }
15855:                 $newdef =~ s/[^\w.\-]//g;
15856:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
15857:             }
15858:             if ($currdef ne $newdef) {
15859:                 my $staticdef;
15860:                 if ($item eq 'anonsurvey_threshold') {
15861:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
15862:                         $changes{$item} = 1;
15863:                     }
15864:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
15865:                     my $setting = $1;
15866:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
15867:                         $changes{$setting} = 1;
15868:                     }
15869:                 }
15870:             }
15871:         }
15872:         my $texengine;
15873:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
15874:             $texengine = $env{'form.texengine'};
15875:             if ($defaultshash{'coursedefaults'}{'texengine'} eq '') {
15876:                 unless ($texengine eq 'MathJax') {
15877:                     $changes{'texengine'} = 1;
15878:                 }
15879:             } elsif ($defaultshash{'coursedefaults'}{'texengine'} ne $texengine) {
15880:                 $changes{'texengine'} = 1;
15881:             }
15882:         }
15883:         if ($texengine ne '') {
15884:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
15885:         }
15886:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
15887:         my @currclonecode;
15888:         if (ref($currclone) eq 'HASH') {
15889:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
15890:                 @currclonecode = @{$currclone->{'instcode'}};
15891:             }
15892:         }
15893:         my $newclone;
15894:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
15895:             $newclone = $env{'form.canclone'};
15896:         }
15897:         if ($newclone eq 'instcode') {
15898:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
15899:             my (%codedefaults,@code_order,@clonecode);
15900:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
15901:                                                     \@code_order);
15902:             foreach my $item (@code_order) {
15903:                 if (grep(/^\Q$item\E$/,@newcodes)) {
15904:                     push(@clonecode,$item);
15905:                 }
15906:             }
15907:             if (@clonecode) {
15908:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
15909:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
15910:                 if (@diffs) {
15911:                     $changes{'canclone'} = 1;
15912:                 }
15913:             } else {
15914:                 $newclone eq '';
15915:             }
15916:         } elsif ($newclone ne '') {
15917:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
15918:         }
15919:         if ($newclone ne $currclone) {
15920:             $changes{'canclone'} = 1;
15921:         }
15922:         my %credits;
15923:         foreach my $type (@types) {
15924:             unless ($type eq 'community') {
15925:                 $credits{$type} = $env{'form.'.$type.'_credits'};
15926:                 $credits{$type} =~ s/[^\d.]+//g;
15927:             }
15928:         }
15929:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
15930:             ($env{'form.coursecredits'} eq '1')) {
15931:             $changes{'coursecredits'} = 1;
15932:             foreach my $type (keys(%credits)) {
15933:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15934:             }
15935:         } else {
15936:             if ($env{'form.coursecredits'} eq '1') {
15937:                 foreach my $type (@types) {
15938:                     unless ($type eq 'community') {
15939:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
15940:                             $changes{'coursecredits'} = 1;
15941:                         }
15942:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15943:                     }
15944:                 }
15945:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15946:                 foreach my $type (@types) {
15947:                     unless ($type eq 'community') {
15948:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
15949:                             $changes{'coursecredits'} = 1;
15950:                             last;
15951:                         }
15952:                     }
15953:                 }
15954:             }
15955:         }
15956:         if ($env{'form.postsubmit'} eq '1') {
15957:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
15958:             my %currtimeout;
15959:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15960:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
15961:                     $changes{'postsubmit'} = 1;
15962:                 }
15963:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15964:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
15965:                 }
15966:             } else {
15967:                 $changes{'postsubmit'} = 1;
15968:             }
15969:             foreach my $type (@types) {
15970:                 my $timeout = $env{'form.'.$type.'_timeout'};
15971:                 $timeout =~ s/\D//g;
15972:                 if ($timeout == $staticdefaults{'postsubmit'}) {
15973:                     $timeout = '';
15974:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
15975:                     $timeout = '0';
15976:                 }
15977:                 unless ($timeout eq '') {
15978:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
15979:                 }
15980:                 if (exists($currtimeout{$type})) {
15981:                     if ($timeout ne $currtimeout{$type}) {
15982:                         $changes{'postsubmit'} = 1;
15983:                     }
15984:                 } elsif ($timeout ne '') {
15985:                     $changes{'postsubmit'} = 1;
15986:                 }
15987:             }
15988:         } else {
15989:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
15990:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15991:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
15992:                     $changes{'postsubmit'} = 1;
15993:                 }
15994:             } else {
15995:                 $changes{'postsubmit'} = 1;
15996:             }
15997:         }
15998:     }
15999:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16000:                                              $dom);
16001:     if ($putresult eq 'ok') {
16002:         if (keys(%changes) > 0) {
16003:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16004:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
16005:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
16006:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
16007:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
16008:                     if ($changes{$item}) {
16009:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
16010:                     }
16011:                 }
16012:                 if ($changes{'coursecredits'}) {
16013:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16014:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
16015:                             $domdefaults{$type.'credits'} =
16016:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
16017:                         }
16018:                     }
16019:                 }
16020:                 if ($changes{'postsubmit'}) {
16021:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16022:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
16023:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16024:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
16025:                                 $domdefaults{$type.'postsubtimeout'} =
16026:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
16027:                             }
16028:                         }
16029:                     }
16030:                 }
16031:                 if ($changes{'uploadquota'}) {
16032:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
16033:                         foreach my $type (@types) {
16034:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
16035:                         }
16036:                     }
16037:                 }
16038:                 if ($changes{'canclone'}) {
16039:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
16040:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
16041:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
16042:                             if (@clonecodes) {
16043:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
16044:                             }
16045:                         }
16046:                     } else {
16047:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
16048:                     }
16049:                 }
16050:                 my $cachetime = 24*60*60;
16051:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16052:                 if (ref($lastactref) eq 'HASH') {
16053:                     $lastactref->{'domdefaults'} = 1;
16054:                 }
16055:             }
16056:             $resulttext = &mt('Changes made:').'<ul>';
16057:             foreach my $item (sort(keys(%changes))) {
16058:                 if ($item eq 'canuse_pdfforms') {
16059:                     if ($env{'form.'.$item} eq '1') {
16060:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
16061:                     } else {
16062:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
16063:                     }
16064:                 } elsif ($item eq 'uselcmath') {
16065:                     if ($env{'form.'.$item} eq '1') {
16066:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
16067:                     } else {
16068:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
16069:                     }
16070:                 } elsif ($item eq 'usejsme') {
16071:                     if ($env{'form.'.$item} eq '1') {
16072:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
16073:                     } else {
16074:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
16075:                     }
16076:                 } elsif ($item eq 'texengine') {
16077:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
16078:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
16079:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
16080:                     }
16081:                 } elsif ($item eq 'anonsurvey_threshold') {
16082:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
16083:                 } elsif ($item eq 'uploadquota') {
16084:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
16085:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
16086:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
16087:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
16088:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
16089:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
16090:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
16091:                                        '</ul>'.
16092:                                        '</li>';
16093:                     } else {
16094:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
16095:                     }
16096:                 } elsif ($item eq 'mysqltables') {
16097:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
16098:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
16099:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
16100:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
16101:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
16102:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
16103:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
16104:                                        '</ul>'.
16105:                                        '</li>';
16106:                     } else {
16107:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
16108:                     }
16109:                 } elsif ($item eq 'postsubmit') {
16110:                     if ($domdefaults{'postsubmit'} eq 'off') {
16111:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
16112:                     } else {
16113:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
16114:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16115:                             $resulttext .= &mt('durations:').'<ul>';
16116:                             foreach my $type (@types) {
16117:                                 $resulttext .= '<li>';
16118:                                 my $timeout;
16119:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16120:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
16121:                                 }
16122:                                 my $display;
16123:                                 if ($timeout eq '0') {
16124:                                     $display = &mt('unlimited');
16125:                                 } elsif ($timeout eq '') {
16126:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
16127:                                 } else {
16128:                                     $display = &mt('[quant,_1,second]',$timeout);
16129:                                 }
16130:                                 if ($type eq 'community') {
16131:                                     $resulttext .= &mt('Communities');
16132:                                 } elsif ($type eq 'official') {
16133:                                     $resulttext .= &mt('Official courses');
16134:                                 } elsif ($type eq 'unofficial') {
16135:                                     $resulttext .= &mt('Unofficial courses');
16136:                                 } elsif ($type eq 'textbook') {
16137:                                     $resulttext .= &mt('Textbook courses');
16138:                                 } elsif ($type eq 'placement') {
16139:                                     $resulttext .= &mt('Placement tests');
16140:                                 }
16141:                                 $resulttext .= ' -- '.$display.'</li>';
16142:                             }
16143:                             $resulttext .= '</ul>';
16144:                         }
16145:                         $resulttext .= '</li>';
16146:                     }
16147:                 } elsif ($item eq 'coursecredits') {
16148:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16149:                         if (($domdefaults{'officialcredits'} eq '') &&
16150:                             ($domdefaults{'unofficialcredits'} eq '') &&
16151:                             ($domdefaults{'textbookcredits'} eq '')) {
16152:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
16153:                         } else {
16154:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
16155:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
16156:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
16157:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
16158:                                            '</ul>'.
16159:                                            '</li>';
16160:                         }
16161:                     } else {
16162:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
16163:                     }
16164:                 } elsif ($item eq 'canclone') {
16165:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
16166:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
16167:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
16168:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
16169:                         }
16170:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
16171:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
16172:                     } else {
16173:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
16174:                     }
16175:                 }
16176:             }
16177:             $resulttext .= '</ul>';
16178:         } else {
16179:             $resulttext = &mt('No changes made to course defaults');
16180:         }
16181:     } else {
16182:         $resulttext = '<span class="LC_error">'.
16183:             &mt('An error occurred: [_1]',$putresult).'</span>';
16184:     }
16185:     return $resulttext;
16186: }
16187: 
16188: sub modify_selfenrollment {
16189:     my ($dom,$lastactref,%domconfig) = @_;
16190:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
16191:     my @types = ('official','unofficial','community','textbook','placement');
16192:     my %titles = &tool_titles();
16193:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
16194:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
16195:     $ordered{'default'} = ['types','registered','approval','limit'];
16196: 
16197:     my (%roles,%shown,%toplevel);
16198:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
16199: 
16200:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
16201:         if ($domconfig{'selfenrollment'} eq '') {
16202:             $domconfig{'selfenrollment'} = {};
16203:         }
16204:     }
16205:     %toplevel = (
16206:                   admin      => 'Configuration Rights',
16207:                   default    => 'Default settings',
16208:                   validation => 'Validation of self-enrollment requests',
16209:                 );
16210:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
16211: 
16212:     if (ref($ordered{'admin'}) eq 'ARRAY') {
16213:         foreach my $item (@{$ordered{'admin'}}) {
16214:             foreach my $type (@types) {
16215:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
16216:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
16217:                 } else {
16218:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
16219:                 }
16220:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
16221:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
16222:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
16223:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
16224:                             push(@{$changes{'admin'}{$type}},$item);
16225:                         }
16226:                     } else {
16227:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
16228:                             push(@{$changes{'admin'}{$type}},$item);
16229:                         }
16230:                     }
16231:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
16232:                     push(@{$changes{'admin'}{$type}},$item);
16233:                 }
16234:             }
16235:         }
16236:     }
16237: 
16238:     foreach my $item (@{$ordered{'default'}}) {
16239:         foreach my $type (@types) {
16240:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
16241:             if ($item eq 'types') {
16242:                 unless (($value eq 'all') || ($value eq 'dom')) {
16243:                     $value = '';
16244:                 }
16245:             } elsif ($item eq 'registered') {
16246:                 unless ($value eq '1') {
16247:                     $value = 0;
16248:                 }
16249:             } elsif ($item eq 'approval') {
16250:                 unless ($value =~ /^[012]$/) {
16251:                     $value = 0;
16252:                 }
16253:             } else {
16254:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16255:                     $value = 'none';
16256:                 }
16257:             }
16258:             $selfenrollhash{'default'}{$type}{$item} = $value;
16259:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
16260:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16261:                     if ($selfenrollhash{'default'}{$type}{$item} ne
16262:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
16263:                          push(@{$changes{'default'}{$type}},$item);
16264:                     }
16265:                 } else {
16266:                     push(@{$changes{'default'}{$type}},$item);
16267:                 }
16268:             } else {
16269:                 push(@{$changes{'default'}{$type}},$item);
16270:             }
16271:             if ($item eq 'limit') {
16272:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16273:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
16274:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
16275:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
16276:                     }
16277:                 } else {
16278:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
16279:                 }
16280:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16281:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
16282:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
16283:                          push(@{$changes{'default'}{$type}},'cap');
16284:                     }
16285:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
16286:                     push(@{$changes{'default'}{$type}},'cap');
16287:                 }
16288:             }
16289:         }
16290:     }
16291: 
16292:     foreach my $item (@{$itemsref}) {
16293:         if ($item eq 'fields') {
16294:             my @changed;
16295:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
16296:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
16297:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
16298:             }
16299:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16300:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
16301:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
16302:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
16303:                 } else {
16304:                     @changed = @{$selfenrollhash{'validation'}{$item}};
16305:                 }
16306:             } else {
16307:                 @changed = @{$selfenrollhash{'validation'}{$item}};
16308:             }
16309:             if (@changed) {
16310:                 if ($selfenrollhash{'validation'}{$item}) { 
16311:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
16312:                 } else {
16313:                     $changes{'validation'}{$item} = &mt('None');
16314:                 }
16315:             }
16316:         } else {
16317:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
16318:             if ($item eq 'markup') {
16319:                if ($env{'form.selfenroll_validation_'.$item}) {
16320:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
16321:                }
16322:             }
16323:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16324:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
16325:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
16326:                 }
16327:             }
16328:         }
16329:     }
16330: 
16331:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
16332:                                              $dom);
16333:     if ($putresult eq 'ok') {
16334:         if (keys(%changes) > 0) {
16335:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16336:             $resulttext = &mt('Changes made:').'<ul>';
16337:             foreach my $key ('admin','default','validation') {
16338:                 if (ref($changes{$key}) eq 'HASH') {
16339:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
16340:                     if ($key eq 'validation') {
16341:                         foreach my $item (@{$itemsref}) {
16342:                             if (exists($changes{$key}{$item})) {
16343:                                 if ($item eq 'markup') {
16344:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16345:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
16346:                                 } else {  
16347:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16348:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
16349:                                 }
16350:                             }
16351:                         }
16352:                     } else {
16353:                         foreach my $type (@types) {
16354:                             if ($type eq 'community') {
16355:                                 $roles{'1'} = &mt('Community personnel');
16356:                             } else {
16357:                                 $roles{'1'} = &mt('Course personnel');
16358:                             }
16359:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
16360:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
16361:                                     if ($key eq 'admin') {
16362:                                         my @mgrdc = ();
16363:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16364:                                             foreach my $item (@{$ordered{'admin'}}) {
16365:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
16366:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
16367:                                                         push(@mgrdc,$item);
16368:                                                     }
16369:                                                 }
16370:                                             }
16371:                                             if (@mgrdc) {
16372:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
16373:                                             } else {
16374:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
16375:                                             }
16376:                                         }
16377:                                     } else {
16378:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16379:                                             foreach my $item (@{$ordered{$key}}) {
16380:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16381:                                                     $domdefaults{$type.'selfenroll'.$item} =
16382:                                                         $selfenrollhash{$key}{$type}{$item};
16383:                                                 }
16384:                                             }
16385:                                         }
16386:                                     }
16387:                                 }
16388:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
16389:                                 foreach my $item (@{$ordered{$key}}) {
16390:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16391:                                         $resulttext .= '<li>';
16392:                                         if ($key eq 'admin') {
16393:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
16394:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
16395:                                         } else {
16396:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
16397:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
16398:                                         }
16399:                                         $resulttext .= '</li>';
16400:                                     }
16401:                                 }
16402:                                 $resulttext .= '</ul></li>';
16403:                             }
16404:                         }
16405:                         $resulttext .= '</ul></li>'; 
16406:                     }
16407:                 }
16408:             }
16409:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
16410:                 my $cachetime = 24*60*60;
16411:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16412:                 if (ref($lastactref) eq 'HASH') {
16413:                     $lastactref->{'domdefaults'} = 1;
16414:                 }
16415:             }
16416:             $resulttext .= '</ul>';
16417:         } else {
16418:             $resulttext = &mt('No changes made to self-enrollment settings');
16419:         }
16420:     } else {
16421:         $resulttext = '<span class="LC_error">'.
16422:             &mt('An error occurred: [_1]',$putresult).'</span>';
16423:     }
16424:     return $resulttext;
16425: }
16426: 
16427: sub modify_usersessions {
16428:     my ($dom,$lastactref,%domconfig) = @_;
16429:     my @hostingtypes = ('version','excludedomain','includedomain');
16430:     my @offloadtypes = ('primary','default');
16431:     my %types = (
16432:                   remote => \@hostingtypes,
16433:                   hosted => \@hostingtypes,
16434:                   spares => \@offloadtypes,
16435:                 );
16436:     my @prefixes = ('remote','hosted','spares');
16437:     my @lcversions = &Apache::lonnet::all_loncaparevs();
16438:     my (%by_ip,%by_location,@intdoms,@instdoms);
16439:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16440:     my @locations = sort(keys(%by_location));
16441:     my (%defaultshash,%changes);
16442:     foreach my $prefix (@prefixes) {
16443:         $defaultshash{'usersessions'}{$prefix} = {};
16444:     }
16445:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16446:     my $resulttext;
16447:     my %iphost = &Apache::lonnet::get_iphost();
16448:     foreach my $prefix (@prefixes) {
16449:         next if ($prefix eq 'spares');
16450:         foreach my $type (@{$types{$prefix}}) {
16451:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16452:             if ($type eq 'version') {
16453:                 my $value = $env{'form.'.$prefix.'_'.$type};
16454:                 my $okvalue;
16455:                 if ($value ne '') {
16456:                     if (grep(/^\Q$value\E$/,@lcversions)) {
16457:                         $okvalue = $value;
16458:                     }
16459:                 }
16460:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16461:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16462:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
16463:                             if ($inuse == 0) {
16464:                                 $changes{$prefix}{$type} = 1;
16465:                             } else {
16466:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
16467:                                     $changes{$prefix}{$type} = 1;
16468:                                 }
16469:                                 if ($okvalue ne '') {
16470:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16471:                                 } 
16472:                             }
16473:                         } else {
16474:                             if (($inuse == 1) && ($okvalue ne '')) {
16475:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16476:                                 $changes{$prefix}{$type} = 1;
16477:                             }
16478:                         }
16479:                     } else {
16480:                         if (($inuse == 1) && ($okvalue ne '')) {
16481:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16482:                             $changes{$prefix}{$type} = 1;
16483:                         }
16484:                     }
16485:                 } else {
16486:                     if (($inuse == 1) && ($okvalue ne '')) {
16487:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16488:                         $changes{$prefix}{$type} = 1;
16489:                     }
16490:                 }
16491:             } else {
16492:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16493:                 my @okvals;
16494:                 foreach my $val (@vals) {
16495:                     if ($val =~ /:/) {
16496:                         my @items = split(/:/,$val);
16497:                         foreach my $item (@items) {
16498:                             if (ref($by_location{$item}) eq 'ARRAY') {
16499:                                 push(@okvals,$item);
16500:                             }
16501:                         }
16502:                     } else {
16503:                         if (ref($by_location{$val}) eq 'ARRAY') {
16504:                             push(@okvals,$val);
16505:                         }
16506:                     }
16507:                 }
16508:                 @okvals = sort(@okvals);
16509:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16510:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16511:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16512:                             if ($inuse == 0) {
16513:                                 $changes{$prefix}{$type} = 1; 
16514:                             } else {
16515:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16516:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
16517:                                 if (@changed > 0) {
16518:                                     $changes{$prefix}{$type} = 1;
16519:                                 }
16520:                             }
16521:                         } else {
16522:                             if ($inuse == 1) {
16523:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16524:                                 $changes{$prefix}{$type} = 1;
16525:                             }
16526:                         } 
16527:                     } else {
16528:                         if ($inuse == 1) {
16529:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16530:                             $changes{$prefix}{$type} = 1;
16531:                         }
16532:                     }
16533:                 } else {
16534:                     if ($inuse == 1) {
16535:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16536:                         $changes{$prefix}{$type} = 1;
16537:                     }
16538:                 }
16539:             }
16540:         }
16541:     }
16542: 
16543:     my @alldoms = &Apache::lonnet::all_domains();
16544:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16545:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
16546:     my $savespares;
16547: 
16548:     foreach my $lonhost (sort(keys(%servers))) {
16549:         my $serverhomeID =
16550:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
16551:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
16552:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
16553:         my %spareschg;
16554:         foreach my $type (@{$types{'spares'}}) {
16555:             my @okspares;
16556:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
16557:             foreach my $server (@checked) {
16558:                 if (&Apache::lonnet::hostname($server) ne '') {
16559:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
16560:                         unless (grep(/^\Q$server\E$/,@okspares)) {
16561:                             push(@okspares,$server);
16562:                         }
16563:                     }
16564:                 }
16565:             }
16566:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
16567:             my $newspare;
16568:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
16569:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
16570:                     $newspare = $new;
16571:                 }
16572:             }
16573:             my @spares;
16574:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
16575:                 @spares = sort(@okspares,$newspare);
16576:             } else {
16577:                 @spares = sort(@okspares);
16578:             }
16579:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
16580:             if (ref($spareid{$lonhost}) eq 'HASH') {
16581:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
16582:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
16583:                     if (@diffs > 0) {
16584:                         $spareschg{$type} = 1;
16585:                     }
16586:                 }
16587:             }
16588:         }
16589:         if (keys(%spareschg) > 0) {
16590:             $changes{'spares'}{$lonhost} = \%spareschg;
16591:         }
16592:     }
16593:     $defaultshash{'usersessions'}{'offloadnow'} = {};
16594:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
16595:     my @okoffload;
16596:     if (@offloadnow) {
16597:         foreach my $server (@offloadnow) {
16598:             if (&Apache::lonnet::hostname($server) ne '') {
16599:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
16600:                     push(@okoffload,$server);
16601:                 }
16602:             }
16603:         }
16604:         if (@okoffload) {
16605:             foreach my $lonhost (@okoffload) {
16606:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
16607:             }
16608:         }
16609:     }
16610:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
16611:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
16612:             if (ref($changes{'spares'}) eq 'HASH') {
16613:                 if (keys(%{$changes{'spares'}}) > 0) {
16614:                     $savespares = 1;
16615:                 }
16616:             }
16617:         } else {
16618:             $savespares = 1;
16619:         }
16620:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
16621:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
16622:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
16623:                     $changes{'offloadnow'} = 1;
16624:                     last;
16625:                 }
16626:             }
16627:             unless ($changes{'offloadnow'}) {
16628:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
16629:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
16630:                         $changes{'offloadnow'} = 1;
16631:                         last;
16632:                     }
16633:                 }
16634:             }
16635:         } elsif (@okoffload) {
16636:             $changes{'offloadnow'} = 1;
16637:         }
16638:     } elsif (@okoffload) {
16639:         $changes{'offloadnow'} = 1;
16640:     }
16641:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
16642:     if ((keys(%changes) > 0) || ($savespares)) {
16643:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16644:                                                  $dom);
16645:         if ($putresult eq 'ok') {
16646:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16647:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
16648:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
16649:                 }
16650:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
16651:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
16652:                 }
16653:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16654:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
16655:                 }
16656:             }
16657:             my $cachetime = 24*60*60;
16658:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16659:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
16660:             if (ref($lastactref) eq 'HASH') {
16661:                 $lastactref->{'domdefaults'} = 1;
16662:                 $lastactref->{'usersessions'} = 1;
16663:             }
16664:             if (keys(%changes) > 0) {
16665:                 my %lt = &usersession_titles();
16666:                 $resulttext = &mt('Changes made:').'<ul>';
16667:                 foreach my $prefix (@prefixes) {
16668:                     if (ref($changes{$prefix}) eq 'HASH') {
16669:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
16670:                         if ($prefix eq 'spares') {
16671:                             if (ref($changes{$prefix}) eq 'HASH') {
16672:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
16673:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
16674:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
16675:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
16676:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16677:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
16678:                                         foreach my $type (@{$types{$prefix}}) {
16679:                                             if ($changes{$prefix}{$lonhost}{$type}) {
16680:                                                 my $offloadto = &mt('None');
16681:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
16682:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
16683:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
16684:                                                     }
16685:                                                 }
16686:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
16687:                                             }
16688:                                         }
16689:                                     }
16690:                                     $resulttext .= '</li>';
16691:                                 }
16692:                             }
16693:                         } else {
16694:                             foreach my $type (@{$types{$prefix}}) {
16695:                                 if (defined($changes{$prefix}{$type})) {
16696:                                     my ($newvalue,$notinuse);
16697:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16698:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
16699:                                             if ($type eq 'version') {
16700:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
16701:                                             } else {
16702:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16703:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
16704:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
16705:                                                     }
16706:                                                 } else {
16707:                                                     $notinuse = 1;
16708:                                                 }
16709:                                             }
16710:                                         }
16711:                                     }
16712:                                     if ($newvalue eq '') {
16713:                                         if ($type eq 'version') {
16714:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
16715:                                         } elsif ($notinuse) {
16716:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
16717:                                         } else {
16718:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
16719:                                         }
16720:                                     } else {
16721:                                         if ($type eq 'version') {
16722:                                             $newvalue .= ' '.&mt('(or later)');
16723:                                         }
16724:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
16725:                                     }
16726:                                 }
16727:                             }
16728:                         }
16729:                         $resulttext .= '</ul>';
16730:                     }
16731:                 }
16732:                 if ($changes{'offloadnow'}) {
16733:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16734:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
16735:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
16736:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
16737:                                 $resulttext .= '<li>'.$lonhost.'</li>';
16738:                             }
16739:                             $resulttext .= '</ul>';
16740:                         } else {
16741:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
16742:                         }
16743:                     } else {
16744:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
16745:                     }
16746:                 }
16747:                 $resulttext .= '</ul>';
16748:             } else {
16749:                 $resulttext = $nochgmsg;
16750:             }
16751:         } else {
16752:             $resulttext = '<span class="LC_error">'.
16753:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16754:         }
16755:     } else {
16756:         $resulttext = $nochgmsg;
16757:     }
16758:     return $resulttext;
16759: }
16760: 
16761: sub modify_ssl {
16762:     my ($dom,$lastactref,%domconfig) = @_;
16763:     my (%by_ip,%by_location,@intdoms,@instdoms);
16764:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16765:     my @locations = sort(keys(%by_location));
16766:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16767:     my (%defaultshash,%changes);
16768:     my $action = 'ssl';
16769:     my @prefixes = ('connto','connfrom','replication');
16770:     foreach my $prefix (@prefixes) {
16771:         $defaultshash{$action}{$prefix} = {};
16772:     }
16773:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16774:     my $resulttext;
16775:     my %iphost = &Apache::lonnet::get_iphost();
16776:     my @reptypes = ('certreq','nocertreq');
16777:     my @connecttypes = ('dom','intdom','other');
16778:     my %types = (
16779:                   connto      => \@connecttypes,
16780:                   connfrom    => \@connecttypes,
16781:                   replication => \@reptypes,
16782:                 );
16783:     foreach my $prefix (sort(keys(%types))) {
16784:         foreach my $type (@{$types{$prefix}}) {
16785:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
16786:                 my $value = 'yes';
16787:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
16788:                     $value = $env{'form.'.$prefix.'_'.$type};
16789:                 }
16790:                 if (ref($domconfig{$action}) eq 'HASH') {
16791:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
16792:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
16793:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
16794:                                 $changes{$prefix}{$type} = 1;
16795:                             }
16796:                             $defaultshash{$action}{$prefix}{$type} = $value;
16797:                         } else {
16798:                             $defaultshash{$action}{$prefix}{$type} = $value;
16799:                             $changes{$prefix}{$type} = 1;
16800:                         }
16801:                     } else {
16802:                         $defaultshash{$action}{$prefix}{$type} = $value;
16803:                         $changes{$prefix}{$type} = 1;
16804:                     }
16805:                 } else {
16806:                     $defaultshash{$action}{$prefix}{$type} = $value;
16807:                     $changes{$prefix}{$type} = 1;
16808:                 }
16809:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
16810:                     delete($changes{$prefix}{$type});
16811:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
16812:                     delete($changes{$prefix}{$type});
16813:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
16814:                     delete($changes{$prefix}{$type});
16815:                 }
16816:             } elsif ($prefix eq 'replication') {
16817:                 if (@locations > 0) {
16818:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16819:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16820:                     my @okvals;
16821:                     foreach my $val (@vals) {
16822:                         if ($val =~ /:/) {
16823:                             my @items = split(/:/,$val);
16824:                             foreach my $item (@items) {
16825:                                 if (ref($by_location{$item}) eq 'ARRAY') {
16826:                                     push(@okvals,$item);
16827:                                 }
16828:                             }
16829:                         } else {
16830:                             if (ref($by_location{$val}) eq 'ARRAY') {
16831:                                 push(@okvals,$val);
16832:                             }
16833:                         }
16834:                     }
16835:                     @okvals = sort(@okvals);
16836:                     if (ref($domconfig{$action}) eq 'HASH') {
16837:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
16838:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
16839:                                 if ($inuse == 0) {
16840:                                     $changes{$prefix}{$type} = 1;
16841:                                 } else {
16842:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
16843:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
16844:                                     if (@changed > 0) {
16845:                                         $changes{$prefix}{$type} = 1;
16846:                                     }
16847:                                 }
16848:                             } else {
16849:                                 if ($inuse == 1) {
16850:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
16851:                                     $changes{$prefix}{$type} = 1;
16852:                                 }
16853:                             }
16854:                         } else {
16855:                             if ($inuse == 1) {
16856:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
16857:                                 $changes{$prefix}{$type} = 1;
16858:                             }
16859:                         }
16860:                     } else {
16861:                         if ($inuse == 1) {
16862:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
16863:                             $changes{$prefix}{$type} = 1;
16864:                         }
16865:                     }
16866:                 }
16867:             }
16868:         }
16869:     }
16870:     if (keys(%changes)) {
16871:         foreach my $prefix (keys(%changes)) {
16872:             if (ref($changes{$prefix}) eq 'HASH') {
16873:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
16874:                     delete($changes{$prefix});
16875:                 }
16876:             } else {
16877:                 delete($changes{$prefix});
16878:             }
16879:         }
16880:     }
16881:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
16882:     if (keys(%changes) > 0) {
16883:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16884:                                                  $dom);
16885:         if ($putresult eq 'ok') {
16886:             if (ref($defaultshash{$action}) eq 'HASH') {
16887:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
16888:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
16889:                 }
16890:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
16891:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
16892:                 }
16893:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
16894:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
16895:                 }
16896:             }
16897:             my $cachetime = 24*60*60;
16898:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16899:             if (ref($lastactref) eq 'HASH') {
16900:                 $lastactref->{'domdefaults'} = 1;
16901:             }
16902:             if (keys(%changes) > 0) {
16903:                 my %titles = &ssl_titles();
16904:                 $resulttext = &mt('Changes made:').'<ul>';
16905:                 foreach my $prefix (@prefixes) {
16906:                     if (ref($changes{$prefix}) eq 'HASH') {
16907:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
16908:                         foreach my $type (@{$types{$prefix}}) {
16909:                             if (defined($changes{$prefix}{$type})) {
16910:                                 my ($newvalue,$notinuse);
16911:                                 if (ref($defaultshash{$action}) eq 'HASH') {
16912:                                     if (ref($defaultshash{$action}{$prefix})) {
16913:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
16914:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
16915:                                         } else {
16916:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
16917:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
16918:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
16919:                                                 }
16920:                                             } else {
16921:                                                 $notinuse = 1;
16922:                                             }
16923:                                         }
16924:                                     }
16925:                                     if ($notinuse) {
16926:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
16927:                                     } elsif ($newvalue eq '') {
16928:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
16929:                                     } else {
16930:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
16931:                                     }
16932:                                 }
16933:                             }
16934:                         }
16935:                         $resulttext .= '</ul>';
16936:                     }
16937:                 }
16938:             } else {
16939:                 $resulttext = $nochgmsg;
16940:             }
16941:         } else {
16942:             $resulttext = '<span class="LC_error">'.
16943:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16944:         }
16945:     } else {
16946:         $resulttext = $nochgmsg;
16947:     }
16948:     return $resulttext;
16949: }
16950: 
16951: sub modify_trust {
16952:     my ($dom,$lastactref,%domconfig) = @_;
16953:     my (%by_ip,%by_location,@intdoms,@instdoms);
16954:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16955:     my @locations = sort(keys(%by_location));
16956:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
16957:     my @types = ('exc','inc');
16958:     my (%defaultshash,%changes);
16959:     foreach my $prefix (@prefixes) {
16960:         $defaultshash{'trust'}{$prefix} = {};
16961:     }
16962:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16963:     my $resulttext;
16964:     foreach my $prefix (@prefixes) {
16965:         foreach my $type (@types) {
16966:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16967:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16968:             my @okvals;
16969:             foreach my $val (@vals) {
16970:                 if ($val =~ /:/) {
16971:                     my @items = split(/:/,$val);
16972:                     foreach my $item (@items) {
16973:                         if (ref($by_location{$item}) eq 'ARRAY') {
16974:                             push(@okvals,$item);
16975:                         }
16976:                     }
16977:                 } else {
16978:                     if (ref($by_location{$val}) eq 'ARRAY') {
16979:                         push(@okvals,$val);
16980:                     }
16981:                 }
16982:             }
16983:             @okvals = sort(@okvals);
16984:             if (ref($domconfig{'trust'}) eq 'HASH') {
16985:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
16986:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
16987:                         if ($inuse == 0) {
16988:                             $changes{$prefix}{$type} = 1;
16989:                         } else {
16990:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
16991:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
16992:                             if (@changed > 0) {
16993:                                 $changes{$prefix}{$type} = 1;
16994:                             }
16995:                         }
16996:                     } else {
16997:                         if ($inuse == 1) {
16998:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
16999:                             $changes{$prefix}{$type} = 1;
17000:                         }
17001:                     }
17002:                 } else {
17003:                     if ($inuse == 1) {
17004:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17005:                         $changes{$prefix}{$type} = 1;
17006:                     }
17007:                 }
17008:             } else {
17009:                 if ($inuse == 1) {
17010:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17011:                     $changes{$prefix}{$type} = 1;
17012:                 }
17013:             }
17014:         }
17015:     }
17016:     my $nochgmsg = &mt('No changes made to trust settings.');
17017:     if (keys(%changes) > 0) {
17018:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17019:                                                  $dom);
17020:         if ($putresult eq 'ok') {
17021:             if (ref($defaultshash{'trust'}) eq 'HASH') {
17022:                 foreach my $prefix (@prefixes) {
17023:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
17024:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
17025:                     }
17026:                 }
17027:             }
17028:             my $cachetime = 24*60*60;
17029:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17030:             if (ref($lastactref) eq 'HASH') {
17031:                 $lastactref->{'domdefaults'} = 1;
17032:             }
17033:             if (keys(%changes) > 0) {
17034:                 my %lt = &trust_titles();
17035:                 $resulttext = &mt('Changes made:').'<ul>';
17036:                 foreach my $prefix (@prefixes) {
17037:                     if (ref($changes{$prefix}) eq 'HASH') {
17038:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
17039:                         foreach my $type (@types) {
17040:                             if (defined($changes{$prefix}{$type})) {
17041:                                 my ($newvalue,$notinuse);
17042:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
17043:                                     if (ref($defaultshash{'trust'}{$prefix})) {
17044:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
17045:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
17046:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
17047:                                             }
17048:                                         } else {
17049:                                             $notinuse = 1;
17050:                                         }
17051:                                     }
17052:                                 }
17053:                                 if ($notinuse) {
17054:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
17055:                                 } elsif ($newvalue eq '') {
17056:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
17057:                                 } else {
17058:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
17059:                                 }
17060:                             }
17061:                         }
17062:                         $resulttext .= '</ul>';
17063:                     }
17064:                 }
17065:                 $resulttext .= '</ul>';
17066:             } else {
17067:                 $resulttext = $nochgmsg;
17068:             }
17069:         } else {
17070:             $resulttext = '<span class="LC_error">'.
17071:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17072:         }
17073:     } else {
17074:         $resulttext = $nochgmsg;
17075:     }
17076:     return $resulttext;
17077: }
17078: 
17079: sub modify_loadbalancing {
17080:     my ($dom,%domconfig) = @_;
17081:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
17082:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
17083:     my ($othertitle,$usertypes,$types) =
17084:         &Apache::loncommon::sorted_inst_types($dom);
17085:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17086:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
17087:     my @sparestypes = ('primary','default');
17088:     my %typetitles = &sparestype_titles();
17089:     my $resulttext;
17090:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
17091:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
17092:         %existing = %{$domconfig{'loadbalancing'}};
17093:     }
17094:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
17095:                               \%currtargets,\%currrules,\%currcookies);
17096:     my ($saveloadbalancing,%defaultshash,%changes);
17097:     my ($alltypes,$othertypes,$titles) =
17098:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
17099:     my %ruletitles = &offloadtype_text();
17100:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
17101:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
17102:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
17103:         if ($balancer eq '') {
17104:             next;
17105:         }
17106:         if (!exists($servers{$balancer})) {
17107:             if (exists($currbalancer{$balancer})) {
17108:                 push(@{$changes{'delete'}},$balancer);
17109:             }
17110:             next;
17111:         }
17112:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
17113:             push(@{$changes{'delete'}},$balancer);
17114:             next;
17115:         }
17116:         if (!exists($currbalancer{$balancer})) {
17117:             push(@{$changes{'add'}},$balancer);
17118:         }
17119:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
17120:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
17121:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
17122:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
17123:             $saveloadbalancing = 1;
17124:         }
17125:         foreach my $sparetype (@sparestypes) {
17126:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
17127:             my @offloadto;
17128:             foreach my $target (@targets) {
17129:                 if (($servers{$target}) && ($target ne $balancer)) {
17130:                     if ($sparetype eq 'default') {
17131:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
17132:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
17133:                         }
17134:                     }
17135:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
17136:                         push(@offloadto,$target);
17137:                     }
17138:                 }
17139:             }
17140:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
17141:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
17142:                     push(@offloadto,$balancer);
17143:                 }
17144:             }
17145:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
17146:         }
17147:         if ($env{'form.loadbalancing_cookie_'.$i}) {
17148:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
17149:             if (exists($currbalancer{$balancer})) { 
17150:                 unless ($currcookies{$balancer}) {
17151:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
17152:                 }
17153:             }
17154:         } elsif (exists($currbalancer{$balancer})) {
17155:             if ($currcookies{$balancer}) {
17156:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
17157:             }
17158:         }
17159:         if (ref($currtargets{$balancer}) eq 'HASH') {
17160:             foreach my $sparetype (@sparestypes) {
17161:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
17162:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
17163:                     if (@targetdiffs > 0) {
17164:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17165:                     }
17166:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17167:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17168:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17169:                     }
17170:                 }
17171:             }
17172:         } else {
17173:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
17174:                 foreach my $sparetype (@sparestypes) {
17175:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17176:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17177:                             $changes{'curr'}{$balancer}{'targets'} = 1;
17178:                         }
17179:                     }
17180:                 }
17181:             }
17182:         }
17183:         my $ishomedom;
17184:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
17185:             $ishomedom = 1;
17186:         }
17187:         if (ref($alltypes) eq 'ARRAY') {
17188:             foreach my $type (@{$alltypes}) {
17189:                 my $rule;
17190:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
17191:                          (!$ishomedom)) {
17192:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
17193:                 }
17194:                 if ($rule eq 'specific') {
17195:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
17196:                     if (exists($servers{$specifiedhost})) {
17197:                         $rule = $specifiedhost;
17198:                     }
17199:                 }
17200:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
17201:                 if (ref($currrules{$balancer}) eq 'HASH') {
17202:                     if ($rule ne $currrules{$balancer}{$type}) {
17203:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17204:                     }
17205:                 } elsif ($rule ne '') {
17206:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17207:                 }
17208:             }
17209:         }
17210:     }
17211:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
17212:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
17213:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
17214:             $defaultshash{'loadbalancing'} = {};
17215:         }
17216:         my $putresult = &Apache::lonnet::put_dom('configuration',
17217:                                                  \%defaultshash,$dom);
17218:         if ($putresult eq 'ok') {
17219:             if (keys(%changes) > 0) {
17220:                 my %toupdate;
17221:                 if (ref($changes{'delete'}) eq 'ARRAY') {
17222:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
17223:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
17224:                         $toupdate{$balancer} = 1;
17225:                     }
17226:                 }
17227:                 if (ref($changes{'add'}) eq 'ARRAY') {
17228:                     foreach my $balancer (sort(@{$changes{'add'}})) {
17229:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
17230:                         $toupdate{$balancer} = 1;
17231:                     }
17232:                 }
17233:                 if (ref($changes{'curr'}) eq 'HASH') {
17234:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
17235:                         $toupdate{$balancer} = 1;
17236:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
17237:                             if ($changes{'curr'}{$balancer}{'targets'}) {
17238:                                 my %offloadstr;
17239:                                 foreach my $sparetype (@sparestypes) {
17240:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17241:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17242:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17243:                                         }
17244:                                     }
17245:                                 }
17246:                                 if (keys(%offloadstr) == 0) {
17247:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
17248:                                 } else {
17249:                                     my $showoffload;
17250:                                     foreach my $sparetype (@sparestypes) {
17251:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
17252:                                         if (defined($offloadstr{$sparetype})) {
17253:                                             $showoffload .= $offloadstr{$sparetype};
17254:                                         } else {
17255:                                             $showoffload .= &mt('None');
17256:                                         }
17257:                                         $showoffload .= ('&nbsp;'x3);
17258:                                     }
17259:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
17260:                                 }
17261:                             }
17262:                         }
17263:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
17264:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
17265:                                 foreach my $type (@{$alltypes}) {
17266:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
17267:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17268:                                         my $balancetext;
17269:                                         if ($rule eq '') {
17270:                                             $balancetext =  $ruletitles{'default'};
17271:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
17272:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17273:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17274:                                                 foreach my $sparetype (@sparestypes) {
17275:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17276:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17277:                                                     }
17278:                                                 }
17279:                                                 foreach my $item (@{$alltypes}) {
17280:                                                     next if ($item =~  /^_LC_ipchange/);
17281:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
17282:                                                     if ($hasrule eq 'homeserver') {
17283:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
17284:                                                     } else {
17285:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
17286:                                                             if ($servers{$hasrule}) {
17287:                                                                 $toupdate{$hasrule} = 1;
17288:                                                             }
17289:                                                         }
17290:                                                     }
17291:                                                 }
17292:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
17293:                                                     $balancetext =  $ruletitles{$rule};
17294:                                                 } else {
17295:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17296:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
17297:                                                     if ($receiver) {
17298:                                                         $toupdate{$receiver};
17299:                                                     }
17300:                                                 }
17301:                                             } else {
17302:                                                 $balancetext =  $ruletitles{$rule};
17303:                                             }
17304:                                         } else {
17305:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
17306:                                         }
17307:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
17308:                                     }
17309:                                 }
17310:                             }
17311:                         }
17312:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
17313:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
17314:                                                       $balancer).'</li>'; 
17315:                         }
17316:                         if (keys(%toupdate)) {
17317:                             my %thismachine;
17318:                             my $updatedhere;
17319:                             my $cachetime = 60*60*24;
17320:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
17321:                             foreach my $lonhost (keys(%toupdate)) {
17322:                                 if ($thismachine{$lonhost}) {
17323:                                     unless ($updatedhere) {
17324:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
17325:                                                                       $defaultshash{'loadbalancing'},
17326:                                                                       $cachetime);
17327:                                         $updatedhere = 1;
17328:                                     }
17329:                                 } else {
17330:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
17331:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
17332:                                 }
17333:                             }
17334:                         }
17335:                     }
17336:                 }
17337:                 if ($resulttext ne '') {
17338:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
17339:                 } else {
17340:                     $resulttext = $nochgmsg;
17341:                 }
17342:             } else {
17343:                 $resulttext = $nochgmsg;
17344:             }
17345:         } else {
17346:             $resulttext = '<span class="LC_error">'.
17347:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17348:         }
17349:     } else {
17350:         $resulttext = $nochgmsg;
17351:     }
17352:     return $resulttext;
17353: }
17354: 
17355: sub recurse_check {
17356:     my ($chkcats,$categories,$depth,$name) = @_;
17357:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
17358:         my $chg = 0;
17359:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
17360:             my $category = $chkcats->[$depth]{$name}[$j];
17361:             my $item;
17362:             if ($category eq '') {
17363:                 $chg ++;
17364:             } else {
17365:                 my $deeper = $depth + 1;
17366:                 $item = &escape($category).':'.&escape($name).':'.$depth;
17367:                 if ($chg) {
17368:                     $categories->{$item} -= $chg;
17369:                 }
17370:                 &recurse_check($chkcats,$categories,$deeper,$category);
17371:                 $deeper --;
17372:             }
17373:         }
17374:     }
17375:     return;
17376: }
17377: 
17378: sub recurse_cat_deletes {
17379:     my ($item,$coursecategories,$deletions) = @_;
17380:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
17381:     my $subdepth = $depth + 1;
17382:     if (ref($coursecategories) eq 'HASH') {
17383:         foreach my $subitem (keys(%{$coursecategories})) {
17384:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
17385:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
17386:                 delete($coursecategories->{$subitem});
17387:                 $deletions->{$subitem} = 1;
17388:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
17389:             }
17390:         }
17391:     }
17392:     return;
17393: }
17394: 
17395: sub active_dc_picker {
17396:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
17397:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17398:     my @domcoord = keys(%domcoords);
17399:     if (keys(%currhash)) {
17400:         foreach my $dc (keys(%currhash)) {
17401:             unless (exists($domcoords{$dc})) {
17402:                 push(@domcoord,$dc);
17403:             }
17404:         }
17405:     }
17406:     @domcoord = sort(@domcoord);
17407:     my $numdcs = scalar(@domcoord);
17408:     my $rows = 0;
17409:     my $table;
17410:     if ($numdcs > 1) {
17411:         $table = '<table>';
17412:         for (my $i=0; $i<@domcoord; $i++) {
17413:             my $rem = $i%($numinrow);
17414:             if ($rem == 0) {
17415:                 if ($i > 0) {
17416:                     $table .= '</tr>';
17417:                 }
17418:                 $table .= '<tr>';
17419:                 $rows ++;
17420:             }
17421:             my $check = '';
17422:             if ($inputtype eq 'radio') {
17423:                 if (keys(%currhash) == 0) {
17424:                     if (!$i) {
17425:                         $check = ' checked="checked"';
17426:                     }
17427:                 } elsif (exists($currhash{$domcoord[$i]})) {
17428:                     $check = ' checked="checked"';
17429:                 }
17430:             } else {
17431:                 if (exists($currhash{$domcoord[$i]})) {
17432:                     $check = ' checked="checked"';
17433:                 }
17434:             }
17435:             if ($i == @domcoord - 1) {
17436:                 my $colsleft = $numinrow - $rem;
17437:                 if ($colsleft > 1) {
17438:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
17439:                 } else {
17440:                     $table .= '<td class="LC_left_item">';
17441:                 }
17442:             } else {
17443:                 $table .= '<td class="LC_left_item">';
17444:             }
17445:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
17446:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17447:             $table .= '<span class="LC_nobreak"><label>'.
17448:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
17449:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
17450:             if ($user ne $dcname.':'.$dcdom) {
17451:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17452:             }
17453:             $table .= '</label></span></td>';
17454:         }
17455:         $table .= '</tr></table>';
17456:     } elsif ($numdcs == 1) {
17457:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
17458:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17459:         if ($inputtype eq 'radio') {
17460:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
17461:             if ($user ne $dcname.':'.$dcdom) {
17462:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17463:             }
17464:         } else {
17465:             my $check;
17466:             if (exists($currhash{$domcoord[0]})) {
17467:                 $check = ' checked="checked"';
17468:             }
17469:             $table = '<span class="LC_nobreak"><label>'.
17470:                      '<input type="checkbox" name="'.$name.'" '.
17471:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
17472:             if ($user ne $dcname.':'.$dcdom) {
17473:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17474:             }
17475:             $table .= '</label></span>';
17476:             $rows ++;
17477:         }
17478:     }
17479:     return ($numdcs,$table,$rows);
17480: }
17481: 
17482: sub usersession_titles {
17483:     return &Apache::lonlocal::texthash(
17484:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
17485:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
17486:                spares => 'Servers offloaded to, when busy',
17487:                version => 'LON-CAPA version requirement',
17488:                excludedomain => 'Allow all, but exclude specific domains',
17489:                includedomain => 'Deny all, but include specific domains',
17490:                primary => 'Primary (checked first)',
17491:                default => 'Default',
17492:            );
17493: }
17494: 
17495: sub id_for_thisdom {
17496:     my (%servers) = @_;
17497:     my %altids;
17498:     foreach my $server (keys(%servers)) {
17499:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
17500:         if ($serverhome ne $server) {
17501:             $altids{$serverhome} = $server;
17502:         }
17503:     }
17504:     return %altids;
17505: }
17506: 
17507: sub count_servers {
17508:     my ($currbalancer,%servers) = @_;
17509:     my (@spares,$numspares);
17510:     foreach my $lonhost (sort(keys(%servers))) {
17511:         next if ($currbalancer eq $lonhost);
17512:         push(@spares,$lonhost);
17513:     }
17514:     if ($currbalancer) {
17515:         $numspares = scalar(@spares);
17516:     } else {
17517:         $numspares = scalar(@spares) - 1;
17518:     }
17519:     return ($numspares,@spares);
17520: }
17521: 
17522: sub lonbalance_targets_js {
17523:     my ($dom,$types,$servers,$settings) = @_;
17524:     my $select = &mt('Select');
17525:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
17526:     if (ref($servers) eq 'HASH') {
17527:         $alltargets = join("','",sort(keys(%{$servers})));
17528:         my @homedoms;
17529:         foreach my $server (sort(keys(%{$servers}))) {
17530:             if (&Apache::lonnet::host_domain($server) eq $dom) {
17531:                 push(@homedoms,'1');
17532:             } else {
17533:                 push(@homedoms,'0');
17534:             }
17535:         }
17536:         $allishome = join("','",@homedoms);
17537:     }
17538:     if (ref($types) eq 'ARRAY') {
17539:         if (@{$types} > 0) {
17540:             @alltypes = @{$types};
17541:         }
17542:     }
17543:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
17544:     $allinsttypes = join("','",@alltypes);
17545:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
17546:     if (ref($settings) eq 'HASH') {
17547:         %existing = %{$settings};
17548:     }
17549:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
17550:                               \%currtargets,\%currrules,\%currcookies);
17551:     my $balancers = join("','",sort(keys(%currbalancer)));
17552:     return <<"END";
17553: 
17554: <script type="text/javascript">
17555: // <![CDATA[
17556: 
17557: currBalancers = new Array('$balancers');
17558: 
17559: function toggleTargets(balnum) {
17560:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17561:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
17562:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
17563:     var prevbalancer = prevhostitem.value;
17564:     var baltotal = document.getElementById('loadbalancing_total').value;
17565:     prevhostitem.value = balancer;
17566:     if (prevbalancer != '') {
17567:         var prevIdx = currBalancers.indexOf(prevbalancer);
17568:         if (prevIdx != -1) {
17569:             currBalancers.splice(prevIdx,1);
17570:         }
17571:     }
17572:     if (balancer == '') {
17573:         hideSpares(balnum);
17574:     } else {
17575:         var currIdx = currBalancers.indexOf(balancer);
17576:         if (currIdx == -1) {
17577:             currBalancers.push(balancer);
17578:         }
17579:         var homedoms = new Array('$allishome');
17580:         var ishomedom = homedoms[lonhostitem.selectedIndex];
17581:         showSpares(balancer,ishomedom,balnum);
17582:     }
17583:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
17584:     return;
17585: }
17586: 
17587: function showSpares(balancer,ishomedom,balnum) {
17588:     var alltargets = new Array('$alltargets');
17589:     var insttypes = new Array('$allinsttypes');
17590:     var offloadtypes = new Array('primary','default');
17591: 
17592:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
17593:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
17594:  
17595:     for (var i=0; i<offloadtypes.length; i++) {
17596:         var count = 0;
17597:         for (var j=0; j<alltargets.length; j++) {
17598:             if (alltargets[j] != balancer) {
17599:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
17600:                 item.value = alltargets[j];
17601:                 item.style.textAlign='left';
17602:                 item.style.textFace='normal';
17603:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
17604:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
17605:                     item.disabled = '';
17606:                 } else {
17607:                     item.disabled = 'disabled';
17608:                     item.checked = false;
17609:                 }
17610:                 count ++;
17611:             }
17612:         }
17613:     }
17614:     for (var k=0; k<insttypes.length; k++) {
17615:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
17616:             if (ishomedom == 1) {
17617:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17618:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17619:             } else {
17620:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17621:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17622:             }
17623:         } else {
17624:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17625:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17626:         }
17627:         if ((insttypes[k] != '_LC_external') && 
17628:             ((insttypes[k] != '_LC_internetdom') ||
17629:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
17630:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
17631:             item.options.length = 0;
17632:             item.options[0] = new Option("","",true,true);
17633:             var idx = 0;
17634:             for (var m=0; m<alltargets.length; m++) {
17635:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
17636:                     idx ++;
17637:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17638:                 }
17639:             }
17640:         }
17641:     }
17642:     return;
17643: }
17644: 
17645: function hideSpares(balnum) {
17646:     var alltargets = new Array('$alltargets');
17647:     var insttypes = new Array('$allinsttypes');
17648:     var offloadtypes = new Array('primary','default');
17649: 
17650:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
17651:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
17652: 
17653:     var total = alltargets.length - 1;
17654:     for (var i=0; i<offloadtypes; i++) {
17655:         for (var j=0; j<total; j++) {
17656:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
17657:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
17658:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
17659:         }
17660:     }
17661:     for (var k=0; k<insttypes.length; k++) {
17662:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17663:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17664:         if (insttypes[k] != '_LC_external') {
17665:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
17666:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
17667:         }
17668:     }
17669:     return;
17670: }
17671: 
17672: function checkOffloads(item,balnum,type) {
17673:     var alltargets = new Array('$alltargets');
17674:     var offloadtypes = new Array('primary','default');
17675:     if (item.checked) {
17676:         var total = alltargets.length - 1;
17677:         var other;
17678:         if (type == offloadtypes[0]) {
17679:             other = offloadtypes[1];
17680:         } else {
17681:             other = offloadtypes[0];
17682:         }
17683:         for (var i=0; i<total; i++) {
17684:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
17685:             if (server == item.value) {
17686:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
17687:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
17688:                 }
17689:             }
17690:         }
17691:     }
17692:     return;
17693: }
17694: 
17695: function singleServerToggle(balnum,type) {
17696:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
17697:     if (offloadtoSelIdx == 0) {
17698:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
17699:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17700: 
17701:     } else {
17702:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
17703:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17704:     }
17705:     return;
17706: }
17707: 
17708: function balanceruleChange(formname,balnum,type) {
17709:     if (type == '_LC_external') {
17710:         return;
17711:     }
17712:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
17713:     for (var i=0; i<typesRules.length; i++) {
17714:         if (formname.elements[typesRules[i]].checked) {
17715:             if (formname.elements[typesRules[i]].value != 'specific') {
17716:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
17717:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17718:             } else {
17719:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17720:             }
17721:         }
17722:     }
17723:     return;
17724: }
17725: 
17726: function balancerDeleteChange(balnum) {
17727:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17728:     var baltotal = document.getElementById('loadbalancing_total').value;
17729:     var addtarget;
17730:     var removetarget;
17731:     var action = 'delete';
17732:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
17733:         var lonhost = hostitem.value;
17734:         var currIdx = currBalancers.indexOf(lonhost);
17735:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
17736:             if (currIdx != -1) {
17737:                 currBalancers.splice(currIdx,1);
17738:             }
17739:             addtarget = lonhost;
17740:         } else {
17741:             if (currIdx == -1) {
17742:                 currBalancers.push(lonhost);
17743:             }
17744:             removetarget = lonhost;
17745:             action = 'undelete';
17746:         }
17747:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
17748:     }
17749:     return;
17750: }
17751: 
17752: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
17753:     if (baltotal > 1) {
17754:         var offloadtypes = new Array('primary','default');
17755:         var alltargets = new Array('$alltargets');
17756:         var insttypes = new Array('$allinsttypes');
17757:         for (var i=0; i<baltotal; i++) {
17758:             if (i != balnum) {
17759:                 for (var j=0; j<offloadtypes.length; j++) {
17760:                     var total = alltargets.length - 1;
17761:                     for (var k=0; k<total; k++) {
17762:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
17763:                         var server = serveritem.value;
17764:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
17765:                             if (server == addtarget) {
17766:                                 serveritem.disabled = '';
17767:                             }
17768:                         }
17769:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
17770:                             if (server == removetarget) {
17771:                                 serveritem.disabled = 'disabled';
17772:                                 serveritem.checked = false;
17773:                             }
17774:                         }
17775:                     }
17776:                 }
17777:                 for (var j=0; j<insttypes.length; j++) {
17778:                     if (insttypes[j] != '_LC_external') {
17779:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
17780:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
17781:                             var currSel = singleserver.selectedIndex;
17782:                             var currVal = singleserver.options[currSel].value;
17783:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
17784:                                 var numoptions = singleserver.options.length;
17785:                                 var needsnew = 1;
17786:                                 for (var k=0; k<numoptions; k++) {
17787:                                     if (singleserver.options[k] == addtarget) {
17788:                                         needsnew = 0;
17789:                                         break;
17790:                                     }
17791:                                 }
17792:                                 if (needsnew == 1) {
17793:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
17794:                                 }
17795:                             }
17796:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
17797:                                 singleserver.options.length = 0;
17798:                                 if ((currVal) && (currVal != removetarget)) {
17799:                                     singleserver.options[0] = new Option("","",false,false);
17800:                                 } else {
17801:                                     singleserver.options[0] = new Option("","",true,true);
17802:                                 }
17803:                                 var idx = 0;
17804:                                 for (var m=0; m<alltargets.length; m++) {
17805:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
17806:                                         idx ++;
17807:                                         if (currVal == alltargets[m]) {
17808:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
17809:                                         } else {
17810:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17811:                                         }
17812:                                     }
17813:                                 }
17814:                             }
17815:                         }
17816:                     }
17817:                 }
17818:             }
17819:         }
17820:     }
17821:     return;
17822: }
17823: 
17824: // ]]>
17825: </script>
17826: 
17827: END
17828: }
17829: 
17830: sub new_spares_js {
17831:     my @sparestypes = ('primary','default');
17832:     my $types = join("','",@sparestypes);
17833:     my $select = &mt('Select');
17834:     return <<"END";
17835: 
17836: <script type="text/javascript">
17837: // <![CDATA[
17838: 
17839: function updateNewSpares(formname,lonhost) {
17840:     var types = new Array('$types');
17841:     var include = new Array();
17842:     var exclude = new Array();
17843:     for (var i=0; i<types.length; i++) {
17844:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
17845:         for (var j=0; j<spareboxes.length; j++) {
17846:             if (formname.elements[spareboxes[j]].checked) {
17847:                 exclude.push(formname.elements[spareboxes[j]].value);
17848:             } else {
17849:                 include.push(formname.elements[spareboxes[j]].value);
17850:             }
17851:         }
17852:     }
17853:     for (var i=0; i<types.length; i++) {
17854:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
17855:         var selIdx = newSpare.selectedIndex;
17856:         var currnew = newSpare.options[selIdx].value;
17857:         var okSpares = new Array();
17858:         for (var j=0; j<newSpare.options.length; j++) {
17859:             var possible = newSpare.options[j].value;
17860:             if (possible != '') {
17861:                 if (exclude.indexOf(possible) == -1) {
17862:                     okSpares.push(possible);
17863:                 } else {
17864:                     if (currnew == possible) {
17865:                         selIdx = 0;
17866:                     }
17867:                 }
17868:             }
17869:         }
17870:         for (var k=0; k<include.length; k++) {
17871:             if (okSpares.indexOf(include[k]) == -1) {
17872:                 okSpares.push(include[k]);
17873:             }
17874:         }
17875:         okSpares.sort();
17876:         newSpare.options.length = 0;
17877:         if (selIdx == 0) {
17878:             newSpare.options[0] = new Option("$select","",true,true);
17879:         } else {
17880:             newSpare.options[0] = new Option("$select","",false,false);
17881:         }
17882:         for (var m=0; m<okSpares.length; m++) {
17883:             var idx = m+1;
17884:             var selThis = 0;
17885:             if (selIdx != 0) {
17886:                 if (okSpares[m] == currnew) {
17887:                     selThis = 1;
17888:                 }
17889:             }
17890:             if (selThis == 1) {
17891:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
17892:             } else {
17893:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
17894:             }
17895:         }
17896:     }
17897:     return;
17898: }
17899: 
17900: function checkNewSpares(lonhost,type) {
17901:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
17902:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
17903:     if (chosen != '') { 
17904:         var othertype;
17905:         var othernewSpare;
17906:         if (type == 'primary') {
17907:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
17908:         }
17909:         if (type == 'default') {
17910:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
17911:         }
17912:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
17913:             othernewSpare.selectedIndex = 0;
17914:         }
17915:     }
17916:     return;
17917: }
17918: 
17919: // ]]>
17920: </script>
17921: 
17922: END
17923: 
17924: }
17925: 
17926: sub common_domprefs_js {
17927:     return <<"END";
17928: 
17929: <script type="text/javascript">
17930: // <![CDATA[
17931: 
17932: function getIndicesByName(formname,item) {
17933:     var group = new Array();
17934:     for (var i=0;i<formname.elements.length;i++) {
17935:         if (formname.elements[i].name == item) {
17936:             group.push(formname.elements[i].id);
17937:         }
17938:     }
17939:     return group;
17940: }
17941: 
17942: // ]]>
17943: </script>
17944: 
17945: END
17946: 
17947: }
17948: 
17949: sub recaptcha_js {
17950:     my %lt = &captcha_phrases();
17951:     return <<"END";
17952: 
17953: <script type="text/javascript">
17954: // <![CDATA[
17955: 
17956: function updateCaptcha(caller,context) {
17957:     var privitem;
17958:     var pubitem;
17959:     var privtext;
17960:     var pubtext;
17961:     var versionitem;
17962:     var versiontext;
17963:     if (document.getElementById(context+'_recaptchapub')) {
17964:         pubitem = document.getElementById(context+'_recaptchapub');
17965:     } else {
17966:         return;
17967:     }
17968:     if (document.getElementById(context+'_recaptchapriv')) {
17969:         privitem = document.getElementById(context+'_recaptchapriv');
17970:     } else {
17971:         return;
17972:     }
17973:     if (document.getElementById(context+'_recaptchapubtxt')) {
17974:         pubtext = document.getElementById(context+'_recaptchapubtxt');
17975:     } else {
17976:         return;
17977:     }
17978:     if (document.getElementById(context+'_recaptchaprivtxt')) {
17979:         privtext = document.getElementById(context+'_recaptchaprivtxt');
17980:     } else {
17981:         return;
17982:     }
17983:     if (document.getElementById(context+'_recaptchaversion')) {
17984:         versionitem = document.getElementById(context+'_recaptchaversion');
17985:     } else {
17986:         return;
17987:     }
17988:     if (document.getElementById(context+'_recaptchavertxt')) {
17989:         versiontext = document.getElementById(context+'_recaptchavertxt');
17990:     } else {
17991:         return;
17992:     }
17993:     if (caller.checked) {
17994:         if (caller.value == 'recaptcha') {
17995:             pubitem.type = 'text';
17996:             privitem.type = 'text';
17997:             pubitem.size = '40';
17998:             privitem.size = '40';
17999:             pubtext.innerHTML = "$lt{'pub'}";
18000:             privtext.innerHTML = "$lt{'priv'}";
18001:             versionitem.type = 'text';
18002:             versionitem.size = '3';
18003:             versiontext.innerHTML = "$lt{'ver'}";
18004:         } else {
18005:             pubitem.type = 'hidden';
18006:             privitem.type = 'hidden';
18007:             versionitem.type = 'hidden';
18008:             pubtext.innerHTML = '';
18009:             privtext.innerHTML = '';
18010:             versiontext.innerHTML = '';
18011:         }
18012:     }
18013:     return;
18014: }
18015: 
18016: // ]]>
18017: </script>
18018: 
18019: END
18020: 
18021: }
18022: 
18023: sub toggle_display_js {
18024:     return <<"END";
18025: 
18026: <script type="text/javascript">
18027: // <![CDATA[
18028: 
18029: function toggleDisplay(domForm,caller) {
18030:     if (document.getElementById(caller)) {
18031:         var divitem = document.getElementById(caller);
18032:         var optionsElement = domForm.coursecredits;
18033:         var checkval = 1;
18034:         var dispval = 'block';
18035:         var selfcreateRegExp = /^cancreate_emailverified/;
18036:         if (caller == 'emailoptions') {
18037:             optionsElement = domForm.cancreate_email; 
18038:         }
18039:         if (caller == 'studentsubmission') {
18040:             optionsElement = domForm.postsubmit;
18041:         }
18042:         if (caller == 'cloneinstcode') {
18043:             optionsElement = domForm.canclone;
18044:             checkval = 'instcode';
18045:         }
18046:         if (selfcreateRegExp.test(caller)) {
18047:             optionsElement = domForm.elements[caller];
18048:             checkval = 'other';
18049:             dispval = 'inline'
18050:         }
18051:         if (optionsElement.length) {
18052:             var currval;
18053:             for (var i=0; i<optionsElement.length; i++) {
18054:                 if (optionsElement[i].checked) {
18055:                    currval = optionsElement[i].value;
18056:                 }
18057:             }
18058:             if (currval == checkval) {
18059:                 divitem.style.display = dispval;
18060:             } else {
18061:                 divitem.style.display = 'none';
18062:             }
18063:         }
18064:     }
18065:     return;
18066: }
18067: 
18068: // ]]>
18069: </script>
18070: 
18071: END
18072: 
18073: }
18074: 
18075: sub captcha_phrases {
18076:     return &Apache::lonlocal::texthash (
18077:                  priv => 'Private key',
18078:                  pub  => 'Public key',
18079:                  original  => 'original (CAPTCHA)',
18080:                  recaptcha => 'successor (ReCAPTCHA)',
18081:                  notused   => 'unused',
18082:                  ver => 'ReCAPTCHA version (1 or 2)',
18083:     );
18084: }
18085: 
18086: sub devalidate_remote_domconfs {
18087:     my ($dom,$cachekeys) = @_;
18088:     return unless (ref($cachekeys) eq 'HASH');
18089:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
18090:     my %thismachine;
18091:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
18092:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
18093:     if (keys(%servers)) {
18094:         foreach my $server (keys(%servers)) {
18095:             next if ($thismachine{$server});
18096:             my @cached;
18097:             foreach my $name (@posscached) {
18098:                 if ($cachekeys->{$name}) {
18099:                     push(@cached,&escape($name).':'.&escape($dom));
18100:                 }
18101:             }
18102:             if (@cached) {
18103:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
18104:             }
18105:         }
18106:     }
18107:     return;
18108: }
18109: 
18110: 1;

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