File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.324: download - view: text, annotated - select for diffs
Mon Jan 1 01:29:38 2018 UTC (6 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754 LON-CAPA as LTI Consumer
  Domain configuration for information sent to Tool Provider on launch
  includes choice of: username, or username:domain in LON-CAPA,
  when "User" is included in the data to be sent (as lis_person_sourcedid).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.324 2018/01/01 01:29:38 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, and placement).  
  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 => 'Ask helpdesk form settings',
  356:                                 col2 => 'Value',},],
  357:                     print => \&print_contacts,
  358:                     modify => \&modify_contacts,
  359:                   },
  360:         'usercreation' => 
  361:                   { text => 'User creation',
  362:                     help => 'Domain_Configuration_User_Creation',
  363:                     header => [{col1 => 'Format rule type',
  364:                                 col2 => 'Format rules in force'},
  365:                                {col1 => 'User account creation',
  366:                                 col2 => 'Usernames which may be created',},
  367:                                {col1 => 'Context',
  368:                                 col2 => 'Assignable authentication types'}],
  369:                     print => \&print_usercreation,
  370:                     modify => \&modify_usercreation,
  371:                   },
  372:         'selfcreation' => 
  373:                   { text => 'Users self-creating accounts',
  374:                     help => 'Domain_Configuration_Self_Creation', 
  375:                     header => [{col1 => 'Self-creation with institutional username',
  376:                                 col2 => 'Enabled?'},
  377:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  378:                                 col2 => 'Information user can enter'},
  379:                                {col1 => 'Self-creation with e-mail verification',
  380:                                 col2 => 'Settings'}],
  381:                     print => \&print_selfcreation,
  382:                     modify => \&modify_selfcreation,
  383:                   },
  384:         'usermodification' =>
  385:                   { text => 'User modification',
  386:                     help => 'Domain_Configuration_User_Modification',
  387:                     header => [{col1 => 'Target user has role',
  388:                                 col2 => 'User information updatable in author context'},
  389:                                {col1 => 'Target user has role',
  390:                                 col2 => 'User information updatable in course context'}],
  391:                     print => \&print_usermodification,
  392:                     modify => \&modify_usermodification,
  393:                   },
  394:         'scantron' =>
  395:                   { text => 'Bubblesheet format file',
  396:                     help => 'Domain_Configuration_Scantron_Format',
  397:                     header => [ {col1 => 'Item',
  398:                                  col2 => '',
  399:                               }],
  400:                     print => \&print_scantron,
  401:                     modify => \&modify_scantron,
  402:                   },
  403:         'requestcourses' => 
  404:                  {text => 'Request creation of courses',
  405:                   help => 'Domain_Configuration_Request_Courses',
  406:                   header => [{col1 => 'User affiliation',
  407:                               col2 => 'Availability/Processing of requests',},
  408:                              {col1 => 'Setting',
  409:                               col2 => 'Value'},
  410:                              {col1 => 'Available textbooks',
  411:                               col2 => ''},
  412:                              {col1 => 'Available templates',
  413:                               col2 => ''},
  414:                              {col1 => 'Validation (not official courses)',
  415:                               col2 => 'Value'},],
  416:                   print => \&print_quotas,
  417:                   modify => \&modify_quotas,
  418:                  },
  419:         'requestauthor' =>
  420:                  {text => 'Request Authoring Space',
  421:                   help => 'Domain_Configuration_Request_Author',
  422:                   header => [{col1 => 'User affiliation',
  423:                               col2 => 'Availability/Processing of requests',},
  424:                              {col1 => 'Setting',
  425:                               col2 => 'Value'}],
  426:                   print => \&print_quotas,
  427:                   modify => \&modify_quotas,
  428:                  },
  429:         'coursecategories' =>
  430:                   { text => 'Cataloging of courses/communities',
  431:                     help => 'Domain_Configuration_Cataloging_Courses',
  432:                     header => [{col1 => 'Catalog type/availability',
  433:                                 col2 => '',},
  434:                                {col1 => 'Category settings for standard catalog',
  435:                                 col2 => '',},
  436:                                {col1 => 'Categories',
  437:                                 col2 => '',
  438:                                }],
  439:                     print => \&print_coursecategories,
  440:                     modify => \&modify_coursecategories,
  441:                   },
  442:         'serverstatuses' =>
  443:                  {text   => 'Access to server status pages',
  444:                   help   => 'Domain_Configuration_Server_Status',
  445:                   header => [{col1 => 'Status Page',
  446:                               col2 => 'Other named users',
  447:                               col3 => 'Specific IPs',
  448:                             }],
  449:                   print => \&print_serverstatuses,
  450:                   modify => \&modify_serverstatuses,
  451:                  },
  452:         'helpsettings' =>
  453:                  {text   => 'Support settings',
  454:                   help   => 'Domain_Configuration_Help_Settings',
  455:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  456:                               col2 => 'Value'},
  457:                              {col1 => 'Helpdesk Roles',
  458:                               col2 => 'Settings'},],
  459:                   print  => \&print_helpsettings,
  460:                   modify => \&modify_helpsettings,
  461:                  },
  462:         'coursedefaults' => 
  463:                  {text => 'Course/Community defaults',
  464:                   help => 'Domain_Configuration_Course_Defaults',
  465:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  466:                               col2 => 'Value',},
  467:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  468:                               col2 => 'Value',},],
  469:                   print => \&print_coursedefaults,
  470:                   modify => \&modify_coursedefaults,
  471:                  },
  472:         'selfenrollment' => 
  473:                  {text   => 'Self-enrollment in Course/Community',
  474:                   help   => 'Domain_Configuration_Selfenrollment',
  475:                   header => [{col1 => 'Configuration Rights',
  476:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  477:                              {col1 => 'Defaults',
  478:                               col2 => 'Value'},
  479:                              {col1 => 'Self-enrollment validation (optional)',
  480:                               col2 => 'Value'},],
  481:                   print => \&print_selfenrollment,
  482:                   modify => \&modify_selfenrollment,
  483:                  },
  484:         'privacy' => 
  485:                  {text   => 'User Privacy',
  486:                   help   => 'Domain_Configuration_User_Privacy',
  487:                   header => [{col1 => 'Setting',
  488:                               col2 => 'Value',}],
  489:                   print => \&print_privacy,
  490:                   modify => \&modify_privacy,
  491:                  },
  492:         'usersessions' =>
  493:                  {text  => 'User session hosting/offloading',
  494:                   help  => 'Domain_Configuration_User_Sessions',
  495:                   header => [{col1 => 'Domain server',
  496:                               col2 => 'Servers to offload sessions to when busy'},
  497:                              {col1 => 'Hosting of users from other domains',
  498:                               col2 => 'Rules'},
  499:                              {col1 => "Hosting domain's own users elsewhere",
  500:                               col2 => 'Rules'}],
  501:                   print => \&print_usersessions,
  502:                   modify => \&modify_usersessions,
  503:                  },
  504:         'loadbalancing' =>
  505:                  {text  => 'Dedicated Load Balancer(s)',
  506:                   help  => 'Domain_Configuration_Load_Balancing',
  507:                   header => [{col1 => 'Balancers',
  508:                               col2 => 'Default destinations',
  509:                               col3 => 'User affiliation',
  510:                               col4 => 'Overrides'},
  511:                             ],
  512:                   print => \&print_loadbalancing,
  513:                   modify => \&modify_loadbalancing,
  514:                  },
  515:         'ltitools' => 
  516:                  {text => 'External Tools (LTI)',
  517:                   help => 'Domain_Configuration_LTI_Tools',
  518:                   header => [{col1 => 'Setting',
  519:                               col2 => 'Value',}],
  520:                   print => \&print_ltitools,
  521:                   modify => \&modify_ltitools,
  522:                  },
  523:         'ssl' =>
  524:                  {text  => 'LON-CAPA Network (SSL)',
  525:                   help  => 'Domain_Configuration_Network_SSL',
  526:                   header => [{col1 => 'Server',
  527:                               col2 => 'Certificate Status'},
  528:                              {col1 => 'Connections to other servers',
  529:                               col2 => 'Rules'},
  530:                              {col1 => 'Connections from other servers',
  531:                               col2 => 'Rules'},
  532:                              {col1 => "Replicating domain's published content",
  533:                               col2 => 'Rules'}],
  534:                   print => \&print_ssl,
  535:                   modify => \&modify_ssl,
  536:                  },
  537:         'trust' =>
  538:                  {text   => 'Trust Settings',
  539:                   help   => 'Domain_Configuration_Trust',
  540:                   header => [{col1 => "Access to this domain's content by others",
  541:                               col2 => 'Rules'},
  542:                              {col1 => "Access to other domain's content by this domain",
  543:                               col2 => 'Rules'},
  544:                              {col1 => "Enrollment in this domain's courses by others",
  545:                               col2 => 'Rules',},
  546:                              {col1 => "Co-author roles in this domain for others",
  547:                               col2 => 'Rules',},
  548:                              {col1 => "Co-author roles for this domain's users elsewhere",
  549:                               col2 => 'Rules',},
  550:                              {col1 => "Domain roles in this domain assignable to others",
  551:                               col2 => 'Rules'},
  552:                              {col1 => "Course catalog for this domain displayed elsewhere",
  553:                               col2 => 'Rules'},
  554:                              {col1 => "Requests for creation of courses in this domain by others",
  555:                               col2 => 'Rules'},
  556:                              {col1 => "Users in other domains can send messages to this domain",
  557:                               col2 => 'Rules'},],
  558:                   print => \&print_trust,
  559:                   modify => \&modify_trust,
  560:                  },
  561:           'lti' =>
  562:                  {text => 'LTI Provider',
  563:                   help => 'Domain_Configuration_LTI_Provider',
  564:                   header => [{col1 => 'Setting',
  565:                               col2 => 'Value',}],
  566:                   print => \&print_lti,
  567:                   modify => \&modify_lti,
  568:                  },
  569:     );
  570:     if (keys(%servers) > 1) {
  571:         $prefs{'login'}  = { text   => 'Log-in page options',
  572:                              help   => 'Domain_Configuration_Login_Page',
  573:                             header => [{col1 => 'Log-in Service',
  574:                                         col2 => 'Server Setting',},
  575:                                        {col1 => 'Log-in Page Items',
  576:                                         col2 => ''},
  577:                                        {col1 => 'Log-in Help',
  578:                                         col2 => 'Value'},
  579:                                        {col1 => 'Custom HTML in document head',
  580:                                         col2 => 'Value'}],
  581:                             print => \&print_login,
  582:                             modify => \&modify_login,
  583:                            };
  584:     }
  585: 
  586:     my @roles = ('student','coordinator','author','admin');
  587:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  588:     &Apache::lonhtmlcommon::add_breadcrumb
  589:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  590:       text=>"Settings to display/modify"});
  591:     my $confname = $dom.'-domainconfig';
  592: 
  593:     if ($phase eq 'process') {
  594:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  595:                                                               \%prefs,\%domconfig,$confname,\@roles);
  596:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  597:             $r->rflush();
  598:             &devalidate_remote_domconfs($dom,$result);
  599:         }
  600:     } elsif ($phase eq 'display') {
  601:         my $js = &recaptcha_js().
  602:                  &toggle_display_js();
  603:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  604:             my ($othertitle,$usertypes,$types) =
  605:                 &Apache::loncommon::sorted_inst_types($dom);
  606:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  607:                                           $domconfig{'loadbalancing'}).
  608:                    &new_spares_js().
  609:                    &common_domprefs_js().
  610:                    &Apache::loncommon::javascript_array_indexof();
  611:         }
  612:         if (grep(/^requestcourses$/,@actions)) {
  613:             my $javascript_validations;
  614:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  615:             $js .= <<END;
  616: <script type="text/javascript">
  617: $javascript_validations
  618: </script>
  619: $coursebrowserjs
  620: END
  621:         }
  622:         if (grep(/^selfcreation$/,@actions)) {
  623:             $js .= &selfcreate_javascript();
  624:         }
  625:         if (grep(/^contacts$/,@actions)) {
  626:             $js .= &contacts_javascript();
  627:         }
  628:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  629:     } else {
  630: # check if domconfig user exists for the domain.
  631:         my $servadm = $r->dir_config('lonAdmEMail');
  632:         my ($configuserok,$author_ok,$switchserver) =
  633:             &config_check($dom,$confname,$servadm);
  634:         unless ($configuserok eq 'ok') {
  635:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  636:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  637:                           $confname).
  638:                       '<br />'
  639:             );
  640:             if ($switchserver) {
  641:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  642:                           '<br />'.
  643:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  644:                           '<br />'.
  645:                           &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).
  646:                           '<br />'.
  647:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  648:                 );
  649:             } else {
  650:                 $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.').
  651:                           '<br />'.
  652:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  653:                 );
  654:             }
  655:             $r->print(&Apache::loncommon::end_page());
  656:             return OK;
  657:         }
  658:         if (keys(%domconfig) == 0) {
  659:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  660:             my @ids=&Apache::lonnet::current_machine_ids();
  661:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  662:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  663:                 my @loginimages = ('img','logo','domlogo','login');
  664:                 my $custom_img_count = 0;
  665:                 foreach my $img (@loginimages) {
  666:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  667:                         $custom_img_count ++;
  668:                     }
  669:                 }
  670:                 foreach my $role (@roles) {
  671:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  672:                         $custom_img_count ++;
  673:                     }
  674:                 }
  675:                 if ($custom_img_count > 0) {
  676:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  677:                     my $switch_server = &check_switchserver($dom,$confname);
  678:                     $r->print(
  679:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  680:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  681:     &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 />'.
  682:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  683:                     if ($switch_server) {
  684:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  685:                     }
  686:                     $r->print(&Apache::loncommon::end_page());
  687:                     return OK;
  688:                 }
  689:             }
  690:         }
  691:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  692:     }
  693:     return OK;
  694: }
  695: 
  696: sub process_changes {
  697:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  698:     my %domconfig;
  699:     if (ref($values) eq 'HASH') {
  700:         %domconfig = %{$values};
  701:     }
  702:     my $output;
  703:     if ($action eq 'login') {
  704:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  705:     } elsif ($action eq 'rolecolors') {
  706:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  707:                                      $lastactref,%domconfig);
  708:     } elsif ($action eq 'quotas') {
  709:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  710:     } elsif ($action eq 'autoenroll') {
  711:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  712:     } elsif ($action eq 'autoupdate') {
  713:         $output = &modify_autoupdate($dom,%domconfig);
  714:     } elsif ($action eq 'autocreate') {
  715:         $output = &modify_autocreate($dom,%domconfig);
  716:     } elsif ($action eq 'directorysrch') {
  717:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  718:     } elsif ($action eq 'usercreation') {
  719:         $output = &modify_usercreation($dom,%domconfig);
  720:     } elsif ($action eq 'selfcreation') {
  721:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  722:     } elsif ($action eq 'usermodification') {
  723:         $output = &modify_usermodification($dom,%domconfig);
  724:     } elsif ($action eq 'contacts') {
  725:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  726:     } elsif ($action eq 'defaults') {
  727:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  728:     } elsif ($action eq 'scantron') {
  729:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  730:     } elsif ($action eq 'coursecategories') {
  731:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  732:     } elsif ($action eq 'serverstatuses') {
  733:         $output = &modify_serverstatuses($dom,%domconfig);
  734:     } elsif ($action eq 'requestcourses') {
  735:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  736:     } elsif ($action eq 'requestauthor') {
  737:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  738:     } elsif ($action eq 'helpsettings') {
  739:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  740:     } elsif ($action eq 'coursedefaults') {
  741:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  742:     } elsif ($action eq 'selfenrollment') {
  743:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  744:     } elsif ($action eq 'usersessions') {
  745:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  746:     } elsif ($action eq 'loadbalancing') {
  747:         $output = &modify_loadbalancing($dom,%domconfig);
  748:     } elsif ($action eq 'ltitools') {
  749:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  750:     } elsif ($action eq 'ssl') {
  751:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  752:     } elsif ($action eq 'trust') {
  753:         $output = &modify_trust($dom,$lastactref,%domconfig);
  754:     } elsif ($action eq 'lti') {
  755:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  756:     }
  757:     return $output;
  758: }
  759: 
  760: sub print_config_box {
  761:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  762:     my $rowtotal = 0;
  763:     my $output;
  764:     if ($action eq 'coursecategories') {
  765:         $output = &coursecategories_javascript($settings);
  766:     } elsif ($action eq 'defaults') {
  767:         $output = &defaults_javascript($settings); 
  768:     } elsif ($action eq 'helpsettings') {
  769:         my (%privs,%levelscurrent);
  770:         my %full=();
  771:         my %levels=(
  772:                      course => {},
  773:                      domain => {},
  774:                      system => {},
  775:                    );
  776:         my $context = 'domain';
  777:         my $crstype = 'Course';
  778:         my $formname = 'display';
  779:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  780:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  781:         $output =
  782:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  783:                                                       \@templateroles);
  784:     }
  785:     $output .=
  786:          '<table class="LC_nested_outer">
  787:           <tr>
  788:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  789:            &mt($item->{text}).'&nbsp;'.
  790:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  791:           '</tr>';
  792:     $rowtotal ++;
  793:     my $numheaders = 1;
  794:     if (ref($item->{'header'}) eq 'ARRAY') {
  795:         $numheaders = scalar(@{$item->{'header'}});
  796:     }
  797:     if ($numheaders > 1) {
  798:         my $colspan = '';
  799:         my $rightcolspan = '';
  800:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  801:             ($action eq 'directorysrch') ||
  802:             (($action eq 'login') && ($numheaders < 4))) {
  803:             $colspan = ' colspan="2"';
  804:         }
  805:         if ($action eq 'usersessions') {
  806:             $rightcolspan = ' colspan="3"'; 
  807:         }
  808:         $output .= '
  809:           <tr>
  810:            <td>
  811:             <table class="LC_nested">
  812:              <tr class="LC_info_row">
  813:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  814:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  815:              </tr>';
  816:         $rowtotal ++;
  817:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  818:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  819:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  820:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  821:             ($action eq 'contacts')) {
  822:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  823:         } elsif ($action eq 'coursecategories') {
  824:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  825:         } elsif ($action eq 'login') {
  826:             if ($numheaders == 4) {
  827:                 $colspan = ' colspan="2"';
  828:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  829:             } else {
  830:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  831:             }
  832:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  833:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  834:         } elsif ($action eq 'rolecolors') {
  835:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  836:         }
  837:         $output .= '
  838:            </table>
  839:           </td>
  840:          </tr>
  841:          <tr>
  842:            <td>
  843:             <table class="LC_nested">
  844:              <tr class="LC_info_row">
  845:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  846:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  847:              </tr>';
  848:             $rowtotal ++;
  849:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  850:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  851:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  852:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults')) {
  853:             if ($action eq 'coursecategories') {
  854:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  855:                 $colspan = ' colspan="2"';
  856:             } elsif ($action eq 'trust') {
  857:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  858:             } else {
  859:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  860:             }
  861:             if ($action eq 'trust') {
  862:                 $output .= '
  863:             </table>
  864:           </td>
  865:          </tr>';
  866:                 my @trusthdrs = qw(2 3 4 5 6 7);
  867:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  868:                 for (my $i=0; $i<@trusthdrs; $i++) {
  869:                     $output .= '
  870:          <tr>
  871:            <td>
  872:             <table class="LC_nested">
  873:              <tr class="LC_info_row">
  874:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  875:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  876:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  877:             </table>
  878:           </td>
  879:          </tr>';
  880:                 }
  881:                 $output .= '
  882:          <tr>
  883:            <td>
  884:             <table class="LC_nested">
  885:              <tr class="LC_info_row">
  886:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  887:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  888:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  889:             } else {
  890:                 $output .= '
  891:            </table>
  892:           </td>
  893:          </tr>
  894:          <tr>
  895:            <td>
  896:             <table class="LC_nested">
  897:              <tr class="LC_info_row">
  898:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  899:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  900:              </tr>'."\n";
  901:                 if ($action eq 'coursecategories') {
  902:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  903:                 } else {
  904:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  905:                 }
  906:             }
  907:             $rowtotal ++;
  908:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  909:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  910:                  ($action eq 'helpsettings')) {
  911:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  912:         } elsif ($action eq 'ssl') {
  913:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  914:             </table>
  915:           </td>
  916:          </tr>
  917:          <tr>
  918:            <td>
  919:             <table class="LC_nested">
  920:              <tr class="LC_info_row">
  921:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  922:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  923:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  924:             </table>
  925:           </td>
  926:          </tr>
  927:          <tr>
  928:            <td>
  929:             <table class="LC_nested">
  930:              <tr class="LC_info_row">
  931:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  932:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  933:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  934:         } elsif ($action eq 'login') {
  935:             if ($numheaders == 4) {
  936:                 $output .= &print_login('page',$dom,$confname,$phase,$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:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  947:                 $rowtotal ++;
  948:             } else {
  949:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  950:             }
  951:             $output .= '
  952:            </table>
  953:           </td>
  954:          </tr>
  955:          <tr>
  956:            <td>
  957:             <table class="LC_nested">
  958:              <tr class="LC_info_row">';
  959:             if ($numheaders == 4) {
  960:                 $output .= '
  961:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  962:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  963:              </tr>';
  964:             } else {
  965:                 $output .= '
  966:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  967:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  968:              </tr>';
  969:             }
  970:             $rowtotal ++;
  971:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  972:         } elsif ($action eq 'requestcourses') {
  973:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  974:             $rowtotal ++;
  975:             $output .= &print_studentcode($settings,\$rowtotal).'
  976:            </table>
  977:           </td>
  978:          </tr>
  979:          <tr>
  980:            <td>
  981:             <table class="LC_nested">
  982:              <tr class="LC_info_row">
  983:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  984:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  985:                        &textbookcourses_javascript($settings).
  986:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  987:             </table>
  988:            </td>
  989:           </tr>
  990:          <tr>
  991:            <td>
  992:             <table class="LC_nested">
  993:              <tr class="LC_info_row">
  994:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  995:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  996:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  997:             </table>
  998:            </td>
  999:           </tr>
 1000:           <tr>
 1001:            <td>
 1002:             <table class="LC_nested">
 1003:              <tr class="LC_info_row">
 1004:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1005:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1006:              </tr>'.
 1007:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1008:         } elsif ($action eq 'requestauthor') {
 1009:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1010:             $rowtotal ++;
 1011:         } elsif ($action eq 'rolecolors') {
 1012:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1013:            </table>
 1014:           </td>
 1015:          </tr>
 1016:          <tr>
 1017:            <td>
 1018:             <table class="LC_nested">
 1019:              <tr class="LC_info_row">
 1020:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1021:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1022:               <td class="LC_right_item" style="vertical-align: top">'.
 1023:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1024:              </tr>'.
 1025:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1026:            </table>
 1027:           </td>
 1028:          </tr>
 1029:          <tr>
 1030:            <td>
 1031:             <table class="LC_nested">
 1032:              <tr class="LC_info_row">
 1033:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1034:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1035:              </tr>'.
 1036:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1037:             $rowtotal += 2;
 1038:         }
 1039:     } else {
 1040:         $output .= '
 1041:           <tr>
 1042:            <td>
 1043:             <table class="LC_nested">
 1044:              <tr class="LC_info_row">';
 1045:         if ($action eq 'login') {
 1046:             $output .= '  
 1047:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1048:         } elsif ($action eq 'serverstatuses') {
 1049:             $output .= '
 1050:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1051:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1052: 
 1053:         } else {
 1054:             $output .= '
 1055:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1056:         }
 1057:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1058:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1059:                        &mt($item->{'header'}->[0]->{'col2'});
 1060:             if ($action eq 'serverstatuses') {
 1061:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1062:             } 
 1063:         } else {
 1064:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1065:                        &mt($item->{'header'}->[0]->{'col2'});
 1066:         }
 1067:         $output .= '</td>';
 1068:         if ($item->{'header'}->[0]->{'col3'}) {
 1069:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1070:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1071:                             &mt($item->{'header'}->[0]->{'col3'});
 1072:             } else {
 1073:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1074:                            &mt($item->{'header'}->[0]->{'col3'});
 1075:             }
 1076:             if ($action eq 'serverstatuses') {
 1077:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1078:             }
 1079:             $output .= '</td>';
 1080:         }
 1081:         if ($item->{'header'}->[0]->{'col4'}) {
 1082:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1083:                        &mt($item->{'header'}->[0]->{'col4'});
 1084:         }
 1085:         $output .= '</tr>';
 1086:         $rowtotal ++;
 1087:         if ($action eq 'quotas') {
 1088:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1089:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1090:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1091:                  ($action eq 'ltitools') || ($action eq 'lti')) {
 1092:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1093:         } elsif ($action eq 'scantron') {
 1094:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1095:         }
 1096:     }
 1097:     $output .= '
 1098:    </table>
 1099:   </td>
 1100:  </tr>
 1101: </table><br />';
 1102:     return ($output,$rowtotal);
 1103: }
 1104: 
 1105: sub print_login {
 1106:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1107:     my ($css_class,$datatable);
 1108:     my %choices = &login_choices();
 1109: 
 1110:     if ($caller eq 'service') {
 1111:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1112:         my $choice = $choices{'disallowlogin'};
 1113:         $css_class = ' class="LC_odd_row"';
 1114:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1115:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1116:                       '<th>'.$choices{'server'}.'</th>'.
 1117:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1118:                       '<th>'.$choices{'custompath'}.'</th>'.
 1119:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1120:         my %disallowed;
 1121:         if (ref($settings) eq 'HASH') {
 1122:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1123:                %disallowed = %{$settings->{'loginvia'}};
 1124:             }
 1125:         }
 1126:         foreach my $lonhost (sort(keys(%servers))) {
 1127:             my $direct = 'selected="selected"';
 1128:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1129:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1130:                     $direct = '';
 1131:                 }
 1132:             }
 1133:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1134:                           '<td><select name="'.$lonhost.'_server">'.
 1135:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1136:                           '</option>';
 1137:             foreach my $hostid (sort(keys(%servers))) {
 1138:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1139:                 my $selected = '';
 1140:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1141:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1142:                         $selected = 'selected="selected"';
 1143:                     }
 1144:                 }
 1145:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1146:                               $servers{$hostid}.'</option>';
 1147:             }
 1148:             $datatable .= '</select></td>'.
 1149:                           '<td><select name="'.$lonhost.'_serverpath">';
 1150:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1151:                 my $pathname = $path;
 1152:                 if ($path eq 'custom') {
 1153:                     $pathname = &mt('Custom Path').' ->';
 1154:                 }
 1155:                 my $selected = '';
 1156:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1157:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1158:                         $selected = 'selected="selected"';
 1159:                     }
 1160:                 } elsif ($path eq '') {
 1161:                     $selected = 'selected="selected"';
 1162:                 }
 1163:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1164:             }
 1165:             $datatable .= '</select></td>';
 1166:             my ($custom,$exempt);
 1167:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1168:                 $custom = $disallowed{$lonhost}{'custompath'};
 1169:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1170:             }
 1171:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1172:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1173:                           '</tr>';
 1174:         }
 1175:         $datatable .= '</table></td></tr>';
 1176:         return $datatable;
 1177:     } elsif ($caller eq 'page') {
 1178:         my %defaultchecked = ( 
 1179:                                'coursecatalog' => 'on',
 1180:                                'helpdesk'      => 'on',
 1181:                                'adminmail'     => 'off',
 1182:                                'newuser'       => 'off',
 1183:                              );
 1184:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1185:         my (%checkedon,%checkedoff);
 1186:         foreach my $item (@toggles) {
 1187:             if ($defaultchecked{$item} eq 'on') { 
 1188:                 $checkedon{$item} = ' checked="checked" ';
 1189:                 $checkedoff{$item} = ' ';
 1190:             } elsif ($defaultchecked{$item} eq 'off') {
 1191:                 $checkedoff{$item} = ' checked="checked" ';
 1192:                 $checkedon{$item} = ' ';
 1193:             }
 1194:         }
 1195:         my @images = ('img','logo','domlogo','login');
 1196:         my @logintext = ('textcol','bgcol');
 1197:         my @bgs = ('pgbg','mainbg','sidebg');
 1198:         my @links = ('link','alink','vlink');
 1199:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1200:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1201:         my (%is_custom,%designs);
 1202:         my %defaults = (
 1203:                        font => $defaultdesign{'login.font'},
 1204:                        );
 1205:         foreach my $item (@images) {
 1206:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1207:             $defaults{'showlogo'}{$item} = 1;
 1208:         }
 1209:         foreach my $item (@bgs) {
 1210:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1211:         }
 1212:         foreach my $item (@logintext) {
 1213:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1214:         }
 1215:         foreach my $item (@links) {
 1216:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1217:         }
 1218:         if (ref($settings) eq 'HASH') {
 1219:             foreach my $item (@toggles) {
 1220:                 if ($settings->{$item} eq '1') {
 1221:                     $checkedon{$item} =  ' checked="checked" ';
 1222:                     $checkedoff{$item} = ' ';
 1223:                 } elsif ($settings->{$item} eq '0') {
 1224:                     $checkedoff{$item} =  ' checked="checked" ';
 1225:                     $checkedon{$item} = ' ';
 1226:                 }
 1227:             }
 1228:             foreach my $item (@images) {
 1229:                 if (defined($settings->{$item})) {
 1230:                     $designs{$item} = $settings->{$item};
 1231:                     $is_custom{$item} = 1;
 1232:                 }
 1233:                 if (defined($settings->{'showlogo'}{$item})) {
 1234:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1235:                 }
 1236:             }
 1237:             foreach my $item (@logintext) {
 1238:                 if ($settings->{$item} ne '') {
 1239:                     $designs{'logintext'}{$item} = $settings->{$item};
 1240:                     $is_custom{$item} = 1;
 1241:                 }
 1242:             }
 1243:             if ($settings->{'font'} ne '') {
 1244:                 $designs{'font'} = $settings->{'font'};
 1245:                 $is_custom{'font'} = 1;
 1246:             }
 1247:             foreach my $item (@bgs) {
 1248:                 if ($settings->{$item} ne '') {
 1249:                     $designs{'bgs'}{$item} = $settings->{$item};
 1250:                     $is_custom{$item} = 1;
 1251:                 }
 1252:             }
 1253:             foreach my $item (@links) {
 1254:                 if ($settings->{$item} ne '') {
 1255:                     $designs{'links'}{$item} = $settings->{$item};
 1256:                     $is_custom{$item} = 1;
 1257:                 }
 1258:             }
 1259:         } else {
 1260:             if ($designhash{$dom.'.login.font'} ne '') {
 1261:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1262:                 $is_custom{'font'} = 1;
 1263:             }
 1264:             foreach my $item (@images) {
 1265:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1266:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1267:                     $is_custom{$item} = 1;
 1268:                 }
 1269:             }
 1270:             foreach my $item (@bgs) {
 1271:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1272:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1273:                     $is_custom{$item} = 1;
 1274:                 }
 1275:             }
 1276:             foreach my $item (@links) {
 1277:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1278:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1279:                     $is_custom{$item} = 1;
 1280:                 }
 1281:             }
 1282:         }
 1283:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1284:                                                       logo => 'Institution Logo',
 1285:                                                       domlogo => 'Domain Logo',
 1286:                                                       login => 'Login box');
 1287:         my $itemcount = 1;
 1288:         foreach my $item (@toggles) {
 1289:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1290:             $datatable .=  
 1291:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1292:                 '</td><td>'.
 1293:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1294:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1295:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1296:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1297:                 '</tr>';
 1298:             $itemcount ++;
 1299:         }
 1300:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1301:         $datatable .= '</tr></table></td></tr>';
 1302:     } elsif ($caller eq 'help') {
 1303:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1304:         my $switchserver = &check_switchserver($dom,$confname);
 1305:         my $itemcount = 1;
 1306:         $defaulturl = '/adm/loginproblems.html';
 1307:         $defaulttype = 'default';
 1308:         %lt = &Apache::lonlocal::texthash (
 1309:                      del     => 'Delete?',
 1310:                      rep     => 'Replace:',
 1311:                      upl     => 'Upload:',
 1312:                      default => 'Default',
 1313:                      custom  => 'Custom',
 1314:                                              );
 1315:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1316:         my @currlangs;
 1317:         if (ref($settings) eq 'HASH') {
 1318:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1319:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1320:                     next if ($settings->{'helpurl'}{$key} eq '');
 1321:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1322:                     $type{$key} = 'custom';
 1323:                     unless ($key eq 'nolang') {
 1324:                         push(@currlangs,$key);
 1325:                     }
 1326:                 }
 1327:             } elsif ($settings->{'helpurl'} ne '') {
 1328:                 $type{'nolang'} = 'custom';
 1329:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1330:             }
 1331:         }
 1332:         foreach my $lang ('nolang',sort(@currlangs)) {
 1333:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1334:             $datatable .= '<tr'.$css_class.'>';
 1335:             if ($url{$lang} eq '') {
 1336:                 $url{$lang} = $defaulturl;
 1337:             }
 1338:             if ($type{$lang} eq '') {
 1339:                 $type{$lang} = $defaulttype;
 1340:             }
 1341:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1342:             if ($lang eq 'nolang') {
 1343:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1344:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1345:             } else {
 1346:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1347:                                   $langchoices{$lang},
 1348:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1349:             }
 1350:             $datatable .= '</span></td>'."\n".
 1351:                           '<td class="LC_left_item">';
 1352:             if ($type{$lang} eq 'custom') {
 1353:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1354:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1355:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1356:             } else {
 1357:                 $datatable .= $lt{'upl'};
 1358:             }
 1359:             $datatable .='<br />';
 1360:             if ($switchserver) {
 1361:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1362:             } else {
 1363:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1364:             }
 1365:             $datatable .= '</td></tr>';
 1366:             $itemcount ++;
 1367:         }
 1368:         my @addlangs;
 1369:         foreach my $lang (sort(keys(%langchoices))) {
 1370:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1371:             push(@addlangs,$lang);
 1372:         }
 1373:         if (@addlangs > 0) {
 1374:             my %toadd;
 1375:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1376:             $toadd{''} = &mt('Select');
 1377:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1378:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1379:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1380:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1381:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1382:             if ($switchserver) {
 1383:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1384:             } else {
 1385:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1386:             }
 1387:             $datatable .= '</td></tr>';
 1388:             $itemcount ++;
 1389:         }
 1390:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1391:     } elsif ($caller eq 'headtag') {
 1392:         my %domservers = &Apache::lonnet::get_servers($dom);
 1393:         my $choice = $choices{'headtag'};
 1394:         $css_class = ' class="LC_odd_row"';
 1395:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1396:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1397:                       '<th>'.$choices{'current'}.'</th>'.
 1398:                       '<th>'.$choices{'action'}.'</th>'.
 1399:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1400:         my (%currurls,%currexempt);
 1401:         if (ref($settings) eq 'HASH') {
 1402:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1403:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1404:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1405:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1406:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1407:                     }
 1408:                 }
 1409:             }
 1410:         }
 1411:         my %lt = &Apache::lonlocal::texthash(
 1412:                                                del  => 'Delete?',
 1413:                                                rep  => 'Replace:',
 1414:                                                upl  => 'Upload:',
 1415:                                                curr => 'View contents',
 1416:                                                none => 'None',
 1417:         );
 1418:         my $switchserver = &check_switchserver($dom,$confname);
 1419:         foreach my $lonhost (sort(keys(%domservers))) {
 1420:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1421:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1422:             if ($currurls{$lonhost}) {
 1423:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1424:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1425:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1426:                               '">'.$lt{'curr'}.'</a></td>'.
 1427:                               '<td><span class="LC_nobreak"><label>'.
 1428:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1429:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1430:             } else {
 1431:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1432:             }
 1433:             $datatable .='<br />';
 1434:             if ($switchserver) {
 1435:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1436:             } else {
 1437:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1438:             }
 1439:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1440:         }
 1441:         $datatable .= '</table></td></tr>';
 1442:     }
 1443:     return $datatable;
 1444: }
 1445: 
 1446: sub login_choices {
 1447:     my %choices =
 1448:         &Apache::lonlocal::texthash (
 1449:             coursecatalog => 'Display Course/Community Catalog link?',
 1450:             adminmail     => "Display Administrator's E-mail Address?",
 1451:             helpdesk      => 'Display "Contact Helpdesk" link',
 1452:             disallowlogin => "Login page requests redirected",
 1453:             hostid        => "Server",
 1454:             server        => "Redirect to:",
 1455:             serverpath    => "Path",
 1456:             custompath    => "Custom", 
 1457:             exempt        => "Exempt IP(s)",
 1458:             directlogin   => "No redirect",
 1459:             newuser       => "Link to create a user account",
 1460:             img           => "Header",
 1461:             logo          => "Main Logo",
 1462:             domlogo       => "Domain Logo",
 1463:             login         => "Log-in Header", 
 1464:             textcol       => "Text color",
 1465:             bgcol         => "Box color",
 1466:             bgs           => "Background colors",
 1467:             links         => "Link colors",
 1468:             font          => "Font color",
 1469:             pgbg          => "Header",
 1470:             mainbg        => "Page",
 1471:             sidebg        => "Login box",
 1472:             link          => "Link",
 1473:             alink         => "Active link",
 1474:             vlink         => "Visited link",
 1475:             headtag       => "Custom markup",
 1476:             action        => "Action",
 1477:             current       => "Current",
 1478:         );
 1479:     return %choices;
 1480: }
 1481: 
 1482: sub print_rolecolors {
 1483:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1484:     my %choices = &color_font_choices();
 1485:     my @bgs = ('pgbg','tabbg','sidebg');
 1486:     my @links = ('link','alink','vlink');
 1487:     my @images = ('img');
 1488:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1489:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1490:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1491:     my (%is_custom,%designs);
 1492:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1493:     if (ref($settings) eq 'HASH') {
 1494:         if (ref($settings->{$role}) eq 'HASH') {
 1495:             if ($settings->{$role}->{'img'} ne '') {
 1496:                 $designs{'img'} = $settings->{$role}->{'img'};
 1497:                 $is_custom{'img'} = 1;
 1498:             }
 1499:             if ($settings->{$role}->{'font'} ne '') {
 1500:                 $designs{'font'} = $settings->{$role}->{'font'};
 1501:                 $is_custom{'font'} = 1;
 1502:             }
 1503:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1504:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1505:                 $is_custom{'fontmenu'} = 1;
 1506:             }
 1507:             foreach my $item (@bgs) {
 1508:                 if ($settings->{$role}->{$item} ne '') {
 1509:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1510:                     $is_custom{$item} = 1;
 1511:                 }
 1512:             }
 1513:             foreach my $item (@links) {
 1514:                 if ($settings->{$role}->{$item} ne '') {
 1515:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1516:                     $is_custom{$item} = 1;
 1517:                 }
 1518:             }
 1519:         }
 1520:     } else {
 1521:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1522:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1523:             $is_custom{'img'} = 1;
 1524:         }
 1525:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1526:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1527:             $is_custom{'fontmenu'} = 1; 
 1528:         }
 1529:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1530:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1531:             $is_custom{'font'} = 1;
 1532:         }
 1533:         foreach my $item (@bgs) {
 1534:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1535:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1536:                 $is_custom{$item} = 1;
 1537:             
 1538:             }
 1539:         }
 1540:         foreach my $item (@links) {
 1541:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1542:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1543:                 $is_custom{$item} = 1;
 1544:             }
 1545:         }
 1546:     }
 1547:     my $itemcount = 1;
 1548:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1549:     $datatable .= '</tr></table></td></tr>';
 1550:     return $datatable;
 1551: }
 1552: 
 1553: sub role_defaults {
 1554:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1555:     my %defaults;
 1556:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1557:         return %defaults;
 1558:     }
 1559:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1560:     if ($role eq 'login') {
 1561:         %defaults = (
 1562:                        font => $defaultdesign{$role.'.font'},
 1563:                     );
 1564:         if (ref($logintext) eq 'ARRAY') {
 1565:             foreach my $item (@{$logintext}) {
 1566:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1567:             }
 1568:         }
 1569:         foreach my $item (@{$images}) {
 1570:             $defaults{'showlogo'}{$item} = 1;
 1571:         }
 1572:     } else {
 1573:         %defaults = (
 1574:                        img => $defaultdesign{$role.'.img'},
 1575:                        font => $defaultdesign{$role.'.font'},
 1576:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1577:                     );
 1578:     }
 1579:     foreach my $item (@{$bgs}) {
 1580:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1581:     }
 1582:     foreach my $item (@{$links}) {
 1583:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1584:     }
 1585:     foreach my $item (@{$images}) {
 1586:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1587:     }
 1588:     return %defaults;
 1589: }
 1590: 
 1591: sub display_color_options {
 1592:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1593:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1594:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1595:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1596:     my $datatable = '<tr'.$css_class.'>'.
 1597:         '<td>'.$choices->{'font'}.'</td>';
 1598:     if (!$is_custom->{'font'}) {
 1599:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1600:     } else {
 1601:         $datatable .= '<td>&nbsp;</td>';
 1602:     }
 1603:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1604: 
 1605:     $datatable .= '<td><span class="LC_nobreak">'.
 1606:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1607:                   ' value="'.$current_color.'" />&nbsp;'.
 1608:                   '&nbsp;</td></tr>';
 1609:     unless ($role eq 'login') { 
 1610:         $datatable .= '<tr'.$css_class.'>'.
 1611:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1612:         if (!$is_custom->{'fontmenu'}) {
 1613:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1614:         } else {
 1615:             $datatable .= '<td>&nbsp;</td>';
 1616:         }
 1617: 	$current_color = $designs->{'fontmenu'} ?
 1618: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1619:         $datatable .= '<td><span class="LC_nobreak">'.
 1620:                       '<input class="colorchooser" type="text" size="10" name="'
 1621: 		      .$role.'_fontmenu"'.
 1622:                       ' value="'.$current_color.'" />&nbsp;'.
 1623:                       '&nbsp;</td></tr>';
 1624:     }
 1625:     my $switchserver = &check_switchserver($dom,$confname);
 1626:     foreach my $img (@{$images}) {
 1627: 	$itemcount ++;
 1628:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1629:         $datatable .= '<tr'.$css_class.'>'.
 1630:                       '<td>'.$choices->{$img};
 1631:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1632:         if ($role eq 'login') {
 1633:             if ($img eq 'login') {
 1634:                 $login_hdr_pick =
 1635:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1636:                 $logincolors =
 1637:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1638:                                        $designs,$defaults);
 1639:             } elsif ($img ne 'domlogo') {
 1640:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1641:             }
 1642:         }
 1643:         $datatable .= '</td>';
 1644:         if ($designs->{$img} ne '') {
 1645:             $imgfile = $designs->{$img};
 1646: 	    $img_import = ($imgfile =~ m{^/adm/});
 1647:         } else {
 1648:             $imgfile = $defaults->{$img};
 1649:         }
 1650:         if ($imgfile) {
 1651:             my ($showfile,$fullsize);
 1652:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1653:                 my $urldir = $1;
 1654:                 my $filename = $2;
 1655:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1656:                 if (@info) {
 1657:                     my $thumbfile = 'tn-'.$filename;
 1658:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1659:                     if (@thumb) {
 1660:                         $showfile = $urldir.'/'.$thumbfile;
 1661:                     } else {
 1662:                         $showfile = $imgfile;
 1663:                     }
 1664:                 } else {
 1665:                     $showfile = '';
 1666:                 }
 1667:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1668:                 $showfile = $imgfile;
 1669:                 my $imgdir = $1;
 1670:                 my $filename = $2;
 1671:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1672:                     $showfile = "/$imgdir/tn-".$filename;
 1673:                 } else {
 1674:                     my $input = $londocroot.$imgfile;
 1675:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1676:                     if (!-e $output) {
 1677:                         my ($width,$height) = &thumb_dimensions();
 1678:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1679:                         if ($fullwidth ne '' && $fullheight ne '') {
 1680:                             if ($fullwidth > $width && $fullheight > $height) { 
 1681:                                 my $size = $width.'x'.$height;
 1682:                                 my @args = ('convert','-sample',$size,$input,$output);
 1683:                                 system({$args[0]} @args);
 1684:                                 $showfile = "/$imgdir/tn-".$filename;
 1685:                             }
 1686:                         }
 1687:                     }
 1688:                 }
 1689:             }
 1690:             if ($showfile) {
 1691:                 if ($showfile =~ m{^/(adm|res)/}) {
 1692:                     if ($showfile =~ m{^/res/}) {
 1693:                         my $local_showfile =
 1694:                             &Apache::lonnet::filelocation('',$showfile);
 1695:                         &Apache::lonnet::repcopy($local_showfile);
 1696:                     }
 1697:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1698:                 }
 1699:                 if ($imgfile) {
 1700:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1701:                         if ($imgfile =~ m{^/res/}) {
 1702:                             my $local_imgfile =
 1703:                                 &Apache::lonnet::filelocation('',$imgfile);
 1704:                             &Apache::lonnet::repcopy($local_imgfile);
 1705:                         }
 1706:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1707:                     } else {
 1708:                         $fullsize = $imgfile;
 1709:                     }
 1710:                 }
 1711:                 $datatable .= '<td>';
 1712:                 if ($img eq 'login') {
 1713:                     $datatable .= $login_hdr_pick;
 1714:                 } 
 1715:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1716:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1717:             } else {
 1718:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1719:                               &mt('Upload:').'<br />';
 1720:             }
 1721:         } else {
 1722:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1723:                           &mt('Upload:').'<br />';
 1724:         }
 1725:         if ($switchserver) {
 1726:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1727:         } else {
 1728:             if ($img ne 'login') { # suppress file selection for Log-in header
 1729:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1730:             }
 1731:         }
 1732:         $datatable .= '</td></tr>';
 1733:     }
 1734:     $itemcount ++;
 1735:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1736:     $datatable .= '<tr'.$css_class.'>'.
 1737:                   '<td>'.$choices->{'bgs'}.'</td>';
 1738:     my $bgs_def;
 1739:     foreach my $item (@{$bgs}) {
 1740:         if (!$is_custom->{$item}) {
 1741:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1742:         }
 1743:     }
 1744:     if ($bgs_def) {
 1745:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1746:     } else {
 1747:         $datatable .= '<td>&nbsp;</td>';
 1748:     }
 1749:     $datatable .= '<td class="LC_right_item">'.
 1750:                   '<table border="0"><tr>';
 1751: 
 1752:     foreach my $item (@{$bgs}) {
 1753:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1754: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1755:         if ($designs->{'bgs'}{$item}) {
 1756:             $datatable .= '&nbsp;';
 1757:         }
 1758:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1759:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1760:     }
 1761:     $datatable .= '</tr></table></td></tr>';
 1762:     $itemcount ++;
 1763:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1764:     $datatable .= '<tr'.$css_class.'>'.
 1765:                   '<td>'.$choices->{'links'}.'</td>';
 1766:     my $links_def;
 1767:     foreach my $item (@{$links}) {
 1768:         if (!$is_custom->{$item}) {
 1769:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1770:         }
 1771:     }
 1772:     if ($links_def) {
 1773:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1774:     } else {
 1775:         $datatable .= '<td>&nbsp;</td>';
 1776:     }
 1777:     $datatable .= '<td class="LC_right_item">'.
 1778:                   '<table border="0"><tr>';
 1779:     foreach my $item (@{$links}) {
 1780: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1781:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1782:         if ($designs->{'links'}{$item}) {
 1783:             $datatable.='&nbsp;';
 1784:         }
 1785:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1786:                       '" /></td>';
 1787:     }
 1788:     $$rowtotal += $itemcount;
 1789:     return $datatable;
 1790: }
 1791: 
 1792: sub logo_display_options {
 1793:     my ($img,$defaults,$designs) = @_;
 1794:     my $checkedon;
 1795:     if (ref($defaults) eq 'HASH') {
 1796:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1797:             if ($defaults->{'showlogo'}{$img}) {
 1798:                 $checkedon = 'checked="checked" ';     
 1799:             }
 1800:         } 
 1801:     }
 1802:     if (ref($designs) eq 'HASH') {
 1803:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1804:             if (defined($designs->{'showlogo'}{$img})) {
 1805:                 if ($designs->{'showlogo'}{$img} == 0) {
 1806:                     $checkedon = '';
 1807:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1808:                     $checkedon = 'checked="checked" ';
 1809:                 }
 1810:             }
 1811:         }
 1812:     }
 1813:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1814:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1815:            &mt('show').'</label>'."\n";
 1816: }
 1817: 
 1818: sub login_header_options  {
 1819:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1820:     my $output = '';
 1821:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1822:         $output .= &mt('Text default(s):').'<br />';
 1823:         if (!$is_custom->{'textcol'}) {
 1824:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1825:                        '&nbsp;&nbsp;&nbsp;';
 1826:         }
 1827:         if (!$is_custom->{'bgcol'}) {
 1828:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1829:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1830:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1831:         }
 1832:         $output .= '<br />';
 1833:     }
 1834:     $output .='<br />';
 1835:     return $output;
 1836: }
 1837: 
 1838: sub login_text_colors {
 1839:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1840:     my $color_menu = '<table border="0"><tr>';
 1841:     foreach my $item (@{$logintext}) {
 1842:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1843:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1844:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1845:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1846:     }
 1847:     $color_menu .= '</tr></table><br />';
 1848:     return $color_menu;
 1849: }
 1850: 
 1851: sub image_changes {
 1852:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1853:     my $output;
 1854:     if ($img eq 'login') {
 1855:             # suppress image for Log-in header
 1856:     } elsif (!$is_custom) {
 1857:         if ($img ne 'domlogo') {
 1858:             $output .= &mt('Default image:').'<br />';
 1859:         } else {
 1860:             $output .= &mt('Default in use:').'<br />';
 1861:         }
 1862:     }
 1863:     if ($img eq 'login') { # suppress image for Log-in header
 1864:         $output .= '<td>'.$logincolors;
 1865:     } else {
 1866:         if ($img_import) {
 1867:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1868:         }
 1869:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1870:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1871:         if ($is_custom) {
 1872:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1873:                        '<input type="checkbox" name="'.
 1874:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1875:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1876:         } else {
 1877:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1878:         }
 1879:     }
 1880:     return $output;
 1881: }
 1882: 
 1883: sub print_quotas {
 1884:     my ($dom,$settings,$rowtotal,$action) = @_;
 1885:     my $context;
 1886:     if ($action eq 'quotas') {
 1887:         $context = 'tools';
 1888:     } else {
 1889:         $context = $action;
 1890:     }
 1891:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1892:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1893:     my $typecount = 0;
 1894:     my ($css_class,%titles);
 1895:     if ($context eq 'requestcourses') {
 1896:         @usertools = ('official','unofficial','community','textbook','placement');
 1897:         @options =('norequest','approval','validate','autolimit');
 1898:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1899:         %titles = &courserequest_titles();
 1900:     } elsif ($context eq 'requestauthor') {
 1901:         @usertools = ('author');
 1902:         @options = ('norequest','approval','automatic');
 1903:         %titles = &authorrequest_titles();
 1904:     } else {
 1905:         @usertools = ('aboutme','blog','webdav','portfolio');
 1906:         %titles = &tool_titles();
 1907:     }
 1908:     if (ref($types) eq 'ARRAY') {
 1909:         foreach my $type (@{$types}) {
 1910:             my ($currdefquota,$currauthorquota);
 1911:             unless (($context eq 'requestcourses') ||
 1912:                     ($context eq 'requestauthor')) {
 1913:                 if (ref($settings) eq 'HASH') {
 1914:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1915:                         $currdefquota = $settings->{defaultquota}->{$type};
 1916:                     } else {
 1917:                         $currdefquota = $settings->{$type};
 1918:                     }
 1919:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1920:                         $currauthorquota = $settings->{authorquota}->{$type};
 1921:                     }
 1922:                 }
 1923:             }
 1924:             if (defined($usertypes->{$type})) {
 1925:                 $typecount ++;
 1926:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1927:                 $datatable .= '<tr'.$css_class.'>'.
 1928:                               '<td>'.$usertypes->{$type}.'</td>'.
 1929:                               '<td class="LC_left_item">';
 1930:                 if ($context eq 'requestcourses') {
 1931:                     $datatable .= '<table><tr>';
 1932:                 }
 1933:                 my %cell;  
 1934:                 foreach my $item (@usertools) {
 1935:                     if ($context eq 'requestcourses') {
 1936:                         my ($curroption,$currlimit);
 1937:                         if (ref($settings) eq 'HASH') {
 1938:                             if (ref($settings->{$item}) eq 'HASH') {
 1939:                                 $curroption = $settings->{$item}->{$type};
 1940:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1941:                                     $currlimit = $1; 
 1942:                                 }
 1943:                             }
 1944:                         }
 1945:                         if (!$curroption) {
 1946:                             $curroption = 'norequest';
 1947:                         }
 1948:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1949:                         foreach my $option (@options) {
 1950:                             my $val = $option;
 1951:                             if ($option eq 'norequest') {
 1952:                                 $val = 0;  
 1953:                             }
 1954:                             if ($option eq 'validate') {
 1955:                                 my $canvalidate = 0;
 1956:                                 if (ref($validations{$item}) eq 'HASH') { 
 1957:                                     if ($validations{$item}{$type}) {
 1958:                                         $canvalidate = 1;
 1959:                                     }
 1960:                                 }
 1961:                                 next if (!$canvalidate);
 1962:                             }
 1963:                             my $checked = '';
 1964:                             if ($option eq $curroption) {
 1965:                                 $checked = ' checked="checked"';
 1966:                             } elsif ($option eq 'autolimit') {
 1967:                                 if ($curroption =~ /^autolimit/) {
 1968:                                     $checked = ' checked="checked"';
 1969:                                 }                       
 1970:                             } 
 1971:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1972:                                   '<input type="radio" name="crsreq_'.$item.
 1973:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1974:                                   $titles{$option}.'</label>';
 1975:                             if ($option eq 'autolimit') {
 1976:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1977:                                                 $item.'_limit_'.$type.'" size="1" '.
 1978:                                                 'value="'.$currlimit.'" />';
 1979:                             }
 1980:                             $cell{$item} .= '</span> ';
 1981:                             if ($option eq 'autolimit') {
 1982:                                 $cell{$item} .= $titles{'unlimited'};
 1983:                             }
 1984:                         }
 1985:                     } elsif ($context eq 'requestauthor') {
 1986:                         my $curroption;
 1987:                         if (ref($settings) eq 'HASH') {
 1988:                             $curroption = $settings->{$type};
 1989:                         }
 1990:                         if (!$curroption) {
 1991:                             $curroption = 'norequest';
 1992:                         }
 1993:                         foreach my $option (@options) {
 1994:                             my $val = $option;
 1995:                             if ($option eq 'norequest') {
 1996:                                 $val = 0;
 1997:                             }
 1998:                             my $checked = '';
 1999:                             if ($option eq $curroption) {
 2000:                                 $checked = ' checked="checked"';
 2001:                             }
 2002:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2003:                                   '<input type="radio" name="authorreq_'.$type.
 2004:                                   '" value="'.$val.'"'.$checked.' />'.
 2005:                                   $titles{$option}.'</label></span>&nbsp; ';
 2006:                         }
 2007:                     } else {
 2008:                         my $checked = 'checked="checked" ';
 2009:                         if (ref($settings) eq 'HASH') {
 2010:                             if (ref($settings->{$item}) eq 'HASH') {
 2011:                                 if ($settings->{$item}->{$type} == 0) {
 2012:                                     $checked = '';
 2013:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2014:                                     $checked =  'checked="checked" ';
 2015:                                 }
 2016:                             }
 2017:                         }
 2018:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2019:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2020:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2021:                                       '</label></span>&nbsp; ';
 2022:                     }
 2023:                 }
 2024:                 if ($context eq 'requestcourses') {
 2025:                     $datatable .= '</tr><tr>';
 2026:                     foreach my $item (@usertools) {
 2027:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2028:                     }
 2029:                     $datatable .= '</tr></table>';
 2030:                 }
 2031:                 $datatable .= '</td>';
 2032:                 unless (($context eq 'requestcourses') ||
 2033:                         ($context eq 'requestauthor')) {
 2034:                     $datatable .= 
 2035:                               '<td class="LC_right_item">'.
 2036:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2037:                               '<input type="text" name="quota_'.$type.
 2038:                               '" value="'.$currdefquota.
 2039:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2040:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2041:                               '<input type="text" name="authorquota_'.$type.
 2042:                               '" value="'.$currauthorquota.
 2043:                               '" size="5" /></span></td>';
 2044:                 }
 2045:                 $datatable .= '</tr>';
 2046:             }
 2047:         }
 2048:     }
 2049:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2050:         $defaultquota = '20';
 2051:         $authorquota = '500';
 2052:         if (ref($settings) eq 'HASH') {
 2053:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2054:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2055:             } elsif (defined($settings->{'default'})) {
 2056:                 $defaultquota = $settings->{'default'};
 2057:             }
 2058:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2059:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2060:             }
 2061:         }
 2062:     }
 2063:     $typecount ++;
 2064:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2065:     $datatable .= '<tr'.$css_class.'>'.
 2066:                   '<td>'.$othertitle.'</td>'.
 2067:                   '<td class="LC_left_item">';
 2068:     if ($context eq 'requestcourses') {
 2069:         $datatable .= '<table><tr>';
 2070:     }
 2071:     my %defcell;
 2072:     foreach my $item (@usertools) {
 2073:         if ($context eq 'requestcourses') {
 2074:             my ($curroption,$currlimit);
 2075:             if (ref($settings) eq 'HASH') {
 2076:                 if (ref($settings->{$item}) eq 'HASH') {
 2077:                     $curroption = $settings->{$item}->{'default'};
 2078:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2079:                         $currlimit = $1;
 2080:                     }
 2081:                 }
 2082:             }
 2083:             if (!$curroption) {
 2084:                 $curroption = 'norequest';
 2085:             }
 2086:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2087:             foreach my $option (@options) {
 2088:                 my $val = $option;
 2089:                 if ($option eq 'norequest') {
 2090:                     $val = 0;
 2091:                 }
 2092:                 if ($option eq 'validate') {
 2093:                     my $canvalidate = 0;
 2094:                     if (ref($validations{$item}) eq 'HASH') {
 2095:                         if ($validations{$item}{'default'}) {
 2096:                             $canvalidate = 1;
 2097:                         }
 2098:                     }
 2099:                     next if (!$canvalidate);
 2100:                 }
 2101:                 my $checked = '';
 2102:                 if ($option eq $curroption) {
 2103:                     $checked = ' checked="checked"';
 2104:                 } elsif ($option eq 'autolimit') {
 2105:                     if ($curroption =~ /^autolimit/) {
 2106:                         $checked = ' checked="checked"';
 2107:                     }
 2108:                 }
 2109:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2110:                                   '<input type="radio" name="crsreq_'.$item.
 2111:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2112:                                   $titles{$option}.'</label>';
 2113:                 if ($option eq 'autolimit') {
 2114:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2115:                                        $item.'_limit_default" size="1" '.
 2116:                                        'value="'.$currlimit.'" />';
 2117:                 }
 2118:                 $defcell{$item} .= '</span> ';
 2119:                 if ($option eq 'autolimit') {
 2120:                     $defcell{$item} .= $titles{'unlimited'};
 2121:                 }
 2122:             }
 2123:         } elsif ($context eq 'requestauthor') {
 2124:             my $curroption;
 2125:             if (ref($settings) eq 'HASH') {
 2126:                 $curroption = $settings->{'default'};
 2127:             }
 2128:             if (!$curroption) {
 2129:                 $curroption = 'norequest';
 2130:             }
 2131:             foreach my $option (@options) {
 2132:                 my $val = $option;
 2133:                 if ($option eq 'norequest') {
 2134:                     $val = 0;
 2135:                 }
 2136:                 my $checked = '';
 2137:                 if ($option eq $curroption) {
 2138:                     $checked = ' checked="checked"';
 2139:                 }
 2140:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2141:                               '<input type="radio" name="authorreq_default"'.
 2142:                               ' value="'.$val.'"'.$checked.' />'.
 2143:                               $titles{$option}.'</label></span>&nbsp; ';
 2144:             }
 2145:         } else {
 2146:             my $checked = 'checked="checked" ';
 2147:             if (ref($settings) eq 'HASH') {
 2148:                 if (ref($settings->{$item}) eq 'HASH') {
 2149:                     if ($settings->{$item}->{'default'} == 0) {
 2150:                         $checked = '';
 2151:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2152:                         $checked = 'checked="checked" ';
 2153:                     }
 2154:                 }
 2155:             }
 2156:             $datatable .= '<span class="LC_nobreak"><label>'.
 2157:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2158:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2159:                           '</label></span>&nbsp; ';
 2160:         }
 2161:     }
 2162:     if ($context eq 'requestcourses') {
 2163:         $datatable .= '</tr><tr>';
 2164:         foreach my $item (@usertools) {
 2165:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2166:         }
 2167:         $datatable .= '</tr></table>';
 2168:     }
 2169:     $datatable .= '</td>';
 2170:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2171:         $datatable .= '<td class="LC_right_item">'.
 2172:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2173:                       '<input type="text" name="defaultquota" value="'.
 2174:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2175:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2176:                       '<input type="text" name="authorquota" value="'.
 2177:                       $authorquota.'" size="5" /></span></td>';
 2178:     }
 2179:     $datatable .= '</tr>';
 2180:     $typecount ++;
 2181:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2182:     $datatable .= '<tr'.$css_class.'>'.
 2183:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2184:     if ($context eq 'requestcourses') {
 2185:         $datatable .= &mt('(overrides affiliation, if set)').
 2186:                       '</td>'.
 2187:                       '<td class="LC_left_item">'.
 2188:                       '<table><tr>';
 2189:     } else {
 2190:         $datatable .= &mt('(overrides affiliation, if checked)').
 2191:                       '</td>'.
 2192:                       '<td class="LC_left_item" colspan="2">'.
 2193:                       '<br />';
 2194:     }
 2195:     my %advcell;
 2196:     foreach my $item (@usertools) {
 2197:         if ($context eq 'requestcourses') {
 2198:             my ($curroption,$currlimit);
 2199:             if (ref($settings) eq 'HASH') {
 2200:                 if (ref($settings->{$item}) eq 'HASH') {
 2201:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2202:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2203:                         $currlimit = $1;
 2204:                     }
 2205:                 }
 2206:             }
 2207:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2208:             my $checked = '';
 2209:             if ($curroption eq '') {
 2210:                 $checked = ' checked="checked"';
 2211:             }
 2212:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2213:                                '<input type="radio" name="crsreq_'.$item.
 2214:                                '__LC_adv" value=""'.$checked.' />'.
 2215:                                &mt('No override set').'</label></span>&nbsp; ';
 2216:             foreach my $option (@options) {
 2217:                 my $val = $option;
 2218:                 if ($option eq 'norequest') {
 2219:                     $val = 0;
 2220:                 }
 2221:                 if ($option eq 'validate') {
 2222:                     my $canvalidate = 0;
 2223:                     if (ref($validations{$item}) eq 'HASH') {
 2224:                         if ($validations{$item}{'_LC_adv'}) {
 2225:                             $canvalidate = 1;
 2226:                         }
 2227:                     }
 2228:                     next if (!$canvalidate);
 2229:                 }
 2230:                 my $checked = '';
 2231:                 if ($val eq $curroption) {
 2232:                     $checked = ' checked="checked"';
 2233:                 } elsif ($option eq 'autolimit') {
 2234:                     if ($curroption =~ /^autolimit/) {
 2235:                         $checked = ' checked="checked"';
 2236:                     }
 2237:                 }
 2238:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2239:                                   '<input type="radio" name="crsreq_'.$item.
 2240:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2241:                                   $titles{$option}.'</label>';
 2242:                 if ($option eq 'autolimit') {
 2243:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2244:                                        $item.'_limit__LC_adv" size="1" '.
 2245:                                        'value="'.$currlimit.'" />';
 2246:                 }
 2247:                 $advcell{$item} .= '</span> ';
 2248:                 if ($option eq 'autolimit') {
 2249:                     $advcell{$item} .= $titles{'unlimited'};
 2250:                 }
 2251:             }
 2252:         } elsif ($context eq 'requestauthor') {
 2253:             my $curroption;
 2254:             if (ref($settings) eq 'HASH') {
 2255:                 $curroption = $settings->{'_LC_adv'};
 2256:             }
 2257:             my $checked = '';
 2258:             if ($curroption eq '') {
 2259:                 $checked = ' checked="checked"';
 2260:             }
 2261:             $datatable .= '<span class="LC_nobreak"><label>'.
 2262:                           '<input type="radio" name="authorreq__LC_adv"'.
 2263:                           ' value=""'.$checked.' />'.
 2264:                           &mt('No override set').'</label></span>&nbsp; ';
 2265:             foreach my $option (@options) {
 2266:                 my $val = $option;
 2267:                 if ($option eq 'norequest') {
 2268:                     $val = 0;
 2269:                 }
 2270:                 my $checked = '';
 2271:                 if ($val eq $curroption) {
 2272:                     $checked = ' checked="checked"';
 2273:                 }
 2274:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2275:                               '<input type="radio" name="authorreq__LC_adv"'.
 2276:                               ' value="'.$val.'"'.$checked.' />'.
 2277:                               $titles{$option}.'</label></span>&nbsp; ';
 2278:             }
 2279:         } else {
 2280:             my $checked = 'checked="checked" ';
 2281:             if (ref($settings) eq 'HASH') {
 2282:                 if (ref($settings->{$item}) eq 'HASH') {
 2283:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2284:                         $checked = '';
 2285:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2286:                         $checked = 'checked="checked" ';
 2287:                     }
 2288:                 }
 2289:             }
 2290:             $datatable .= '<span class="LC_nobreak"><label>'.
 2291:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2292:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2293:                           '</label></span>&nbsp; ';
 2294:         }
 2295:     }
 2296:     if ($context eq 'requestcourses') {
 2297:         $datatable .= '</tr><tr>';
 2298:         foreach my $item (@usertools) {
 2299:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2300:         }
 2301:         $datatable .= '</tr></table>';
 2302:     }
 2303:     $datatable .= '</td></tr>';
 2304:     $$rowtotal += $typecount;
 2305:     return $datatable;
 2306: }
 2307: 
 2308: sub print_requestmail {
 2309:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2310:     my ($now,$datatable,%currapp);
 2311:     $now = time;
 2312:     if (ref($settings) eq 'HASH') {
 2313:         if (ref($settings->{'notify'}) eq 'HASH') {
 2314:             if ($settings->{'notify'}{'approval'} ne '') {
 2315:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2316:             }
 2317:         }
 2318:     }
 2319:     my $numinrow = 2;
 2320:     my $css_class;
 2321:     if ($$rowtotal%2) {
 2322:         $css_class = 'LC_odd_row';
 2323:     }
 2324:     if ($customcss) {
 2325:         $css_class .= " $customcss";
 2326:     }
 2327:     $css_class =~ s/^\s+//;
 2328:     if ($css_class) {
 2329:         $css_class = ' class="'.$css_class.'"';
 2330:     }
 2331:     if ($rowstyle) {
 2332:         $css_class .= ' style="'.$rowstyle.'"';
 2333:     }
 2334:     my $text;
 2335:     if ($action eq 'requestcourses') {
 2336:         $text = &mt('Receive notification of course requests requiring approval');
 2337:     } elsif ($action eq 'requestauthor') {
 2338:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2339:     } else {
 2340:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2341:     }
 2342:     $datatable = '<tr'.$css_class.'>'.
 2343:                  ' <td>'.$text.'</td>'.
 2344:                  ' <td class="LC_left_item">';
 2345:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2346:                                                  $action.'notifyapproval',%currapp);
 2347:     if ($numdc > 0) {
 2348:         $datatable .= $table;
 2349:     } else {
 2350:         $datatable .= &mt('There are no active Domain Coordinators');
 2351:     }
 2352:     $datatable .='</td></tr>';
 2353:     return $datatable;
 2354: }
 2355: 
 2356: sub print_studentcode {
 2357:     my ($settings,$rowtotal) = @_;
 2358:     my $rownum = 0; 
 2359:     my ($output,%current);
 2360:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2361:     if (ref($settings) eq 'HASH') {
 2362:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2363:             foreach my $type (@crstypes) {
 2364:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2365:             }
 2366:         }
 2367:     }
 2368:     $output .= '<tr>'.
 2369:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2370:                '<td class="LC_left_item">';
 2371:     foreach my $type (@crstypes) {
 2372:         my $check = ' ';
 2373:         if ($current{$type}) {
 2374:             $check = ' checked="checked" ';
 2375:         }
 2376:         $output .= '<span class="LC_nobreak"><label>'.
 2377:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2378:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2379:     }
 2380:     $output .= '</td></tr>';
 2381:     $$rowtotal ++;
 2382:     return $output;
 2383: }
 2384: 
 2385: sub print_textbookcourses {
 2386:     my ($dom,$type,$settings,$rowtotal) = @_;
 2387:     my $rownum = 0;
 2388:     my $css_class;
 2389:     my $itemcount = 1;
 2390:     my $maxnum = 0;
 2391:     my $bookshash;
 2392:     if (ref($settings) eq 'HASH') {
 2393:         $bookshash = $settings->{$type};
 2394:     }
 2395:     my %ordered;
 2396:     if (ref($bookshash) eq 'HASH') {
 2397:         foreach my $item (keys(%{$bookshash})) {
 2398:             if (ref($bookshash->{$item}) eq 'HASH') {
 2399:                 my $num = $bookshash->{$item}{'order'};
 2400:                 $ordered{$num} = $item;
 2401:             }
 2402:         }
 2403:     }
 2404:     my $confname = $dom.'-domainconfig';
 2405:     my $switchserver = &check_switchserver($dom,$confname);
 2406:     my $maxnum = scalar(keys(%ordered));
 2407:     my $datatable;
 2408:     if (keys(%ordered)) {
 2409:         my @items = sort { $a <=> $b } keys(%ordered);
 2410:         for (my $i=0; $i<@items; $i++) {
 2411:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2412:             my $key = $ordered{$items[$i]};
 2413:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2414:             my $coursetitle = $coursehash{'description'};
 2415:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2416:             if (ref($bookshash->{$key}) eq 'HASH') {
 2417:                 $subject = $bookshash->{$key}->{'subject'};
 2418:                 $title = $bookshash->{$key}->{'title'};
 2419:                 if ($type eq 'textbooks') {
 2420:                     $publisher = $bookshash->{$key}->{'publisher'};
 2421:                     $author = $bookshash->{$key}->{'author'};
 2422:                     $image = $bookshash->{$key}->{'image'};
 2423:                     if ($image ne '') {
 2424:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2425:                         my $imagethumb = "$path/tn-".$imagefile;
 2426:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2427:                     }
 2428:                 }
 2429:             }
 2430:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2431:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2432:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2433:             for (my $k=0; $k<=$maxnum; $k++) {
 2434:                 my $vpos = $k+1;
 2435:                 my $selstr;
 2436:                 if ($k == $i) {
 2437:                     $selstr = ' selected="selected" ';
 2438:                 }
 2439:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2440:             }
 2441:             $datatable .= '</select>'.('&nbsp;'x2).
 2442:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2443:                 &mt('Delete?').'</label></span></td>'.
 2444:                 '<td colspan="2">'.
 2445:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2446:                 ('&nbsp;'x2).
 2447:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2448:             if ($type eq 'textbooks') {
 2449:                 $datatable .= ('&nbsp;'x2).
 2450:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2451:                               ('&nbsp;'x2).
 2452:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2453:                               ('&nbsp;'x2).
 2454:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2455:                 if ($image) {
 2456:                     $datatable .= $imgsrc.
 2457:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2458:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2459:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2460:                 }
 2461:                 if ($switchserver) {
 2462:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2463:                 } else {
 2464:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2465:                 }
 2466:             }
 2467:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2468:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2469:                           $coursetitle.'</span></td></tr>'."\n";
 2470:             $itemcount ++;
 2471:         }
 2472:     }
 2473:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2474:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2475:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2476:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2477:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2478:     for (my $k=0; $k<$maxnum+1; $k++) {
 2479:         my $vpos = $k+1;
 2480:         my $selstr;
 2481:         if ($k == $maxnum) {
 2482:             $selstr = ' selected="selected" ';
 2483:         }
 2484:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2485:     }
 2486:     $datatable .= '</select>&nbsp;'."\n".
 2487:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2488:                   '<td colspan="2">'.
 2489:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2490:                   ('&nbsp;'x2).
 2491:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2492:                   ('&nbsp;'x2);
 2493:     if ($type eq 'textbooks') {
 2494:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2495:                       ('&nbsp;'x2).
 2496:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2497:                       ('&nbsp;'x2).
 2498:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2499:         if ($switchserver) {
 2500:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2501:         } else {
 2502:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2503:         }
 2504:     }
 2505:     $datatable .= '</span>'."\n".
 2506:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2507:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2508:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2509:                   &Apache::loncommon::selectcourse_link
 2510:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2511:                   '</span></td>'."\n".
 2512:                   '</tr>'."\n";
 2513:     $itemcount ++;
 2514:     return $datatable;
 2515: }
 2516: 
 2517: sub textbookcourses_javascript {
 2518:     my ($settings) = @_;
 2519:     return unless(ref($settings) eq 'HASH');
 2520:     my (%ordered,%total,%jstext);
 2521:     foreach my $type ('textbooks','templates') {
 2522:         $total{$type} = 0;
 2523:         if (ref($settings->{$type}) eq 'HASH') {
 2524:             foreach my $item (keys(%{$settings->{$type}})) {
 2525:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2526:                     my $num = $settings->{$type}->{$item}{'order'};
 2527:                     $ordered{$type}{$num} = $item;
 2528:                 }
 2529:             }
 2530:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2531:         }
 2532:         my @jsarray = ();
 2533:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2534:             push(@jsarray,$ordered{$type}{$item});
 2535:         }
 2536:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2537:     }
 2538:     return <<"ENDSCRIPT";
 2539: <script type="text/javascript">
 2540: // <![CDATA[
 2541: function reorderBooks(form,item,caller) {
 2542:     var changedVal;
 2543: $jstext{'textbooks'};
 2544: $jstext{'templates'};
 2545:     var newpos;
 2546:     var maxh;
 2547:     if (caller == 'textbooks') {  
 2548:         newpos = 'textbooks_addbook_pos';
 2549:         maxh = 1 + $total{'textbooks'};
 2550:     } else {
 2551:         newpos = 'templates_addbook_pos';
 2552:         maxh = 1 + $total{'templates'};
 2553:     }
 2554:     var current = new Array;
 2555:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2556:     if (item == newpos) {
 2557:         changedVal = newitemVal;
 2558:     } else {
 2559:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2560:         current[newitemVal] = newpos;
 2561:     }
 2562:     if (caller == 'textbooks') {
 2563:         for (var i=0; i<textbooks.length; i++) {
 2564:             var elementName = 'textbooks_'+textbooks[i];
 2565:             if (elementName != item) {
 2566:                 if (form.elements[elementName]) {
 2567:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2568:                     current[currVal] = elementName;
 2569:                 }
 2570:             }
 2571:         }
 2572:     }
 2573:     if (caller == 'templates') {
 2574:         for (var i=0; i<templates.length; i++) {
 2575:             var elementName = 'templates_'+templates[i];
 2576:             if (elementName != item) {
 2577:                 if (form.elements[elementName]) {
 2578:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2579:                     current[currVal] = elementName;
 2580:                 }
 2581:             }
 2582:         }
 2583:     }
 2584:     var oldVal;
 2585:     for (var j=0; j<maxh; j++) {
 2586:         if (current[j] == undefined) {
 2587:             oldVal = j;
 2588:         }
 2589:     }
 2590:     if (oldVal < changedVal) {
 2591:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2592:            var elementName = current[k];
 2593:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2594:         }
 2595:     } else {
 2596:         for (var k=changedVal; k<oldVal; k++) {
 2597:             var elementName = current[k];
 2598:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2599:         }
 2600:     }
 2601:     return;
 2602: }
 2603: 
 2604: // ]]>
 2605: </script>
 2606: 
 2607: ENDSCRIPT
 2608: }
 2609: 
 2610: sub ltitools_javascript {
 2611:     my ($settings) = @_;
 2612:     my $togglejs = &ltitools_toggle_js();
 2613:     unless (ref($settings) eq 'HASH') {
 2614:         return $togglejs;
 2615:     }
 2616:     my (%ordered,$total,%jstext);
 2617:     $total = 0;
 2618:     foreach my $item (keys(%{$settings})) {
 2619:         if (ref($settings->{$item}) eq 'HASH') {
 2620:             my $num = $settings->{$item}{'order'};
 2621:             $ordered{$num} = $item;
 2622:         }
 2623:     }
 2624:     $total = scalar(keys(%{$settings}));
 2625:     my @jsarray = ();
 2626:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2627:         push(@jsarray,$ordered{$item});
 2628:     }
 2629:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2630:     return <<"ENDSCRIPT";
 2631: <script type="text/javascript">
 2632: // <![CDATA[
 2633: function reorderLTITools(form,item) {
 2634:     var changedVal;
 2635: $jstext
 2636:     var newpos = 'ltitools_add_pos';
 2637:     var maxh = 1 + $total;
 2638:     var current = new Array;
 2639:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2640:     if (item == newpos) {
 2641:         changedVal = newitemVal;
 2642:     } else {
 2643:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2644:         current[newitemVal] = newpos;
 2645:     }
 2646:     for (var i=0; i<ltitools.length; i++) {
 2647:         var elementName = 'ltitools_'+ltitools[i];
 2648:         if (elementName != item) {
 2649:             if (form.elements[elementName]) {
 2650:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2651:                 current[currVal] = elementName;
 2652:             }
 2653:         }
 2654:     }
 2655:     var oldVal;
 2656:     for (var j=0; j<maxh; j++) {
 2657:         if (current[j] == undefined) {
 2658:             oldVal = j;
 2659:         }
 2660:     }
 2661:     if (oldVal < changedVal) {
 2662:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2663:            var elementName = current[k];
 2664:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2665:         }
 2666:     } else {
 2667:         for (var k=changedVal; k<oldVal; k++) {
 2668:             var elementName = current[k];
 2669:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2670:         }
 2671:     }
 2672:     return;
 2673: }
 2674: 
 2675: // ]]>
 2676: </script>
 2677: 
 2678: $togglejs
 2679: 
 2680: ENDSCRIPT
 2681: }
 2682: 
 2683: sub ltitools_toggle_js {
 2684:     return <<"ENDSCRIPT";
 2685: <script type="text/javascript">
 2686: // <![CDATA[
 2687: 
 2688: function toggleLTITools(form,setting,item) {
 2689:     var radioname = '';
 2690:     var divid = '';
 2691:     if ((setting == 'passback') || (setting == 'roster')) {
 2692:         radioname = 'ltitools_'+setting+'_'+item;
 2693:         divid = 'ltitools_'+setting+'time_'+item;
 2694:         var num = form.elements[radioname].length;
 2695:         if (num) {
 2696:             var setvis = '';
 2697:             for (var i=0; i<num; i++) {
 2698:                 if (form.elements[radioname][i].checked) {
 2699:                     if (form.elements[radioname][i].value == '1') {
 2700:                         if (document.getElementById(divid)) {
 2701:                             document.getElementById(divid).style.display = 'inline-block';
 2702:                         }
 2703:                         setvis = 1;
 2704:                     }
 2705:                     break;
 2706:                 }
 2707:             }
 2708:         }
 2709:         if (!setvis) {
 2710:             if (document.getElementById(divid)) {
 2711:                 document.getElementById(divid).style.display = 'none';
 2712:             }
 2713:         }
 2714:     }
 2715:     if (setting == 'user') {
 2716:         divid = 'ltitools_'+setting+'_div_'+item;
 2717:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2718:         if (document.getElementById(divid)) {
 2719:             if (document.getElementById(checkid)) {
 2720:                 if (document.getElementById(checkid).checked) {
 2721:                     document.getElementById(divid).style.display = 'inline-block';
 2722:                 } else {
 2723:                     document.getElementById(divid).style.display = 'none';
 2724:                 }
 2725:             }
 2726:         }
 2727:     }
 2728:     return;
 2729: }
 2730: // ]]>
 2731: </script>
 2732: 
 2733: ENDSCRIPT
 2734: }
 2735: 
 2736: sub lti_javascript {
 2737:     my ($settings) = @_;
 2738:     my $togglejs = &lti_toggle_js();
 2739:     unless (ref($settings) eq 'HASH') {
 2740:         return $togglejs;
 2741:     }
 2742:     my (%ordered,$total,%jstext);
 2743:     $total = 0;
 2744:     foreach my $item (keys(%{$settings})) {
 2745:         if (ref($settings->{$item}) eq 'HASH') {
 2746:             my $num = $settings->{$item}{'order'};
 2747:             $ordered{$num} = $item;
 2748:         }
 2749:     }
 2750:     $total = scalar(keys(%{$settings}));
 2751:     my @jsarray = ();
 2752:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2753:         push(@jsarray,$ordered{$item});
 2754:     }
 2755:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 2756:     return <<"ENDSCRIPT";
 2757: <script type="text/javascript">
 2758: // <![CDATA[
 2759: function reorderLTI(form,item) {
 2760:     var changedVal;
 2761: $jstext
 2762:     var newpos = 'lti_pos_add';
 2763:     var maxh = 1 + $total;
 2764:     var current = new Array;
 2765:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2766:     if (item == newpos) {
 2767:         changedVal = newitemVal;
 2768:     } else {
 2769:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2770:         current[newitemVal] = newpos;
 2771:     }
 2772:     for (var i=0; i<lti.length; i++) {
 2773:         var elementName = 'lti_pos_'+lti[i];
 2774:         if (elementName != item) {
 2775:             if (form.elements[elementName]) {
 2776:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2777:                 current[currVal] = elementName;
 2778:             }
 2779:         }
 2780:     }
 2781:     var oldVal;
 2782:     for (var j=0; j<maxh; j++) {
 2783:         if (current[j] == undefined) {
 2784:             oldVal = j;
 2785:         }
 2786:     }
 2787:     if (oldVal < changedVal) {
 2788:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2789:            var elementName = current[k];
 2790:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2791:         }
 2792:     } else {
 2793:         for (var k=changedVal; k<oldVal; k++) {
 2794:             var elementName = current[k];
 2795:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2796:         }
 2797:     }
 2798:     return;
 2799: }
 2800: // ]]>
 2801: </script>
 2802: 
 2803: $togglejs
 2804: 
 2805: ENDSCRIPT
 2806: }
 2807: 
 2808: sub lti_toggle_js {
 2809:     return <<"ENDSCRIPT";
 2810: <script type="text/javascript">
 2811: // <![CDATA[
 2812: 
 2813: function toggleLTI(form,setting,item) {
 2814:     if ((setting == 'user') || (setting == 'crs')) {
 2815:         var radioname = '';
 2816:         var divid = '';
 2817:         if (setting == 'user') {
 2818:             radioname = 'lti_mapuser_'+item;
 2819:             divid = 'lti_userfield_'+item;
 2820:         } else {
 2821:             radioname = 'lti_mapcrs_'+item;
 2822:             divid = 'lti_crsfield_'+item;
 2823:         }
 2824:         var num = form.elements[radioname].length;
 2825:         if (num) {
 2826:             var setvis = '';
 2827:             for (var i=0; i<num; i++) {
 2828:                if (form.elements[radioname][i].checked) {
 2829:                    if (form.elements[radioname][i].value == 'other') {
 2830:                        if (document.getElementById(divid)) {
 2831:                            document.getElementById(divid).style.display = 'inline-block';
 2832:                        }
 2833:                        setvis = 1;
 2834:                        break;
 2835:                    }
 2836:                } 
 2837:             }
 2838:             if (!setvis) {
 2839:                 if (document.getElementById(divid)) {
 2840:                     document.getElementById(divid).style.display = 'none';
 2841:                 }
 2842:             }
 2843:         }
 2844:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 2845:         var numsec = form.elements['lti_crssec_'+item].length;
 2846:         if (numsec) {
 2847:             var setvis = '';
 2848:             for (var i=0; i<numsec; i++) {
 2849:                 if (form.elements['lti_crssec_'+item][i].checked) {
 2850:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 2851:                         if (document.getElementById('lti_crssecfield_'+item)) {
 2852:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 2853:                             setvis = 1;
 2854:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 2855:                             if (numsrcsec) {
 2856:                                 var setsrcvis = '';
 2857:                                 for (var j=0; j<numsrcsec; j++) {
 2858:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 2859:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 2860:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 2861:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 2862:                                                 setsrcvis = 1;
 2863:                                             }
 2864:                                         }
 2865:                                     }
 2866:                                 }
 2867:                                 if (!setsrcvis) {
 2868:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 2869:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2870:                                     }
 2871:                                 }
 2872:                             }
 2873:                         }
 2874:                     }
 2875:                 }
 2876:             }
 2877:             if (!setvis) {
 2878:                 if (document.getElementById('lti_crssecfield_'+item)) {
 2879:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 2880:                 }
 2881:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 2882:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2883:                 }
 2884:             }
 2885:         }
 2886:     }
 2887:     return;
 2888: }
 2889: // ]]>
 2890: </script>
 2891: 
 2892: ENDSCRIPT
 2893: }
 2894: 
 2895: sub print_autoenroll {
 2896:     my ($dom,$settings,$rowtotal) = @_;
 2897:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2898:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2899:     if (ref($settings) eq 'HASH') {
 2900:         if (exists($settings->{'run'})) {
 2901:             if ($settings->{'run'} eq '0') {
 2902:                 $runoff = ' checked="checked" ';
 2903:                 $runon = ' ';
 2904:             } else {
 2905:                 $runon = ' checked="checked" ';
 2906:                 $runoff = ' ';
 2907:             }
 2908:         } else {
 2909:             if ($autorun) {
 2910:                 $runon = ' checked="checked" ';
 2911:                 $runoff = ' ';
 2912:             } else {
 2913:                 $runoff = ' checked="checked" ';
 2914:                 $runon = ' ';
 2915:             }
 2916:         }
 2917:         if (exists($settings->{'co-owners'})) {
 2918:             if ($settings->{'co-owners'} eq '0') {
 2919:                 $coownersoff = ' checked="checked" ';
 2920:                 $coownerson = ' ';
 2921:             } else {
 2922:                 $coownerson = ' checked="checked" ';
 2923:                 $coownersoff = ' ';
 2924:             }
 2925:         } else {
 2926:             $coownersoff = ' checked="checked" ';
 2927:             $coownerson = ' ';
 2928:         }
 2929:         if (exists($settings->{'sender_domain'})) {
 2930:             $defdom = $settings->{'sender_domain'};
 2931:         }
 2932:         if (exists($settings->{'autofailsafe'})) {
 2933:             $failsafe = $settings->{'autofailsafe'};
 2934:         }
 2935:     } else {
 2936:         if ($autorun) {
 2937:             $runon = ' checked="checked" ';
 2938:             $runoff = ' ';
 2939:         } else {
 2940:             $runoff = ' checked="checked" ';
 2941:             $runon = ' ';
 2942:         }
 2943:     }
 2944:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2945:     my $notif_sender;
 2946:     if (ref($settings) eq 'HASH') {
 2947:         $notif_sender = $settings->{'sender_uname'};
 2948:     }
 2949:     my $datatable='<tr class="LC_odd_row">'.
 2950:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2951:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2952:                   '<input type="radio" name="autoenroll_run"'.
 2953:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2954:                   '<label><input type="radio" name="autoenroll_run"'.
 2955:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2956:                   '</tr><tr>'.
 2957:                   '<td>'.&mt('Notification messages - sender').
 2958:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2959:                   &mt('username').':&nbsp;'.
 2960:                   '<input type="text" name="sender_uname" value="'.
 2961:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2962:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2963:                   '<tr class="LC_odd_row">'.
 2964:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2965:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2966:                   '<input type="radio" name="autoassign_coowners"'.
 2967:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2968:                   '<label><input type="radio" name="autoassign_coowners"'.
 2969:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2970:                   '</tr><tr>'.
 2971:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2972:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2973:                   '<input type="text" name="autoenroll_failsafe"'.
 2974:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2975:     $$rowtotal += 4;
 2976:     return $datatable;
 2977: }
 2978: 
 2979: sub print_autoupdate {
 2980:     my ($position,$dom,$settings,$rowtotal) = @_;
 2981:     my $datatable;
 2982:     if ($position eq 'top') {
 2983:         my $updateon = ' ';
 2984:         my $updateoff = ' checked="checked" ';
 2985:         my $classlistson = ' ';
 2986:         my $classlistsoff = ' checked="checked" ';
 2987:         if (ref($settings) eq 'HASH') {
 2988:             if ($settings->{'run'} eq '1') {
 2989:                 $updateon = $updateoff;
 2990:                 $updateoff = ' ';
 2991:             }
 2992:             if ($settings->{'classlists'} eq '1') {
 2993:                 $classlistson = $classlistsoff;
 2994:                 $classlistsoff = ' ';
 2995:             }
 2996:         }
 2997:         my %title = (
 2998:                    run => 'Auto-update active?',
 2999:                    classlists => 'Update information in classlists?',
 3000:                     );
 3001:         $datatable = '<tr class="LC_odd_row">'. 
 3002:                   '<td>'.&mt($title{'run'}).'</td>'.
 3003:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3004:                   '<input type="radio" name="autoupdate_run"'.
 3005:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3006:                   '<label><input type="radio" name="autoupdate_run"'.
 3007:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3008:                   '</tr><tr>'.
 3009:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 3010:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3011:                   '<label><input type="radio" name="classlists"'.
 3012:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3013:                   '<label><input type="radio" name="classlists"'.
 3014:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3015:                   '</tr>';
 3016:         $$rowtotal += 2;
 3017:     } elsif ($position eq 'middle') {
 3018:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3019:         my $numinrow = 3;
 3020:         my $locknamesettings;
 3021:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3022:                                      $dom,$numinrow,$othertitle,
 3023:                                     'lockablenames',$rowtotal);
 3024:         $$rowtotal ++;
 3025:     } else {
 3026:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3027:         my @fields = ('lastname','firstname','middlename','generation',
 3028:                       'permanentemail','id');
 3029:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3030:         my $numrows = 0;
 3031:         if (ref($types) eq 'ARRAY') {
 3032:             if (@{$types} > 0) {
 3033:                 $datatable = 
 3034:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3035:                                          \@fields,$types,\$numrows);
 3036:                     $$rowtotal += @{$types}; 
 3037:             }
 3038:         }
 3039:         $datatable .= 
 3040:             &usertype_update_row($settings,{'default' => $othertitle},
 3041:                                  \%fieldtitles,\@fields,['default'],
 3042:                                  \$numrows);
 3043:         $$rowtotal ++;     
 3044:     }
 3045:     return $datatable;
 3046: }
 3047: 
 3048: sub print_autocreate {
 3049:     my ($dom,$settings,$rowtotal) = @_;
 3050:     my (%createon,%createoff,%currhash);
 3051:     my @types = ('xml','req');
 3052:     if (ref($settings) eq 'HASH') {
 3053:         foreach my $item (@types) {
 3054:             $createoff{$item} = ' checked="checked" ';
 3055:             $createon{$item} = ' ';
 3056:             if (exists($settings->{$item})) {
 3057:                 if ($settings->{$item}) {
 3058:                     $createon{$item} = ' checked="checked" ';
 3059:                     $createoff{$item} = ' ';
 3060:                 }
 3061:             }
 3062:         }
 3063:         if ($settings->{'xmldc'} ne '') {
 3064:             $currhash{$settings->{'xmldc'}} = 1;
 3065:         }
 3066:     } else {
 3067:         foreach my $item (@types) {
 3068:             $createoff{$item} = ' checked="checked" ';
 3069:             $createon{$item} = ' ';
 3070:         }
 3071:     }
 3072:     $$rowtotal += 2;
 3073:     my $numinrow = 2;
 3074:     my $datatable='<tr class="LC_odd_row">'.
 3075:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3076:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3077:                   '<input type="radio" name="autocreate_xml"'.
 3078:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3079:                   '<label><input type="radio" name="autocreate_xml"'.
 3080:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3081:                   '</td></tr><tr>'.
 3082:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3083:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3084:                   '<input type="radio" name="autocreate_req"'.
 3085:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3086:                   '<label><input type="radio" name="autocreate_req"'.
 3087:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3088:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3089:                                                    'autocreate_xmldc',%currhash);
 3090:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3091:     if ($numdc > 1) {
 3092:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3093:                       '</td><td class="LC_left_item">';
 3094:     } else {
 3095:         $datatable .= &mt('Course creation processed as:').
 3096:                       '</td><td class="LC_right_item">';
 3097:     }
 3098:     $datatable .= $dctable.'</td></tr>';
 3099:     $$rowtotal += $rows;
 3100:     return $datatable;
 3101: }
 3102: 
 3103: sub print_directorysrch {
 3104:     my ($position,$dom,$settings,$rowtotal) = @_;
 3105:     my $datatable;
 3106:     if ($position eq 'top') {
 3107:         my $instsrchon = ' ';
 3108:         my $instsrchoff = ' checked="checked" ';
 3109:         my ($exacton,$containson,$beginson);
 3110:         my $instlocalon = ' ';
 3111:         my $instlocaloff = ' checked="checked" ';
 3112:         if (ref($settings) eq 'HASH') {
 3113:             if ($settings->{'available'} eq '1') {
 3114:                 $instsrchon = $instsrchoff;
 3115:                 $instsrchoff = ' ';
 3116:             }
 3117:             if ($settings->{'localonly'} eq '1') {
 3118:                 $instlocalon = $instlocaloff;
 3119:                 $instlocaloff = ' ';
 3120:             }
 3121:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3122:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3123:                     if ($type eq 'exact') {
 3124:                         $exacton = ' checked="checked" ';
 3125:                     } elsif ($type eq 'contains') {
 3126:                         $containson = ' checked="checked" ';
 3127:                     } elsif ($type eq 'begins') {
 3128:                         $beginson = ' checked="checked" ';
 3129:                     }
 3130:                 }
 3131:             } else {
 3132:                 if ($settings->{'searchtypes'} eq 'exact') {
 3133:                     $exacton = ' checked="checked" ';
 3134:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3135:                     $containson = ' checked="checked" ';
 3136:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3137:                     $exacton = ' checked="checked" ';
 3138:                     $containson = ' checked="checked" ';
 3139:                 }
 3140:             }
 3141:         }
 3142:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3143:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3144: 
 3145:         my $numinrow = 4;
 3146:         my $cansrchrow = 0;
 3147:         $datatable='<tr class="LC_odd_row">'.
 3148:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3149:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3150:                    '<input type="radio" name="dirsrch_available"'.
 3151:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3152:                    '<label><input type="radio" name="dirsrch_available"'.
 3153:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3154:                    '</tr><tr>'.
 3155:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3156:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3157:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3158:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3159:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3160:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3161:                    '</tr>';
 3162:         $$rowtotal += 2;
 3163:         if (ref($usertypes) eq 'HASH') {
 3164:             if (keys(%{$usertypes}) > 0) {
 3165:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3166:                                              $numinrow,$othertitle,'cansearch',
 3167:                                              $rowtotal);
 3168:                 $cansrchrow = 1;
 3169:             }
 3170:         }
 3171:         if ($cansrchrow) {
 3172:             $$rowtotal ++;
 3173:             $datatable .= '<tr>';
 3174:         } else {
 3175:             $datatable .= '<tr class="LC_odd_row">';
 3176:         }
 3177:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3178:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3179:         foreach my $title (@{$titleorder}) {
 3180:             if (defined($searchtitles->{$title})) {
 3181:                 my $check = ' ';
 3182:                 if (ref($settings) eq 'HASH') {
 3183:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3184:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3185:                             $check = ' checked="checked" ';
 3186:                         }
 3187:                     }
 3188:                 }
 3189:                 $datatable .= '<td class="LC_left_item">'.
 3190:                               '<span class="LC_nobreak"><label>'.
 3191:                               '<input type="checkbox" name="searchby" '.
 3192:                               'value="'.$title.'"'.$check.'/>'.
 3193:                               $searchtitles->{$title}.'</label></span></td>';
 3194:             }
 3195:         }
 3196:         $datatable .= '</tr></table></td></tr>';
 3197:         $$rowtotal ++;
 3198:         if ($cansrchrow) {
 3199:             $datatable .= '<tr class="LC_odd_row">';
 3200:         } else {
 3201:             $datatable .= '<tr>';
 3202:         }
 3203:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3204:                       '<td class="LC_left_item" colspan="2">'.
 3205:                       '<span class="LC_nobreak"><label>'.
 3206:                       '<input type="checkbox" name="searchtypes" '.
 3207:                       $exacton.' value="exact" />'.&mt('Exact match').
 3208:                       '</label>&nbsp;'.
 3209:                       '<label><input type="checkbox" name="searchtypes" '.
 3210:                       $beginson.' value="begins" />'.&mt('Begins with').
 3211:                       '</label>&nbsp;'.
 3212:                       '<label><input type="checkbox" name="searchtypes" '.
 3213:                       $containson.' value="contains" />'.&mt('Contains').
 3214:                       '</label></span></td></tr>';
 3215:         $$rowtotal ++;
 3216:     } else {
 3217:         my $domsrchon = ' checked="checked" ';
 3218:         my $domsrchoff = ' ';
 3219:         my $domlocalon = ' ';
 3220:         my $domlocaloff = ' checked="checked" ';
 3221:         if (ref($settings) eq 'HASH') {
 3222:             if ($settings->{'lclocalonly'} eq '1') {
 3223:                 $domlocalon = $domlocaloff;
 3224:                 $domlocaloff = ' ';
 3225:             }
 3226:             if ($settings->{'lcavailable'} eq '0') {
 3227:                 $domsrchoff = $domsrchon;
 3228:                 $domsrchon = ' ';
 3229:             }
 3230:         }
 3231:         $datatable='<tr class="LC_odd_row">'.
 3232:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3233:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3234:                       '<input type="radio" name="dirsrch_domavailable"'.
 3235:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3236:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3237:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3238:                       '</tr><tr>'.
 3239:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3240:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3241:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3242:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3243:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3244:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3245:                       '</tr>';
 3246:         $$rowtotal += 2;
 3247:     }
 3248:     return $datatable;
 3249: }
 3250: 
 3251: sub print_contacts {
 3252:     my ($position,$dom,$settings,$rowtotal) = @_;
 3253:     my $datatable;
 3254:     my @contacts = ('adminemail','supportemail');
 3255:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3256:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 3257:     if ($position eq 'top') {
 3258:         if (ref($settings) eq 'HASH') {
 3259:             foreach my $item (@contacts) {
 3260:                 if (exists($settings->{$item})) {
 3261:                     $to{$item} = $settings->{$item};
 3262:                 }
 3263:             }
 3264:         }
 3265:     } elsif ($position eq 'middle') {
 3266:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3267:                      'updatesmail','idconflictsmail');
 3268:         foreach my $type (@mailings) {
 3269:             $otheremails{$type} = '';
 3270:         }
 3271:     } else {
 3272:         @mailings = ('helpdeskmail','otherdomsmail');
 3273:         foreach my $type (@mailings) {
 3274:             $otheremails{$type} = '';
 3275:         }
 3276:         $bccemails{'helpdeskmail'} = '';
 3277:         $bccemails{'otherdomsmail'} = '';
 3278:         $includestr{'helpdeskmail'} = '';
 3279:         $includestr{'otherdomsmail'} = '';
 3280:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3281:     }
 3282:     if (ref($settings) eq 'HASH') {
 3283:         unless ($position eq 'top') {
 3284:             foreach my $type (@mailings) {
 3285:                 if (exists($settings->{$type})) {
 3286:                     if (ref($settings->{$type}) eq 'HASH') {
 3287:                         foreach my $item (@contacts) {
 3288:                             if ($settings->{$type}{$item}) {
 3289:                                 $checked{$type}{$item} = ' checked="checked" ';
 3290:                             }
 3291:                         }
 3292:                         $otheremails{$type} = $settings->{$type}{'others'};
 3293:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3294:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3295:                             if ($settings->{$type}{'include'} ne '') {
 3296:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3297:                                 $includestr{$type} = &unescape($includestr{$type});
 3298:                             }
 3299:                         }
 3300:                     }
 3301:                 } elsif ($type eq 'lonstatusmail') {
 3302:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3303:                 }
 3304:             }
 3305:         }
 3306:         if ($position eq 'bottom') {
 3307:             foreach my $type (@mailings) {
 3308:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3309:                 if ($settings->{$type}{'include'} ne '') {
 3310:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3311:                     $includestr{$type} = &unescape($includestr{$type});
 3312:                 }
 3313:             }
 3314:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3315:                 if (ref($fields) eq 'ARRAY') {
 3316:                     foreach my $field (@{$fields}) {
 3317:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3318:                     }
 3319:                 }
 3320:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3321:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3322:                 } else {
 3323:                     $maxsize = '1.0';
 3324:                 }
 3325:             } else {
 3326:                 if (ref($fields) eq 'ARRAY') {
 3327:                     foreach my $field (@{$fields}) {
 3328:                         $currfield{$field} = 'yes';
 3329:                     }
 3330:                 }
 3331:                 $maxsize = '1.0';
 3332:             }
 3333:         }
 3334:     } else {
 3335:         if ($position eq 'top') {
 3336:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3337:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3338:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3339:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3340:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3341:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3342:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3343:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3344:         } elsif ($position eq 'bottom') {
 3345:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3346:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3347:             if (ref($fields) eq 'ARRAY') {
 3348:                 foreach my $field (@{$fields}) {
 3349:                     $currfield{$field} = 'yes';
 3350:                 }
 3351:             }
 3352:             $maxsize = '1.0';
 3353:         }
 3354:     }
 3355:     my ($titles,$short_titles) = &contact_titles();
 3356:     my $rownum = 0;
 3357:     my $css_class;
 3358:     if ($position eq 'top') {
 3359:         foreach my $item (@contacts) {
 3360:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3361:             $datatable .= '<tr'.$css_class.'>'. 
 3362:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3363:                           '</span></td><td class="LC_right_item">'.
 3364:                           '<input type="text" name="'.$item.'" value="'.
 3365:                           $to{$item}.'" /></td></tr>';
 3366:             $rownum ++;
 3367:         }
 3368:     } elsif ($position eq 'bottom') {
 3369:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3370:         $datatable .= '<tr'.$css_class.'>'.
 3371:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3372:                       &mt('(e-mail, subject, and description always shown)').
 3373:                       '</td><td class="LC_left_item">';
 3374:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3375:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3376:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3377:             foreach my $field (@{$fields}) {
 3378:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3379:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3380:                     $datatable .= ' '.&mt('(logged-in users)');
 3381:                 }
 3382:                 $datatable .='</td><td>';
 3383:                 my $clickaction;
 3384:                 if ($field eq 'screenshot') {
 3385:                     $clickaction = ' onclick="screenshotSize(this);"';
 3386:                 }
 3387:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3388:                     foreach my $option (@{$possoptions->{$field}}) {
 3389:                         my $checked;
 3390:                         if ($currfield{$field} eq $option) {
 3391:                             $checked = ' checked="checked"';
 3392:                         }
 3393:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3394:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3395:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3396:                                       '</label></span>'.('&nbsp;'x2);
 3397:                     }
 3398:                 }
 3399:                 if ($field eq 'screenshot') {
 3400:                     my $display;
 3401:                     if ($currfield{$field} eq 'no') {
 3402:                         $display = ' style="display:none"';
 3403:                     }
 3404:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3405:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3406:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3407:                 }
 3408:                 $datatable .= '</td></tr>';
 3409:             }
 3410:             $datatable .= '</table>';
 3411:         }
 3412:         $datatable .= '</td></tr>'."\n";
 3413:         $rownum ++;
 3414:     }
 3415:     unless ($position eq 'top') {
 3416:         foreach my $type (@mailings) {
 3417:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3418:             $datatable .= '<tr'.$css_class.'>'.
 3419:                           '<td><span class="LC_nobreak">'.
 3420:                           $titles->{$type}.': </span></td>'.
 3421:                           '<td class="LC_left_item">';
 3422:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3423:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3424:             }
 3425:             $datatable .= '<span class="LC_nobreak">';
 3426:             foreach my $item (@contacts) {
 3427:                 $datatable .= '<label>'.
 3428:                               '<input type="checkbox" name="'.$type.'"'.
 3429:                               $checked{$type}{$item}.
 3430:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3431:                               '</label>&nbsp;';
 3432:             }
 3433:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3434:                           '<input type="text" name="'.$type.'_others" '.
 3435:                           'value="'.$otheremails{$type}.'"  />';
 3436:             my %locchecked;
 3437:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3438:                 foreach my $loc ('s','b') {
 3439:                     if ($includeloc{$type} eq $loc) {
 3440:                         $locchecked{$loc} = ' checked="checked"';
 3441:                         last;
 3442:                     }
 3443:                 }
 3444:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3445:                               '<input type="text" name="'.$type.'_bcc" '.
 3446:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3447:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3448:                               &mt('Text automatically added to e-mail:').' '.
 3449:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3450:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3451:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3452:                               ('&nbsp;'x2).
 3453:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3454:                               '</span></fieldset>';
 3455:             }
 3456:             $datatable .= '</td></tr>'."\n";
 3457:             $rownum ++;
 3458:         }
 3459:     }
 3460:     if ($position eq 'middle') {
 3461:         my %choices;
 3462:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3463:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3464:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3465:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3466:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3467:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3468:         my @toggles = ('reporterrors','reportupdates');
 3469:         my %defaultchecked = ('reporterrors'  => 'on',
 3470:                               'reportupdates' => 'on');
 3471:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3472:                                                    \%choices,$rownum);
 3473:         $datatable .= $reports;
 3474:     } elsif ($position eq 'bottom') {
 3475:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3476:         my (@posstypes,%usertypeshash);
 3477:         if (ref($types) eq 'ARRAY') {
 3478:             @posstypes = @{$types};
 3479:         }
 3480:         if (@posstypes) {
 3481:             if (ref($usertypes) eq 'HASH') {
 3482:                 %usertypeshash = %{$usertypes};
 3483:             }
 3484:             my @overridden;
 3485:             my $numinrow = 4;
 3486:             if (ref($settings) eq 'HASH') {
 3487:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3488:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3489:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3490:                             push(@overridden,$key);
 3491:                             foreach my $item (@contacts) {
 3492:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3493:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3494:                                 }
 3495:                             }
 3496:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3497:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3498:                             $includeloc{'override_'.$key} = '';
 3499:                             $includestr{'override_'.$key} = '';
 3500:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3501:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3502:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3503:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3504:                             }
 3505:                         }
 3506:                     }
 3507:                 }
 3508:             }
 3509:             my $customclass = 'LC_helpdesk_override';
 3510:             my $optionsprefix = 'LC_options_helpdesk_';
 3511: 
 3512:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3513:  
 3514:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3515:                                          $numinrow,$othertitle,'overrides',
 3516:                                          \$rownum,$onclicktypes,$customclass);
 3517:             $rownum ++;
 3518:             $usertypeshash{'default'} = $othertitle;
 3519:             foreach my $status (@posstypes) {
 3520:                 my $css_class;
 3521:                 if ($rownum%2) {
 3522:                     $css_class = 'LC_odd_row ';
 3523:                 }
 3524:                 $css_class .= $customclass;
 3525:                 my $rowid = $optionsprefix.$status;
 3526:                 my $hidden = 1;
 3527:                 my $currstyle = 'display:none';
 3528:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3529:                     $currstyle = 'display:table-row';
 3530:                     $hidden = 0;
 3531:                 }
 3532:                 my $key = 'override_'.$status;
 3533:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3534:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3535:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3536:                                                   \@contacts,$short_titles);
 3537:                 unless ($hidden) {
 3538:                     $rownum ++;
 3539:                 }
 3540:             }
 3541:         }
 3542:     }
 3543:     $$rowtotal += $rownum;
 3544:     return $datatable;
 3545: }
 3546: 
 3547: sub overridden_helpdesk {
 3548:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3549:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3550:     my $class = 'LC_left_item';
 3551:     if ($css_class) {
 3552:         $css_class = ' class="'.$css_class.'"';
 3553:     }
 3554:     if ($rowid) {
 3555:         $rowid = ' id="'.$rowid.'"';
 3556:     }
 3557:     if ($rowstyle) {
 3558:         $rowstyle = ' style="'.$rowstyle.'"';
 3559:     }
 3560:     my ($output,$description);
 3561:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3562:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3563:               "<td>$description</td>\n".
 3564:               '<td class="'.$class.'" colspan="2">'.
 3565:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3566:               '<span class="LC_nobreak">';
 3567:     if (ref($contacts) eq 'ARRAY') {
 3568:         foreach my $item (@{$contacts}) {
 3569:             my $check;
 3570:             if (ref($checked) eq 'HASH') {
 3571:                $check = $checked->{$item};
 3572:             }
 3573:             my $title;
 3574:             if (ref($short_titles) eq 'HASH') {
 3575:                 $title = $short_titles->{$item}; 
 3576:             }
 3577:             $output .= '<label>'.
 3578:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3579:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3580:         }
 3581:     }
 3582:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3583:                '<input type="text" name="override_'.$type.'_others" '.
 3584:                'value="'.$otheremails.'"  />';
 3585:     my %locchecked;
 3586:     foreach my $loc ('s','b') {
 3587:         if ($includeloc eq $loc) {
 3588:             $locchecked{$loc} = ' checked="checked"';
 3589:             last;
 3590:         }
 3591:     }
 3592:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3593:                '<input type="text" name="override_'.$type.'_bcc" '.
 3594:                'value="'.$bccemails.'"  /></fieldset>'.
 3595:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3596:                &mt('Text automatically added to e-mail:').' '.
 3597:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br >'.
 3598:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3599:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3600:                ('&nbsp;'x2).
 3601:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3602:                '</span></fieldset>'.
 3603:                '</td></tr>'."\n";
 3604:     return $output;
 3605: }
 3606: 
 3607: sub contacts_javascript {
 3608:     return <<"ENDSCRIPT";
 3609: 
 3610: <script type="text/javascript">
 3611: // <![CDATA[
 3612: 
 3613: function screenshotSize(field) {
 3614:     if (document.getElementById('help_screenshotsize')) {
 3615:         if (field.value == 'no') {
 3616:             document.getElementById('help_screenshotsize').style.display="none";
 3617:         } else {
 3618:             document.getElementById('help_screenshotsize').style.display="";
 3619:         }
 3620:     }
 3621:     return;
 3622: }
 3623: 
 3624: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3625:     if (form.elements[checkbox].length != undefined) {
 3626:         var count = 0;
 3627:         if (docount) {
 3628:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3629:                 if (form.elements[checkbox][i].checked) {
 3630:                     count ++;
 3631:                 }
 3632:             }
 3633:         }
 3634:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3635:             var type = form.elements[checkbox][i].value;
 3636:             if (document.getElementById(prefix+type)) {
 3637:                 if (form.elements[checkbox][i].checked) {
 3638:                     document.getElementById(prefix+type).style.display = 'table-row';
 3639:                     if (count % 2 == 1) {
 3640:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3641:                     } else {
 3642:                         document.getElementById(prefix+type).className = target;
 3643:                     }
 3644:                     count ++;
 3645:                 } else {
 3646:                     document.getElementById(prefix+type).style.display = 'none';
 3647:                 }
 3648:             }
 3649:         }
 3650:     }
 3651:     return;
 3652: }
 3653: 
 3654: 
 3655: // ]]>
 3656: </script>
 3657: 
 3658: ENDSCRIPT
 3659: }
 3660: 
 3661: sub print_helpsettings {
 3662:     my ($position,$dom,$settings,$rowtotal) = @_;
 3663:     my $confname = $dom.'-domainconfig';
 3664:     my $formname = 'display';
 3665:     my ($datatable,$itemcount);
 3666:     if ($position eq 'top') {
 3667:         $itemcount = 1;
 3668:         my (%choices,%defaultchecked,@toggles);
 3669:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3670:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3671:                                      &mt('LON-CAPA bug tracker'),600,500));
 3672:         %defaultchecked = ('submitbugs' => 'on');
 3673:         @toggles = ('submitbugs');
 3674:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3675:                                                      \%choices,$itemcount);
 3676:         $$rowtotal ++;
 3677:     } else {
 3678:         my $css_class;
 3679:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3680:         my (%customroles,%ordered,%current);
 3681:         if (ref($settings) eq 'HASH') {
 3682:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3683:                 %current = %{$settings->{'adhoc'}};
 3684:             }
 3685:         }
 3686:         my $count = 0;
 3687:         foreach my $key (sort(keys(%existing))) {
 3688:             if ($key=~/^rolesdef\_(\w+)$/) {
 3689:                 my $rolename = $1;
 3690:                 my (%privs,$order);
 3691:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3692:                 $customroles{$rolename} = \%privs;
 3693:                 if (ref($current{$rolename}) eq 'HASH') {
 3694:                     $order = $current{$rolename}{'order'};
 3695:                 }
 3696:                 if ($order eq '') {
 3697:                     $order = $count;
 3698:                 }
 3699:                 $ordered{$order} = $rolename;
 3700:                 $count++;
 3701:             }
 3702:         }
 3703:         my $maxnum = scalar(keys(%ordered));
 3704:         my @roles_by_num = ();
 3705:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3706:             push(@roles_by_num,$item);
 3707:         }
 3708:         my $context = 'domprefs';
 3709:         my $crstype = 'Course';
 3710:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3711:         my @accesstypes = ('all','dh','da','none');
 3712:         my ($numstatustypes,@jsarray);
 3713:         if (ref($types) eq 'ARRAY') {
 3714:             if (@{$types} > 0) {
 3715:                 $numstatustypes = scalar(@{$types});
 3716:                 push(@accesstypes,'status');
 3717:                 @jsarray = ('bystatus');
 3718:             }
 3719:         }
 3720:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3721:         if (keys(%domhelpdesk)) {
 3722:             push(@accesstypes,('inc','exc'));
 3723:             push(@jsarray,('notinc','notexc'));
 3724:         }
 3725:         my $hiddenstr = join("','",@jsarray);
 3726:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3727:         my $context = 'domprefs';
 3728:         my $crstype = 'Course';
 3729:         my $prefix = 'helproles_';
 3730:         my $add_class = 'LC_hidden';
 3731:         foreach my $num (@roles_by_num) {
 3732:             my $role = $ordered{$num};
 3733:             my ($desc,$access,@statuses);
 3734:             if (ref($current{$role}) eq 'HASH') {
 3735:                 $desc = $current{$role}{'desc'};
 3736:                 $access = $current{$role}{'access'};
 3737:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3738:                     @statuses = @{$current{$role}{'insttypes'}};
 3739:                 }
 3740:             }
 3741:             if ($desc eq '') {
 3742:                 $desc = $role;
 3743:             }
 3744:             my $identifier = 'custhelp'.$num;
 3745:             my %full=();
 3746:             my %levels= (
 3747:                          course => {},
 3748:                          domain => {},
 3749:                          system => {},
 3750:                         );
 3751:             my %levelscurrent=(
 3752:                                course => {},
 3753:                                domain => {},
 3754:                                system => {},
 3755:                               );
 3756:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3757:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3758:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3759:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3760:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 3761:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3762:             for (my $k=0; $k<=$maxnum; $k++) {
 3763:                 my $vpos = $k+1;
 3764:                 my $selstr;
 3765:                 if ($k == $num) {
 3766:                     $selstr = ' selected="selected" ';
 3767:                 }
 3768:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3769:             }
 3770:             $datatable .= '</select>'.('&nbsp;'x2).
 3771:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3772:                           '</td>'.
 3773:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3774:                           &mt('Name shown to users:').
 3775:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3776:                           '</fieldset>'.
 3777:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3778:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3779:                           '<fieldset>'.
 3780:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3781:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3782:                                                                    \%levelscurrent,$identifier,
 3783:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3784:                           '</fieldset></td>';
 3785:             $itemcount ++;
 3786:         }
 3787:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3788:         my $newcust = 'custhelp'.$count;
 3789:         my (%privs,%levelscurrent);
 3790:         my %full=();
 3791:         my %levels= (
 3792:                      course => {},
 3793:                      domain => {},
 3794:                      system => {},
 3795:                     );
 3796:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3797:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3798:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3799:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 3800:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3801:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3802:         for (my $k=0; $k<$maxnum+1; $k++) {
 3803:             my $vpos = $k+1;
 3804:             my $selstr;
 3805:             if ($k == $maxnum) {
 3806:                 $selstr = ' selected="selected" ';
 3807:             }
 3808:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3809:         }
 3810:         $datatable .= '</select>&nbsp;'."\n".
 3811:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3812:                       '</label></span></td>'.
 3813:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3814:                       '<span class="LC_nobreak">'.
 3815:                       &mt('Internal name:').
 3816:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3817:                       '</span>'.('&nbsp;'x4).
 3818:                       '<span class="LC_nobreak">'.
 3819:                       &mt('Name shown to users:').
 3820:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3821:                       '</span></fieldset>'.
 3822:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3823:                                              $usertypes,$types,\%domhelpdesk).
 3824:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3825:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3826:                                                                 \@templateroles,$newcust).
 3827:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3828:                                                                \%levelscurrent,$newcust).
 3829:                       '</fieldset></td></tr>';
 3830:         $count ++;
 3831:         $$rowtotal += $count;
 3832:     }
 3833:     return $datatable;
 3834: }
 3835: 
 3836: sub adhocbutton {
 3837:     my ($prefix,$num,$field,$visibility) = @_;
 3838:     my %lt = &Apache::lonlocal::texthash(
 3839:                                           show => 'Show details',
 3840:                                           hide => 'Hide details',
 3841:                                         );
 3842:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3843:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3844:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3845:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3846: }
 3847: 
 3848: sub helpsettings_javascript {
 3849:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3850:     return unless(ref($roles_by_num) eq 'ARRAY');
 3851:     my %html_js_lt = &Apache::lonlocal::texthash(
 3852:                                           show => 'Show details',
 3853:                                           hide => 'Hide details',
 3854:                                         );
 3855:     &html_escape(\%html_js_lt);
 3856:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3857:     return <<"ENDSCRIPT";
 3858: <script type="text/javascript">
 3859: // <![CDATA[
 3860: 
 3861: function reorderHelpRoles(form,item) {
 3862:     var changedVal;
 3863: $jstext
 3864:     var newpos = 'helproles_${total}_pos';
 3865:     var maxh = 1 + $total;
 3866:     var current = new Array();
 3867:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3868:     if (item == newpos) {
 3869:         changedVal = newitemVal;
 3870:     } else {
 3871:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3872:         current[newitemVal] = newpos;
 3873:     }
 3874:     for (var i=0; i<helproles.length; i++) {
 3875:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3876:         if (elementName != item) {
 3877:             if (form.elements[elementName]) {
 3878:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3879:                 current[currVal] = elementName;
 3880:             }
 3881:         }
 3882:     }
 3883:     var oldVal;
 3884:     for (var j=0; j<maxh; j++) {
 3885:         if (current[j] == undefined) {
 3886:             oldVal = j;
 3887:         }
 3888:     }
 3889:     if (oldVal < changedVal) {
 3890:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3891:            var elementName = current[k];
 3892:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3893:         }
 3894:     } else {
 3895:         for (var k=changedVal; k<oldVal; k++) {
 3896:             var elementName = current[k];
 3897:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3898:         }
 3899:     }
 3900:     return;
 3901: }
 3902: 
 3903: function helpdeskAccess(num) {
 3904:     var curraccess = null;
 3905:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3906:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3907:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3908:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3909:             }
 3910:         }
 3911:     }
 3912:     var shown = Array();
 3913:     var hidden = Array();
 3914:     if (curraccess == 'none') {
 3915:         hidden = Array('$hiddenstr');
 3916:     } else {
 3917:         if (curraccess == 'status') {
 3918:             shown = Array('bystatus');
 3919:             hidden = Array('notinc','notexc');
 3920:         } else {
 3921:             if (curraccess == 'exc') {
 3922:                 shown = Array('notexc');
 3923:                 hidden = Array('notinc','bystatus');
 3924:             }
 3925:             if (curraccess == 'inc') {
 3926:                 shown = Array('notinc');
 3927:                 hidden = Array('notexc','bystatus');
 3928:             }
 3929:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3930:                 hidden = Array('notinc','notexc','bystatus');
 3931:             }
 3932:         }
 3933:     }
 3934:     if (hidden.length > 0) {
 3935:         for (var i=0; i<hidden.length; i++) {
 3936:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3937:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3938:             }
 3939:         }
 3940:     }
 3941:     if (shown.length > 0) {
 3942:         for (var i=0; i<shown.length; i++) {
 3943:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3944:                 if (shown[i] == 'privs') {
 3945:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3946:                 } else {
 3947:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3948:                 }
 3949:             }
 3950:         }
 3951:     }
 3952:     return;
 3953: }
 3954: 
 3955: function toggleHelpdeskItem(num,field) {
 3956:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3957:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3958:             document.getElementById('helproles_'+num+'_'+field).className =
 3959:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3960:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3961:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3962:             }
 3963:         } else {
 3964:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3965:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3966:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3967:             }
 3968:         }
 3969:     }
 3970:     return;
 3971: }
 3972: 
 3973: // ]]>
 3974: </script>
 3975: 
 3976: ENDSCRIPT
 3977: }
 3978: 
 3979: sub helpdeskroles_access {
 3980:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3981:         $usertypes,$types,$domhelpdesk) = @_;
 3982:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3983:     my %lt = &Apache::lonlocal::texthash(
 3984:                     'rou'    => 'Role usage',
 3985:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3986:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3987:                     'dh'     => 'All with domain helpdesk role',
 3988:                     'da'     => 'All with domain helpdesk assistant role',
 3989:                     'none'   => 'None',
 3990:                     'status' => 'Determined based on institutional status',
 3991:                     'inc'    => 'Include all, but exclude specific personnel',
 3992:                     'exc'    => 'Exclude all, but include specific personnel',
 3993:                   );
 3994:     my %usecheck = (
 3995:                      all => ' checked="checked"',
 3996:                    );
 3997:     my %displaydiv = (
 3998:                       status => 'none',
 3999:                       inc    => 'none',
 4000:                       exc    => 'none',
 4001:                       priv   => 'block',
 4002:                      );
 4003:     my $output;
 4004:     if (ref($current) eq 'HASH') {
 4005:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4006:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4007:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4008:                 delete($usecheck{'all'});
 4009:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4010:                     my $access = $1;
 4011:                     $displaydiv{$access} = 'inline';
 4012:                 } elsif ($current->{access} eq 'none') {
 4013:                     $displaydiv{'priv'} = 'none';
 4014:                 }
 4015:             }
 4016:         }
 4017:     }
 4018:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4019:               '<p>'.$lt{'whi'}.'</p>';
 4020:     foreach my $access (@{$accesstypes}) {
 4021:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4022:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4023:                    $lt{$access}.'</label>';
 4024:         if ($access eq 'status') {
 4025:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4026:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4027:                                                                  $othertitle,$usertypes,$types).
 4028:                        '</div>';
 4029:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4030:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4031:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4032:                        '</div>';
 4033:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4034:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4035:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4036:                        '</div>';
 4037:         }
 4038:         $output .= '</p>';
 4039:     }
 4040:     $output .= '</fieldset>';
 4041:     return $output;
 4042: }
 4043: 
 4044: sub radiobutton_prefs {
 4045:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4046:         $additional,$align) = @_;
 4047:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4048:                    (ref($choices) eq 'HASH'));
 4049: 
 4050:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4051: 
 4052:     foreach my $item (@{$toggles}) {
 4053:         if ($defaultchecked->{$item} eq 'on') {
 4054:             $checkedon{$item} = ' checked="checked" ';
 4055:             $checkedoff{$item} = ' ';
 4056:         } elsif ($defaultchecked->{$item} eq 'off') {
 4057:             $checkedoff{$item} = ' checked="checked" ';
 4058:             $checkedon{$item} = ' ';
 4059:         }
 4060:     }
 4061:     if (ref($settings) eq 'HASH') {
 4062:         foreach my $item (@{$toggles}) {
 4063:             if ($settings->{$item} eq '1') {
 4064:                 $checkedon{$item} =  ' checked="checked" ';
 4065:                 $checkedoff{$item} = ' ';
 4066:             } elsif ($settings->{$item} eq '0') {
 4067:                 $checkedoff{$item} =  ' checked="checked" ';
 4068:                 $checkedon{$item} = ' ';
 4069:             }
 4070:         }
 4071:     }
 4072:     if ($onclick) {
 4073:         $onclick = ' onclick="'.$onclick.'"';
 4074:     }
 4075:     foreach my $item (@{$toggles}) {
 4076:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4077:         $datatable .=
 4078:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4079:             '<span class="LC_nobreak">'.$choices->{$item}.
 4080:             '</span></td>';
 4081:         if ($align eq 'left') {
 4082:             $datatable .= '<td class="LC_left_item">';
 4083:         } else {
 4084:             $datatable .= '<td class="LC_right_item">';
 4085:         }
 4086:         $datatable .=
 4087:             '<span class="LC_nobreak">'.
 4088:             '<label><input type="radio" name="'.
 4089:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4090:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4091:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4092:             '</span>'.$additional.
 4093:             '</td>'.
 4094:             '</tr>';
 4095:         $itemcount ++;
 4096:     }
 4097:     return ($datatable,$itemcount);
 4098: }
 4099: 
 4100: sub print_ltitools {
 4101:     my ($dom,$settings,$rowtotal) = @_;
 4102:     my $rownum = 0;
 4103:     my $css_class;
 4104:     my $itemcount = 1;
 4105:     my $maxnum = 0;
 4106:     my %ordered;
 4107:     if (ref($settings) eq 'HASH') {
 4108:         foreach my $item (keys(%{$settings})) {
 4109:             if (ref($settings->{$item}) eq 'HASH') {
 4110:                 my $num = $settings->{$item}{'order'};
 4111:                 $ordered{$num} = $item;
 4112:             }
 4113:         }
 4114:     }
 4115:     my $confname = $dom.'-domainconfig';
 4116:     my $switchserver = &check_switchserver($dom,$confname);
 4117:     my $maxnum = scalar(keys(%ordered));
 4118:     my $datatable = &ltitools_javascript($settings);
 4119:     my %lt = &ltitools_names();
 4120:     my @courseroles = ('cc','in','ta','ep','st');
 4121:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4122:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4123:     if (keys(%ordered)) {
 4124:         my @items = sort { $a <=> $b } keys(%ordered);
 4125:         for (my $i=0; $i<@items; $i++) {
 4126:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4127:             my $item = $ordered{$items[$i]};
 4128:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4129:             if (ref($settings->{$item}) eq 'HASH') {
 4130:                 $title = $settings->{$item}->{'title'};
 4131:                 $url = $settings->{$item}->{'url'};
 4132:                 $key = $settings->{$item}->{'key'};
 4133:                 $secret = $settings->{$item}->{'secret'};
 4134:                 $lifetime = $settings->{$item}->{'lifetime'};
 4135:                 my $image = $settings->{$item}->{'image'};
 4136:                 if ($image ne '') {
 4137:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4138:                 }
 4139:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4140:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4141:                 } else {
 4142:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4143:                 }
 4144:             }
 4145:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4146:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4147:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4148:             for (my $k=0; $k<=$maxnum; $k++) {
 4149:                 my $vpos = $k+1;
 4150:                 my $selstr;
 4151:                 if ($k == $i) {
 4152:                     $selstr = ' selected="selected" ';
 4153:                 }
 4154:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4155:             }
 4156:             $datatable .= '</select>'.('&nbsp;'x2).
 4157:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4158:                 &mt('Delete?').'</label></span></td>'.
 4159:                 '<td colspan="2">'.
 4160:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4161:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4162:                 ('&nbsp;'x2).
 4163:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4164:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4165:                 ('&nbsp;'x2).
 4166:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4167:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4168:                 ('&nbsp;'x2).
 4169:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4170:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4171:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4172:                 '<br /><br />'.
 4173:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4174:                 ' value="'.$url.'" /></span>'.
 4175:                 ('&nbsp;'x2).
 4176:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4177:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4178:                 ('&nbsp;'x2).
 4179:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4180:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4181:                 ('&nbsp;'x2).
 4182:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4183:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4184:                 '<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>'.
 4185:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4186:                 '</fieldset>'.
 4187:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4188:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4189:             my %currdisp;
 4190:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4191:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4192:                     $currdisp{'window'} = ' checked="checked"';
 4193:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4194:                     $currdisp{'tab'} = ' checked="checked"';
 4195:                 } else {
 4196:                     $currdisp{'iframe'} = ' checked="checked"';
 4197:                 }
 4198:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4199:                     $currdisp{'width'} = $1;
 4200:                 }
 4201:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4202:                      $currdisp{'height'} = $1;
 4203:                 }
 4204:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4205:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4206:             } else {
 4207:                 $currdisp{'iframe'} = ' checked="checked"';
 4208:             }
 4209:             foreach my $disp ('iframe','tab','window') {
 4210:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4211:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4212:             }
 4213:             $datatable .= ('&nbsp;'x4);
 4214:             foreach my $dimen ('width','height') {
 4215:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4216:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4217:                               ('&nbsp;'x2);
 4218:             }
 4219:             $datatable .= '<br />'.
 4220:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4221:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 4222:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4223:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4224:                           '</textarea></div><div style=""></div><br />';
 4225:             my %units = (
 4226:                           'passback' => 'days',
 4227:                           'roster'   => 'seconds',
 4228:                         );
 4229:             foreach my $extra ('passback','roster') {
 4230:                 my $validsty = 'none';
 4231:                 my $currvalid;
 4232:                 my $checkedon = '';
 4233:                 my $checkedoff = ' checked="checked"';
 4234:                 if ($settings->{$item}->{$extra}) {
 4235:                     $checkedon = $checkedoff;
 4236:                     $checkedoff = '';
 4237:                     $validsty = 'inline-block';
 4238:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4239:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4240:                     }
 4241:                 }
 4242:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4243:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4244:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4245:                               &mt('No').'</label>'.('&nbsp;'x2).
 4246:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4247:                               &mt('Yes').'</label></span></div>'.
 4248:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4249:                               '<span class="LC_nobreak">'.
 4250:                               &mt("at least [_1] $units{$extra} after launch",
 4251:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4252:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4253:             }
 4254:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4255:             if ($imgsrc) {
 4256:                 $datatable .= $imgsrc.
 4257:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4258:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4259:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4260:             } else {
 4261:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4262:             }
 4263:             if ($switchserver) {
 4264:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4265:             } else {
 4266:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4267:             }
 4268:             $datatable .= '</span></fieldset>';
 4269:             my (%checkedfields,%rolemaps,$userincdom);
 4270:             if (ref($settings->{$item}) eq 'HASH') {
 4271:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4272:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4273:                 }
 4274:                 $userincdom = $settings->{$item}->{'incdom'};
 4275:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4276:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4277:                     $checkedfields{'roles'} = 1;
 4278:                 }
 4279:             }
 4280:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4281:                           '<span class="LC_nobreak">';
 4282:             my $userfieldstyle = 'display:none;';
 4283:             my $seluserdom = '';
 4284:             my $unseluserdom = ' selected="selected"';
 4285:             foreach my $field (@fields) {
 4286:                 my ($checked,$onclick,$id,$spacer);
 4287:                 if ($checkedfields{$field}) {
 4288:                     $checked = ' checked="checked"';
 4289:                 }
 4290:                 if ($field eq 'user') {
 4291:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4292:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4293:                     if ($checked) {
 4294:                         $userfieldstyle = 'display:inline-block';
 4295:                         if ($userincdom) {
 4296:                             $seluserdom = $unseluserdom;
 4297:                             $unseluserdom = '';
 4298:                         }
 4299:                     }
 4300:                 } else {
 4301:                     $spacer = ('&nbsp;' x2);
 4302:                 }
 4303:                 $datatable .= '<label>'.
 4304:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4305:                               $lt{$field}.'</label>'.$spacer;
 4306:             }
 4307:             $datatable .= '</span>';
 4308:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4309:                           '<span class="LC_nobreak"> : '.
 4310:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4311:                           '<option value="">'.&mt('Select').'</option>'.
 4312:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4313:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4314:                           '</select></span></div>';
 4315:             $datatable .= '</fieldset>'.
 4316:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4317:             foreach my $role (@courseroles) {
 4318:                 my ($selected,$selectnone);
 4319:                 if (!$rolemaps{$role}) {
 4320:                     $selectnone = ' selected="selected"';
 4321:                 }
 4322:                 $datatable .= '<td style="text-align: center">'. 
 4323:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4324:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4325:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4326:                 foreach my $ltirole (@ltiroles) {
 4327:                     unless ($selectnone) {
 4328:                         if ($rolemaps{$role} eq $ltirole) {
 4329:                             $selected = ' selected="selected"';
 4330:                         } else {
 4331:                             $selected = '';
 4332:                         }
 4333:                     }
 4334:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4335:                 }
 4336:                 $datatable .= '</select></td>';
 4337:             }
 4338:             $datatable .= '</tr></table></fieldset>';
 4339:             my %courseconfig;
 4340:             if (ref($settings->{$item}) eq 'HASH') {
 4341:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4342:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4343:                 }
 4344:             }
 4345:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4346:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4347:                 my $checked;
 4348:                 if ($courseconfig{$item}) {
 4349:                     $checked = ' checked="checked"';
 4350:                 }
 4351:                 $datatable .= '<label>'.
 4352:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4353:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4354:             }
 4355:             $datatable .= '</span></fieldset>'.
 4356:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4357:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4358:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4359:                 my %custom = %{$settings->{$item}->{'custom'}};
 4360:                 if (keys(%custom) > 0) {
 4361:                     foreach my $key (sort(keys(%custom))) {
 4362:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4363:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4364:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4365:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4366:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4367:                     }
 4368:                 }
 4369:             }
 4370:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4371:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4372:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4373:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4374:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4375:             $itemcount ++;
 4376:         }
 4377:     }
 4378:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4379:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4380:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4381:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4382:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4383:     for (my $k=0; $k<$maxnum+1; $k++) {
 4384:         my $vpos = $k+1;
 4385:         my $selstr;
 4386:         if ($k == $maxnum) {
 4387:             $selstr = ' selected="selected" ';
 4388:         }
 4389:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4390:     }
 4391:     $datatable .= '</select>&nbsp;'."\n".
 4392:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 4393:                   '<td colspan="2">'.
 4394:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4395:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4396:                   ('&nbsp;'x2).
 4397:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4398:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4399:                   ('&nbsp;'x2).
 4400:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4401:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4402:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4403:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4404:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4405:                   '<br />'.
 4406:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4407:                   ('&nbsp;'x2).
 4408:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4409:                   ('&nbsp;'x2).
 4410:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4411:                   ('&nbsp;'x2).
 4412:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4413:                   '<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".
 4414:                   '</fieldset>'.
 4415:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4416:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4417:     my %defaultdisp;
 4418:     $defaultdisp{'iframe'} = ' checked="checked"';
 4419:     foreach my $disp ('iframe','tab','window') {
 4420:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4421:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4422:     }
 4423:     $datatable .= ('&nbsp;'x4);
 4424:     foreach my $dimen ('width','height') {
 4425:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4426:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4427:                       ('&nbsp;'x2);
 4428:     }
 4429:     $datatable .= '<br />'.
 4430:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4431:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 4432:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4433:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4434:                   '</div><div style=""></div><br />';
 4435:     my %units = (
 4436:                   'passback' => 'days',
 4437:                   'roster'   => 'seconds',
 4438:                 );
 4439:     my %defaulttimes = (
 4440:                      'passback' => '7',
 4441:                      'roster'   => '300',
 4442:                    );
 4443:     foreach my $extra ('passback','roster') {
 4444:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4445:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4446:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4447:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4448:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4449:                       &mt('Yes').'</label></span></div>'.
 4450:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4451:                       '<span class="LC_nobreak">'.
 4452:                       &mt("at least [_1] $units{$extra} after launch",
 4453:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4454:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4455:     }
 4456:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4457:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4458:     if ($switchserver) {
 4459:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4460:     } else {
 4461:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4462:     }
 4463:     $datatable .= '</span></fieldset>'.
 4464:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4465:                   '<span class="LC_nobreak">';
 4466:     foreach my $field (@fields) {
 4467:         my ($id,$onclick,$spacer);
 4468:         if ($field eq 'user') {
 4469:             $id = ' id="ltitools_user_field_add"';
 4470:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4471:         } else {
 4472:             $spacer = ('&nbsp;' x2);
 4473:         }
 4474:         $datatable .= '<label>'.
 4475:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4476:                       $lt{$field}.'</label>'.$spacer;
 4477:     }
 4478:     $datatable .= '</span>'.
 4479:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4480:                   '<span class="LC_nobreak"> : '.
 4481:                   '<select name="ltitools_userincdom_add">'.
 4482:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4483:                   '<option value="0">'.&mt('username').'</option>'.
 4484:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4485:                   '</select></span></div></fieldset>';
 4486:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4487:     foreach my $role (@courseroles) {
 4488:         my ($checked,$checkednone);
 4489:         $datatable .= '<td style="text-align: center">'.
 4490:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4491:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4492:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4493:         foreach my $ltirole (@ltiroles) {
 4494:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4495:         }
 4496:         $datatable .= '</select></td>';
 4497:     }
 4498:     $datatable .= '</tr></table></fieldset>'.
 4499:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4500:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4501:         $datatable .= '<label>'.
 4502:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4503:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4504:     }
 4505:     $datatable .= '</span></fieldset>'.
 4506:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4507:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4508:                   '<tr><td><span class="LC_nobreak">'.
 4509:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4510:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4511:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4512:                   '</table></fieldset></td></tr>'."\n".
 4513:                   '</td>'."\n".
 4514:                   '</tr>'."\n";
 4515:     $itemcount ++;
 4516:     return $datatable;
 4517: }
 4518: 
 4519: sub ltitools_names {
 4520:     my %lt = &Apache::lonlocal::texthash(
 4521:                                           'title'          => 'Title',
 4522:                                           'version'        => 'Version',
 4523:                                           'msgtype'        => 'Message Type',
 4524:                                           'sigmethod'      => 'Signature Method',
 4525:                                           'url'            => 'URL',
 4526:                                           'key'            => 'Key',
 4527:                                           'lifetime'       => 'Nonce lifetime (s)',
 4528:                                           'secret'         => 'Secret',
 4529:                                           'icon'           => 'Icon',   
 4530:                                           'user'           => 'User',
 4531:                                           'fullname'       => 'Full Name',
 4532:                                           'firstname'      => 'First Name',
 4533:                                           'lastname'       => 'Last Name',
 4534:                                           'email'          => 'E-mail',
 4535:                                           'roles'          => 'Role',
 4536:                                           'window'         => 'Window',
 4537:                                           'tab'            => 'Tab',
 4538:                                           'iframe'         => 'iFrame',
 4539:                                           'height'         => 'Height',
 4540:                                           'width'          => 'Width',
 4541:                                           'linktext'       => 'Default Link Text',
 4542:                                           'explanation'    => 'Default Explanation',
 4543:                                           'passback'       => 'Tool can return grades:',
 4544:                                           'roster'         => 'Tool can retrieve roster:',
 4545:                                           'crstarget'      => 'Display target',
 4546:                                           'crslabel'       => 'Course label',
 4547:                                           'crstitle'       => 'Course title', 
 4548:                                           'crslinktext'    => 'Link Text',
 4549:                                           'crsexplanation' => 'Explanation',
 4550:                                           'crsappend'      => 'Provider URL',
 4551:                                         );
 4552:     return %lt;
 4553: }
 4554: 
 4555: sub print_lti {
 4556:     my ($dom,$settings,$rowtotal) = @_;
 4557:     my $itemcount = 1;
 4558:     my $maxnum = 0;
 4559:     my $css_class;
 4560:     my %ordered;
 4561:     if (ref($settings) eq 'HASH') {
 4562:         foreach my $item (keys(%{$settings})) {
 4563:             if (ref($settings->{$item}) eq 'HASH') {
 4564:                 my $num = $settings->{$item}{'order'};
 4565:                 $ordered{$num} = $item;
 4566:             }
 4567:         }
 4568:     }
 4569:     my $maxnum = scalar(keys(%ordered));
 4570:     my $datatable = &lti_javascript($settings);
 4571:     my %lt = &lti_names();
 4572:     if (keys(%ordered)) {
 4573:         my @items = sort { $a <=> $b } keys(%ordered);
 4574:         for (my $i=0; $i<@items; $i++) {
 4575:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4576:             my $item = $ordered{$items[$i]};
 4577:             my ($key,$secret,$lifetime,$consumer,$current);
 4578:             if (ref($settings->{$item}) eq 'HASH') {
 4579:                 $key = $settings->{$item}->{'key'};
 4580:                 $secret = $settings->{$item}->{'secret'};
 4581:                 $lifetime = $settings->{$item}->{'lifetime'};
 4582:                 $consumer = $settings->{$item}->{'consumer'};
 4583:                 $current = $settings->{$item};
 4584:             }
 4585:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 4586:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4587:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 4588:             for (my $k=0; $k<=$maxnum; $k++) {
 4589:                 my $vpos = $k+1;
 4590:                 my $selstr;
 4591:                 if ($k == $i) {
 4592:                     $selstr = ' selected="selected" ';
 4593:                 }
 4594:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4595:             }
 4596:             $datatable .= '</select>'.('&nbsp;'x2).
 4597:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 4598:                 &mt('Delete?').'</label></span></td>'.
 4599:                 '<td colspan="2">'.
 4600:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4601:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 4602:                 ':<input type="text" size="20" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 4603:                 ('&nbsp;'x2).
 4604:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 4605:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4606:                 ('&nbsp;'x2).
 4607:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 4608:                 'value="'.$lifetime.'" size="5" /></span>'.
 4609:                 '<br /><br />'.
 4610:                 '<span class="LC_nobreak">'.$lt{'key'}.
 4611:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 4612:                 ('&nbsp;'x2).
 4613:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4614:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 4615:                 '<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>'.
 4616:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 4617:                 '</fieldset>'.&lti_options($i,$current,%lt).'</td></tr>';
 4618:             $itemcount ++;
 4619:         }
 4620:     }
 4621:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4622:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 4623:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4624:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 4625:                   '<select name="lti_pos_add"'.$chgstr.'>';
 4626:     for (my $k=0; $k<$maxnum+1; $k++) {
 4627:         my $vpos = $k+1;
 4628:         my $selstr;
 4629:         if ($k == $maxnum) {
 4630:             $selstr = ' selected="selected" ';
 4631:         }
 4632:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4633:     }
 4634:     $datatable .= '</select>&nbsp;'."\n".
 4635:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</td>'."\n".
 4636:                   '<td colspan="2">'.
 4637:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4638:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 4639:                   ':<input type="text" size="20" name="lti_consumer_add" value="" /></span> '."\n".
 4640:                   ('&nbsp;'x2).
 4641:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 4642:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4643:                   ('&nbsp;'x2).
 4644:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="lti_lifetime_add" value="300" /></span> '."\n".
 4645:                   '<br /><br />'.
 4646:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 4647:                   ('&nbsp;'x2).
 4648:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 4649:                   '<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".
 4650:                   '</fieldset>'.&lti_options('add',undef,%lt).
 4651:                   '</td>'."\n".
 4652:                   '</tr>'."\n";
 4653:     $$rowtotal ++;
 4654:     return $datatable;;
 4655: }
 4656: 
 4657: sub lti_names {
 4658:     my %lt = &Apache::lonlocal::texthash(
 4659:                                           'version'   => 'LTI Version',
 4660:                                           'url'       => 'URL',
 4661:                                           'key'       => 'Key',
 4662:                                           'lifetime'  => 'Nonce lifetime (s)',
 4663:                                           'consumer'  => 'LTI Consumer', 
 4664:                                           'secret'    => 'Secret',
 4665:                                           'email'     => 'Email address',
 4666:                                           'sourcedid' => 'User ID',
 4667:                                           'other'     => 'Other',
 4668:                                           'passback'  => 'Can return grades to Consumer:',
 4669:                                           'roster'    => 'Can retrieve roster from Consumer:',
 4670:                                         );
 4671:     return %lt;
 4672: }
 4673: 
 4674: sub lti_options {
 4675:     my ($num,$current,%lt) = @_;
 4676:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield);
 4677:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 4678:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 4679:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 4680:     $checked{'mapcrstype'} = {};
 4681:     $checked{'makeuser'} = {};
 4682:     $checked{'selfenroll'} = {};
 4683:     $checked{'crssec'} = {};
 4684:     $checked{'crssecsrc'} = {};
 4685: 
 4686:     my $userfieldsty = 'none';
 4687:     my $crsfieldsty = 'none';
 4688:     my $crssecfieldsty = 'none';
 4689:     my $secsrcfieldsty = 'none';
 4690: 
 4691:     if (ref($current) eq 'HASH') {
 4692:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 4693:             $checked{'mapuser'}{'sourcedid'} = '';
 4694:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 4695:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 4696:             } else {
 4697:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 4698:                 $userfield = $current->{'mapuser'};
 4699:                 $userfieldsty = 'inline-block';
 4700:             }
 4701:         }
 4702:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 4703:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 4704:             if ($current->{'mapcrs'} eq 'context_id') {
 4705:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 4706:             } else {
 4707:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 4708:                 $cidfield = $current->{'mapcrs'};
 4709:                 $crsfieldsty = 'inline-block';
 4710:             }
 4711:         }
 4712:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 4713:             foreach my $type (@{$current->{'mapcrstype'}}) {
 4714:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 4715:             }
 4716:         }
 4717:         if ($current->{'makecrs'}) { 
 4718:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 4719:         } 
 4720:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 4721:             foreach my $role (@{$current->{'makeuser'}}) {
 4722:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 4723:             }
 4724:         }
 4725:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 4726:             foreach my $role (@{$current->{'selfenroll'}}) {
 4727:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 4728:             }
 4729:         }
 4730:         if (ref($current->{'maproles'}) eq 'HASH') {
 4731:             %rolemaps = %{$current->{'maproles'}};
 4732:         }
 4733:         if ($current->{'section'} ne '') {
 4734:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 4735:             $crssecfieldsty = 'inline-block';
 4736:             if ($current->{'section'} eq 'course_section_sourcedid') {
 4737:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 4738:             } else {
 4739:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 4740:                 $crssecsrc = $current->{'section'};
 4741:                 $secsrcfieldsty = 'inline-block';
 4742:             }
 4743:         } else {
 4744:             $checked{'crssec'}{'N'} = ' checked="checked"';
 4745:         }
 4746:     } else {
 4747:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 4748:         $checked{'crssec'}{'N'} = ' checked="checked"';
 4749:     }
 4750:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 4751:     my %coursetypetitles = &Apache::lonlocal::texthash (
 4752:                                official   => 'Official',
 4753:                                unofficial => 'Unofficial',
 4754:                                community  => 'Community',
 4755:                                textbook   => 'Textbook',
 4756:                                placement  => 'Placement Test',
 4757:     );
 4758:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 4759:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 4760:     my @courseroles = ('cc','in','ta','ep','st');
 4761:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 4762:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 4763:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 4764:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 4765:     my $output = '<fieldset><legend>'.&mt('Mapping users').'</legend>'.
 4766:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 4767:     foreach my $option ('sourcedid','email','other') {
 4768:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 4769:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 4770:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 4771:     }
 4772:     $output .= '</span></div>'.
 4773:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 4774:                '<input type="text" name="lti_customuser_'.$num.'" '.
 4775:                'value="'.$userfield.'" /></div></fieldset>'. 
 4776:                '<fieldset><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 4777:     foreach my $ltirole (@lticourseroles) {
 4778:         my ($selected,$selectnone);
 4779:         if ($rolemaps{$ltirole} eq '') {
 4780:             $selectnone = ' selected="selected"';
 4781:         }
 4782:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 4783:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 4784:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4785:         foreach my $role (@courseroles) {
 4786:             unless ($selectnone) {
 4787:                 if ($rolemaps{$ltirole} eq $role) {
 4788:                     $selected = ' selected="selected"';
 4789:                 } else {
 4790:                     $selected = '';
 4791:                 }
 4792:             }
 4793:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 4794:                        &Apache::lonnet::plaintext($role,'Course').
 4795:                        '</option>';
 4796:         }
 4797:         $output .= '</select></td>';
 4798:     }
 4799:     $output .= '</tr></table></fieldset>'.
 4800:                '<fieldset><legend>'.&mt('Roles which may create user accounts').'</legend>';
 4801:     foreach my $ltirole (@ltiroles) {
 4802:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 4803:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 4804:     }
 4805:     $output .= '</fieldset>'.
 4806:                '<fieldset><legend>'.&mt('Mapping courses').'</legend>'.
 4807:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 4808:                &mt('Unique course identifier').':&nbsp;';
 4809:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 4810:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 4811:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 4812:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 4813:     }
 4814:     $output .= '</div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'".>'.
 4815:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 4816:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 4817:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 4818:     foreach my $type (@coursetypes) {
 4819:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 4820:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 4821:                    ('&nbsp;'x2);
 4822:     }
 4823:     $output .= '</span></fieldset>'.
 4824:                '<fieldset><legend>'.&mt('Creating courses').'</legend>'.
 4825:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 4826:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 4827:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 4828:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 4829:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 4830:                '</fieldset>'.
 4831:                '<fieldset><legend>'.&mt('Roles which may self-enroll').'</legend>';
 4832:     foreach my $lticrsrole (@lticourseroles) {
 4833:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 4834:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 4835:     }
 4836:     $output .= '</fieldset>'.
 4837:                '<fieldset><legend>'.&mt('Course options').'</legend>'.
 4838:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 4839:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 4840:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 4841:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 4842:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label><span></div>'.
 4843:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 4844:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 4845:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 4846:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 4847:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 4848:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 4849:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 4850:                '</label></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 4851:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 4852:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'; 
 4853:     foreach my $extra ('passback','roster') {
 4854:         my $checkedon = '';
 4855:         my $checkedoff = ' checked="checked"';
 4856:         if (ref($current) eq 'HASH') {
 4857:             if (($current->{$extra})) {
 4858:                 $checkedon = $checkedoff;
 4859:                 $checkedoff = '';
 4860:             }
 4861:         }
 4862:         $output .= $lt{$extra}.'&nbsp;'.
 4863:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.' />'.
 4864:                    &mt('No').'</label>'.('&nbsp;'x2).
 4865:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.' />'.
 4866:                    &mt('Yes').'</label><br />';
 4867:     }
 4868:     $output .= '</span></fieldset>';
 4869: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 4870: #
 4871: #    $output .= '</fieldset>'.
 4872: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 4873:     return $output;
 4874: }
 4875: 
 4876: sub print_coursedefaults {
 4877:     my ($position,$dom,$settings,$rowtotal) = @_;
 4878:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4879:     my $itemcount = 1;
 4880:     my %choices =  &Apache::lonlocal::texthash (
 4881:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4882:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4883:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4884:         coursecredits        => 'Credits can be specified for courses',
 4885:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4886:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4887:         texengine            => 'Default method to display mathematics',
 4888:         postsubmit           => 'Disable submit button/keypress following student submission',
 4889:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4890:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4891:     );
 4892:     my %staticdefaults = (
 4893:                            texengine            => 'MathJax',
 4894:                            anonsurvey_threshold => 10,
 4895:                            uploadquota          => 500,
 4896:                            postsubmit           => 60,
 4897:                            mysqltables          => 172800,
 4898:                          );
 4899:     if ($position eq 'top') {
 4900:         %defaultchecked = (
 4901:                             'canuse_pdfforms' => 'off',
 4902:                             'uselcmath'       => 'on',
 4903:                             'usejsme'         => 'on',
 4904:                             'canclone'        => 'none',
 4905:                           );
 4906:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4907:         my $deftex = $staticdefaults{'texengine'};
 4908:         if (ref($settings) eq 'HASH') {
 4909:             if ($settings->{'texengine'}) {
 4910:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 4911:                     $deftex = $settings->{'texengine'};
 4912:                 }
 4913:             }
 4914:         }
 4915:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4916:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 4917:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 4918:                        '</span></td><td class="LC_right_item">'.
 4919:                        '<select name="texengine">'."\n";
 4920:         my %texoptions = (
 4921:                             MathJax  => 'MathJax',
 4922:                             mimetex  => &mt('Convert to Images'),
 4923:                             tth      => &mt('TeX to HTML'),
 4924:                          );
 4925:         foreach my $renderer ('MathJax','mimetex','tth') {
 4926:             my $selected = '';
 4927:             if ($renderer eq $deftex) {
 4928:                 $selected = ' selected="selected"';
 4929:             }
 4930:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 4931:         }
 4932:         $mathdisp .= '</select></td></tr>'."\n";
 4933:         $itemcount ++;
 4934:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4935:                                                      \%choices,$itemcount);
 4936:         $datatable = $mathdisp.$datatable;
 4937:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4938:         $datatable .=
 4939:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4940:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4941:             '</span></td><td class="LC_left_item">';
 4942:         my $currcanclone = 'none';
 4943:         my $onclick;
 4944:         my @cloneoptions = ('none','domain');
 4945:         my %clonetitles = (
 4946:                              none     => 'No additional course requesters',
 4947:                              domain   => "Any course requester in course's domain",
 4948:                              instcode => 'Course requests for official courses ...',
 4949:                           );
 4950:         my (%codedefaults,@code_order,@posscodes);
 4951:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4952:                                                     \@code_order) eq 'ok') {
 4953:             if (@code_order > 0) {
 4954:                 push(@cloneoptions,'instcode');
 4955:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4956:             }
 4957:         }
 4958:         if (ref($settings) eq 'HASH') {
 4959:             if ($settings->{'canclone'}) {
 4960:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4961:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4962:                         if (@code_order > 0) {
 4963:                             $currcanclone = 'instcode';
 4964:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4965:                         }
 4966:                     }
 4967:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4968:                     $currcanclone = $settings->{'canclone'};
 4969:                 }
 4970:             }
 4971:         }
 4972:         foreach my $option (@cloneoptions) {
 4973:             my ($checked,$additional);
 4974:             if ($currcanclone eq $option) {
 4975:                 $checked = ' checked="checked"';
 4976:             }
 4977:             if ($option eq 'instcode') {
 4978:                 if (@code_order) {
 4979:                     my $show = 'none';
 4980:                     if ($checked) {
 4981:                         $show = 'block';
 4982:                     }
 4983:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 4984:                                   &mt('Institutional codes for new and cloned course have identical:').
 4985:                                   '<br />';
 4986:                     foreach my $item (@code_order) {
 4987:                         my $codechk;
 4988:                         if ($checked) {
 4989:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4990:                                 $codechk = ' checked="checked"';
 4991:                             }
 4992:                         }
 4993:                         $additional .= '<label>'.
 4994:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4995:                                        $item.'</label>';
 4996:                     }
 4997:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4998:                 }
 4999:             }
 5000:             $datatable .=
 5001:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 5002:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 5003:                 '</label>&nbsp;'.$additional.'</span><br />';
 5004:         }
 5005:         $datatable .= '</td>'.
 5006:                       '</tr>';
 5007:         $itemcount ++;
 5008:     } else {
 5009:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5010:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 5011:         my $currusecredits = 0;
 5012:         my $postsubmitclient = 1;
 5013:         my @types = ('official','unofficial','community','textbook','placement');
 5014:         if (ref($settings) eq 'HASH') {
 5015:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 5016:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 5017:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 5018:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 5019:                 }
 5020:             }
 5021:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 5022:                 foreach my $type (@types) {
 5023:                     next if ($type eq 'community');
 5024:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 5025:                     if ($defcredits{$type} ne '') {
 5026:                         $currusecredits = 1;
 5027:                     }
 5028:                 }
 5029:             }
 5030:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 5031:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 5032:                     $postsubmitclient = 0;
 5033:                     foreach my $type (@types) {
 5034:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5035:                     }
 5036:                 } else {
 5037:                     foreach my $type (@types) {
 5038:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 5039:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 5040:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 5041:                             } else {
 5042:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5043:                             }
 5044:                         } else {
 5045:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5046:                         }
 5047:                     }
 5048:                 }
 5049:             } else {
 5050:                 foreach my $type (@types) {
 5051:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5052:                 }
 5053:             }
 5054:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 5055:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 5056:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 5057:                 }
 5058:             } else {
 5059:                 foreach my $type (@types) {
 5060:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 5061:                 }
 5062:             }
 5063:         } else {
 5064:             foreach my $type (@types) {
 5065:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5066:             }
 5067:         }
 5068:         if (!$currdefresponder) {
 5069:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 5070:         } elsif ($currdefresponder < 1) {
 5071:             $currdefresponder = 1;
 5072:         }
 5073:         foreach my $type (@types) {
 5074:             if ($curruploadquota{$type} eq '') {
 5075:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 5076:             }
 5077:         }
 5078:         $datatable .=
 5079:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5080:                 $choices{'anonsurvey_threshold'}.
 5081:                 '</span></td>'.
 5082:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 5083:                 '<input type="text" name="anonsurvey_threshold"'.
 5084:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 5085:                 '</td></tr>'."\n";
 5086:         $itemcount ++;
 5087:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5088:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5089:                       $choices{'uploadquota'}.
 5090:                       '</span></td>'.
 5091:                       '<td style="text-align: right" class="LC_right_item">'.
 5092:                       '<table><tr>';
 5093:         foreach my $type (@types) {
 5094:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5095:                            '<input type="text" name="uploadquota_'.$type.'"'.
 5096:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 5097:         }
 5098:         $datatable .= '</tr></table></td></tr>'."\n";
 5099:         $itemcount ++;
 5100:         my $onclick = "toggleDisplay(this.form,'credits');";
 5101:         my $display = 'none';
 5102:         if ($currusecredits) {
 5103:             $display = 'block';
 5104:         }
 5105:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 5106:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 5107:         foreach my $type (@types) {
 5108:             next if ($type eq 'community');
 5109:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5110:                            '<input type="text" name="'.$type.'_credits"'.
 5111:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 5112:         }
 5113:         $additional .= '</tr></table></div>'."\n";
 5114:         %defaultchecked = ('coursecredits' => 'off');
 5115:         @toggles = ('coursecredits');
 5116:         my $current = {
 5117:                         'coursecredits' => $currusecredits,
 5118:                       };
 5119:         (my $table,$itemcount) =
 5120:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5121:                                \%choices,$itemcount,$onclick,$additional,'left');
 5122:         $datatable .= $table;
 5123:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 5124:         my $display = 'none';
 5125:         if ($postsubmitclient) {
 5126:             $display = 'block';
 5127:         }
 5128:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 5129:                       &mt('Number of seconds submit is disabled').'<br />'.
 5130:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 5131:                       '<table><tr>';
 5132:         foreach my $type (@types) {
 5133:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5134:                            '<input type="text" name="'.$type.'_timeout" value="'.
 5135:                            $deftimeout{$type}.'" size="5" /></td>';
 5136:         }
 5137:         $additional .= '</tr></table></div>'."\n";
 5138:         %defaultchecked = ('postsubmit' => 'on');
 5139:         @toggles = ('postsubmit');
 5140:         $current = {
 5141:                        'postsubmit' => $postsubmitclient,
 5142:                    };
 5143:         ($table,$itemcount) =
 5144:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5145:                                \%choices,$itemcount,$onclick,$additional,'left');
 5146:         $datatable .= $table;
 5147:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5148:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5149:                       $choices{'mysqltables'}.
 5150:                       '</span></td>'.
 5151:                       '<td style="text-align: right" class="LC_right_item">'.
 5152:                       '<table><tr>';
 5153:         foreach my $type (@types) {
 5154:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5155:                            '<input type="text" name="mysqltables_'.$type.'"'.
 5156:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 5157:         }
 5158:         $datatable .= '</tr></table></td></tr>'."\n";
 5159:         $itemcount ++;
 5160: 
 5161:     }
 5162:     $$rowtotal += $itemcount;
 5163:     return $datatable;
 5164: }
 5165: 
 5166: sub print_selfenrollment {
 5167:     my ($position,$dom,$settings,$rowtotal) = @_;
 5168:     my ($css_class,$datatable);
 5169:     my $itemcount = 1;
 5170:     my @types = ('official','unofficial','community','textbook','placement');
 5171:     if (($position eq 'top') || ($position eq 'middle')) {
 5172:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 5173:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 5174:         my @rows;
 5175:         my $key;
 5176:         if ($position eq 'top') {
 5177:             $key = 'admin'; 
 5178:             if (ref($rowsref) eq 'ARRAY') {
 5179:                 @rows = @{$rowsref};
 5180:             }
 5181:         } elsif ($position eq 'middle') {
 5182:             $key = 'default';
 5183:             @rows = ('types','registered','approval','limit');
 5184:         }
 5185:         foreach my $row (@rows) {
 5186:             if (defined($titlesref->{$row})) {
 5187:                 $itemcount ++;
 5188:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5189:                 $datatable .= '<tr'.$css_class.'>'.
 5190:                               '<td>'.$titlesref->{$row}.'</td>'.
 5191:                               '<td class="LC_left_item">'.
 5192:                               '<table><tr>';
 5193:                 my (%current,%currentcap);
 5194:                 if (ref($settings) eq 'HASH') {
 5195:                     if (ref($settings->{$key}) eq 'HASH') {
 5196:                         foreach my $type (@types) {
 5197:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5198:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 5199:                             }
 5200:                             if (($row eq 'limit') && ($key eq 'default')) {
 5201:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5202:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 5203:                                 }
 5204:                             }
 5205:                         }
 5206:                     }
 5207:                 }
 5208:                 my %roles = (
 5209:                              '0' => &Apache::lonnet::plaintext('dc'),
 5210:                             ); 
 5211:             
 5212:                 foreach my $type (@types) {
 5213:                     unless (($row eq 'registered') && ($key eq 'default')) {
 5214:                         $datatable .= '<th>'.&mt($type).'</th>';
 5215:                     }
 5216:                 }
 5217:                 unless (($row eq 'registered') && ($key eq 'default')) {
 5218:                     $datatable .= '</tr><tr>';
 5219:                 }
 5220:                 foreach my $type (@types) {
 5221:                     if ($type eq 'community') {
 5222:                         $roles{'1'} = &mt('Community personnel');
 5223:                     } else {
 5224:                         $roles{'1'} = &mt('Course personnel');
 5225:                     }
 5226:                     $datatable .= '<td style="vertical-align: top">';
 5227:                     if ($position eq 'top') {
 5228:                         my %checked;
 5229:                         if ($current{$type} eq '0') {
 5230:                             $checked{'0'} = ' checked="checked"';
 5231:                         } else {
 5232:                             $checked{'1'} = ' checked="checked"';
 5233:                         }
 5234:                         foreach my $role ('1','0') {
 5235:                             $datatable .= '<span class="LC_nobreak"><label>'.
 5236:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 5237:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 5238:                                           $roles{$role}.'</label></span> ';
 5239:                         }
 5240:                     } else {
 5241:                         if ($row eq 'types') {
 5242:                             my %checked;
 5243:                             if ($current{$type} =~ /^(all|dom)$/) {
 5244:                                 $checked{$1} = ' checked="checked"';
 5245:                             } else {
 5246:                                 $checked{''} = ' checked="checked"';
 5247:                             }
 5248:                             foreach my $val ('','dom','all') {
 5249:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5250:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5251:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5252:                             }
 5253:                         } elsif ($row eq 'registered') {
 5254:                             my %checked;
 5255:                             if ($current{$type} eq '1') {
 5256:                                 $checked{'1'} = ' checked="checked"';
 5257:                             } else {
 5258:                                 $checked{'0'} = ' checked="checked"';
 5259:                             }
 5260:                             foreach my $val ('0','1') {
 5261:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5262:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5263:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5264:                             }
 5265:                         } elsif ($row eq 'approval') {
 5266:                             my %checked;
 5267:                             if ($current{$type} =~ /^([12])$/) {
 5268:                                 $checked{$1} = ' checked="checked"';
 5269:                             } else {
 5270:                                 $checked{'0'} = ' checked="checked"';
 5271:                             }
 5272:                             for my $val (0..2) {
 5273:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5274:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5275:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5276:                             }
 5277:                         } elsif ($row eq 'limit') {
 5278:                             my %checked;
 5279:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 5280:                                 $checked{$1} = ' checked="checked"';
 5281:                             } else {
 5282:                                 $checked{'none'} = ' checked="checked"';
 5283:                             }
 5284:                             my $cap;
 5285:                             if ($currentcap{$type} =~ /^\d+$/) {
 5286:                                 $cap = $currentcap{$type};
 5287:                             }
 5288:                             foreach my $val ('none','allstudents','selfenrolled') {
 5289:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5290:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5291:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5292:                             }
 5293:                             $datatable .= '<br />'.
 5294:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 5295:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 5296:                                           '</span>'; 
 5297:                         }
 5298:                     }
 5299:                     $datatable .= '</td>';
 5300:                 }
 5301:                 $datatable .= '</tr>';
 5302:             }
 5303:             $datatable .= '</table></td></tr>';
 5304:         }
 5305:     } elsif ($position eq 'bottom') {
 5306:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 5307:     }
 5308:     $$rowtotal += $itemcount;
 5309:     return $datatable;
 5310: }
 5311: 
 5312: sub print_validation_rows {
 5313:     my ($caller,$dom,$settings,$rowtotal) = @_;
 5314:     my ($itemsref,$namesref,$fieldsref);
 5315:     if ($caller eq 'selfenroll') { 
 5316:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 5317:     } elsif ($caller eq 'requestcourses') {
 5318:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 5319:     }
 5320:     my %currvalidation;
 5321:     if (ref($settings) eq 'HASH') {
 5322:         if (ref($settings->{'validation'}) eq 'HASH') {
 5323:             %currvalidation = %{$settings->{'validation'}};
 5324:         }
 5325:     }
 5326:     my $datatable;
 5327:     my $itemcount = 0;
 5328:     foreach my $item (@{$itemsref}) {
 5329:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5330:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5331:                       $namesref->{$item}.
 5332:                       '</span></td>'.
 5333:                       '<td class="LC_left_item">';
 5334:         if (($item eq 'url') || ($item eq 'button')) {
 5335:             $datatable .= '<span class="LC_nobreak">'.
 5336:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 5337:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 5338:         } elsif ($item eq 'fields') {
 5339:             my @currfields;
 5340:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 5341:                 @currfields = @{$currvalidation{$item}};
 5342:             }
 5343:             foreach my $field (@{$fieldsref}) {
 5344:                 my $check = '';
 5345:                 if (grep(/^\Q$field\E$/,@currfields)) {
 5346:                     $check = ' checked="checked"';
 5347:                 }
 5348:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5349:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 5350:                               ' value="'.$field.'"'.$check.' />'.$field.
 5351:                               '</label></span> ';
 5352:             }
 5353:         } elsif ($item eq 'markup') {
 5354:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 5355:                            $currvalidation{$item}.
 5356:                               '</textarea>';
 5357:         }
 5358:         $datatable .= '</td></tr>'."\n";
 5359:         if (ref($rowtotal)) {
 5360:             $itemcount ++;
 5361:         }
 5362:     }
 5363:     if ($caller eq 'requestcourses') {
 5364:         my %currhash;
 5365:         if (ref($settings) eq 'HASH') {
 5366:             if (ref($settings->{'validation'}) eq 'HASH') {
 5367:                 if ($settings->{'validation'}{'dc'} ne '') {
 5368:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 5369:                 }
 5370:             }
 5371:         }
 5372:         my $numinrow = 2;
 5373:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 5374:                                                        'validationdc',%currhash);
 5375:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5376:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 5377:         if ($numdc > 1) {
 5378:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 5379:         } else {
 5380:             $datatable .=  &mt('Course creation processed as: ');
 5381:         }
 5382:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 5383:         $itemcount ++;
 5384:     }
 5385:     if (ref($rowtotal)) {
 5386:         $$rowtotal += $itemcount;
 5387:     }
 5388:     return $datatable;
 5389: }
 5390: 
 5391: sub print_usersessions {
 5392:     my ($position,$dom,$settings,$rowtotal) = @_;
 5393:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 5394:     my (%by_ip,%by_location,@intdoms,@instdoms);
 5395:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5396: 
 5397:     my @alldoms = &Apache::lonnet::all_domains();
 5398:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5399:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5400:     my %altids = &id_for_thisdom(%servers);
 5401:     if ($position eq 'top') {
 5402:         if (keys(%serverhomes) > 1) {
 5403:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 5404:             my $curroffloadnow;
 5405:             if (ref($settings) eq 'HASH') {
 5406:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 5407:                     $curroffloadnow = $settings->{'offloadnow'};
 5408:                 }
 5409:             }
 5410:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 5411:         } else {
 5412:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5413:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 5414:                           '</td></tr>';
 5415:         }
 5416:     } else {
 5417:         my %titles = &usersession_titles();
 5418:         my ($prefix,@types);
 5419:         if ($position eq 'bottom') {
 5420:             $prefix = 'remote';
 5421:             @types = ('version','excludedomain','includedomain');
 5422:         } else {
 5423:             $prefix = 'hosted';
 5424:             @types = ('excludedomain','includedomain');
 5425:         }
 5426:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 5427:     }
 5428:     $$rowtotal += $itemcount;
 5429:     return $datatable;
 5430: }
 5431: 
 5432: sub rules_by_location {
 5433:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 5434:     my ($datatable,$itemcount,$css_class);
 5435:     if (keys(%{$by_location}) == 0) {
 5436:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5437:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 5438:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 5439:                      '</td></tr>';
 5440:         $itemcount = 1;
 5441:     } else {
 5442:         $itemcount = 0;
 5443:         my $numinrow = 5;
 5444:         my (%current,%checkedon,%checkedoff);
 5445:         my @locations = sort(keys(%{$by_location}));
 5446:         foreach my $type (@{$types}) {
 5447:             $checkedon{$type} = '';
 5448:             $checkedoff{$type} = ' checked="checked"';
 5449:         }
 5450:         if (ref($settings) eq 'HASH') {
 5451:             if (ref($settings->{$prefix}) eq 'HASH') {
 5452:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 5453:                     $current{$key} = $settings->{$prefix}{$key};
 5454:                     if ($key eq 'version') {
 5455:                         if ($current{$key} ne '') {
 5456:                             $checkedon{$key} = ' checked="checked"';
 5457:                             $checkedoff{$key} = '';
 5458:                         }
 5459:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 5460:                         $checkedon{$key} = ' checked="checked"';
 5461:                         $checkedoff{$key} = '';
 5462:                     }
 5463:                 }
 5464:             }
 5465:         }
 5466:         foreach my $type (@{$types}) {
 5467:             next if ($type ne 'version' && !@locations);
 5468:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5469:             $datatable .= '<tr'.$css_class.'>
 5470:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 5471:                            <span class="LC_nobreak">&nbsp;
 5472:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 5473:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 5474:             if ($type eq 'version') {
 5475:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 5476:                 my $selector = '<select name="'.$prefix.'_version">';
 5477:                 foreach my $version (@lcversions) {
 5478:                     my $selected = '';
 5479:                     if ($current{'version'} eq $version) {
 5480:                         $selected = ' selected="selected"';
 5481:                     }
 5482:                     $selector .= ' <option value="'.$version.'"'.
 5483:                                  $selected.'>'.$version.'</option>';
 5484:                 }
 5485:                 $selector .= '</select> ';
 5486:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 5487:             } else {
 5488:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 5489:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 5490:                              ' />'.('&nbsp;'x2).
 5491:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 5492:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 5493:                              "\n".
 5494:                              '</div><div><table>';
 5495:                 my $rem;
 5496:                 for (my $i=0; $i<@locations; $i++) {
 5497:                     my ($showloc,$value,$checkedtype);
 5498:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 5499:                         my $ip = $by_location->{$locations[$i]}->[0];
 5500:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5501:                             $value = join(':',@{$by_ip->{$ip}});
 5502:                             $showloc = join(', ',@{$by_ip->{$ip}});
 5503:                             if (ref($current{$type}) eq 'ARRAY') {
 5504:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 5505:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 5506:                                         $checkedtype = ' checked="checked"';
 5507:                                         last;
 5508:                                     }
 5509:                                 }
 5510:                             }
 5511:                         }
 5512:                     }
 5513:                     $rem = $i%($numinrow);
 5514:                     if ($rem == 0) {
 5515:                         if ($i > 0) {
 5516:                             $datatable .= '</tr>';
 5517:                         }
 5518:                         $datatable .= '<tr>';
 5519:                     }
 5520:                     $datatable .= '<td class="LC_left_item">'.
 5521:                                   '<span class="LC_nobreak"><label>'.
 5522:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 5523:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 5524:                                   '</label></span></td>';
 5525:                 }
 5526:                 $rem = @locations%($numinrow);
 5527:                 my $colsleft = $numinrow - $rem;
 5528:                 if ($colsleft > 1 ) {
 5529:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5530:                                   '&nbsp;</td>';
 5531:                 } elsif ($colsleft == 1) {
 5532:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5533:                 }
 5534:                 $datatable .= '</tr></table>';
 5535:             }
 5536:             $datatable .= '</td></tr>';
 5537:             $itemcount ++;
 5538:         }
 5539:     }
 5540:     return ($datatable,$itemcount);
 5541: }
 5542: 
 5543: sub print_ssl {
 5544:     my ($position,$dom,$settings,$rowtotal) = @_;
 5545:     my ($css_class,$datatable);
 5546:     my $itemcount = 1;
 5547:     if ($position eq 'top') {
 5548:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5549:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5550:         my $same_institution;
 5551:         if ($intdom ne '') {
 5552:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 5553:             if (ref($internet_names) eq 'ARRAY') {
 5554:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 5555:                     $same_institution = 1;
 5556:                 }
 5557:             }
 5558:         }
 5559:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5560:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 5561:         if ($same_institution) {
 5562:             my %domservers = &Apache::lonnet::get_servers($dom);
 5563:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 5564:         } else {
 5565:             $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.");
 5566:         }
 5567:         $datatable .= '</td></tr>';
 5568:         $itemcount ++;
 5569:     } else {
 5570:         my %titles = &ssl_titles();
 5571:         my (%by_ip,%by_location,@intdoms,@instdoms);
 5572:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5573:         my @alldoms = &Apache::lonnet::all_domains();
 5574:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5575:         my @domservers = &Apache::lonnet::get_servers($dom);
 5576:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5577:         my %altids = &id_for_thisdom(%servers);
 5578:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 5579:             my $legacy;
 5580:             unless (ref($settings) eq 'HASH') {
 5581:                 my $name;
 5582:                 if ($position eq 'connto') {
 5583:                     $name = 'loncAllowInsecure';
 5584:                 } else {
 5585:                     $name = 'londAllowInsecure';
 5586:                 }
 5587:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 5588:                 my @ids=&Apache::lonnet::current_machine_ids();
 5589:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 5590:                     my %what = (
 5591:                                    $name => 1,
 5592:                                );
 5593:                     my ($result,$returnhash) =
 5594:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 5595:                     if ($result eq 'ok') {
 5596:                         if (ref($returnhash) eq 'HASH') {
 5597:                             $legacy = $returnhash->{$name};
 5598:                         }
 5599:                     }
 5600:                 } else {
 5601:                     $legacy = $Apache::lonnet::perlvar{$name};
 5602:                 }
 5603:             }
 5604:             foreach my $type ('dom','intdom','other') {
 5605:                 my %checked;
 5606:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5607:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 5608:                               '<td class="LC_right_item">';
 5609:                 my $skip; 
 5610:                 if ($type eq 'dom') {
 5611:                     unless (keys(%servers) > 1) {
 5612:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 5613:                         $skip = 1;
 5614:                     }
 5615:                 }
 5616:                 if ($type eq 'intdom') {
 5617:                     unless (@instdoms > 1) {
 5618:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 5619:                         $skip = 1;
 5620:                     } 
 5621:                 } elsif ($type eq 'other') {
 5622:                     if (keys(%by_location) == 0) {
 5623:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 5624:                         $skip = 1;
 5625:                     }
 5626:                 }
 5627:                 unless ($skip) {
 5628:                     $checked{'yes'} = ' checked="checked"'; 
 5629:                     if (ref($settings) eq 'HASH') {
 5630:                         if (ref($settings->{$position}) eq 'HASH') {
 5631:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 5632:                                 $checked{$1} = $checked{'yes'};
 5633:                                 delete($checked{'yes'}); 
 5634:                             }
 5635:                         }
 5636:                     } else {
 5637:                         if ($legacy == 0) {
 5638:                             $checked{'req'} = $checked{'yes'};
 5639:                             delete($checked{'yes'});    
 5640:                         }
 5641:                     }
 5642:                     foreach my $option ('no','yes','req') {
 5643:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5644:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 5645:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 5646:                                       '</label></span>'.('&nbsp;'x2);
 5647:                     }
 5648:                 }
 5649:                 $datatable .= '</td></tr>';
 5650:                 $itemcount ++; 
 5651:             }
 5652:         } else {
 5653:             my $prefix = 'replication';
 5654:             my @types = ('certreq','nocertreq');
 5655:             if (keys(%by_location) == 0) {
 5656:                 $datatable .= '<tr'.$css_class.'><td>'.
 5657:                               &mt('Nothing to set here, as there are no other institutions').
 5658:                               '</td></tr>';
 5659:                 $itemcount ++;
 5660:             } else {
 5661:                 ($datatable,$itemcount) = 
 5662:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 5663:             }
 5664:         }
 5665:     }
 5666:     $$rowtotal += $itemcount;
 5667:     return $datatable;
 5668: }
 5669: 
 5670: sub ssl_titles {
 5671:     return &Apache::lonlocal::texthash (
 5672:                dom           => 'LON-CAPA servers/VMs from same domain',
 5673:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 5674:                other         => 'External LON-CAPA servers/VMs',
 5675:                connto        => 'Connections to other servers',
 5676:                connfrom      => 'Connections from other servers',
 5677:                replication   => 'Replicating content to other institutions',
 5678:                certreq       => 'Client certificate required, but specific domains exempt',
 5679:                nocertreq     => 'No client certificate required, except for specific domains',
 5680:                no            => 'SSL not used',
 5681:                yes           => 'SSL Optional (used if available)',
 5682:                req           => 'SSL Required',
 5683:     );
 5684: }
 5685: 
 5686: sub print_trust {
 5687:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 5688:     my ($css_class,$datatable,%checked,%choices);
 5689:     my (%by_ip,%by_location,@intdoms,@instdoms);
 5690:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5691:     my $itemcount = 1;
 5692:     my %titles = &trust_titles();
 5693:     my @types = ('exc','inc');
 5694:     if ($prefix eq 'top') {
 5695:         $prefix = 'content';
 5696:     } elsif ($prefix eq 'bottom') {
 5697:         $prefix = 'msg';
 5698:     }
 5699:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 5700:     $$rowtotal += $itemcount;
 5701:     return $datatable;
 5702: }
 5703: 
 5704: sub trust_titles {
 5705:     return &Apache::lonlocal::texthash(
 5706:                content  => "Access to this domain's content by others",
 5707:                shared   => "Access to other domain's content by this domain",
 5708:                enroll   => "Enrollment in this domain's courses by others", 
 5709:                othcoau  => "Co-author roles in this domain for others",
 5710:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 5711:                domroles => "Domain roles in this domain assignable to others",
 5712:                catalog  => "Course Catalog for this domain displayed elsewhere",
 5713:                reqcrs   => "Requests for creation of courses in this domain by others",
 5714:                msg      => "Users in other domains can send messages to this domain",
 5715:                exc      => "Allow all, but exclude specific domains",
 5716:                inc      => "Deny all, but include specific domains",
 5717:            );
 5718: } 
 5719: 
 5720: sub build_location_hashes {
 5721:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 5722:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 5723:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 5724:     my %iphost = &Apache::lonnet::get_iphost();
 5725:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 5726:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 5727:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 5728:         foreach my $id (@{$iphost{$primary_ip}}) {
 5729:             my $intdom = &Apache::lonnet::internet_dom($id);
 5730:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 5731:                 push(@{$intdoms},$intdom);
 5732:             }
 5733:         }
 5734:     }
 5735:     foreach my $ip (keys(%iphost)) {
 5736:         if (ref($iphost{$ip}) eq 'ARRAY') {
 5737:             foreach my $id (@{$iphost{$ip}}) {
 5738:                 my $location = &Apache::lonnet::internet_dom($id);
 5739:                 if ($location) {
 5740:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 5741:                         my $dom = &Apache::lonnet::host_domain($id);
 5742:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 5743:                             push(@{$instdoms},$dom);
 5744:                         }
 5745:                         next;
 5746:                     }
 5747:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5748:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 5749:                             push(@{$by_ip->{$ip}},$location);
 5750:                         }
 5751:                     } else {
 5752:                         $by_ip->{$ip} = [$location];
 5753:                     }
 5754:                 }
 5755:             }
 5756:         }
 5757:     }
 5758:     foreach my $ip (sort(keys(%{$by_ip}))) {
 5759:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5760:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 5761:             my $first = $by_ip->{$ip}->[0];
 5762:             if (ref($by_location->{$first}) eq 'ARRAY') {
 5763:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 5764:                     push(@{$by_location->{$first}},$ip);
 5765:                 }
 5766:             } else {
 5767:                 $by_location->{$first} = [$ip];
 5768:             }
 5769:         }
 5770:     }
 5771:     return;
 5772: }
 5773: 
 5774: sub current_offloads_to {
 5775:     my ($dom,$settings,$servers) = @_;
 5776:     my (%spareid,%otherdomconfigs);
 5777:     if (ref($servers) eq 'HASH') {
 5778:         foreach my $lonhost (sort(keys(%{$servers}))) {
 5779:             my $gotspares;
 5780:             if (ref($settings) eq 'HASH') {
 5781:                 if (ref($settings->{'spares'}) eq 'HASH') {
 5782:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 5783:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 5784:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 5785:                         $gotspares = 1;
 5786:                     }
 5787:                 }
 5788:             }
 5789:             unless ($gotspares) {
 5790:                 my $gotspares;
 5791:                 my $serverhomeID =
 5792:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 5793:                 my $serverhomedom =
 5794:                     &Apache::lonnet::host_domain($serverhomeID);
 5795:                 if ($serverhomedom ne $dom) {
 5796:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 5797:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5798:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5799:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5800:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5801:                                 $gotspares = 1;
 5802:                             }
 5803:                         }
 5804:                     } else {
 5805:                         $otherdomconfigs{$serverhomedom} =
 5806:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 5807:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 5808:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5809:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5810:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 5811:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5812:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5813:                                         $gotspares = 1;
 5814:                                     }
 5815:                                 }
 5816:                             }
 5817:                         }
 5818:                     }
 5819:                 }
 5820:             }
 5821:             unless ($gotspares) {
 5822:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5823:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5824:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5825:                } else {
 5826:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 5827:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 5828:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5829:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5830:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5831:                     } else {
 5832:                         my %what = (
 5833:                              spareid => 1,
 5834:                         );
 5835:                         my ($result,$returnhash) = 
 5836:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 5837:                         if ($result eq 'ok') { 
 5838:                             if (ref($returnhash) eq 'HASH') {
 5839:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 5840:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 5841:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 5842:                                 }
 5843:                             }
 5844:                         }
 5845:                     }
 5846:                 }
 5847:             }
 5848:         }
 5849:     }
 5850:     return %spareid;
 5851: }
 5852: 
 5853: sub spares_row {
 5854:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 5855:     my $css_class;
 5856:     my $numinrow = 4;
 5857:     my $itemcount = 1;
 5858:     my $datatable;
 5859:     my %typetitles = &sparestype_titles();
 5860:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5861:         foreach my $server (sort(keys(%{$servers}))) {
 5862:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5863:             my ($othercontrol,$serverdom);
 5864:             if ($serverhome ne $server) {
 5865:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5866:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5867:             } else {
 5868:                 $serverdom = &Apache::lonnet::host_domain($server);
 5869:                 if ($serverdom ne $dom) {
 5870:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5871:                 }
 5872:             }
 5873:             next unless (ref($spareid->{$server}) eq 'HASH');
 5874:             my $checkednow;
 5875:             if (ref($curroffloadnow) eq 'HASH') {
 5876:                 if ($curroffloadnow->{$server}) {
 5877:                     $checkednow = ' checked="checked"';
 5878:                 }
 5879:             }
 5880:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5881:             $datatable .= '<tr'.$css_class.'>
 5882:                            <td rowspan="2">
 5883:                             <span class="LC_nobreak">'.
 5884:                           &mt('[_1] when busy, offloads to:'
 5885:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5886:                           '<span class="LC_nobreak">'."\n".
 5887:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5888:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5889:                           "\n";
 5890:             my (%current,%canselect);
 5891:             my @choices = 
 5892:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5893:             foreach my $type ('primary','default') {
 5894:                 if (ref($spareid->{$server}) eq 'HASH') {
 5895:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5896:                         my @spares = @{$spareid->{$server}{$type}};
 5897:                         if (@spares > 0) {
 5898:                             if ($othercontrol) {
 5899:                                 $current{$type} = join(', ',@spares);
 5900:                             } else {
 5901:                                 $current{$type} .= '<table>';
 5902:                                 my $numspares = scalar(@spares);
 5903:                                 for (my $i=0;  $i<@spares; $i++) {
 5904:                                     my $rem = $i%($numinrow);
 5905:                                     if ($rem == 0) {
 5906:                                         if ($i > 0) {
 5907:                                             $current{$type} .= '</tr>';
 5908:                                         }
 5909:                                         $current{$type} .= '<tr>';
 5910:                                     }
 5911:                                     $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;'.
 5912:                                                        $spareid->{$server}{$type}[$i].
 5913:                                                        '</label></td>'."\n";
 5914:                                 }
 5915:                                 my $rem = @spares%($numinrow);
 5916:                                 my $colsleft = $numinrow - $rem;
 5917:                                 if ($colsleft > 1 ) {
 5918:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5919:                                                        '" class="LC_left_item">'.
 5920:                                                        '&nbsp;</td>';
 5921:                                 } elsif ($colsleft == 1) {
 5922:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5923:                                 }
 5924:                                 $current{$type} .= '</tr></table>';
 5925:                             }
 5926:                         }
 5927:                     }
 5928:                     if ($current{$type} eq '') {
 5929:                         $current{$type} = &mt('None specified');
 5930:                     }
 5931:                     if ($othercontrol) {
 5932:                         if ($type eq 'primary') {
 5933:                             $canselect{$type} = $othercontrol;
 5934:                         }
 5935:                     } else {
 5936:                         $canselect{$type} = 
 5937:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5938:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5939:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5940:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5941:                         if (@choices > 0) {
 5942:                             foreach my $lonhost (@choices) {
 5943:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5944:                             }
 5945:                         }
 5946:                         $canselect{$type} .= '</select>'."\n";
 5947:                     }
 5948:                 } else {
 5949:                     $current{$type} = &mt('Could not be determined');
 5950:                     if ($type eq 'primary') {
 5951:                         $canselect{$type} =  $othercontrol;
 5952:                     }
 5953:                 }
 5954:                 if ($type eq 'default') {
 5955:                     $datatable .= '<tr'.$css_class.'>';
 5956:                 }
 5957:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5958:                               '<td>'.$current{$type}.'</td>'."\n".
 5959:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5960:             }
 5961:             $itemcount ++;
 5962:         }
 5963:     }
 5964:     $$rowtotal += $itemcount;
 5965:     return $datatable;
 5966: }
 5967: 
 5968: sub possible_newspares {
 5969:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5970:     my $serverhostname = &Apache::lonnet::hostname($server);
 5971:     my %excluded;
 5972:     if ($serverhostname ne '') {
 5973:         %excluded = (
 5974:                        $serverhostname => 1,
 5975:                     );
 5976:     }
 5977:     if (ref($currspares) eq 'HASH') {
 5978:         foreach my $type (keys(%{$currspares})) {
 5979:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5980:                 if (@{$currspares->{$type}} > 0) {
 5981:                     foreach my $curr (@{$currspares->{$type}}) {
 5982:                         my $hostname = &Apache::lonnet::hostname($curr);
 5983:                         $excluded{$hostname} = 1;
 5984:                     }
 5985:                 }
 5986:             }
 5987:         }
 5988:     }
 5989:     my @choices;
 5990:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5991:         if (keys(%{$serverhomes}) > 1) {
 5992:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5993:                 unless ($excluded{$name}) {
 5994:                     if (exists($altids->{$serverhomes->{$name}})) {
 5995:                         push(@choices,$altids->{$serverhomes->{$name}});
 5996:                     } else {
 5997:                         push(@choices,$serverhomes->{$name});
 5998:                     }
 5999:                 }
 6000:             }
 6001:         }
 6002:     }
 6003:     return sort(@choices);
 6004: }
 6005: 
 6006: sub print_loadbalancing {
 6007:     my ($dom,$settings,$rowtotal) = @_;
 6008:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6009:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6010:     my $numinrow = 1;
 6011:     my $datatable;
 6012:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6013:     my (%currbalancer,%currtargets,%currrules,%existing);
 6014:     if (ref($settings) eq 'HASH') {
 6015:         %existing = %{$settings};
 6016:     }
 6017:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 6018:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 6019:                                   \%currtargets,\%currrules);
 6020:     } else {
 6021:         return;
 6022:     }
 6023:     my ($othertitle,$usertypes,$types) =
 6024:         &Apache::loncommon::sorted_inst_types($dom);
 6025:     my $rownum = 8;
 6026:     if (ref($types) eq 'ARRAY') {
 6027:         $rownum += scalar(@{$types});
 6028:     }
 6029:     my @css_class = ('LC_odd_row','LC_even_row');
 6030:     my $balnum = 0;
 6031:     my $islast;
 6032:     my (@toshow,$disabledtext);
 6033:     if (keys(%currbalancer) > 0) {
 6034:         @toshow = sort(keys(%currbalancer));
 6035:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 6036:             push(@toshow,'');
 6037:         }
 6038:     } else {
 6039:         @toshow = ('');
 6040:         $disabledtext = &mt('No existing load balancer');
 6041:     }
 6042:     foreach my $lonhost (@toshow) {
 6043:         if ($balnum == scalar(@toshow)-1) {
 6044:             $islast = 1;
 6045:         } else {
 6046:             $islast = 0;
 6047:         }
 6048:         my $cssidx = $balnum%2;
 6049:         my $targets_div_style = 'display: none';
 6050:         my $disabled_div_style = 'display: block';
 6051:         my $homedom_div_style = 'display: none';
 6052:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 6053:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6054:                       '<p>';
 6055:         if ($lonhost eq '') {
 6056:             $datatable .= '<span class="LC_nobreak">';
 6057:             if (keys(%currbalancer) > 0) {
 6058:                 $datatable .= &mt('Add balancer:');
 6059:             } else {
 6060:                 $datatable .= &mt('Enable balancer:');
 6061:             }
 6062:             $datatable .= '&nbsp;'.
 6063:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 6064:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 6065:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 6066:                           '<option value="" selected="selected">'.&mt('None').
 6067:                           '</option>'."\n";
 6068:             foreach my $server (sort(keys(%servers))) {
 6069:                 next if ($currbalancer{$server});
 6070:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 6071:             }
 6072:             $datatable .=
 6073:                 '</select>'."\n".
 6074:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 6075:         } else {
 6076:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 6077:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 6078:                            &mt('Stop balancing').'</label>'.
 6079:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 6080:             $targets_div_style = 'display: block';
 6081:             $disabled_div_style = 'display: none';
 6082:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 6083:                 $homedom_div_style = 'display: block';
 6084:             }
 6085:         }
 6086:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6087:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 6088:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 6089:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 6090:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 6091:         my @sparestypes = ('primary','default');
 6092:         my %typetitles = &sparestype_titles();
 6093:         my %hostherechecked = (
 6094:                                   no => ' checked="checked"',
 6095:                               );
 6096:         foreach my $sparetype (@sparestypes) {
 6097:             my $targettable;
 6098:             for (my $i=0; $i<$numspares; $i++) {
 6099:                 my $checked;
 6100:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 6101:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6102:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6103:                             $checked = ' checked="checked"';
 6104:                         }
 6105:                     }
 6106:                 }
 6107:                 my ($chkboxval,$disabled);
 6108:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 6109:                     $chkboxval = $spares[$i];
 6110:                 }
 6111:                 if (exists($currbalancer{$spares[$i]})) {
 6112:                     $disabled = ' disabled="disabled"';
 6113:                 }
 6114:                 $targettable .=
 6115:                     '<td><span class="LC_nobreak"><label>'.
 6116:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 6117:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 6118:                     '</span></label></span></td>';
 6119:                 my $rem = $i%($numinrow);
 6120:                 if ($rem == 0) {
 6121:                     if (($i > 0) && ($i < $numspares-1)) {
 6122:                         $targettable .= '</tr>';
 6123:                     }
 6124:                     if ($i < $numspares-1) {
 6125:                         $targettable .= '<tr>';
 6126:                     }
 6127:                 }
 6128:             }
 6129:             if ($targettable ne '') {
 6130:                 my $rem = $numspares%($numinrow);
 6131:                 my $colsleft = $numinrow - $rem;
 6132:                 if ($colsleft > 1 ) {
 6133:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6134:                                     '&nbsp;</td>';
 6135:                 } elsif ($colsleft == 1) {
 6136:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 6137:                 }
 6138:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 6139:                                '<table><tr>'.$targettable.'</tr></table><br />';
 6140:             }
 6141:             $hostherechecked{$sparetype} = '';
 6142:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 6143:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6144:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6145:                         $hostherechecked{$sparetype} = ' checked="checked"';
 6146:                         $hostherechecked{'no'} = '';
 6147:                     }
 6148:                 }
 6149:             }
 6150:         }
 6151:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 6152:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 6153:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 6154:         foreach my $sparetype (@sparestypes) {
 6155:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 6156:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 6157:                           '</i></label><br />';
 6158:         }
 6159:         $datatable .= '</div></td></tr>'.
 6160:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 6161:                                            $othertitle,$usertypes,$types,\%servers,
 6162:                                            \%currbalancer,$lonhost,
 6163:                                            $targets_div_style,$homedom_div_style,
 6164:                                            $css_class[$cssidx],$balnum,$islast);
 6165:         $$rowtotal += $rownum;
 6166:         $balnum ++;
 6167:     }
 6168:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 6169:     return $datatable;
 6170: }
 6171: 
 6172: sub get_loadbalancers_config {
 6173:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 6174:     return unless ((ref($servers) eq 'HASH') &&
 6175:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 6176:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 6177:     if (keys(%{$existing}) > 0) {
 6178:         my $oldlonhost;
 6179:         foreach my $key (sort(keys(%{$existing}))) {
 6180:             if ($key eq 'lonhost') {
 6181:                 $oldlonhost = $existing->{'lonhost'};
 6182:                 $currbalancer->{$oldlonhost} = 1;
 6183:             } elsif ($key eq 'targets') {
 6184:                 if ($oldlonhost) {
 6185:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 6186:                 }
 6187:             } elsif ($key eq 'rules') {
 6188:                 if ($oldlonhost) {
 6189:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 6190:                 }
 6191:             } elsif (ref($existing->{$key}) eq 'HASH') {
 6192:                 $currbalancer->{$key} = 1;
 6193:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 6194:                 $currrules->{$key} = $existing->{$key}{'rules'};
 6195:             }
 6196:         }
 6197:     } else {
 6198:         my ($balancerref,$targetsref) =
 6199:                 &Apache::lonnet::get_lonbalancer_config($servers);
 6200:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 6201:             foreach my $server (sort(keys(%{$balancerref}))) {
 6202:                 $currbalancer->{$server} = 1;
 6203:                 $currtargets->{$server} = $targetsref->{$server};
 6204:             }
 6205:         }
 6206:     }
 6207:     return;
 6208: }
 6209: 
 6210: sub loadbalancing_rules {
 6211:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 6212:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 6213:         $css_class,$balnum,$islast) = @_;
 6214:     my $output;
 6215:     my $num = 0;
 6216:     my ($alltypes,$othertypes,$titles) =
 6217:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 6218:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 6219:         foreach my $type (@{$alltypes}) {
 6220:             $num ++;
 6221:             my $current;
 6222:             if (ref($currrules) eq 'HASH') {
 6223:                 $current = $currrules->{$type};
 6224:             }
 6225:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6226:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 6227:                     $current = '';
 6228:                 }
 6229:             }
 6230:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 6231:                                              $servers,$currbalancer,$lonhost,$dom,
 6232:                                              $targets_div_style,$homedom_div_style,
 6233:                                              $css_class,$balnum,$num,$islast);
 6234:         }
 6235:     }
 6236:     return $output;
 6237: }
 6238: 
 6239: sub loadbalancing_titles {
 6240:     my ($dom,$intdom,$usertypes,$types) = @_;
 6241:     my %othertypes = (
 6242:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 6243:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 6244:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 6245:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 6246:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 6247:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 6248:                      );
 6249:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 6250:     my @available;
 6251:     if (ref($types) eq 'ARRAY') {
 6252:         @available = @{$types};
 6253:     }
 6254:     unless (grep(/^default$/,@available)) {
 6255:         push(@available,'default');
 6256:     }
 6257:     unshift(@alltypes,@available);
 6258:     my %titles;
 6259:     foreach my $type (@alltypes) {
 6260:         if ($type =~ /^_LC_/) {
 6261:             $titles{$type} = $othertypes{$type};
 6262:         } elsif ($type eq 'default') {
 6263:             $titles{$type} = &mt('All users from [_1]',$dom);
 6264:             if (ref($types) eq 'ARRAY') {
 6265:                 if (@{$types} > 0) {
 6266:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 6267:                 }
 6268:             }
 6269:         } elsif (ref($usertypes) eq 'HASH') {
 6270:             $titles{$type} = $usertypes->{$type};
 6271:         }
 6272:     }
 6273:     return (\@alltypes,\%othertypes,\%titles);
 6274: }
 6275: 
 6276: sub loadbalance_rule_row {
 6277:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 6278:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 6279:     my @rulenames;
 6280:     my %ruletitles = &offloadtype_text();
 6281:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 6282:         @rulenames = ('balancer','offloadedto','specific');
 6283:     } else {
 6284:         @rulenames = ('default','homeserver');
 6285:         if ($type eq '_LC_external') {
 6286:             push(@rulenames,'externalbalancer');
 6287:         } else {
 6288:             push(@rulenames,'specific');
 6289:         }
 6290:         push(@rulenames,'none');
 6291:     }
 6292:     my $style = $targets_div_style;
 6293:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6294:         $style = $homedom_div_style;
 6295:     }
 6296:     my $space;
 6297:     if ($islast && $num == 1) {
 6298:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 6299:     }
 6300:     my $output =
 6301:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 6302:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 6303:         '<td valaign="top">'.$space.
 6304:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 6305:     for (my $i=0; $i<@rulenames; $i++) {
 6306:         my $rule = $rulenames[$i];
 6307:         my ($checked,$extra);
 6308:         if ($rulenames[$i] eq 'default') {
 6309:             $rule = '';
 6310:         }
 6311:         if ($rulenames[$i] eq 'specific') {
 6312:             if (ref($servers) eq 'HASH') {
 6313:                 my $default;
 6314:                 if (($current ne '') && (exists($servers->{$current}))) {
 6315:                     $checked = ' checked="checked"';
 6316:                 }
 6317:                 unless ($checked) {
 6318:                     $default = ' selected="selected"';
 6319:                 }
 6320:                 $extra =
 6321:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6322:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6323:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 6324:                     '<option value=""'.$default.'></option>'."\n";
 6325:                 foreach my $server (sort(keys(%{$servers}))) {
 6326:                     if (ref($currbalancer) eq 'HASH') {
 6327:                         next if (exists($currbalancer->{$server}));
 6328:                     }
 6329:                     my $selected;
 6330:                     if ($server eq $current) {
 6331:                         $selected = ' selected="selected"';
 6332:                     }
 6333:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 6334:                 }
 6335:                 $extra .= '</select>';
 6336:             }
 6337:         } elsif ($rule eq $current) {
 6338:             $checked = ' checked="checked"';
 6339:         }
 6340:         $output .= '<span class="LC_nobreak"><label>'.
 6341:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 6342:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 6343:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 6344:                    ')"'.$checked.' />&nbsp;';
 6345:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 6346:             $output .= $ruletitles{'particular'};
 6347:         } else {
 6348:             $output .= $ruletitles{$rulenames[$i]};
 6349:         }
 6350:         $output .= '</label>'.$extra.'</span><br />'."\n";
 6351:     }
 6352:     $output .= '</div></td></tr>'."\n";
 6353:     return $output;
 6354: }
 6355: 
 6356: sub offloadtype_text {
 6357:     my %ruletitles = &Apache::lonlocal::texthash (
 6358:            'default'          => 'Offloads to default destinations',
 6359:            'homeserver'       => "Offloads to user's home server",
 6360:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 6361:            'specific'         => 'Offloads to specific server',
 6362:            'none'             => 'No offload',
 6363:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 6364:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 6365:            'particular'       => 'Session hosted (after re-auth) on server:',
 6366:     );
 6367:     return %ruletitles;
 6368: }
 6369: 
 6370: sub sparestype_titles {
 6371:     my %typestitles = &Apache::lonlocal::texthash (
 6372:                           'primary' => 'primary',
 6373:                           'default' => 'default',
 6374:                       );
 6375:     return %typestitles;
 6376: }
 6377: 
 6378: sub contact_titles {
 6379:     my %titles = &Apache::lonlocal::texthash (
 6380:                    'supportemail'    => 'Support E-mail address',
 6381:                    'adminemail'      => 'Default Server Admin E-mail address',
 6382:                    'errormail'       => 'Error reports to be e-mailed to',
 6383:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 6384:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 6385:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 6386:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 6387:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 6388:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 6389:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 6390:                  );
 6391:     my %short_titles = &Apache::lonlocal::texthash (
 6392:                            adminemail   => 'Admin E-mail address',
 6393:                            supportemail => 'Support E-mail',
 6394:                        );   
 6395:     return (\%titles,\%short_titles);
 6396: }
 6397: 
 6398: sub helpform_fields {
 6399:     my %titles =  &Apache::lonlocal::texthash (
 6400:                        'username'   => 'Name',
 6401:                        'user'       => 'Username/domain',
 6402:                        'phone'      => 'Phone',
 6403:                        'cc'         => 'Cc e-mail',
 6404:                        'course'     => 'Course Details',
 6405:                        'section'    => 'Sections',
 6406:                        'screenshot' => 'File upload',
 6407:     );
 6408:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 6409:     my %possoptions = (
 6410:                         username     => ['yes','no','req'],
 6411:                         phone        => ['yes','no','req'],
 6412:                         user         => ['yes','no'],
 6413:                         cc           => ['yes','no'],
 6414:                         course       => ['yes','no'],
 6415:                         section      => ['yes','no'],
 6416:                         screenshot   => ['yes','no'],
 6417:                       );
 6418:     my %fieldoptions = &Apache::lonlocal::texthash (
 6419:                          'yes'  => 'Optional',
 6420:                          'req'  => 'Required',
 6421:                          'no'   => "Not shown",
 6422:     );
 6423:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 6424: }
 6425: 
 6426: sub tool_titles {
 6427:     my %titles = &Apache::lonlocal::texthash (
 6428:                      aboutme    => 'Personal web page',
 6429:                      blog       => 'Blog',
 6430:                      webdav     => 'WebDAV',
 6431:                      portfolio  => 'Portfolio',
 6432:                      official   => 'Official courses (with institutional codes)',
 6433:                      unofficial => 'Unofficial courses',
 6434:                      community  => 'Communities',
 6435:                      textbook   => 'Textbook courses',
 6436:                      placement  => 'Placement tests',
 6437:                  );
 6438:     return %titles;
 6439: }
 6440: 
 6441: sub courserequest_titles {
 6442:     my %titles = &Apache::lonlocal::texthash (
 6443:                                    official   => 'Official',
 6444:                                    unofficial => 'Unofficial',
 6445:                                    community  => 'Communities',
 6446:                                    textbook   => 'Textbook',
 6447:                                    placement  => 'Placement tests',
 6448:                                    norequest  => 'Not allowed',
 6449:                                    approval   => 'Approval by Dom. Coord.',
 6450:                                    validate   => 'With validation',
 6451:                                    autolimit  => 'Numerical limit',
 6452:                                    unlimited  => '(blank for unlimited)',
 6453:                  );
 6454:     return %titles;
 6455: }
 6456: 
 6457: sub authorrequest_titles {
 6458:     my %titles = &Apache::lonlocal::texthash (
 6459:                                    norequest  => 'Not allowed',
 6460:                                    approval   => 'Approval by Dom. Coord.',
 6461:                                    automatic  => 'Automatic approval',
 6462:                  );
 6463:     return %titles;
 6464: }
 6465: 
 6466: sub courserequest_conditions {
 6467:     my %conditions = &Apache::lonlocal::texthash (
 6468:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6469:        validate   => '(Processing of request subject to institutional validation).',
 6470:                  );
 6471:     return %conditions;
 6472: }
 6473: 
 6474: 
 6475: sub print_usercreation {
 6476:     my ($position,$dom,$settings,$rowtotal) = @_;
 6477:     my $numinrow = 4;
 6478:     my $datatable;
 6479:     if ($position eq 'top') {
 6480:         $$rowtotal ++;
 6481:         my $rowcount = 0;
 6482:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6483:         if (ref($rules) eq 'HASH') {
 6484:             if (keys(%{$rules}) > 0) {
 6485:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6486:                                                 $ruleorder,$numinrow,$rowcount);
 6487:                 $$rowtotal ++;
 6488:                 $rowcount ++;
 6489:             }
 6490:         }
 6491:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6492:         if (ref($idrules) eq 'HASH') {
 6493:             if (keys(%{$idrules}) > 0) {
 6494:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6495:                                                 $idruleorder,$numinrow,$rowcount);
 6496:                 $$rowtotal ++;
 6497:                 $rowcount ++;
 6498:             }
 6499:         }
 6500:         if ($rowcount == 0) {
 6501:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6502:             $$rowtotal ++;
 6503:             $rowcount ++;
 6504:         }
 6505:     } elsif ($position eq 'middle') {
 6506:         my @creators = ('author','course','requestcrs');
 6507:         my ($rules,$ruleorder) =
 6508:             &Apache::lonnet::inst_userrules($dom,'username');
 6509:         my %lt = &usercreation_types();
 6510:         my %checked;
 6511:         if (ref($settings) eq 'HASH') {
 6512:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6513:                 foreach my $item (@creators) {
 6514:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6515:                 }
 6516:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6517:                 foreach my $item (@creators) {
 6518:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6519:                         $checked{$item} = 'none';
 6520:                     }
 6521:                 }
 6522:             }
 6523:         }
 6524:         my $rownum = 0;
 6525:         foreach my $item (@creators) {
 6526:             $rownum ++;
 6527:             if ($checked{$item} eq '') {
 6528:                 $checked{$item} = 'any';
 6529:             }
 6530:             my $css_class;
 6531:             if ($rownum%2) {
 6532:                 $css_class = '';
 6533:             } else {
 6534:                 $css_class = ' class="LC_odd_row" ';
 6535:             }
 6536:             $datatable .= '<tr'.$css_class.'>'.
 6537:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6538:                          '</span></td><td style="text-align: right">';
 6539:             my @options = ('any');
 6540:             if (ref($rules) eq 'HASH') {
 6541:                 if (keys(%{$rules}) > 0) {
 6542:                     push(@options,('official','unofficial'));
 6543:                 }
 6544:             }
 6545:             push(@options,'none');
 6546:             foreach my $option (@options) {
 6547:                 my $type = 'radio';
 6548:                 my $check = ' ';
 6549:                 if ($checked{$item} eq $option) {
 6550:                     $check = ' checked="checked" ';
 6551:                 } 
 6552:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6553:                               '<input type="'.$type.'" name="can_createuser_'.
 6554:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6555:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6556:             }
 6557:             $datatable .= '</td></tr>';
 6558:         }
 6559:     } else {
 6560:         my @contexts = ('author','course','domain');
 6561:         my @authtypes = ('int','krb4','krb5','loc');
 6562:         my %checked;
 6563:         if (ref($settings) eq 'HASH') {
 6564:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6565:                 foreach my $item (@contexts) {
 6566:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6567:                         foreach my $auth (@authtypes) {
 6568:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6569:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6570:                             }
 6571:                         }
 6572:                     }
 6573:                 }
 6574:             }
 6575:         } else {
 6576:             foreach my $item (@contexts) {
 6577:                 foreach my $auth (@authtypes) {
 6578:                     $checked{$item}{$auth} = ' checked="checked" ';
 6579:                 }
 6580:             }
 6581:         }
 6582:         my %title = &context_names();
 6583:         my %authname = &authtype_names();
 6584:         my $rownum = 0;
 6585:         my $css_class; 
 6586:         foreach my $item (@contexts) {
 6587:             if ($rownum%2) {
 6588:                 $css_class = '';
 6589:             } else {
 6590:                 $css_class = ' class="LC_odd_row" ';
 6591:             }
 6592:             $datatable .=   '<tr'.$css_class.'>'.
 6593:                             '<td>'.$title{$item}.
 6594:                             '</td><td class="LC_left_item">'.
 6595:                             '<span class="LC_nobreak">';
 6596:             foreach my $auth (@authtypes) {
 6597:                 $datatable .= '<label>'. 
 6598:                               '<input type="checkbox" name="'.$item.'_auth" '.
 6599:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 6600:                               $authname{$auth}.'</label>&nbsp;';
 6601:             }
 6602:             $datatable .= '</span></td></tr>';
 6603:             $rownum ++;
 6604:         }
 6605:         $$rowtotal += $rownum;
 6606:     }
 6607:     return $datatable;
 6608: }
 6609: 
 6610: sub print_selfcreation {
 6611:     my ($position,$dom,$settings,$rowtotal) = @_;
 6612:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 6613:         $emaildomain,$datatable);
 6614:     if (ref($settings) eq 'HASH') {
 6615:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 6616:             $createsettings = $settings->{'cancreate'};
 6617:             if (ref($createsettings) eq 'HASH') {
 6618:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 6619:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 6620:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 6621:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 6622:                         @selfcreate = ('email','login','sso');
 6623:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 6624:                         @selfcreate = ($createsettings->{'selfcreate'});
 6625:                     }
 6626:                 }
 6627:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 6628:                     $processing = $createsettings->{'selfcreateprocessing'};
 6629:                 }
 6630:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 6631:                     $emailoptions = $createsettings->{'emailoptions'};
 6632:                 }
 6633:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 6634:                     $emailverified = $createsettings->{'emailverified'};
 6635:                 }
 6636:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 6637:                     $emaildomain = $createsettings->{'emaildomain'};
 6638:                 }
 6639:             }
 6640:         }
 6641:     }
 6642:     my %radiohash;
 6643:     my $numinrow = 4;
 6644:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 6645:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6646:     if ($position eq 'top') {
 6647:         my %choices = &Apache::lonlocal::texthash (
 6648:                                                       cancreate_login      => 'Institutional Login',
 6649:                                                       cancreate_sso        => 'Institutional Single Sign On',
 6650:                                                   );
 6651:         my @toggles = sort(keys(%choices));
 6652:         my %defaultchecked = (
 6653:                                'cancreate_login' => 'off',
 6654:                                'cancreate_sso'   => 'off',
 6655:                              );
 6656:         my ($onclick,$itemcount);
 6657:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6658:                                                      \%choices,$itemcount,$onclick);
 6659:         $$rowtotal += $itemcount;
 6660:         
 6661:         if (ref($usertypes) eq 'HASH') {
 6662:             if (keys(%{$usertypes}) > 0) {
 6663:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 6664:                                              $dom,$numinrow,$othertitle,
 6665:                                              'statustocreate',$rowtotal);
 6666:                 $$rowtotal ++;
 6667:             }
 6668:         }
 6669:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 6670:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6671:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 6672:         my $rem;
 6673:         my $numperrow = 2;
 6674:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 6675:         $datatable .= '<tr'.$css_class.'>'.
 6676:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 6677:                      '<td class="LC_left_item">'."\n".
 6678:                      '<table><tr><td>'."\n";
 6679:         for (my $i=0; $i<@fields; $i++) {
 6680:             $rem = $i%($numperrow);
 6681:             if ($rem == 0) {
 6682:                 if ($i > 0) {
 6683:                     $datatable .= '</tr>';
 6684:                 }
 6685:                 $datatable .= '<tr>';
 6686:             }
 6687:             my $currval;
 6688:             if (ref($createsettings) eq 'HASH') {
 6689:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 6690:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 6691:                 }
 6692:             }
 6693:             $datatable .= '<td class="LC_left_item">'.
 6694:                           '<span class="LC_nobreak">'.
 6695:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 6696:                           'value="'.$currval.'" size="10" />&nbsp;'.
 6697:                           $fieldtitles{$fields[$i]}.'</span></td>';
 6698:         }
 6699:         my $colsleft = $numperrow - $rem;
 6700:         if ($colsleft > 1 ) {
 6701:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6702:                          '&nbsp;</td>';
 6703:         } elsif ($colsleft == 1) {
 6704:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6705:         }
 6706:         $datatable .= '</tr></table></td></tr>';
 6707:         $$rowtotal ++;
 6708:     } elsif ($position eq 'middle') {
 6709:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 6710:         my @posstypes;
 6711:         if (ref($types) eq 'ARRAY') {
 6712:             @posstypes = @{$types};
 6713:         }
 6714:         unless (grep(/^default$/,@posstypes)) {
 6715:             push(@posstypes,'default');
 6716:         }
 6717:         my %usertypeshash;
 6718:         if (ref($usertypes) eq 'HASH') {
 6719:             %usertypeshash = %{$usertypes};
 6720:         }
 6721:         $usertypeshash{'default'} = $othertitle;
 6722:         foreach my $status (@posstypes) {
 6723:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 6724:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 6725:             $$rowtotal ++;
 6726:         }
 6727:     } else {
 6728:         my %choices = &Apache::lonlocal::texthash (
 6729:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 6730:                                                   );
 6731:         my @toggles = sort(keys(%choices));
 6732:         my %defaultchecked = (
 6733:                                'cancreate_email' => 'off',
 6734:                              );
 6735:         my $customclass = 'LC_selfcreate_email';
 6736:         my $classprefix = 'LC_canmodify_emailusername_';
 6737:         my $optionsprefix = 'LC_options_emailusername_';
 6738:         my $display = 'none';
 6739:         my $rowstyle = 'display:none';
 6740:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 6741:             $display = 'block';
 6742:             $rowstyle = 'display:table-row';
 6743:         }
 6744:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 6745:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6746:                                                      \%choices,$$rowtotal,$onclick);
 6747:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 6748:                                          $rowstyle);
 6749:         $$rowtotal ++;
 6750:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 6751:                                       $rowstyle);
 6752:         $$rowtotal ++;
 6753:         my (@ordered,@posstypes,%usertypeshash);
 6754:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6755:         my ($emailrules,$emailruleorder) =
 6756:             &Apache::lonnet::inst_userrules($dom,'email');
 6757:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6758:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6759:         if (ref($types) eq 'ARRAY') {
 6760:             @posstypes = @{$types};
 6761:         }
 6762:         if (@posstypes) {
 6763:             unless (grep(/^default$/,@posstypes)) {
 6764:                 push(@posstypes,'default');
 6765:             }
 6766:             if (ref($usertypes) eq 'HASH') {
 6767:                 %usertypeshash = %{$usertypes};
 6768:             }
 6769:             my $currassign;
 6770:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 6771:                 $currassign = {
 6772:                                   selfassign => $domdefaults{'inststatusguest'},
 6773:                               };
 6774:                 @ordered = @{$domdefaults{'inststatusguest'}};
 6775:             } else {
 6776:                 $currassign = { selfassign => [] };
 6777:             }
 6778:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 6779:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 6780:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 6781:                                          $numinrow,$othertitle,'selfassign',
 6782:                                          $rowtotal,$onclicktypes,$customclass,
 6783:                                          $rowstyle);
 6784:             $$rowtotal ++;
 6785:             $usertypeshash{'default'} = $othertitle;
 6786:             foreach my $status (@posstypes) {
 6787:                 my $css_class;
 6788:                 if ($$rowtotal%2) {
 6789:                     $css_class = 'LC_odd_row ';
 6790:                 }
 6791:                 $css_class .= $customclass;
 6792:                 my $rowid = $optionsprefix.$status;
 6793:                 my $hidden = 1;
 6794:                 my $currstyle = 'display:none';
 6795:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6796:                     $currstyle = $rowstyle;
 6797:                     $hidden = 0; 
 6798:                 }
 6799:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6800:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 6801:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 6802:                 unless ($hidden) {
 6803:                     $$rowtotal ++;
 6804:                 }
 6805:             }
 6806:         } else {
 6807:             my $css_class;
 6808:             if ($$rowtotal%2) {
 6809:                 $css_class = 'LC_odd_row ';
 6810:             }
 6811:             $css_class .= $customclass;
 6812:             $usertypeshash{'default'} = $othertitle;
 6813:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6814:                                          $emailrules,$emailruleorder,$settings,'default','',
 6815:                                          $othertitle,$css_class,$rowstyle,$intdom);
 6816:             $$rowtotal ++;
 6817:         }
 6818:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 6819:         $numinrow = 1;
 6820:         if (@posstypes) {
 6821:             foreach my $status (@posstypes) {
 6822:                 my $rowid = $classprefix.$status;
 6823:                 my $datarowstyle = 'display:none';
 6824:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 6825:                     $datarowstyle = $rowstyle; 
 6826:                 }
 6827:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 6828:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6829:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 6830:                 unless ($datarowstyle eq 'display:none') {
 6831:                     $$rowtotal ++;
 6832:                 }
 6833:             }
 6834:         } else {
 6835:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 6836:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6837:                                                    $infotitles,'',$customclass,$rowstyle);
 6838:         }
 6839:     }
 6840:     return $datatable;
 6841: }
 6842: 
 6843: sub selfcreate_javascript {
 6844:     return <<"ENDSCRIPT";
 6845: 
 6846: <script type="text/javascript">
 6847: // <![CDATA[
 6848: 
 6849: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 6850:     var x = document.getElementsByClassName(target);
 6851:     var insttypes = 0;
 6852:     var insttypeRegExp = new RegExp(prefix);
 6853:     if ((x.length != undefined) && (x.length > 0)) {
 6854:         if (form.elements[radio].length != undefined) {
 6855:             for (var i=0; i<form.elements[radio].length; i++) {
 6856:                 if (form.elements[radio][i].checked) {
 6857:                     if (form.elements[radio][i].value == 1) {
 6858:                         for (var j=0; j<x.length; j++) {
 6859:                             if (x[j].id == 'undefined') {
 6860:                                 x[j].style.display = 'table-row';
 6861:                             } else if (insttypeRegExp.test(x[j].id)) {
 6862:                                 insttypes ++;
 6863:                             } else {
 6864:                                 x[j].style.display = 'table-row';
 6865:                             }
 6866:                         }
 6867:                     } else {
 6868:                         for (var j=0; j<x.length; j++) {
 6869:                             x[j].style.display = 'none';
 6870:                         }
 6871:                     }
 6872:                     break;
 6873:                 }
 6874:             }
 6875:             if (insttypes > 0) {
 6876:                 toggleDataRow(form,checkbox,target,altprefix);
 6877:                 toggleDataRow(form,checkbox,target,prefix,1);
 6878:             }
 6879:         }
 6880:     }
 6881:     return;
 6882: }
 6883: 
 6884: function toggleDataRow(form,checkbox,target,prefix,docount) {
 6885:     if (form.elements[checkbox].length != undefined) {
 6886:         var count = 0;
 6887:         if (docount) {
 6888:             for (var i=0; i<form.elements[checkbox].length; i++) {
 6889:                 if (form.elements[checkbox][i].checked) {
 6890:                     count ++;
 6891:                 }
 6892:             }
 6893:         }
 6894:         for (var i=0; i<form.elements[checkbox].length; i++) {
 6895:             var type = form.elements[checkbox][i].value;
 6896:             if (document.getElementById(prefix+type)) {
 6897:                 if (form.elements[checkbox][i].checked) {
 6898:                     document.getElementById(prefix+type).style.display = 'table-row';
 6899:                     if (count % 2 == 1) {
 6900:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 6901:                     } else {
 6902:                         document.getElementById(prefix+type).className = target;
 6903:                     }
 6904:                     count ++;
 6905:                 } else {
 6906:                     document.getElementById(prefix+type).style.display = 'none';
 6907:                 }
 6908:             }
 6909:         }
 6910:     }
 6911:     return;
 6912: }
 6913: 
 6914: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 6915:     var caller = radio+'_'+status;
 6916:     if (form.elements[caller].length != undefined) {
 6917:         for (var i=0; i<form.elements[caller].length; i++) {
 6918:             if (form.elements[caller][i].checked) {
 6919:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 6920:                     var curr = form.elements[caller][i].value;
 6921:                     if (prefix) {
 6922:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 6923:                     }
 6924:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 6925:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6926:                     if (curr == 'custom') {
 6927:                         if (prefix) { 
 6928:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6929:                         }
 6930:                     } else if (curr == 'inst') {
 6931:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6932:                     } else if (curr == 'noninst') {
 6933:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6934:                     }
 6935:                     break;
 6936:                 }
 6937:             }
 6938:         }
 6939:     }
 6940: }
 6941: 
 6942: // ]]>
 6943: </script>
 6944: 
 6945: ENDSCRIPT
 6946: }
 6947: 
 6948: sub noninst_users {
 6949:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6950:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 6951:     my $class = 'LC_left_item';
 6952:     if ($css_class) {
 6953:         $css_class = ' class="'.$css_class.'"'; 
 6954:     }
 6955:     if ($rowid) {
 6956:         $rowid = ' id="'.$rowid.'"';
 6957:     }
 6958:     if ($rowstyle) {
 6959:         $rowstyle = ' style="'.$rowstyle.'"';
 6960:     }
 6961:     my ($output,$description);
 6962:     if ($type eq 'default') {
 6963:         $description = &mt('Requests for: [_1]',$typetitle);
 6964:     } else {
 6965:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6966:     }
 6967:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6968:               "<td>$description</td>\n".           
 6969:               '<td class="'.$class.'" colspan="2">'.
 6970:               '<table><tr>';
 6971:     my %headers = &Apache::lonlocal::texthash( 
 6972:               approve  => 'Processing',
 6973:               email    => 'E-mail',
 6974:               username => 'Username',
 6975:     );
 6976:     foreach my $item ('approve','email','username') {
 6977:         $output .= '<th>'.$headers{$item}.'</th>';
 6978:     }
 6979:     $output .= '</tr><tr>';
 6980:     foreach my $item ('approve','email','username') {
 6981:         $output .= '<td style="vertical-align: top">';
 6982:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6983:         if ($item eq 'approve') {
 6984:             %choices = &Apache::lonlocal::texthash (
 6985:                                                      automatic => 'Automatically approved',
 6986:                                                      approval  => 'Queued for approval',
 6987:                                                    );
 6988:             @options = ('automatic','approval');
 6989:             $hashref = $processing;
 6990:             $defoption = 'automatic';
 6991:             $name = 'cancreate_emailprocess_'.$type;
 6992:         } elsif ($item eq 'email') {
 6993:             %choices = &Apache::lonlocal::texthash (
 6994:                                                      any     => 'Any e-mail',
 6995:                                                      inst    => 'Institutional only',
 6996:                                                      noninst => 'Non-institutional only',
 6997:                                                      custom  => 'Custom restrictions',
 6998:                                                    );
 6999:             @options = ('any','inst','noninst');
 7000:             my $showcustom;
 7001:             if (ref($emailrules) eq 'HASH') {
 7002:                 if (keys(%{$emailrules}) > 0) {
 7003:                     push(@options,'custom');
 7004:                     $showcustom = 'cancreate_emailrule';
 7005:                     if (ref($settings) eq 'HASH') {
 7006:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 7007:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 7008:                                 if (exists($emailrules->{$rule})) {
 7009:                                     $hascustom ++;
 7010:                                 }
 7011:                             }
 7012:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 7013:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 7014:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 7015:                                     if (exists($emailrules->{$rule})) {
 7016:                                         $hascustom ++;
 7017:                                     }
 7018:                                 }
 7019:                             }
 7020:                         }
 7021:                     }
 7022:                 }
 7023:             }
 7024:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 7025:                                                      "'cancreate_emaildomain','$type'".');"';
 7026:             $hashref = $emailoptions;
 7027:             $defoption = 'any';
 7028:             $name = 'cancreate_emailoptions_'.$type;
 7029:         } elsif ($item eq 'username') {
 7030:             %choices = &Apache::lonlocal::texthash (
 7031:                                                      all    => 'Same as e-mail',
 7032:                                                      first  => 'Omit @domain',
 7033:                                                      free   => 'Free to choose',
 7034:                                                    );
 7035:             @options = ('all','first','free');
 7036:             $hashref = $emailverified;
 7037:             $defoption = 'all';
 7038:             $name = 'cancreate_usernameoptions_'.$type;
 7039:         }
 7040:         foreach my $option (@options) {
 7041:             my $checked;
 7042:             if (ref($hashref) eq 'HASH') {
 7043:                 if ($type eq '') {
 7044:                     if (!exists($hashref->{'default'})) {
 7045:                         if ($option eq $defoption) {
 7046:                             $checked = ' checked="checked"';
 7047:                         }
 7048:                     } else {
 7049:                         if ($hashref->{'default'} eq $option) {
 7050:                             $checked = ' checked="checked"';
 7051:                         }
 7052:                     }
 7053:                 } else {
 7054:                     if (!exists($hashref->{$type})) {
 7055:                         if ($option eq $defoption) {
 7056:                             $checked = ' checked="checked"';
 7057:                         }
 7058:                     } else {
 7059:                         if ($hashref->{$type} eq $option) {
 7060:                             $checked = ' checked="checked"';
 7061:                         }
 7062:                     }
 7063:                 }
 7064:             } elsif (($item eq 'email') && ($hascustom)) {
 7065:                 if ($option eq 'custom') {
 7066:                     $checked = ' checked="checked"';
 7067:                 }
 7068:             } elsif ($option eq $defoption) {
 7069:                 $checked = ' checked="checked"';
 7070:             }
 7071:             $output .= '<span class="LC_nobreak"><label>'.
 7072:                        '<input type="radio" name="'.$name.'"'.
 7073:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 7074:                        $choices{$option}.'</label></span><br />';
 7075:             if ($item eq 'email') {
 7076:                 if ($option eq 'custom') {
 7077:                     my $id = 'cancreate_emailrule_'.$type;
 7078:                     my $display = 'none';
 7079:                     if ($checked) {
 7080:                         $display = 'inline';
 7081:                     }
 7082:                     my $numinrow = 2;
 7083:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 7084:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 7085:                                &user_formats_row('email',$settings,$emailrules,
 7086:                                                  $emailruleorder,$numinrow,'',$type);
 7087:                               '</table></fieldset>';
 7088:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 7089:                     my %text = &Apache::lonlocal::texthash (
 7090:                                                              inst    => 'must end:',
 7091:                                                              noninst => 'cannot end:',
 7092:                                                            );
 7093:                     my $value;
 7094:                     if (ref($emaildomain) eq 'HASH') {
 7095:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 7096:                             $value = $emaildomain->{$type}->{$option}; 
 7097:                         }
 7098:                     }
 7099:                     if ($value eq '') {
 7100:                         $value = '@'.$intdom;
 7101:                     }
 7102:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 7103:                     my $display = 'none';
 7104:                     if ($checked) {
 7105:                         $display = 'inline';
 7106:                     }
 7107:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 7108:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 7109:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 7110:                                '</div>';
 7111:                 }
 7112:             }
 7113:         }
 7114:         $output .= '</td>'."\n";
 7115:     }
 7116:     $output .= "</tr></table></td></tr>\n";
 7117:     return $output;
 7118: }
 7119: 
 7120: sub captcha_choice {
 7121:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 7122:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 7123:         $vertext,$currver);
 7124:     my %lt = &captcha_phrases();
 7125:     $keyentry = 'hidden';
 7126:     if ($context eq 'cancreate') {
 7127:         $rowname = &mt('CAPTCHA validation');
 7128:     } elsif ($context eq 'login') {
 7129:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 7130:     }
 7131:     if (ref($settings) eq 'HASH') {
 7132:         if ($settings->{'captcha'}) {
 7133:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 7134:         } else {
 7135:             $checked{'original'} = ' checked="checked"';
 7136:         }
 7137:         if ($settings->{'captcha'} eq 'recaptcha') {
 7138:             $pubtext = $lt{'pub'};
 7139:             $privtext = $lt{'priv'};
 7140:             $keyentry = 'text';
 7141:             $vertext = $lt{'ver'};
 7142:             $currver = $settings->{'recaptchaversion'};
 7143:             if ($currver ne '2') {
 7144:                 $currver = 1;
 7145:             }
 7146:         }
 7147:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 7148:             $currpub = $settings->{'recaptchakeys'}{'public'};
 7149:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 7150:         }
 7151:     } else {
 7152:         $checked{'original'} = ' checked="checked"';
 7153:     }
 7154:     my $css_class;
 7155:     if ($itemcount%2) {
 7156:         $css_class = 'LC_odd_row';
 7157:     }
 7158:     if ($customcss) {
 7159:         $css_class .= " $customcss";
 7160:     }
 7161:     $css_class =~ s/^\s+//;
 7162:     if ($css_class) {
 7163:         $css_class = ' class="'.$css_class.'"';
 7164:     }
 7165:     if ($rowstyle) {
 7166:         $css_class .= ' style="'.$rowstyle.'"';
 7167:     }
 7168:     my $output = '<tr'.$css_class.'>'.
 7169:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 7170:                  '<table><tr><td>'."\n";
 7171:     foreach my $option ('original','recaptcha','notused') {
 7172:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 7173:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 7174:                    $lt{$option}.'</label></span>';
 7175:         unless ($option eq 'notused') {
 7176:             $output .= ('&nbsp;'x2)."\n";
 7177:         }
 7178:     }
 7179: #
 7180: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 7181: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 7182: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 7183: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 7184: #
 7185:     $output .= '</td></tr>'."\n".
 7186:                '<tr><td class="LC_zero_height">'."\n".
 7187:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 7188:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 7189:                $currpub.'" size="40" /></span><br />'."\n".
 7190:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 7191:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 7192:                $currpriv.'" size="40" /></span><br />'.
 7193:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 7194:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 7195:                $currver.'" size="3" /></span><br />'.
 7196:                '</td></tr></table>'."\n".
 7197:                '</td></tr>';
 7198:     return $output;
 7199: }
 7200: 
 7201: sub user_formats_row {
 7202:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 7203:     my $output;
 7204:     my %text = (
 7205:                    'username' => 'new usernames',
 7206:                    'id'       => 'IDs',
 7207:                );
 7208:     unless ($type eq 'email') {
 7209:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7210:         $output = '<tr '.$css_class.'>'.
 7211:                   '<td><span class="LC_nobreak">'.
 7212:                   &mt("Format rules to check for $text{$type}: ").
 7213:                   '</td><td class="LC_left_item" colspan="2"><table>';
 7214:     }
 7215:     my $rem;
 7216:     if (ref($ruleorder) eq 'ARRAY') {
 7217:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 7218:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 7219:                 my $rem = $i%($numinrow);
 7220:                 if ($rem == 0) {
 7221:                     if ($i > 0) {
 7222:                         $output .= '</tr>';
 7223:                     }
 7224:                     $output .= '<tr>';
 7225:                 }
 7226:                 my $check = ' ';
 7227:                 if (ref($settings) eq 'HASH') {
 7228:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 7229:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 7230:                             $check = ' checked="checked" ';
 7231:                         }
 7232:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 7233:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 7234:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 7235:                                 $check = ' checked="checked" ';
 7236:                             }
 7237:                         }
 7238:                     }
 7239:                 }
 7240:                 my $name = $type.'_rule';
 7241:                 if ($type eq 'email') {
 7242:                     $name .= '_'.$status;
 7243:                 }
 7244:                 $output .= '<td class="LC_left_item">'.
 7245:                            '<span class="LC_nobreak"><label>'.
 7246:                            '<input type="checkbox" name="'.$name.'" '.
 7247:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 7248:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 7249:             }
 7250:         }
 7251:         $rem = @{$ruleorder}%($numinrow);
 7252:     }
 7253:     my $colsleft;
 7254:     if ($rem) {
 7255:         $colsleft = $numinrow - $rem;
 7256:     }
 7257:     if ($colsleft > 1 ) {
 7258:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7259:                    '&nbsp;</td>';
 7260:     } elsif ($colsleft == 1) {
 7261:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7262:     }
 7263:     $output .= '</tr></table>';
 7264:     unless ($type eq 'email') {
 7265:         $output .= '</td></tr>';
 7266:     }
 7267:     return $output;
 7268: }
 7269: 
 7270: sub usercreation_types {
 7271:     my %lt = &Apache::lonlocal::texthash (
 7272:                     author     => 'When adding a co-author',
 7273:                     course     => 'When adding a user to a course',
 7274:                     requestcrs => 'When requesting a course',
 7275:                     any        => 'Any',
 7276:                     official   => 'Institutional only ',
 7277:                     unofficial => 'Non-institutional only',
 7278:                     none       => 'None',
 7279:     );
 7280:     return %lt;
 7281: }
 7282: 
 7283: sub selfcreation_types {
 7284:     my %lt = &Apache::lonlocal::texthash (
 7285:                     selfcreate => 'User creates own account',
 7286:                     any        => 'Any',
 7287:                     official   => 'Institutional only ',
 7288:                     unofficial => 'Non-institutional only',
 7289:                     email      => 'E-mail address',
 7290:                     login      => 'Institutional Login',
 7291:                     sso        => 'SSO',
 7292:              );
 7293: }
 7294: 
 7295: sub authtype_names {
 7296:     my %lt = &Apache::lonlocal::texthash(
 7297:                       int    => 'Internal',
 7298:                       krb4   => 'Kerberos 4',
 7299:                       krb5   => 'Kerberos 5',
 7300:                       loc    => 'Local',
 7301:                   );
 7302:     return %lt;
 7303: }
 7304: 
 7305: sub context_names {
 7306:     my %context_title = &Apache::lonlocal::texthash(
 7307:        author => 'Creating users when an Author',
 7308:        course => 'Creating users when in a course',
 7309:        domain => 'Creating users when a Domain Coordinator',
 7310:     );
 7311:     return %context_title;
 7312: }
 7313: 
 7314: sub print_usermodification {
 7315:     my ($position,$dom,$settings,$rowtotal) = @_;
 7316:     my $numinrow = 4;
 7317:     my ($context,$datatable,$rowcount);
 7318:     if ($position eq 'top') {
 7319:         $rowcount = 0;
 7320:         $context = 'author'; 
 7321:         foreach my $role ('ca','aa') {
 7322:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7323:                                                    $numinrow,$rowcount);
 7324:             $$rowtotal ++;
 7325:             $rowcount ++;
 7326:         }
 7327:     } elsif ($position eq 'bottom') {
 7328:         $context = 'course';
 7329:         $rowcount = 0;
 7330:         foreach my $role ('st','ep','ta','in','cr') {
 7331:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7332:                                                    $numinrow,$rowcount);
 7333:             $$rowtotal ++;
 7334:             $rowcount ++;
 7335:         }
 7336:     }
 7337:     return $datatable;
 7338: }
 7339: 
 7340: sub print_defaults {
 7341:     my ($position,$dom,$settings,$rowtotal) = @_;
 7342:     my $rownum = 0;
 7343:     my ($datatable,$css_class,$titles);
 7344:     unless ($position eq 'bottom') {
 7345:         $titles = &defaults_titles($dom);
 7346:     }
 7347:     if ($position eq 'top') {
 7348:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 7349:                      'datelocale_def','portal_def');
 7350:         my %defaults;
 7351:         if (ref($settings) eq 'HASH') {
 7352:             %defaults = %{$settings};
 7353:         } else {
 7354:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7355:             foreach my $item (@items) {
 7356:                 $defaults{$item} = $domdefaults{$item};
 7357:             }
 7358:         }
 7359:         foreach my $item (@items) {
 7360:             if ($rownum%2) {
 7361:                 $css_class = '';
 7362:             } else {
 7363:                 $css_class = ' class="LC_odd_row" ';
 7364:             }
 7365:             $datatable .= '<tr'.$css_class.'>'.
 7366:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7367:                           '</span></td><td class="LC_right_item" colspan="3">';
 7368:             if ($item eq 'auth_def') {
 7369:                 my @authtypes = ('internal','krb4','krb5','localauth');
 7370:                 my %shortauth = (
 7371:                                  internal => 'int',
 7372:                                  krb4 => 'krb4',
 7373:                                  krb5 => 'krb5',
 7374:                                  localauth  => 'loc'
 7375:                                 );
 7376:                 my %authnames = &authtype_names();
 7377:                 foreach my $auth (@authtypes) {
 7378:                     my $checked = ' ';
 7379:                     if ($defaults{$item} eq $auth) {
 7380:                         $checked = ' checked="checked" ';
 7381:                     }
 7382:                     $datatable .= '<label><input type="radio" name="'.$item.
 7383:                                   '" value="'.$auth.'"'.$checked.'/>'.
 7384:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 7385:                 }
 7386:             } elsif ($item eq 'timezone_def') {
 7387:                 my $includeempty = 1;
 7388:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 7389:             } elsif ($item eq 'datelocale_def') {
 7390:                 my $includeempty = 1;
 7391:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 7392:             } elsif ($item eq 'lang_def') {
 7393:                 my $includeempty = 1;
 7394:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 7395:             } else {
 7396:                 my $size;
 7397:                 if ($item eq 'portal_def') {
 7398:                     $size = ' size="25"';
 7399:                 }
 7400:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7401:                               $defaults{$item}.'"'.$size.' />';
 7402:             }
 7403:             $datatable .= '</td></tr>';
 7404:             $rownum ++;
 7405:         }
 7406:     } elsif ($position eq 'middle') {
 7407:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7408:         my %defaults;
 7409:         if (ref($settings) eq 'HASH') {
 7410:             %defaults = %{$settings};
 7411:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7412:                 $defaults{'intauth_cost'} = 10;
 7413:             }
 7414:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7415:                 $defaults{'intauth_check'} = 0;
 7416:             }
 7417:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7418:                 $defaults{'intauth_switch'} = 0;
 7419:             }
 7420:         } else {
 7421:             %defaults = (
 7422:                           'intauth_cost'   => 10,
 7423:                           'intauth_check'  => 0,
 7424:                           'intauth_switch' => 0,
 7425:                         );
 7426:         }
 7427:         foreach my $item (@items) {
 7428:             if ($rownum%2) {
 7429:                 $css_class = '';
 7430:             } else {
 7431:                 $css_class = ' class="LC_odd_row" ';
 7432:             }
 7433:             $datatable .= '<tr'.$css_class.'>'.
 7434:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7435:                           '</span></td><td class="LC_left_item" colspan="3">';
 7436:             if ($item eq 'intauth_switch') {
 7437:                 my @options = (0,1,2);
 7438:                 my %optiondesc = &Apache::lonlocal::texthash (
 7439:                                    0 => 'No',
 7440:                                    1 => 'Yes',
 7441:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7442:                                  );
 7443:                 $datatable .= '<table width="100%">';
 7444:                 foreach my $option (@options) {
 7445:                     my $checked = ' ';
 7446:                     if ($defaults{$item} eq $option) {
 7447:                         $checked = ' checked="checked"';
 7448:                     }
 7449:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7450:                                   '<label><input type="radio" name="'.$item.
 7451:                                   '" value="'.$option.'"'.$checked.' />'.
 7452:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7453:                 }
 7454:                 $datatable .= '</table>';
 7455:             } elsif ($item eq 'intauth_check') {
 7456:                 my @options = (0,1,2);
 7457:                 my %optiondesc = &Apache::lonlocal::texthash (
 7458:                                    0 => 'No',
 7459:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7460:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7461:                                  );
 7462:                 $datatable .= '<table wisth="100%">';
 7463:                 foreach my $option (@options) {
 7464:                     my $checked = ' ';
 7465:                     my $onclick;
 7466:                     if ($defaults{$item} eq $option) {
 7467:                         $checked = ' checked="checked"';
 7468:                     }
 7469:                     if ($option == 2) {
 7470:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7471:                     }
 7472:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7473:                                   '<label><input type="radio" name="'.$item.
 7474:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7475:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7476:                 }
 7477:                 $datatable .= '</table>';
 7478:             } else {
 7479:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7480:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 7481:             }
 7482:             $datatable .= '</td></tr>';
 7483:             $rownum ++;
 7484:         }
 7485:     } else {
 7486:         my %defaults;
 7487:         if (ref($settings) eq 'HASH') {
 7488:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7489:                 my $maxnum = @{$settings->{'inststatusorder'}};
 7490:                 for (my $i=0; $i<$maxnum; $i++) {
 7491:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 7492:                     my $item = $settings->{'inststatusorder'}->[$i];
 7493:                     my $title = $settings->{'inststatustypes'}->{$item};
 7494:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 7495:                     $datatable .= '<tr'.$css_class.'>'.
 7496:                                   '<td><span class="LC_nobreak">'.
 7497:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 7498:                     for (my $k=0; $k<=$maxnum; $k++) {
 7499:                         my $vpos = $k+1;
 7500:                         my $selstr;
 7501:                         if ($k == $i) {
 7502:                             $selstr = ' selected="selected" ';
 7503:                         }
 7504:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7505:                     }
 7506:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 7507:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 7508:                                   &mt('delete').'</span></td>'.
 7509:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 7510:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 7511:                                   '</span></td></tr>';
 7512:                 }
 7513:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 7514:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 7515:                 $datatable .= '<tr '.$css_class.'>'.
 7516:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 7517:                 for (my $k=0; $k<=$maxnum; $k++) {
 7518:                     my $vpos = $k+1;
 7519:                     my $selstr;
 7520:                     if ($k == $maxnum) {
 7521:                         $selstr = ' selected="selected" ';
 7522:                     }
 7523:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7524:                 }
 7525:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 7526:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 7527:                               '&nbsp;'.&mt('(new)').
 7528:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 7529:                               &mt('Name displayed:').
 7530:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 7531:                               '</tr>'."\n";
 7532:                 $rownum ++;
 7533:             }
 7534:         }
 7535:     }
 7536:     $$rowtotal += $rownum;
 7537:     return $datatable;
 7538: }
 7539: 
 7540: sub get_languages_hash {
 7541:     my %langchoices;
 7542:     foreach my $id (&Apache::loncommon::languageids()) {
 7543:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7544:         if ($code ne '') {
 7545:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7546:         }
 7547:     }
 7548:     return %langchoices;
 7549: }
 7550: 
 7551: sub defaults_titles {
 7552:     my ($dom) = @_;
 7553:     my %titles = &Apache::lonlocal::texthash (
 7554:                    'auth_def'      => 'Default authentication type',
 7555:                    'auth_arg_def'  => 'Default authentication argument',
 7556:                    'lang_def'      => 'Default language',
 7557:                    'timezone_def'  => 'Default timezone',
 7558:                    'datelocale_def' => 'Default locale for dates',
 7559:                    'portal_def'     => 'Portal/Default URL',
 7560:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7561:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7562:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7563:                  );
 7564:     if ($dom) {
 7565:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7566:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7567:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7568:         $protocol = 'http' if ($protocol ne 'https');
 7569:         if ($uint_dom) {
 7570:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7571:                                          $uint_dom);
 7572:         }
 7573:     }
 7574:     return (\%titles);
 7575: }
 7576: 
 7577: sub print_scantronformat {
 7578:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 7579:     my $itemcount = 1;
 7580:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 7581:         %confhash);
 7582:     my $switchserver = &check_switchserver($dom,$confname);
 7583:     my %lt = &Apache::lonlocal::texthash (
 7584:                 default => 'Default bubblesheet format file error',
 7585:                 custom  => 'Custom bubblesheet format file error',
 7586:              );
 7587:     my %scantronfiles = (
 7588:         default => 'default.tab',
 7589:         custom => 'custom.tab',
 7590:     );
 7591:     foreach my $key (keys(%scantronfiles)) {
 7592:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 7593:                               .$scantronfiles{$key};
 7594:     }
 7595:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 7596:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 7597:         if (!$switchserver) {
 7598:             my $servadm = $r->dir_config('lonAdmEMail');
 7599:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 7600:             if ($configuserok eq 'ok') {
 7601:                 if ($author_ok eq 'ok') {
 7602:                     my %legacyfile = (
 7603:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 7604:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 7605:                     );
 7606:                     my %md5chk;
 7607:                     foreach my $type (keys(%legacyfile)) {
 7608:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 7609:                         chomp($md5chk{$type});
 7610:                     }
 7611:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 7612:                         foreach my $type (keys(%legacyfile)) {
 7613:                             ($scantronurls{$type},my $error) = 
 7614:                                 &legacy_scantronformat($r,$dom,$confname,
 7615:                                                  $type,$legacyfile{$type},
 7616:                                                  $scantronurls{$type},
 7617:                                                  $scantronfiles{$type});
 7618:                             if ($error ne '') {
 7619:                                 $error{$type} = $error;
 7620:                             }
 7621:                         }
 7622:                         if (keys(%error) == 0) {
 7623:                             $is_custom = 1;
 7624:                             $confhash{'scantron'}{'scantronformat'} = 
 7625:                                 $scantronurls{'custom'};
 7626:                             my $putresult = 
 7627:                                 &Apache::lonnet::put_dom('configuration',
 7628:                                                          \%confhash,$dom);
 7629:                             if ($putresult ne 'ok') {
 7630:                                 $error{'custom'} = 
 7631:                                     '<span class="LC_error">'.
 7632:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7633:                             }
 7634:                         }
 7635:                     } else {
 7636:                         ($scantronurls{'default'},my $error) =
 7637:                             &legacy_scantronformat($r,$dom,$confname,
 7638:                                           'default',$legacyfile{'default'},
 7639:                                           $scantronurls{'default'},
 7640:                                           $scantronfiles{'default'});
 7641:                         if ($error eq '') {
 7642:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 7643:                             my $putresult =
 7644:                                 &Apache::lonnet::put_dom('configuration',
 7645:                                                          \%confhash,$dom);
 7646:                             if ($putresult ne 'ok') {
 7647:                                 $error{'default'} =
 7648:                                     '<span class="LC_error">'.
 7649:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7650:                             }
 7651:                         } else {
 7652:                             $error{'default'} = $error;
 7653:                         }
 7654:                     }
 7655:                 }
 7656:             }
 7657:         } else {
 7658:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 7659:         }
 7660:     }
 7661:     if (ref($settings) eq 'HASH') {
 7662:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 7663:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 7664:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 7665:                 $scantronurl = '';
 7666:             } else {
 7667:                 $scantronurl = $settings->{'scantronformat'};
 7668:             }
 7669:             $is_custom = 1;
 7670:         } else {
 7671:             $scantronurl = $scantronurls{'default'};
 7672:         }
 7673:     } else {
 7674:         if ($is_custom) {
 7675:             $scantronurl = $scantronurls{'custom'};
 7676:         } else {
 7677:             $scantronurl = $scantronurls{'default'};
 7678:         }
 7679:     }
 7680:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7681:     $datatable .= '<tr'.$css_class.'>';
 7682:     if (!$is_custom) {
 7683:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 7684:                       '<span class="LC_nobreak">';
 7685:         if ($scantronurl) {
 7686:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 7687:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 7688:         } else {
 7689:             $datatable = &mt('File unavailable for display');
 7690:         }
 7691:         $datatable .= '</span></td>';
 7692:         if (keys(%error) == 0) { 
 7693:             $datatable .= '<td style="vertical-align: bottom">';
 7694:             if (!$switchserver) {
 7695:                 $datatable .= &mt('Upload:').'<br />';
 7696:             }
 7697:         } else {
 7698:             my $errorstr;
 7699:             foreach my $key (sort(keys(%error))) {
 7700:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7701:             }
 7702:             $datatable .= '<td>'.$errorstr;
 7703:         }
 7704:     } else {
 7705:         if (keys(%error) > 0) {
 7706:             my $errorstr;
 7707:             foreach my $key (sort(keys(%error))) {
 7708:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7709:             } 
 7710:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 7711:         } elsif ($scantronurl) {
 7712:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 7713:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7714:             $datatable .= '<td><span class="LC_nobreak">'.
 7715:                           $link.
 7716:                           '<label><input type="checkbox" name="scantronformat_del"'.
 7717:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 7718:                           '<td><span class="LC_nobreak">&nbsp;'.
 7719:                           &mt('Replace:').'</span><br />';
 7720:         }
 7721:     }
 7722:     if (keys(%error) == 0) {
 7723:         if ($switchserver) {
 7724:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 7725:         } else {
 7726:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7727:                          '<input type="file" name="scantronformat" /></span>';
 7728:         }
 7729:     }
 7730:     $datatable .= '</td></tr>';
 7731:     $$rowtotal ++;
 7732:     return $datatable;
 7733: }
 7734: 
 7735: sub legacy_scantronformat {
 7736:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 7737:     my ($url,$error);
 7738:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 7739:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 7740:         (my $result,$url) =
 7741:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 7742:                          '','',$newfile);
 7743:         if ($result ne 'ok') {
 7744:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 7745:         }
 7746:     }
 7747:     return ($url,$error);
 7748: }
 7749: 
 7750: sub print_coursecategories {
 7751:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 7752:     my $datatable;
 7753:     if ($position eq 'top') {
 7754:         my (%checked);
 7755:         my @catitems = ('unauth','auth');
 7756:         my @cattypes = ('std','domonly','codesrch','none');
 7757:         $checked{'unauth'} = 'std';
 7758:         $checked{'auth'} = 'std';
 7759:         if (ref($settings) eq 'HASH') {
 7760:             foreach my $type (@cattypes) {
 7761:                 if ($type eq $settings->{'unauth'}) {
 7762:                     $checked{'unauth'} = $type;
 7763:                 }
 7764:                 if ($type eq $settings->{'auth'}) {
 7765:                     $checked{'auth'} = $type;
 7766:                 }
 7767:             }
 7768:         }
 7769:         my %lt = &Apache::lonlocal::texthash (
 7770:                                                unauth   => 'Catalog type for unauthenticated users',
 7771:                                                auth     => 'Catalog type for authenticated users',
 7772:                                                none     => 'No catalog',
 7773:                                                std      => 'Standard catalog',
 7774:                                                domonly  => 'Domain-only catalog',
 7775:                                                codesrch => "Code search form",
 7776:                                              );
 7777:        my $itemcount = 0;
 7778:        foreach my $item (@catitems) {
 7779:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7780:            $datatable .= '<tr '.$css_class.'>'.
 7781:                          '<td>'.$lt{$item}.'</td>'.
 7782:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 7783:            foreach my $type (@cattypes) {
 7784:                my $ischecked;
 7785:                if ($checked{$item} eq $type) {
 7786:                    $ischecked=' checked="checked"';
 7787:                }
 7788:                $datatable .= '<label>'.
 7789:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 7790:                              ' />'.$lt{$type}.'</label>&nbsp;';
 7791:            }
 7792:            $datatable .= '</td></tr>';
 7793:            $itemcount ++;
 7794:         }
 7795:         $$rowtotal += $itemcount;
 7796:     } elsif ($position eq 'middle') {
 7797:         my $toggle_cats_crs = ' ';
 7798:         my $toggle_cats_dom = ' checked="checked" ';
 7799:         my $can_cat_crs = ' ';
 7800:         my $can_cat_dom = ' checked="checked" ';
 7801:         my $toggle_catscomm_comm = ' ';
 7802:         my $toggle_catscomm_dom = ' checked="checked" ';
 7803:         my $can_catcomm_comm = ' ';
 7804:         my $can_catcomm_dom = ' checked="checked" ';
 7805:         my $toggle_catsplace_place = ' ';
 7806:         my $toggle_catsplace_dom = ' checked="checked" ';
 7807:         my $can_catplace_place = ' ';
 7808:         my $can_catplace_dom = ' checked="checked" ';
 7809: 
 7810:         if (ref($settings) eq 'HASH') {
 7811:             if ($settings->{'togglecats'} eq 'crs') {
 7812:                 $toggle_cats_crs = $toggle_cats_dom;
 7813:                 $toggle_cats_dom = ' ';
 7814:             }
 7815:             if ($settings->{'categorize'} eq 'crs') {
 7816:                 $can_cat_crs = $can_cat_dom;
 7817:                 $can_cat_dom = ' ';
 7818:             }
 7819:             if ($settings->{'togglecatscomm'} eq 'comm') {
 7820:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 7821:                 $toggle_catscomm_dom = ' ';
 7822:             }
 7823:             if ($settings->{'categorizecomm'} eq 'comm') {
 7824:                 $can_catcomm_comm = $can_catcomm_dom;
 7825:                 $can_catcomm_dom = ' ';
 7826:             }
 7827:             if ($settings->{'togglecatsplace'} eq 'place') {
 7828:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 7829:                 $toggle_catsplace_dom = ' ';
 7830:             }
 7831:             if ($settings->{'categorizeplace'} eq 'place') {
 7832:                 $can_catplace_place = $can_catplace_dom;
 7833:                 $can_catplace_dom = ' ';
 7834:             }
 7835:         }
 7836:         my %title = &Apache::lonlocal::texthash (
 7837:                      togglecats      => 'Show/Hide a course in catalog',
 7838:                      togglecatscomm  => 'Show/Hide a community in catalog',
 7839:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 7840:                      categorize      => 'Assign a category to a course',
 7841:                      categorizecomm  => 'Assign a category to a community',
 7842:                      categorizeplace => 'Assign a category to a placement test',
 7843:                     );
 7844:         my %level = &Apache::lonlocal::texthash (
 7845:                      dom   => 'Set in Domain',
 7846:                      crs   => 'Set in Course',
 7847:                      comm  => 'Set in Community',
 7848:                      place => 'Set in Placement Test',
 7849:                     );
 7850:         $datatable = '<tr class="LC_odd_row">'.
 7851:                   '<td>'.$title{'togglecats'}.'</td>'.
 7852:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7853:                   '<input type="radio" name="togglecats"'.
 7854:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7855:                   '<label><input type="radio" name="togglecats"'.
 7856:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7857:                   '</tr><tr>'.
 7858:                   '<td>'.$title{'categorize'}.'</td>'.
 7859:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7860:                   '<label><input type="radio" name="categorize"'.
 7861:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7862:                   '<label><input type="radio" name="categorize"'.
 7863:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7864:                   '</tr><tr class="LC_odd_row">'.
 7865:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7866:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7867:                   '<input type="radio" name="togglecatscomm"'.
 7868:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7869:                   '<label><input type="radio" name="togglecatscomm"'.
 7870:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7871:                   '</tr><tr>'.
 7872:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7873:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7874:                   '<label><input type="radio" name="categorizecomm"'.
 7875:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7876:                   '<label><input type="radio" name="categorizecomm"'.
 7877:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7878:                   '</tr><tr>'.
 7879:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 7880:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7881:                   '<input type="radio" name="togglecatsplace"'.
 7882:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7883:                   '<label><input type="radio" name="togglecatscomm"'.
 7884:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 7885:                   '</tr><tr>'.
 7886:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 7887:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7888:                   '<label><input type="radio" name="categorizeplace"'.
 7889:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7890:                   '<label><input type="radio" name="categorizeplace"'.
 7891:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 7892:                   '</tr>';
 7893:         $$rowtotal += 6;
 7894:     } else {
 7895:         my $css_class;
 7896:         my $itemcount = 1;
 7897:         my $cathash; 
 7898:         if (ref($settings) eq 'HASH') {
 7899:             $cathash = $settings->{'cats'};
 7900:         }
 7901:         if (ref($cathash) eq 'HASH') {
 7902:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7903:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7904:                                                    \%allitems,\%idx,\@jsarray);
 7905:             my $maxdepth = scalar(@cats);
 7906:             my $colattrib = '';
 7907:             if ($maxdepth > 2) {
 7908:                 $colattrib = ' colspan="2" ';
 7909:             }
 7910:             my @path;
 7911:             if (@cats > 0) {
 7912:                 if (ref($cats[0]) eq 'ARRAY') {
 7913:                     my $numtop = @{$cats[0]};
 7914:                     my $maxnum = $numtop;
 7915:                     my %default_names = (
 7916:                           instcode    => &mt('Official courses'),
 7917:                           communities => &mt('Communities'),
 7918:                           placement   => &mt('Placement Tests'),
 7919:                     );
 7920: 
 7921:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7922:                         ($cathash->{'instcode::0'} eq '') ||
 7923:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7924:                         ($cathash->{'communities::0'} eq '') ||
 7925:                         (!grep(/^placement$/,@{$cats[0]})) ||
 7926:                         ($cathash->{'placement::0'} eq '')) {
 7927:                         $maxnum ++;
 7928:                     }
 7929:                     my $lastidx;
 7930:                     for (my $i=0; $i<$numtop; $i++) {
 7931:                         my $parent = $cats[0][$i];
 7932:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7933:                         my $item = &escape($parent).'::0';
 7934:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7935:                         $lastidx = $idx{$item};
 7936:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7937:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7938:                         for (my $k=0; $k<=$maxnum; $k++) {
 7939:                             my $vpos = $k+1;
 7940:                             my $selstr;
 7941:                             if ($k == $i) {
 7942:                                 $selstr = ' selected="selected" ';
 7943:                             }
 7944:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7945:                         }
 7946:                         $datatable .= '</select></span></td><td>';
 7947:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 7948:                             $datatable .=  '<span class="LC_nobreak">'
 7949:                                            .$default_names{$parent}.'</span>';
 7950:                             if ($parent eq 'instcode') {
 7951:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7952:                                               .&mt('with institutional codes')
 7953:                                               .')</span></td><td'.$colattrib.'>';
 7954:                             } else {
 7955:                                 $datatable .= '<table><tr><td>';
 7956:                             }
 7957:                             $datatable .= '<span class="LC_nobreak">'
 7958:                                           .'<label><input type="radio" name="'
 7959:                                           .$parent.'" value="1" checked="checked" />'
 7960:                                           .&mt('Display').'</label>';
 7961:                             if ($parent eq 'instcode') {
 7962:                                 $datatable .= '&nbsp;';
 7963:                             } else {
 7964:                                 $datatable .= '</span></td></tr><tr><td>'
 7965:                                               .'<span class="LC_nobreak">';
 7966:                             }
 7967:                             $datatable .= '<label><input type="radio" name="'
 7968:                                           .$parent.'" value="0" />'
 7969:                                           .&mt('Do not display').'</label></span>';
 7970:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 7971:                                 $datatable .= '</td></tr></table>';
 7972:                             }
 7973:                             $datatable .= '</td>';
 7974:                         } else {
 7975:                             $datatable .= $parent
 7976:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7977:                                           .'<input type="checkbox" name="deletecategory" '
 7978:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7979:                         }
 7980:                         my $depth = 1;
 7981:                         push(@path,$parent);
 7982:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7983:                         pop(@path);
 7984:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7985:                         $itemcount ++;
 7986:                     }
 7987:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7988:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7989:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7990:                     for (my $k=0; $k<=$maxnum; $k++) {
 7991:                         my $vpos = $k+1;
 7992:                         my $selstr;
 7993:                         if ($k == $numtop) {
 7994:                             $selstr = ' selected="selected" ';
 7995:                         }
 7996:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7997:                     }
 7998:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7999:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 8000:                                   .'</tr>'."\n";
 8001:                     $itemcount ++;
 8002:                     foreach my $default ('instcode','communities','placement') {
 8003:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 8004:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8005:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 8006:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 8007:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 8008:                             for (my $k=0; $k<=$maxnum; $k++) {
 8009:                                 my $vpos = $k+1;
 8010:                                 my $selstr;
 8011:                                 if ($k == $maxnum) {
 8012:                                     $selstr = ' selected="selected" ';
 8013:                                 }
 8014:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8015:                             }
 8016:                             $datatable .= '</select></span></td>'.
 8017:                                           '<td><span class="LC_nobreak">'.
 8018:                                           $default_names{$default}.'</span>';
 8019:                             if ($default eq 'instcode') {
 8020:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 8021:                                               .&mt('with institutional codes').')</span>';
 8022:                             }
 8023:                             $datatable .= '</td>'
 8024:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 8025:                                           .&mt('Display').'</label>&nbsp;'
 8026:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 8027:                                           .&mt('Do not display').'</label></span></td></tr>';
 8028:                         }
 8029:                     }
 8030:                 }
 8031:             } else {
 8032:                 $datatable .= &initialize_categories($itemcount);
 8033:             }
 8034:         } else {
 8035:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 8036:                           .&initialize_categories($itemcount);
 8037:         }
 8038:         $$rowtotal += $itemcount;
 8039:     }
 8040:     return $datatable;
 8041: }
 8042: 
 8043: sub print_serverstatuses {
 8044:     my ($dom,$settings,$rowtotal) = @_;
 8045:     my $datatable;
 8046:     my @pages = &serverstatus_pages();
 8047:     my (%namedaccess,%machineaccess);
 8048:     foreach my $type (@pages) {
 8049:         $namedaccess{$type} = '';
 8050:         $machineaccess{$type}= '';
 8051:     }
 8052:     if (ref($settings) eq 'HASH') {
 8053:         foreach my $type (@pages) {
 8054:             if (exists($settings->{$type})) {
 8055:                 if (ref($settings->{$type}) eq 'HASH') {
 8056:                     foreach my $key (keys(%{$settings->{$type}})) {
 8057:                         if ($key eq 'namedusers') {
 8058:                             $namedaccess{$type} = $settings->{$type}->{$key};
 8059:                         } elsif ($key eq 'machines') {
 8060:                             $machineaccess{$type} = $settings->{$type}->{$key};
 8061:                         }
 8062:                     }
 8063:                 }
 8064:             }
 8065:         }
 8066:     }
 8067:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8068:     my $rownum = 0;
 8069:     my $css_class;
 8070:     foreach my $type (@pages) {
 8071:         $rownum ++;
 8072:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 8073:         $datatable .= '<tr'.$css_class.'>'.
 8074:                       '<td><span class="LC_nobreak">'.
 8075:                       $titles->{$type}.'</span></td>'.
 8076:                       '<td class="LC_left_item">'.
 8077:                       '<input type="text" name="'.$type.'_namedusers" '.
 8078:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 8079:                       '<td class="LC_right_item">'.
 8080:                       '<span class="LC_nobreak">'.
 8081:                       '<input type="text" name="'.$type.'_machines" '.
 8082:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 8083:                       '</td></tr>'."\n";
 8084:     }
 8085:     $$rowtotal += $rownum;
 8086:     return $datatable;
 8087: }
 8088: 
 8089: sub serverstatus_pages {
 8090:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 8091:             'checksums','clusterstatus','certstatus','metadata_keywords',
 8092:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 8093:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 8094: }
 8095: 
 8096: sub defaults_javascript {
 8097:     my ($settings) = @_;
 8098:     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.');
 8099:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 8100:     &js_escape(\$intauthcheck);
 8101:     &js_escape(\$intauthcost);
 8102:     my $intauthjs = <<"ENDSCRIPT";
 8103: 
 8104: function warnIntAuth(field) {
 8105:     if (field.name == 'intauth_check') {
 8106:         if (field.value == '2') {
 8107:             alert('$intauthcheck');
 8108:         }
 8109:     }
 8110:     if (field.name == 'intauth_cost') {
 8111:         field.value.replace(/\s/g,'');
 8112:         if (field.value != '') {
 8113:             var regexdigit=/^\\d+\$/;
 8114:             if (!regexdigit.test(field.value)) {
 8115:                 alert('$intauthcost');
 8116:             }
 8117:         }
 8118:     }
 8119:     return;
 8120: }
 8121: 
 8122: ENDSCRIPT
 8123: 
 8124:     if (ref($settings) ne 'HASH') {
 8125:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8126:     }
 8127:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 8128:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 8129:         if ($maxnum eq '') {
 8130:             $maxnum = 0;
 8131:         }
 8132:         $maxnum ++;
 8133:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 8134:         return <<"ENDSCRIPT";
 8135: <script type="text/javascript">
 8136: // <![CDATA[
 8137: function reorderTypes(form,caller) {
 8138:     var changedVal;
 8139: $jstext 
 8140:     var newpos = 'addinststatus_pos';
 8141:     var current = new Array;
 8142:     var maxh = $maxnum;
 8143:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8144:     var oldVal;
 8145:     if (caller == newpos) {
 8146:         changedVal = newitemVal;
 8147:     } else {
 8148:         var curritem = 'inststatus_pos_'+caller;
 8149:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 8150:         current[newitemVal] = newpos;
 8151:     }
 8152:     for (var i=0; i<inststatuses.length; i++) {
 8153:         if (inststatuses[i] != caller) {
 8154:             var elementName = 'inststatus_pos_'+inststatuses[i];
 8155:             if (form.elements[elementName]) {
 8156:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8157:                 current[currVal] = elementName;
 8158:             }
 8159:         }
 8160:     }
 8161:     for (var j=0; j<maxh; j++) {
 8162:         if (current[j] == undefined) {
 8163:             oldVal = j;
 8164:         }
 8165:     }
 8166:     if (oldVal < changedVal) {
 8167:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8168:            var elementName = current[k];
 8169:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8170:         }
 8171:     } else {
 8172:         for (var k=changedVal; k<oldVal; k++) {
 8173:             var elementName = current[k];
 8174:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8175:         }
 8176:     }
 8177:     return;
 8178: }
 8179: 
 8180: $intauthjs
 8181: 
 8182: // ]]>
 8183: </script>
 8184: 
 8185: ENDSCRIPT
 8186:     } else {
 8187:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8188:     }
 8189: }
 8190: 
 8191: sub coursecategories_javascript {
 8192:     my ($settings) = @_;
 8193:     my ($output,$jstext,$cathash);
 8194:     if (ref($settings) eq 'HASH') {
 8195:         $cathash = $settings->{'cats'};
 8196:     }
 8197:     if (ref($cathash) eq 'HASH') {
 8198:         my (@cats,@jsarray,%idx);
 8199:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 8200:         if (@jsarray > 0) {
 8201:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 8202:             for (my $i=0; $i<@jsarray; $i++) {
 8203:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 8204:                     my $catstr = join('","',@{$jsarray[$i]});
 8205:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 8206:                 }
 8207:             }
 8208:         }
 8209:     } else {
 8210:         $jstext  = '    var categories = Array(1);'."\n".
 8211:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 8212:     }
 8213:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 8214:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 8215:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 8216:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 8217:     &js_escape(\$instcode_reserved);
 8218:     &js_escape(\$communities_reserved);
 8219:     &js_escape(\$placement_reserved);
 8220:     &js_escape(\$choose_again);
 8221:     $output = <<"ENDSCRIPT";
 8222: <script type="text/javascript">
 8223: // <![CDATA[
 8224: function reorderCats(form,parent,item,idx) {
 8225:     var changedVal;
 8226: $jstext
 8227:     var newpos = 'addcategory_pos';
 8228:     if (parent == '') {
 8229:         var has_instcode = 0;
 8230:         var maxtop = categories[idx].length;
 8231:         for (var j=0; j<maxtop; j++) {
 8232:             if (categories[idx][j] == 'instcode::0') {
 8233:                 has_instcode == 1;
 8234:             }
 8235:         }
 8236:         if (has_instcode == 0) {
 8237:             categories[idx][maxtop] = 'instcode_pos';
 8238:         }
 8239:     } else {
 8240:         newpos += '_'+parent;
 8241:     }
 8242:     var maxh = 1 + categories[idx].length;
 8243:     var current = new Array;
 8244:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8245:     if (item == newpos) {
 8246:         changedVal = newitemVal;
 8247:     } else {
 8248:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 8249:         current[newitemVal] = newpos;
 8250:     }
 8251:     for (var i=0; i<categories[idx].length; i++) {
 8252:         var elementName = categories[idx][i];
 8253:         if (elementName != item) {
 8254:             if (form.elements[elementName]) {
 8255:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8256:                 current[currVal] = elementName;
 8257:             }
 8258:         }
 8259:     }
 8260:     var oldVal;
 8261:     for (var j=0; j<maxh; j++) {
 8262:         if (current[j] == undefined) {
 8263:             oldVal = j;
 8264:         }
 8265:     }
 8266:     if (oldVal < changedVal) {
 8267:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8268:            var elementName = current[k];
 8269:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8270:         }
 8271:     } else {
 8272:         for (var k=changedVal; k<oldVal; k++) {
 8273:             var elementName = current[k];
 8274:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8275:         }
 8276:     }
 8277:     return;
 8278: }
 8279: 
 8280: function categoryCheck(form) {
 8281:     if (form.elements['addcategory_name'].value == 'instcode') {
 8282:         alert('$instcode_reserved\\n$choose_again');
 8283:         return false;
 8284:     }
 8285:     if (form.elements['addcategory_name'].value == 'communities') {
 8286:         alert('$communities_reserved\\n$choose_again');
 8287:         return false;
 8288:     }
 8289:     if (form.elements['addcategory_name'].value == 'placement') {
 8290:         alert('$placement_reserved\\n$choose_again');
 8291:         return false;
 8292:     }
 8293:     return true;
 8294: }
 8295: 
 8296: // ]]>
 8297: </script>
 8298: 
 8299: ENDSCRIPT
 8300:     return $output;
 8301: }
 8302: 
 8303: sub initialize_categories {
 8304:     my ($itemcount) = @_;
 8305:     my ($datatable,$css_class,$chgstr);
 8306:     my %default_names = (
 8307:                       instcode    => 'Official courses (with institutional codes)',
 8308:                       communities => 'Communities',
 8309:                       placement   => 'Placement Tests',
 8310:                         );
 8311:     my $select0 = ' selected="selected"';
 8312:     my $select1 = '';
 8313:     foreach my $default ('instcode','communities','placement') {
 8314:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8315:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 8316:         if (($default eq 'communities') || ($default eq 'placement')) {
 8317:             $select1 = $select0;
 8318:             $select0 = '';
 8319:         }
 8320:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8321:                      .'<select name="'.$default.'_pos">'
 8322:                      .'<option value="0"'.$select0.'>1</option>'
 8323:                      .'<option value="1"'.$select1.'>2</option>'
 8324:                      .'<option value="2">3</option></select>&nbsp;'
 8325:                      .$default_names{$default}
 8326:                      .'</span></td><td><span class="LC_nobreak">'
 8327:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 8328:                      .&mt('Display').'</label>&nbsp;<label>'
 8329:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 8330:                  .'</label></span></td></tr>';
 8331:         $itemcount ++;
 8332:     }
 8333:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8334:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 8335:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8336:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 8337:                   .'<option value="0">1</option>'
 8338:                   .'<option value="1">2</option>'
 8339:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 8340:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 8341:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 8342:     return $datatable;
 8343: }
 8344: 
 8345: sub build_category_rows {
 8346:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 8347:     my ($text,$name,$item,$chgstr);
 8348:     if (ref($cats) eq 'ARRAY') {
 8349:         my $maxdepth = scalar(@{$cats});
 8350:         if (ref($cats->[$depth]) eq 'HASH') {
 8351:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8352:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8353:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8354:                 $text .= '<td><table class="LC_data_table">';
 8355:                 my ($idxnum,$parent_name,$parent_item);
 8356:                 my $higher = $depth - 1;
 8357:                 if ($higher == 0) {
 8358:                     $parent_name = &escape($parent).'::'.$higher;
 8359:                 } else {
 8360:                     if (ref($path) eq 'ARRAY') {
 8361:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8362:                     }
 8363:                 }
 8364:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8365:                 for (my $j=0; $j<=$numchildren; $j++) {
 8366:                     if ($j < $numchildren) {
 8367:                         $name = $cats->[$depth]{$parent}[$j];
 8368:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8369:                         $idxnum = $idx->{$item};
 8370:                     } else {
 8371:                         $name = $parent_name;
 8372:                         $item = $parent_item;
 8373:                     }
 8374:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8375:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8376:                     for (my $i=0; $i<=$numchildren; $i++) {
 8377:                         my $vpos = $i+1;
 8378:                         my $selstr;
 8379:                         if ($j == $i) {
 8380:                             $selstr = ' selected="selected" ';
 8381:                         }
 8382:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8383:                     }
 8384:                     $text .= '</select>&nbsp;';
 8385:                     if ($j < $numchildren) {
 8386:                         my $deeper = $depth+1;
 8387:                         $text .= $name.'&nbsp;'
 8388:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8389:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8390:                         if(ref($path) eq 'ARRAY') {
 8391:                             push(@{$path},$name);
 8392:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8393:                             pop(@{$path});
 8394:                         }
 8395:                     } else {
 8396:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 8397:                         if ($j == $numchildren) {
 8398:                             $text .= $name;
 8399:                         } else {
 8400:                             $text .= $item;
 8401:                         }
 8402:                         $text .= '" value="" />';
 8403:                     }
 8404:                     $text .= '</td></tr>';
 8405:                 }
 8406:                 $text .= '</table></td>';
 8407:             } else {
 8408:                 my $higher = $depth-1;
 8409:                 if ($higher == 0) {
 8410:                     $name = &escape($parent).'::'.$higher;
 8411:                 } else {
 8412:                     if (ref($path) eq 'ARRAY') {
 8413:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8414:                     }
 8415:                 }
 8416:                 my $colspan;
 8417:                 if ($parent ne 'instcode') {
 8418:                     $colspan = $maxdepth - $depth - 1;
 8419:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 8420:                 }
 8421:             }
 8422:         }
 8423:     }
 8424:     return $text;
 8425: }
 8426: 
 8427: sub modifiable_userdata_row {
 8428:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 8429:         $rowid,$customcss,$rowstyle) = @_;
 8430:     my ($role,$rolename,$statustype);
 8431:     $role = $item;
 8432:     if ($context eq 'cancreate') {
 8433:         if ($item =~ /^(emailusername)_(.+)$/) {
 8434:             $role = $1;
 8435:             $statustype = $2;
 8436:             if (ref($usertypes) eq 'HASH') {
 8437:                 if ($usertypes->{$statustype}) {
 8438:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 8439:                 } else {
 8440:                     $rolename = &mt('Data provided by user');
 8441:                 }
 8442:             }
 8443:         }
 8444:     } elsif ($context eq 'selfcreate') {
 8445:         if (ref($usertypes) eq 'HASH') {
 8446:             $rolename = $usertypes->{$role};
 8447:         } else {
 8448:             $rolename = $role;
 8449:         }
 8450:     } else {
 8451:         if ($role eq 'cr') {
 8452:             $rolename = &mt('Custom role');
 8453:         } else {
 8454:             $rolename = &Apache::lonnet::plaintext($role);
 8455:         }
 8456:     }
 8457:     my (@fields,%fieldtitles);
 8458:     if (ref($fieldsref) eq 'ARRAY') {
 8459:         @fields = @{$fieldsref};
 8460:     } else {
 8461:         @fields = ('lastname','firstname','middlename','generation',
 8462:                    'permanentemail','id');
 8463:     }
 8464:     if ((ref($titlesref) eq 'HASH')) {
 8465:         %fieldtitles = %{$titlesref};
 8466:     } else {
 8467:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8468:     }
 8469:     my $output;
 8470:     my $css_class;
 8471:     if ($rowcount%2) {
 8472:         $css_class = 'LC_odd_row';
 8473:     }
 8474:     if ($customcss) {
 8475:         $css_class .= " $customcss";
 8476:     }
 8477:     $css_class =~ s/^\s+//;
 8478:     if ($css_class) {
 8479:         $css_class = ' class="'.$css_class.'"';
 8480:     }
 8481:     if ($rowstyle) {
 8482:         $css_class .= ' style="'.$rowstyle.'"';
 8483:     }
 8484:     if ($rowid) {
 8485:         $rowid = ' id="'.$rowid.'"';
 8486:     }
 8487:     $output = '<tr '.$css_class.$rowid.'>'.
 8488:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 8489:               '<td class="LC_left_item" colspan="2"><table>';
 8490:     my $rem;
 8491:     my %checks;
 8492:     if (ref($settings) eq 'HASH') {
 8493:         if (ref($settings->{$context}) eq 'HASH') {
 8494:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 8495:                 my $hashref = $settings->{$context}->{$role};
 8496:                 if ($role eq 'emailusername') {
 8497:                     if ($statustype) {
 8498:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 8499:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 8500:                             if (ref($hashref) eq 'HASH') { 
 8501:                                 foreach my $field (@fields) {
 8502:                                     if ($hashref->{$field}) {
 8503:                                         $checks{$field} = $hashref->{$field};
 8504:                                     }
 8505:                                 }
 8506:                             }
 8507:                         }
 8508:                     }
 8509:                 } else {
 8510:                     if (ref($hashref) eq 'HASH') {
 8511:                         foreach my $field (@fields) {
 8512:                             if ($hashref->{$field}) {
 8513:                                 $checks{$field} = ' checked="checked" ';
 8514:                             }
 8515:                         }
 8516:                     }
 8517:                 }
 8518:             }
 8519:         }
 8520:     }
 8521:  
 8522:     my $total = scalar(@fields);
 8523:     for (my $i=0; $i<$total; $i++) {
 8524:         $rem = $i%($numinrow);
 8525:         if ($rem == 0) {
 8526:             if ($i > 0) {
 8527:                 $output .= '</tr>';
 8528:             }
 8529:             $output .= '<tr>';
 8530:         }
 8531:         my $check = ' ';
 8532:         unless ($role eq 'emailusername') {
 8533:             if (exists($checks{$fields[$i]})) {
 8534:                 $check = $checks{$fields[$i]}
 8535:             } else {
 8536:                 if ($role eq 'st') {
 8537:                     if (ref($settings) ne 'HASH') {
 8538:                         $check = ' checked="checked" '; 
 8539:                     }
 8540:                 }
 8541:             }
 8542:         }
 8543:         $output .= '<td class="LC_left_item">'.
 8544:                    '<span class="LC_nobreak">';
 8545:         if ($role eq 'emailusername') {
 8546:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 8547:                 $checks{$fields[$i]} = 'omit';
 8548:             }
 8549:             foreach my $option ('required','optional','omit') {
 8550:                 my $checked='';
 8551:                 if ($checks{$fields[$i]} eq $option) {
 8552:                     $checked='checked="checked" ';
 8553:                 }
 8554:                 $output .= '<label>'.
 8555:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 8556:                            &mt($option).'</label>'.('&nbsp;' x2);
 8557:             }
 8558:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 8559:         } else {
 8560:             $output .= '<label>'.
 8561:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 8562:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 8563:                        '</label>';
 8564:         }
 8565:         $output .= '</span></td>';
 8566:     }
 8567:     $rem = $total%$numinrow;
 8568:     my $colsleft;
 8569:     if ($rem) {
 8570:         $colsleft = $numinrow - $rem;
 8571:     }
 8572:     if ($colsleft > 1) {
 8573:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8574:                    '&nbsp;</td>';
 8575:     } elsif ($colsleft == 1) {
 8576:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8577:     }
 8578:     $output .= '</tr></table></td></tr>';
 8579:     return $output;
 8580: }
 8581: 
 8582: sub insttypes_row {
 8583:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 8584:         $customcss,$rowstyle) = @_;
 8585:     my %lt = &Apache::lonlocal::texthash (
 8586:                       cansearch => 'Users allowed to search',
 8587:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 8588:                       lockablenames => 'User preference to lock name',
 8589:                       selfassign    => 'Self-reportable affiliations',
 8590:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 8591:              );
 8592:     my $showdom;
 8593:     if ($context eq 'cansearch') {
 8594:         $showdom = ' ('.$dom.')';
 8595:     }
 8596:     my $class = 'LC_left_item';
 8597:     if ($context eq 'statustocreate') {
 8598:         $class = 'LC_right_item';
 8599:     }
 8600:     my $css_class;
 8601:     if ($$rowtotal%2) {
 8602:         $css_class = 'LC_odd_row';
 8603:     }
 8604:     if ($customcss) {
 8605:         $css_class .= ' '.$customcss;
 8606:     }
 8607:     $css_class =~ s/^\s+//;
 8608:     if ($css_class) {
 8609:         $css_class = ' class="'.$css_class.'"';
 8610:     }
 8611:     if ($rowstyle) {
 8612:         $css_class .= ' style="'.$rowstyle.'"';
 8613:     }
 8614:     if ($onclick) {
 8615:         $onclick = 'onclick="'.$onclick.'" ';
 8616:     }
 8617:     my $output = '<tr'.$css_class.'>'.
 8618:                  '<td>'.$lt{$context}.$showdom.
 8619:                  '</td><td class="'.$class.'" colspan="2"><table>';
 8620:     my $rem;
 8621:     if (ref($types) eq 'ARRAY') {
 8622:         for (my $i=0; $i<@{$types}; $i++) {
 8623:             if (defined($usertypes->{$types->[$i]})) {
 8624:                 my $rem = $i%($numinrow);
 8625:                 if ($rem == 0) {
 8626:                     if ($i > 0) {
 8627:                         $output .= '</tr>';
 8628:                     }
 8629:                     $output .= '<tr>';
 8630:                 }
 8631:                 my $check = ' ';
 8632:                 if (ref($settings) eq 'HASH') {
 8633:                     if (ref($settings->{$context}) eq 'ARRAY') {
 8634:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 8635:                             $check = ' checked="checked" ';
 8636:                         }
 8637:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 8638:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 8639:                             $check = ' checked="checked" ';
 8640:                         }
 8641:                     } elsif ($context eq 'statustocreate') {
 8642:                         $check = ' checked="checked" ';
 8643:                     }
 8644:                 }
 8645:                 $output .= '<td class="LC_left_item">'.
 8646:                            '<span class="LC_nobreak"><label>'.
 8647:                            '<input type="checkbox" name="'.$context.'" '.
 8648:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 8649:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 8650:             }
 8651:         }
 8652:         $rem = @{$types}%($numinrow);
 8653:     }
 8654:     my $colsleft = $numinrow - $rem;
 8655:     if ($context eq 'overrides') {
 8656:         if ($colsleft > 1) {
 8657:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8658:         } else {
 8659:             $output .= '<td class="LC_left_item">';
 8660:         }
 8661:         $output .= '&nbsp;';  
 8662:     } else {
 8663:         if (($rem == 0) && (@{$types} > 0)) {
 8664:             $output .= '<tr>';
 8665:         }
 8666:         if ($colsleft > 1) {
 8667:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8668:         } else {
 8669:             $output .= '<td class="LC_left_item">';
 8670:         }
 8671:         my $defcheck = ' ';
 8672:         if (ref($settings) eq 'HASH') {  
 8673:             if (ref($settings->{$context}) eq 'ARRAY') {
 8674:                 if (grep(/^default$/,@{$settings->{$context}})) {
 8675:                     $defcheck = ' checked="checked" ';
 8676:                 }
 8677:             } elsif ($context eq 'statustocreate') {
 8678:                 $defcheck = ' checked="checked" ';
 8679:             }
 8680:         }
 8681:         $output .= '<span class="LC_nobreak"><label>'.
 8682:                    '<input type="checkbox" name="'.$context.'" '.
 8683:                    'value="default"'.$defcheck.$onclick.' />'.
 8684:                    $othertitle.'</label></span>';
 8685:     }
 8686:     $output .= '</td></tr></table></td></tr>';
 8687:     return $output;
 8688: }
 8689: 
 8690: sub sorted_searchtitles {
 8691:     my %searchtitles = &Apache::lonlocal::texthash(
 8692:                          'uname' => 'username',
 8693:                          'lastname' => 'last name',
 8694:                          'lastfirst' => 'last name, first name',
 8695:                      );
 8696:     my @titleorder = ('uname','lastname','lastfirst');
 8697:     return (\%searchtitles,\@titleorder);
 8698: }
 8699: 
 8700: sub sorted_searchtypes {
 8701:     my %srchtypes_desc = (
 8702:                            exact    => 'is exact match',
 8703:                            contains => 'contains ..',
 8704:                            begins   => 'begins with ..',
 8705:                          );
 8706:     my @srchtypeorder = ('exact','begins','contains');
 8707:     return (\%srchtypes_desc,\@srchtypeorder);
 8708: }
 8709: 
 8710: sub usertype_update_row {
 8711:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 8712:     my $datatable;
 8713:     my $numinrow = 4;
 8714:     foreach my $type (@{$types}) {
 8715:         if (defined($usertypes->{$type})) {
 8716:             $$rownums ++;
 8717:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 8718:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 8719:                           '</td><td class="LC_left_item"><table>';
 8720:             for (my $i=0; $i<@{$fields}; $i++) {
 8721:                 my $rem = $i%($numinrow);
 8722:                 if ($rem == 0) {
 8723:                     if ($i > 0) {
 8724:                         $datatable .= '</tr>';
 8725:                     }
 8726:                     $datatable .= '<tr>';
 8727:                 }
 8728:                 my $check = ' ';
 8729:                 if (ref($settings) eq 'HASH') {
 8730:                     if (ref($settings->{'fields'}) eq 'HASH') {
 8731:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 8732:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 8733:                                 $check = ' checked="checked" ';
 8734:                             }
 8735:                         }
 8736:                     }
 8737:                 }
 8738: 
 8739:                 if ($i == @{$fields}-1) {
 8740:                     my $colsleft = $numinrow - $rem;
 8741:                     if ($colsleft > 1) {
 8742:                         $datatable .= '<td colspan="'.$colsleft.'">';
 8743:                     } else {
 8744:                         $datatable .= '<td>';
 8745:                     }
 8746:                 } else {
 8747:                     $datatable .= '<td>';
 8748:                 }
 8749:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8750:                               '<input type="checkbox" name="updateable_'.$type.
 8751:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 8752:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 8753:             }
 8754:             $datatable .= '</tr></table></td></tr>';
 8755:         }
 8756:     }
 8757:     return $datatable;
 8758: }
 8759: 
 8760: sub modify_login {
 8761:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8762:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 8763:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 8764:     %title = ( coursecatalog => 'Display course catalog',
 8765:                adminmail => 'Display administrator E-mail address',
 8766:                helpdesk  => 'Display "Contact Helpdesk" link',
 8767:                newuser => 'Link for visitors to create a user account',
 8768:                loginheader => 'Log-in box header');
 8769:     @offon = ('off','on');
 8770:     if (ref($domconfig{login}) eq 'HASH') {
 8771:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 8772:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 8773:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 8774:             }
 8775:         }
 8776:     }
 8777:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 8778:                                            \%domconfig,\%loginhash);
 8779:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8780:     foreach my $item (@toggles) {
 8781:         $loginhash{login}{$item} = $env{'form.'.$item};
 8782:     }
 8783:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 8784:     if (ref($colchanges{'login'}) eq 'HASH') {  
 8785:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 8786:                                          \%loginhash);
 8787:     }
 8788: 
 8789:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8790:     my %domservers = &Apache::lonnet::get_servers($dom);
 8791:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 8792:     if (keys(%servers) > 1) {
 8793:         foreach my $lonhost (keys(%servers)) {
 8794:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 8795:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 8796:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 8797:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 8798:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 8799:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8800:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8801:                         $changes{'loginvia'}{$lonhost} = 1;
 8802:                     } else {
 8803:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 8804:                         $changes{'loginvia'}{$lonhost} = 1;
 8805:                     }
 8806:                 } else {
 8807:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8808:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8809:                         $changes{'loginvia'}{$lonhost} = 1;
 8810:                     }
 8811:                 }
 8812:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 8813:                     foreach my $item (@loginvia_attribs) {
 8814:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 8815:                     }
 8816:                 } else {
 8817:                     foreach my $item (@loginvia_attribs) {
 8818:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8819:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8820:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 8821:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8822:                                 $new = '/';
 8823:                             }
 8824:                         }
 8825:                         if (($item eq 'custompath') && 
 8826:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8827:                             $new = '';
 8828:                         }
 8829:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8830:                             $changes{'loginvia'}{$lonhost} = 1;
 8831:                         }
 8832:                         if ($item eq 'exempt') {
 8833:                             $new = &check_exempt_addresses($new);
 8834:                         }
 8835:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8836:                     }
 8837:                 }
 8838:             } else {
 8839:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8840:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8841:                     $changes{'loginvia'}{$lonhost} = 1;
 8842:                     foreach my $item (@loginvia_attribs) {
 8843:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8844:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8845:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8846:                                 $new = '/';
 8847:                             }
 8848:                         }
 8849:                         if (($item eq 'custompath') && 
 8850:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8851:                             $new = '';
 8852:                         }
 8853:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8854:                     }
 8855:                 }
 8856:             }
 8857:         }
 8858:     }
 8859: 
 8860:     my $servadm = $r->dir_config('lonAdmEMail');
 8861:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8862:     if (ref($domconfig{'login'}) eq 'HASH') {
 8863:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8864:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8865:                 if ($lang eq 'nolang') {
 8866:                     push(@currlangs,$lang);
 8867:                 } elsif (defined($langchoices{$lang})) {
 8868:                     push(@currlangs,$lang);
 8869:                 } else {
 8870:                     next;
 8871:                 }
 8872:             }
 8873:         }
 8874:     }
 8875:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8876:     if (@currlangs > 0) {
 8877:         foreach my $lang (@currlangs) {
 8878:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8879:                 $changes{'helpurl'}{$lang} = 1;
 8880:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8881:                 $changes{'helpurl'}{$lang} = 1;
 8882:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8883:                 push(@newlangs,$lang);
 8884:             } else {
 8885:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8886:             }
 8887:         }
 8888:     }
 8889:     unless (grep(/^nolang$/,@currlangs)) {
 8890:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8891:             $changes{'helpurl'}{'nolang'} = 1;
 8892:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8893:             push(@newlangs,'nolang');
 8894:         }
 8895:     }
 8896:     if ($env{'form.loginhelpurl_add_lang'}) {
 8897:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8898:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8899:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8900:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8901:         }
 8902:     }
 8903:     if ((@newlangs > 0) || ($addedfile)) {
 8904:         my $error;
 8905:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8906:         if ($configuserok eq 'ok') {
 8907:             if ($switchserver) {
 8908:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8909:             } elsif ($author_ok eq 'ok') {
 8910:                 my @allnew = @newlangs;
 8911:                 if ($addedfile ne '') {
 8912:                     push(@allnew,$addedfile);
 8913:                 }
 8914:                 foreach my $lang (@allnew) {
 8915:                     my $formelem = 'loginhelpurl_'.$lang;
 8916:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8917:                         $formelem = 'loginhelpurl_add_file';
 8918:                     }
 8919:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8920:                                                                "help/$lang",'','',$newfile{$lang});
 8921:                     if ($result eq 'ok') {
 8922:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8923:                         $changes{'helpurl'}{$lang} = 1;
 8924:                     } else {
 8925:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8926:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8927:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8928:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8929:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8930:                         }
 8931:                     }
 8932:                 }
 8933:             } else {
 8934:                 $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);
 8935:             }
 8936:         } else {
 8937:             $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);
 8938:         }
 8939:         if ($error) {
 8940:             &Apache::lonnet::logthis($error);
 8941:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8942:         }
 8943:     }
 8944: 
 8945:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8946:     if (ref($domconfig{'login'}) eq 'HASH') {
 8947:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8948:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8949:                 if ($domservers{$lonhost}) {
 8950:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8951:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8952:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8953:                     }
 8954:                 }
 8955:             }
 8956:         }
 8957:     }
 8958:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8959:     foreach my $lonhost (sort(keys(%domservers))) {
 8960:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8961:             $changes{'headtag'}{$lonhost} = 1;
 8962:         } else {
 8963:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8964:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8965:             }
 8966:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8967:                 push(@newhosts,$lonhost);
 8968:             } elsif ($currheadtagurls{$lonhost}) {
 8969:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8970:                 if ($currexempt{$lonhost}) {
 8971:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8972:                         $changes{'headtag'}{$lonhost} = 1;
 8973:                     }
 8974:                 } elsif ($possexempt{$lonhost}) {
 8975:                     $changes{'headtag'}{$lonhost} = 1;
 8976:                 }
 8977:                 if ($possexempt{$lonhost}) {
 8978:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8979:                 }
 8980:             }
 8981:         }
 8982:     }
 8983:     if (@newhosts) {
 8984:         my $error;
 8985:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8986:         if ($configuserok eq 'ok') {
 8987:             if ($switchserver) {
 8988:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8989:             } elsif ($author_ok eq 'ok') {
 8990:                 foreach my $lonhost (@newhosts) {
 8991:                     my $formelem = 'loginheadtag_'.$lonhost;
 8992:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8993:                                                                           "login/headtag/$lonhost",'','',
 8994:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8995:                     if ($result eq 'ok') {
 8996:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8997:                         $changes{'headtag'}{$lonhost} = 1;
 8998:                         if ($possexempt{$lonhost}) {
 8999:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9000:                         }
 9001:                     } else {
 9002:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 9003:                                            $newheadtagurls{$lonhost},$result);
 9004:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9005:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 9006:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 9007:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 9008:                         }
 9009:                     }
 9010:                 }
 9011:             } else {
 9012:                 $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);
 9013:             }
 9014:         } else {
 9015:             $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);
 9016:         }
 9017:         if ($error) {
 9018:             &Apache::lonnet::logthis($error);
 9019:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9020:         }
 9021:     }
 9022:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 9023: 
 9024:     my $defaulthelpfile = '/adm/loginproblems.html';
 9025:     my $defaulttext = &mt('Default in use');
 9026: 
 9027:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 9028:                                              $dom);
 9029:     if ($putresult eq 'ok') {
 9030:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9031:         my %defaultchecked = (
 9032:                     'coursecatalog' => 'on',
 9033:                     'helpdesk'      => 'on',
 9034:                     'adminmail'     => 'off',
 9035:                     'newuser'       => 'off',
 9036:         );
 9037:         if (ref($domconfig{'login'}) eq 'HASH') {
 9038:             foreach my $item (@toggles) {
 9039:                 if ($defaultchecked{$item} eq 'on') { 
 9040:                     if (($domconfig{'login'}{$item} eq '0') &&
 9041:                         ($env{'form.'.$item} eq '1')) {
 9042:                         $changes{$item} = 1;
 9043:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9044:                               $domconfig{'login'}{$item} eq '1') &&
 9045:                              ($env{'form.'.$item} eq '0')) {
 9046:                         $changes{$item} = 1;
 9047:                     }
 9048:                 } elsif ($defaultchecked{$item} eq 'off') {
 9049:                     if (($domconfig{'login'}{$item} eq '1') &&
 9050:                         ($env{'form.'.$item} eq '0')) {
 9051:                         $changes{$item} = 1;
 9052:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9053:                               $domconfig{'login'}{$item} eq '0') &&
 9054:                              ($env{'form.'.$item} eq '1')) {
 9055:                         $changes{$item} = 1;
 9056:                     }
 9057:                 }
 9058:             }
 9059:         }
 9060:         if (keys(%changes) > 0 || $colchgtext) {
 9061:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9062:             if (ref($lastactref) eq 'HASH') {
 9063:                 $lastactref->{'domainconfig'} = 1;
 9064:             }
 9065:             $resulttext = &mt('Changes made:').'<ul>';
 9066:             foreach my $item (sort(keys(%changes))) {
 9067:                 if ($item eq 'loginvia') {
 9068:                     if (ref($changes{$item}) eq 'HASH') {
 9069:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 9070:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9071:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 9072:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 9073:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 9074:                                     $protocol = 'http' if ($protocol ne 'https');
 9075:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 9076: 
 9077:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 9078:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 9079:                                     } else {
 9080:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 9081:                                     }
 9082:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 9083:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 9084:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 9085:                                     }
 9086:                                     $resulttext .= '</li>';
 9087:                                 } else {
 9088:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 9089:                                 }
 9090:                             } else {
 9091:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 9092:                             }
 9093:                         }
 9094:                         $resulttext .= '</ul></li>';
 9095:                     }
 9096:                 } elsif ($item eq 'helpurl') {
 9097:                     if (ref($changes{$item}) eq 'HASH') {
 9098:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 9099:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 9100:                                 my ($chg,$link);
 9101:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 9102:                                 if ($lang eq 'nolang') {
 9103:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 9104:                                 } else {
 9105:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 9106:                                 }
 9107:                                 $resulttext .= '<li>'.$chg.'</li>';
 9108:                             } else {
 9109:                                 my $chg;
 9110:                                 if ($lang eq 'nolang') {
 9111:                                     $chg = &mt('custom log-in help file for no preferred language');
 9112:                                 } else {
 9113:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 9114:                                 }
 9115:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 9116:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 9117:                                                       '?inhibitmenu=yes',$chg,600,500).
 9118:                                                '</li>';
 9119:                             }
 9120:                         }
 9121:                     }
 9122:                 } elsif ($item eq 'headtag') {
 9123:                     if (ref($changes{$item}) eq 'HASH') {
 9124:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9125:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9126:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 9127:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9128:                                 $resulttext .= '<li><a href="'.
 9129:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 9130:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 9131:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 9132:                                 if ($possexempt{$lonhost}) {
 9133:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 9134:                                 } else {
 9135:                                     $resulttext .= &mt('included for any client IP');
 9136:                                 }
 9137:                                 $resulttext .= '</li>';
 9138:                             }
 9139:                         }
 9140:                     }
 9141:                 } elsif ($item eq 'captcha') {
 9142:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9143:                         my $chgtxt;
 9144:                         if ($loginhash{'login'}{$item} eq 'notused') {
 9145:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 9146:                         } else {
 9147:                             my %captchas = &captcha_phrases();
 9148:                             if ($captchas{$loginhash{'login'}{$item}}) {
 9149:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 9150:                             } else {
 9151:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 9152:                             }
 9153:                         }
 9154:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9155:                     }
 9156:                 } elsif ($item eq 'recaptchakeys') {
 9157:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9158:                         my ($privkey,$pubkey);
 9159:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 9160:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 9161:                             $privkey = $loginhash{'login'}{$item}{'private'};
 9162:                         }
 9163:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 9164:                         if (!$pubkey) {
 9165:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 9166:                         } else {
 9167:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9168:                         }
 9169:                         if (!$privkey) {
 9170:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 9171:                         } else {
 9172:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 9173:                         }
 9174:                         $chgtxt .= '</ul>';
 9175:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9176:                     }
 9177:                 } elsif ($item eq 'recaptchaversion') {
 9178:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9179:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 9180:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 9181:                                            '</li>';
 9182:                         }
 9183:                     }
 9184:                 } else {
 9185:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 9186:                 }
 9187:             }
 9188:             $resulttext .= $colchgtext.'</ul>';
 9189:         } else {
 9190:             $resulttext = &mt('No changes made to log-in page settings');
 9191:         }
 9192:     } else {
 9193:         $resulttext = '<span class="LC_error">'.
 9194: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9195:     }
 9196:     if ($errors) {
 9197:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9198:                        $errors.'</ul>';
 9199:     }
 9200:     return $resulttext;
 9201: }
 9202: 
 9203: sub check_exempt_addresses {
 9204:     my ($iplist) = @_;
 9205:     $iplist =~ s/^\s+//;
 9206:     $iplist =~ s/\s+$//;
 9207:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 9208:     my (@okips,$new);
 9209:     foreach my $ip (@poss_ips) {
 9210:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 9211:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 9212:                 push(@okips,$ip);
 9213:             }
 9214:         }
 9215:     }
 9216:     if (@okips > 0) {
 9217:         $new = join(',',@okips);
 9218:     } else {
 9219:         $new = '';
 9220:     }
 9221:     return $new;
 9222: }
 9223: 
 9224: sub color_font_choices {
 9225:     my %choices =
 9226:         &Apache::lonlocal::texthash (
 9227:             img => "Header",
 9228:             bgs => "Background colors",
 9229:             links => "Link colors",
 9230:             images => "Images",
 9231:             font => "Font color",
 9232:             fontmenu => "Font menu",
 9233:             pgbg => "Page",
 9234:             tabbg => "Header",
 9235:             sidebg => "Border",
 9236:             link => "Link",
 9237:             alink => "Active link",
 9238:             vlink => "Visited link",
 9239:         );
 9240:     return %choices;
 9241: }
 9242: 
 9243: sub modify_rolecolors {
 9244:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 9245:     my ($resulttext,%rolehash);
 9246:     $rolehash{'rolecolors'} = {};
 9247:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 9248:         if ($domconfig{'rolecolors'} eq '') {
 9249:             $domconfig{'rolecolors'} = {};
 9250:         }
 9251:     }
 9252:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 9253:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 9254:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 9255:                                              $dom);
 9256:     if ($putresult eq 'ok') {
 9257:         if (keys(%changes) > 0) {
 9258:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9259:             if (ref($lastactref) eq 'HASH') {
 9260:                 $lastactref->{'domainconfig'} = 1;
 9261:             }
 9262:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 9263:                                              $rolehash{'rolecolors'});
 9264:         } else {
 9265:             $resulttext = &mt('No changes made to default color schemes');
 9266:         }
 9267:     } else {
 9268:         $resulttext = '<span class="LC_error">'.
 9269: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9270:     }
 9271:     if ($errors) {
 9272:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9273:                        $errors.'</ul>';
 9274:     }
 9275:     return $resulttext;
 9276: }
 9277: 
 9278: sub modify_colors {
 9279:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 9280:     my (%changes,%choices);
 9281:     my @bgs;
 9282:     my @links = ('link','alink','vlink');
 9283:     my @logintext;
 9284:     my @images;
 9285:     my $servadm = $r->dir_config('lonAdmEMail');
 9286:     my $errors;
 9287:     my %defaults;
 9288:     foreach my $role (@{$roles}) {
 9289:         if ($role eq 'login') {
 9290:             %choices = &login_choices();
 9291:             @logintext = ('textcol','bgcol');
 9292:         } else {
 9293:             %choices = &color_font_choices();
 9294:         }
 9295:         if ($role eq 'login') {
 9296:             @images = ('img','logo','domlogo','login');
 9297:             @bgs = ('pgbg','mainbg','sidebg');
 9298:         } else {
 9299:             @images = ('img');
 9300:             @bgs = ('pgbg','tabbg','sidebg');
 9301:         }
 9302:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 9303:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 9304:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 9305:         }
 9306:         if ($role eq 'login') {
 9307:             foreach my $item (@logintext) {
 9308:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9309:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9310:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9311:                 }
 9312:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 9313:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9314:                 }
 9315:             }
 9316:         } else {
 9317:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 9318:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 9319:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 9320:             }
 9321:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 9322:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 9323:             }
 9324:         }
 9325:         foreach my $item (@bgs) {
 9326:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9327:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9328:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9329:             }
 9330:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 9331:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9332:             }
 9333:         }
 9334:         foreach my $item (@links) {
 9335:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9336:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9337:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9338:             }
 9339:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 9340:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9341:             }
 9342:         }
 9343:         my ($configuserok,$author_ok,$switchserver) = 
 9344:             &config_check($dom,$confname,$servadm);
 9345:         my ($width,$height) = &thumb_dimensions();
 9346:         if (ref($domconfig->{$role}) ne 'HASH') {
 9347:             $domconfig->{$role} = {};
 9348:         }
 9349:         foreach my $img (@images) {
 9350:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 9351:                 if (defined($env{'form.login_showlogo_'.$img})) {
 9352:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 9353:                 } else { 
 9354:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 9355:                 }
 9356:             } 
 9357: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 9358: 		 && !defined($domconfig->{$role}{$img})
 9359: 		 && !$env{'form.'.$role.'_del_'.$img}
 9360: 		 && $env{'form.'.$role.'_import_'.$img}) {
 9361: 		# import the old configured image from the .tab setting
 9362: 		# if they haven't provided a new one 
 9363: 		$domconfig->{$role}{$img} = 
 9364: 		    $env{'form.'.$role.'_import_'.$img};
 9365: 	    }
 9366:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 9367:                 my $error;
 9368:                 if ($configuserok eq 'ok') {
 9369:                     if ($switchserver) {
 9370:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 9371:                     } else {
 9372:                         if ($author_ok eq 'ok') {
 9373:                             my ($result,$logourl) = 
 9374:                                 &publishlogo($r,'upload',$role.'_'.$img,
 9375:                                            $dom,$confname,$img,$width,$height);
 9376:                             if ($result eq 'ok') {
 9377:                                 $confhash->{$role}{$img} = $logourl;
 9378:                                 $changes{$role}{'images'}{$img} = 1;
 9379:                             } else {
 9380:                                 $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);
 9381:                             }
 9382:                         } else {
 9383:                             $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);
 9384:                         }
 9385:                     }
 9386:                 } else {
 9387:                     $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);
 9388:                 }
 9389:                 if ($error) {
 9390:                     &Apache::lonnet::logthis($error);
 9391:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9392:                 }
 9393:             } elsif ($domconfig->{$role}{$img} ne '') {
 9394:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 9395:                     my $error;
 9396:                     if ($configuserok eq 'ok') {
 9397: # is confname an author?
 9398:                         if ($switchserver eq '') {
 9399:                             if ($author_ok eq 'ok') {
 9400:                                 my ($result,$logourl) = 
 9401:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 9402:                                             $dom,$confname,$img,$width,$height);
 9403:                                 if ($result eq 'ok') {
 9404:                                     $confhash->{$role}{$img} = $logourl;
 9405: 				    $changes{$role}{'images'}{$img} = 1;
 9406:                                 }
 9407:                             }
 9408:                         }
 9409:                     }
 9410:                 }
 9411:             }
 9412:         }
 9413:         if (ref($domconfig) eq 'HASH') {
 9414:             if (ref($domconfig->{$role}) eq 'HASH') {
 9415:                 foreach my $img (@images) {
 9416:                     if ($domconfig->{$role}{$img} ne '') {
 9417:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9418:                             $confhash->{$role}{$img} = '';
 9419:                             $changes{$role}{'images'}{$img} = 1;
 9420:                         } else {
 9421:                             if ($confhash->{$role}{$img} eq '') {
 9422:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 9423:                             }
 9424:                         }
 9425:                     } else {
 9426:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9427:                             $confhash->{$role}{$img} = '';
 9428:                             $changes{$role}{'images'}{$img} = 1;
 9429:                         } 
 9430:                     }
 9431:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 9432:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 9433:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 9434:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 9435:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 9436:                             }
 9437:                         } else {
 9438:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9439:                                 $changes{$role}{'showlogo'}{$img} = 1;
 9440:                             }
 9441:                         }
 9442:                     }
 9443:                 }
 9444:                 if ($domconfig->{$role}{'font'} ne '') {
 9445:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 9446:                         $changes{$role}{'font'} = 1;
 9447:                     }
 9448:                 } else {
 9449:                     if ($confhash->{$role}{'font'}) {
 9450:                         $changes{$role}{'font'} = 1;
 9451:                     }
 9452:                 }
 9453:                 if ($role ne 'login') {
 9454:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 9455:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 9456:                             $changes{$role}{'fontmenu'} = 1;
 9457:                         }
 9458:                     } else {
 9459:                         if ($confhash->{$role}{'fontmenu'}) {
 9460:                             $changes{$role}{'fontmenu'} = 1;
 9461:                         }
 9462:                     }
 9463:                 }
 9464:                 foreach my $item (@bgs) {
 9465:                     if ($domconfig->{$role}{$item} ne '') {
 9466:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9467:                             $changes{$role}{'bgs'}{$item} = 1;
 9468:                         } 
 9469:                     } else {
 9470:                         if ($confhash->{$role}{$item}) {
 9471:                             $changes{$role}{'bgs'}{$item} = 1;
 9472:                         }
 9473:                     }
 9474:                 }
 9475:                 foreach my $item (@links) {
 9476:                     if ($domconfig->{$role}{$item} ne '') {
 9477:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9478:                             $changes{$role}{'links'}{$item} = 1;
 9479:                         }
 9480:                     } else {
 9481:                         if ($confhash->{$role}{$item}) {
 9482:                             $changes{$role}{'links'}{$item} = 1;
 9483:                         }
 9484:                     }
 9485:                 }
 9486:                 foreach my $item (@logintext) {
 9487:                     if ($domconfig->{$role}{$item} ne '') {
 9488:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9489:                             $changes{$role}{'logintext'}{$item} = 1;
 9490:                         }
 9491:                     } else {
 9492:                         if ($confhash->{$role}{$item}) {
 9493:                             $changes{$role}{'logintext'}{$item} = 1;
 9494:                         }
 9495:                     }
 9496:                 }
 9497:             } else {
 9498:                 &default_change_checker($role,\@images,\@links,\@bgs,
 9499:                                         \@logintext,$confhash,\%changes); 
 9500:             }
 9501:         } else {
 9502:             &default_change_checker($role,\@images,\@links,\@bgs,
 9503:                                     \@logintext,$confhash,\%changes); 
 9504:         }
 9505:     }
 9506:     return ($errors,%changes);
 9507: }
 9508: 
 9509: sub config_check {
 9510:     my ($dom,$confname,$servadm) = @_;
 9511:     my ($configuserok,$author_ok,$switchserver,%currroles);
 9512:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 9513:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 9514:                                                    $confname,$servadm);
 9515:     if ($configuserok eq 'ok') {
 9516:         $switchserver = &check_switchserver($dom,$confname);
 9517:         if ($switchserver eq '') {
 9518:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 9519:         }
 9520:     }
 9521:     return ($configuserok,$author_ok,$switchserver);
 9522: }
 9523: 
 9524: sub default_change_checker {
 9525:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 9526:     foreach my $item (@{$links}) {
 9527:         if ($confhash->{$role}{$item}) {
 9528:             $changes->{$role}{'links'}{$item} = 1;
 9529:         }
 9530:     }
 9531:     foreach my $item (@{$bgs}) {
 9532:         if ($confhash->{$role}{$item}) {
 9533:             $changes->{$role}{'bgs'}{$item} = 1;
 9534:         }
 9535:     }
 9536:     foreach my $item (@{$logintext}) {
 9537:         if ($confhash->{$role}{$item}) {
 9538:             $changes->{$role}{'logintext'}{$item} = 1;
 9539:         }
 9540:     }
 9541:     foreach my $img (@{$images}) {
 9542:         if ($env{'form.'.$role.'_del_'.$img}) {
 9543:             $confhash->{$role}{$img} = '';
 9544:             $changes->{$role}{'images'}{$img} = 1;
 9545:         }
 9546:         if ($role eq 'login') {
 9547:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9548:                 $changes->{$role}{'showlogo'}{$img} = 1;
 9549:             }
 9550:         }
 9551:     }
 9552:     if ($confhash->{$role}{'font'}) {
 9553:         $changes->{$role}{'font'} = 1;
 9554:     }
 9555: }
 9556: 
 9557: sub display_colorchgs {
 9558:     my ($dom,$changes,$roles,$confhash) = @_;
 9559:     my (%choices,$resulttext);
 9560:     if (!grep(/^login$/,@{$roles})) {
 9561:         $resulttext = &mt('Changes made:').'<br />';
 9562:     }
 9563:     foreach my $role (@{$roles}) {
 9564:         if ($role eq 'login') {
 9565:             %choices = &login_choices();
 9566:         } else {
 9567:             %choices = &color_font_choices();
 9568:         }
 9569:         if (ref($changes->{$role}) eq 'HASH') {
 9570:             if ($role ne 'login') {
 9571:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 9572:             }
 9573:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 9574:                 if ($role ne 'login') {
 9575:                     $resulttext .= '<ul>';
 9576:                 }
 9577:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 9578:                     if ($role ne 'login') {
 9579:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 9580:                     }
 9581:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 9582:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 9583:                             if ($confhash->{$role}{$key}{$item}) {
 9584:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 9585:                             } else {
 9586:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 9587:                             }
 9588:                         } elsif ($confhash->{$role}{$item} eq '') {
 9589:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 9590:                         } else {
 9591:                             my $newitem = $confhash->{$role}{$item};
 9592:                             if ($key eq 'images') {
 9593:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
 9594:                             }
 9595:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 9596:                         }
 9597:                     }
 9598:                     if ($role ne 'login') {
 9599:                         $resulttext .= '</ul></li>';
 9600:                     }
 9601:                 } else {
 9602:                     if ($confhash->{$role}{$key} eq '') {
 9603:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 9604:                     } else {
 9605:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 9606:                     }
 9607:                 }
 9608:                 if ($role ne 'login') {
 9609:                     $resulttext .= '</ul>';
 9610:                 }
 9611:             }
 9612:         }
 9613:     }
 9614:     return $resulttext;
 9615: }
 9616: 
 9617: sub thumb_dimensions {
 9618:     return ('200','50');
 9619: }
 9620: 
 9621: sub check_dimensions {
 9622:     my ($inputfile) = @_;
 9623:     my ($fullwidth,$fullheight);
 9624:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 9625:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 9626:             my $imageinfo = <PIPE>;
 9627:             if (!close(PIPE)) {
 9628:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 9629:             }
 9630:             chomp($imageinfo);
 9631:             my ($fullsize) = 
 9632:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 9633:             if ($fullsize) {
 9634:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 9635:             }
 9636:         }
 9637:     }
 9638:     return ($fullwidth,$fullheight);
 9639: }
 9640: 
 9641: sub check_configuser {
 9642:     my ($uhome,$dom,$confname,$servadm) = @_;
 9643:     my ($configuserok,%currroles);
 9644:     if ($uhome eq 'no_host') {
 9645:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 9646:         my $configpass = &LONCAPA::Enrollment::create_password();
 9647:         $configuserok = 
 9648:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 9649:                              $configpass,'','','','','',undef,$servadm);
 9650:     } else {
 9651:         $configuserok = 'ok';
 9652:         %currroles = 
 9653:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 9654:     }
 9655:     return ($configuserok,%currroles);
 9656: }
 9657: 
 9658: sub check_authorstatus {
 9659:     my ($dom,$confname,%currroles) = @_;
 9660:     my $author_ok;
 9661:     if (!$currroles{':'.$dom.':au'}) {
 9662:         my $start = time;
 9663:         my $end = 0;
 9664:         $author_ok = 
 9665:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 9666:                                         'au',$end,$start,'','','domconfig');
 9667:     } else {
 9668:         $author_ok = 'ok';
 9669:     }
 9670:     return $author_ok;
 9671: }
 9672: 
 9673: sub publishlogo {
 9674:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 9675:     my ($output,$fname,$logourl,$madethumb);
 9676:     if ($action eq 'upload') {
 9677:         $fname=$env{'form.'.$formname.'.filename'};
 9678:         chop($env{'form.'.$formname});
 9679:     } else {
 9680:         ($fname) = ($formname =~ /([^\/]+)$/);
 9681:     }
 9682:     if ($savefileas ne '') {
 9683:         $fname = $savefileas;
 9684:     }
 9685:     $fname=&Apache::lonnet::clean_filename($fname);
 9686: # See if there is anything left
 9687:     unless ($fname) { return ('error: no uploaded file'); }
 9688:     $fname="$subdir/$fname";
 9689:     my $docroot=$r->dir_config('lonDocRoot');
 9690:     my $filepath="$docroot/priv";
 9691:     my $relpath = "$dom/$confname";
 9692:     my ($fnamepath,$file,$fetchthumb);
 9693:     $file=$fname;
 9694:     if ($fname=~m|/|) {
 9695:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 9696:     }
 9697:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 9698:     my $count;
 9699:     for ($count=5;$count<=$#parts;$count++) {
 9700:         $filepath.="/$parts[$count]";
 9701:         if ((-e $filepath)!=1) {
 9702:             mkdir($filepath,02770);
 9703:         }
 9704:     }
 9705:     # Check for bad extension and disallow upload
 9706:     if ($file=~/\.(\w+)$/ &&
 9707:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 9708:         $output = 
 9709:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 9710:     } elsif ($file=~/\.(\w+)$/ &&
 9711:         !defined(&Apache::loncommon::fileembstyle($1))) {
 9712:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 9713:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 9714:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 9715:     } elsif (-d "$filepath/$file") {
 9716:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 9717:     } else {
 9718:         my $source = $filepath.'/'.$file;
 9719:         my $logfile;
 9720:         if (!open($logfile,">>",$source.'.log')) {
 9721:             return (&mt('No write permission to Authoring Space'));
 9722:         }
 9723:         print $logfile
 9724: "\n================= Publish ".localtime()." ================\n".
 9725: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 9726: # Save the file
 9727:         if (!open(FH,">",$source)) {
 9728:             &Apache::lonnet::logthis('Failed to create '.$source);
 9729:             return (&mt('Failed to create file'));
 9730:         }
 9731:         if ($action eq 'upload') {
 9732:             if (!print FH ($env{'form.'.$formname})) {
 9733:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 9734:                 return (&mt('Failed to write file'));
 9735:             }
 9736:         } else {
 9737:             my $original = &Apache::lonnet::filelocation('',$formname);
 9738:             if(!copy($original,$source)) {
 9739:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 9740:                 return (&mt('Failed to write file'));
 9741:             }
 9742:         }
 9743:         close(FH);
 9744:         chmod(0660, $source); # Permissions to rw-rw---.
 9745: 
 9746:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 9747:         my $copyfile=$targetdir.'/'.$file;
 9748: 
 9749:         my @parts=split(/\//,$targetdir);
 9750:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 9751:         for (my $count=5;$count<=$#parts;$count++) {
 9752:             $path.="/$parts[$count]";
 9753:             if (!-e $path) {
 9754:                 print $logfile "\nCreating directory ".$path;
 9755:                 mkdir($path,02770);
 9756:             }
 9757:         }
 9758:         my $versionresult;
 9759:         if (-e $copyfile) {
 9760:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 9761:         } else {
 9762:             $versionresult = 'ok';
 9763:         }
 9764:         if ($versionresult eq 'ok') {
 9765:             if (copy($source,$copyfile)) {
 9766:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 9767:                 $output = 'ok';
 9768:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 9769:                 push(@{$modified_urls},[$copyfile,$source]);
 9770:                 my $metaoutput = 
 9771:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 9772:                 unless ($registered_cleanup) {
 9773:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9774:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9775:                     $registered_cleanup=1;
 9776:                 }
 9777:             } else {
 9778:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 9779:                 $output = &mt('Failed to copy file to RES space').", $!";
 9780:             }
 9781:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 9782:                 my $inputfile = $filepath.'/'.$file;
 9783:                 my $outfile = $filepath.'/'.'tn-'.$file;
 9784:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 9785:                 if ($fullwidth ne '' && $fullheight ne '') { 
 9786:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 9787:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 9788:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 9789:                         system({$args[0]} @args);
 9790:                         chmod(0660, $filepath.'/tn-'.$file);
 9791:                         if (-e $outfile) {
 9792:                             my $copyfile=$targetdir.'/tn-'.$file;
 9793:                             if (copy($outfile,$copyfile)) {
 9794:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 9795:                                 my $thumb_metaoutput = 
 9796:                                     &write_metadata($dom,$confname,$formname,
 9797:                                                     $targetdir,'tn-'.$file,$logfile);
 9798:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 9799:                                 unless ($registered_cleanup) {
 9800:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9801:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9802:                                     $registered_cleanup=1;
 9803:                                 }
 9804:                                 $madethumb = 1;
 9805:                             } else {
 9806:                                 print $logfile "\nUnable to write ".$copyfile.
 9807:                                                ':'.$!."\n";
 9808:                             }
 9809:                         }
 9810:                     }
 9811:                 }
 9812:             }
 9813:         } else {
 9814:             $output = $versionresult;
 9815:         }
 9816:     }
 9817:     return ($output,$logourl,$madethumb);
 9818: }
 9819: 
 9820: sub logo_versioning {
 9821:     my ($targetdir,$file,$logfile) = @_;
 9822:     my $target = $targetdir.'/'.$file;
 9823:     my ($maxversion,$fn,$extn,$output);
 9824:     $maxversion = 0;
 9825:     if ($file =~ /^(.+)\.(\w+)$/) {
 9826:         $fn=$1;
 9827:         $extn=$2;
 9828:     }
 9829:     opendir(DIR,$targetdir);
 9830:     while (my $filename=readdir(DIR)) {
 9831:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9832:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9833:         }
 9834:     }
 9835:     $maxversion++;
 9836:     print $logfile "\nCreating old version ".$maxversion."\n";
 9837:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9838:     if (copy($target,$copyfile)) {
 9839:         print $logfile "Copied old target to ".$copyfile."\n";
 9840:         $copyfile=$copyfile.'.meta';
 9841:         if (copy($target.'.meta',$copyfile)) {
 9842:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9843:             $output = 'ok';
 9844:         } else {
 9845:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9846:             $output = &mt('Failed to copy old meta').", $!, ";
 9847:         }
 9848:     } else {
 9849:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9850:         $output = &mt('Failed to copy old target').", $!, ";
 9851:     }
 9852:     return $output;
 9853: }
 9854: 
 9855: sub write_metadata {
 9856:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9857:     my (%metadatafields,%metadatakeys,$output);
 9858:     $metadatafields{'title'}=$formname;
 9859:     $metadatafields{'creationdate'}=time;
 9860:     $metadatafields{'lastrevisiondate'}=time;
 9861:     $metadatafields{'copyright'}='public';
 9862:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9863:                                          $env{'user.domain'};
 9864:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9865:     $metadatafields{'domain'}=$dom;
 9866:     {
 9867:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9868:         my $mfh;
 9869:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 9870:             foreach (sort(keys(%metadatafields))) {
 9871:                 unless ($_=~/\./) {
 9872:                     my $unikey=$_;
 9873:                     $unikey=~/^([A-Za-z]+)/;
 9874:                     my $tag=$1;
 9875:                     $tag=~tr/A-Z/a-z/;
 9876:                     print $mfh "\n\<$tag";
 9877:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9878:                         my $value=$metadatafields{$unikey.'.'.$_};
 9879:                         $value=~s/\"/\'\'/g;
 9880:                         print $mfh ' '.$_.'="'.$value.'"';
 9881:                     }
 9882:                     print $mfh '>'.
 9883:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9884:                             .'</'.$tag.'>';
 9885:                 }
 9886:             }
 9887:             $output = 'ok';
 9888:             print $logfile "\nWrote metadata";
 9889:             close($mfh);
 9890:         } else {
 9891:             print $logfile "\nFailed to open metadata file";
 9892:             $output = &mt('Could not write metadata');
 9893:         }
 9894:     }
 9895:     return $output;
 9896: }
 9897: 
 9898: sub notifysubscribed {
 9899:     foreach my $targetsource (@{$modified_urls}){
 9900:         next unless (ref($targetsource) eq 'ARRAY');
 9901:         my ($target,$source)=@{$targetsource};
 9902:         if ($source ne '') {
 9903:             if (open(my $logfh,">>",$source.'.log')) {
 9904:                 print $logfh "\nCleanup phase: Notifications\n";
 9905:                 my @subscribed=&subscribed_hosts($target);
 9906:                 foreach my $subhost (@subscribed) {
 9907:                     print $logfh "\nNotifying host ".$subhost.':';
 9908:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9909:                     print $logfh $reply;
 9910:                 }
 9911:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9912:                 foreach my $subhost (@subscribedmeta) {
 9913:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9914:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9915:                                                         $subhost);
 9916:                     print $logfh $reply;
 9917:                 }
 9918:                 print $logfh "\n============ Done ============\n";
 9919:                 close($logfh);
 9920:             }
 9921:         }
 9922:     }
 9923:     return OK;
 9924: }
 9925: 
 9926: sub subscribed_hosts {
 9927:     my ($target) = @_;
 9928:     my @subscribed;
 9929:     if (open(my $fh,"<","$target.subscription")) {
 9930:         while (my $subline=<$fh>) {
 9931:             if ($subline =~ /^($match_lonid):/) {
 9932:                 my $host = $1;
 9933:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9934:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9935:                         push(@subscribed,$host);
 9936:                     }
 9937:                 }
 9938:             }
 9939:         }
 9940:     }
 9941:     return @subscribed;
 9942: }
 9943: 
 9944: sub check_switchserver {
 9945:     my ($dom,$confname) = @_;
 9946:     my ($allowed,$switchserver);
 9947:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9948:     if ($home eq 'no_host') {
 9949:         $home = &Apache::lonnet::domain($dom,'primary');
 9950:     }
 9951:     my @ids=&Apache::lonnet::current_machine_ids();
 9952:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9953:     if (!$allowed) {
 9954: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9955:     }
 9956:     return $switchserver;
 9957: }
 9958: 
 9959: sub modify_quotas {
 9960:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9961:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9962:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9963:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9964:         $validationfieldsref);
 9965:     if ($action eq 'quotas') {
 9966:         $context = 'tools'; 
 9967:     } else {
 9968:         $context = $action;
 9969:     }
 9970:     if ($context eq 'requestcourses') {
 9971:         @usertools = ('official','unofficial','community','textbook','placement');
 9972:         @options =('norequest','approval','validate','autolimit');
 9973:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9974:         %titles = &courserequest_titles();
 9975:         $toolregexp = join('|',@usertools);
 9976:         %conditions = &courserequest_conditions();
 9977:         $confname = $dom.'-domainconfig';
 9978:         my $servadm = $r->dir_config('lonAdmEMail');
 9979:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9980:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9981:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9982:     } elsif ($context eq 'requestauthor') {
 9983:         @usertools = ('author');
 9984:         %titles = &authorrequest_titles();
 9985:     } else {
 9986:         @usertools = ('aboutme','blog','webdav','portfolio');
 9987:         %titles = &tool_titles();
 9988:     }
 9989:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9990:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9991:     foreach my $key (keys(%env)) {
 9992:         if ($context eq 'requestcourses') {
 9993:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9994:                 my $item = $1;
 9995:                 my $type = $2;
 9996:                 if ($type =~ /^limit_(.+)/) {
 9997:                     $limithash{$item}{$1} = $env{$key};
 9998:                 } else {
 9999:                     $confhash{$item}{$type} = $env{$key};
10000:                 }
10001:             }
10002:         } elsif ($context eq 'requestauthor') {
10003:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
10004:                 $confhash{$1} = $env{$key};
10005:             }
10006:         } else {
10007:             if ($key =~ /^form\.quota_(.+)$/) {
10008:                 $confhash{'defaultquota'}{$1} = $env{$key};
10009:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
10010:                 $confhash{'authorquota'}{$1} = $env{$key};
10011:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
10012:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
10013:             }
10014:         }
10015:     }
10016:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10017:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
10018:         @approvalnotify = sort(@approvalnotify);
10019:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
10020:         my @crstypes = ('official','unofficial','community','textbook','placement');
10021:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
10022:         foreach my $type (@hasuniquecode) {
10023:             if (grep(/^\Q$type\E$/,@crstypes)) {
10024:                 $confhash{'uniquecode'}{$type} = 1;
10025:             }
10026:         }
10027:         my (%newbook,%allpos);
10028:         if ($context eq 'requestcourses') {
10029:             foreach my $type ('textbooks','templates') {
10030:                 @{$allpos{$type}} = (); 
10031:                 my $invalid;
10032:                 if ($type eq 'textbooks') {
10033:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
10034:                 } else {
10035:                     $invalid = &mt('Invalid LON-CAPA course for template');
10036:                 }
10037:                 if ($env{'form.'.$type.'_addbook'}) {
10038:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
10039:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
10040:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
10041:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
10042:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10043:                         } else {
10044:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
10045:                             my $position = $env{'form.'.$type.'_addbook_pos'};
10046:                             $position =~ s/\D+//g;
10047:                             if ($position ne '') {
10048:                                 $allpos{$type}[$position] = $newbook{$type};
10049:                             }
10050:                         }
10051:                     } else {
10052:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10053:                     }
10054:                 }
10055:             } 
10056:         }
10057:         if (ref($domconfig{$action}) eq 'HASH') {
10058:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
10059:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
10060:                     $changes{'notify'}{'approval'} = 1;
10061:                 }
10062:             } else {
10063:                 if ($confhash{'notify'}{'approval'}) {
10064:                     $changes{'notify'}{'approval'} = 1;
10065:                 }
10066:             }
10067:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
10068:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
10069:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
10070:                         unless ($confhash{'uniquecode'}{$crstype}) {
10071:                             $changes{'uniquecode'} = 1;
10072:                         }
10073:                     }
10074:                     unless ($changes{'uniquecode'}) {
10075:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
10076:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
10077:                                 $changes{'uniquecode'} = 1;
10078:                             }
10079:                         }
10080:                     }
10081:                } else {
10082:                    $changes{'uniquecode'} = 1;
10083:                }
10084:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
10085:                 $changes{'uniquecode'} = 1;
10086:             }
10087:             if ($context eq 'requestcourses') {
10088:                 foreach my $type ('textbooks','templates') {
10089:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
10090:                         my %deletions;
10091:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
10092:                         if (@todelete) {
10093:                             map { $deletions{$_} = 1; } @todelete;
10094:                         }
10095:                         my %imgdeletions;
10096:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
10097:                         if (@todeleteimages) {
10098:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
10099:                         }
10100:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
10101:                         for (my $i=0; $i<=$maxnum; $i++) {
10102:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
10103:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
10104:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
10105:                                 if ($deletions{$key}) {
10106:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
10107:                                         #FIXME need to obsolete item in RES space
10108:                                     }
10109:                                     next;
10110:                                 } else {
10111:                                     my $newpos = $env{'form.'.$itemid};
10112:                                     $newpos =~ s/\D+//g;
10113:                                     foreach my $item ('subject','title','publisher','author') {
10114:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10115:                                                  ($type eq 'templates'));
10116:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
10117:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
10118:                                             $changes{$type}{$key} = 1;
10119:                                         }
10120:                                     }
10121:                                     $allpos{$type}[$newpos] = $key;
10122:                                 }
10123:                                 if ($imgdeletions{$key}) {
10124:                                     $changes{$type}{$key} = 1;
10125:                                     #FIXME need to obsolete item in RES space
10126:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
10127:                                     my ($cdom,$cnum) = split(/_/,$key);
10128:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10129:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10130:                                     } else {
10131:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
10132:                                                                                       $cdom,$cnum,$type,$configuserok,
10133:                                                                                       $switchserver,$author_ok);
10134:                                         if ($imgurl) {
10135:                                             $confhash{$type}{$key}{'image'} = $imgurl;
10136:                                             $changes{$type}{$key} = 1; 
10137:                                         }
10138:                                         if ($error) {
10139:                                             &Apache::lonnet::logthis($error);
10140:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10141:                                         }
10142:                                     }
10143:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
10144:                                     $confhash{$type}{$key}{'image'} = 
10145:                                         $domconfig{$action}{$type}{$key}{'image'};
10146:                                 }
10147:                             }
10148:                         }
10149:                     }
10150:                 }
10151:             }
10152:         } else {
10153:             if ($confhash{'notify'}{'approval'}) {
10154:                 $changes{'notify'}{'approval'} = 1;
10155:             }
10156:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
10157:                 $changes{'uniquecode'} = 1;
10158:             }
10159:         }
10160:         if ($context eq 'requestcourses') {
10161:             foreach my $type ('textbooks','templates') {
10162:                 if ($newbook{$type}) {
10163:                     $changes{$type}{$newbook{$type}} = 1;
10164:                     foreach my $item ('subject','title','publisher','author') {
10165:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10166:                                  ($type eq 'template'));
10167:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
10168:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
10169:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
10170:                         }
10171:                     }
10172:                     if ($type eq 'textbooks') {
10173:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
10174:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
10175:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10176:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10177:                             } else {
10178:                                 my ($imageurl,$error) =
10179:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
10180:                                                             $configuserok,$switchserver,$author_ok);
10181:                                 if ($imageurl) {
10182:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
10183:                                 }
10184:                                 if ($error) {
10185:                                     &Apache::lonnet::logthis($error);
10186:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10187:                                 }
10188:                             }
10189:                         }
10190:                     }
10191:                 }
10192:                 if (@{$allpos{$type}} > 0) {
10193:                     my $idx = 0;
10194:                     foreach my $item (@{$allpos{$type}}) {
10195:                         if ($item ne '') {
10196:                             $confhash{$type}{$item}{'order'} = $idx;
10197:                             if (ref($domconfig{$action}) eq 'HASH') {
10198:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
10199:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
10200:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
10201:                                             $changes{$type}{$item} = 1;
10202:                                         }
10203:                                     }
10204:                                 }
10205:                             }
10206:                             $idx ++;
10207:                         }
10208:                     }
10209:                 }
10210:             }
10211:             if (ref($validationitemsref) eq 'ARRAY') {
10212:                 foreach my $item (@{$validationitemsref}) {
10213:                     if ($item eq 'fields') {
10214:                         my @changed;
10215:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
10216:                         if (@{$confhash{'validation'}{$item}} > 0) {
10217:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
10218:                         }
10219:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10220:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10221:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
10222:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
10223:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
10224:                                 } else {
10225:                                     @changed = @{$confhash{'validation'}{$item}};
10226:                                 }
10227:                             } else {
10228:                                 @changed = @{$confhash{'validation'}{$item}};
10229:                             }
10230:                         } else {
10231:                             @changed = @{$confhash{'validation'}{$item}};
10232:                         }
10233:                         if (@changed) {
10234:                             if ($confhash{'validation'}{$item}) {
10235:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
10236:                             } else {
10237:                                 $changes{'validation'}{$item} = &mt('None');
10238:                             }
10239:                         }
10240:                     } else {
10241:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
10242:                         if ($item eq 'markup') {
10243:                             if ($env{'form.requestcourses_validation_'.$item}) {
10244:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10245:                             }
10246:                         }
10247:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10248:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10249:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
10250:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10251:                                 }
10252:                             } else {
10253:                                 if ($confhash{'validation'}{$item} ne '') {
10254:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10255:                                 }
10256:                             }
10257:                         } else {
10258:                             if ($confhash{'validation'}{$item} ne '') {
10259:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10260:                             }
10261:                         }
10262:                     }
10263:                 }
10264:             }
10265:             if ($env{'form.validationdc'}) {
10266:                 my $newval = $env{'form.validationdc'};
10267:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10268:                 if (exists($domcoords{$newval})) {
10269:                     $confhash{'validation'}{'dc'} = $newval;
10270:                 }
10271:             }
10272:             if (ref($confhash{'validation'}) eq 'HASH') {
10273:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10274:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10275:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10276:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
10277:                                 if ($confhash{'validation'}{'dc'} eq '') {
10278:                                     $changes{'validation'}{'dc'} = &mt('None');
10279:                                 } else {
10280:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10281:                                 }
10282:                             }
10283:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
10284:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10285:                         }
10286:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
10287:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10288:                     }
10289:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
10290:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10291:                 }
10292:             } else {
10293:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10294:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10295:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10296:                             $changes{'validation'}{'dc'} = &mt('None');
10297:                         }
10298:                     }
10299:                 }
10300:             }
10301:         }
10302:     } else {
10303:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
10304:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
10305:     }
10306:     foreach my $item (@usertools) {
10307:         foreach my $type (@{$types},'default','_LC_adv') {
10308:             my $unset; 
10309:             if ($context eq 'requestcourses') {
10310:                 $unset = '0';
10311:                 if ($type eq '_LC_adv') {
10312:                     $unset = '';
10313:                 }
10314:                 if ($confhash{$item}{$type} eq 'autolimit') {
10315:                     $confhash{$item}{$type} .= '=';
10316:                     unless ($limithash{$item}{$type} =~ /\D/) {
10317:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
10318:                     }
10319:                 }
10320:             } elsif ($context eq 'requestauthor') {
10321:                 $unset = '0';
10322:                 if ($type eq '_LC_adv') {
10323:                     $unset = '';
10324:                 }
10325:             } else {
10326:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
10327:                     $confhash{$item}{$type} = 1;
10328:                 } else {
10329:                     $confhash{$item}{$type} = 0;
10330:                 }
10331:             }
10332:             if (ref($domconfig{$action}) eq 'HASH') {
10333:                 if ($action eq 'requestauthor') {
10334:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
10335:                         $changes{$type} = 1;
10336:                     }
10337:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
10338:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
10339:                         $changes{$item}{$type} = 1;
10340:                     }
10341:                 } else {
10342:                     if ($context eq 'requestcourses') {
10343:                         if ($confhash{$item}{$type} ne $unset) {
10344:                             $changes{$item}{$type} = 1;
10345:                         }
10346:                     } else {
10347:                         if (!$confhash{$item}{$type}) {
10348:                             $changes{$item}{$type} = 1;
10349:                         }
10350:                     }
10351:                 }
10352:             } else {
10353:                 if ($context eq 'requestcourses') {
10354:                     if ($confhash{$item}{$type} ne $unset) {
10355:                         $changes{$item}{$type} = 1;
10356:                     }
10357:                 } elsif ($context eq 'requestauthor') {
10358:                     if ($confhash{$type} ne $unset) {
10359:                         $changes{$type} = 1;
10360:                     }
10361:                 } else {
10362:                     if (!$confhash{$item}{$type}) {
10363:                         $changes{$item}{$type} = 1;
10364:                     }
10365:                 }
10366:             }
10367:         }
10368:     }
10369:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10370:         if (ref($domconfig{'quotas'}) eq 'HASH') {
10371:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10372:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
10373:                     if (exists($confhash{'defaultquota'}{$key})) {
10374:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
10375:                             $changes{'defaultquota'}{$key} = 1;
10376:                         }
10377:                     } else {
10378:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
10379:                     }
10380:                 }
10381:             } else {
10382:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
10383:                     if (exists($confhash{'defaultquota'}{$key})) {
10384:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
10385:                             $changes{'defaultquota'}{$key} = 1;
10386:                         }
10387:                     } else {
10388:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
10389:                     }
10390:                 }
10391:             }
10392:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10393:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
10394:                     if (exists($confhash{'authorquota'}{$key})) {
10395:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
10396:                             $changes{'authorquota'}{$key} = 1;
10397:                         }
10398:                     } else {
10399:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
10400:                     }
10401:                 }
10402:             }
10403:         }
10404:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
10405:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
10406:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10407:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10408:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
10409:                             $changes{'defaultquota'}{$key} = 1;
10410:                         }
10411:                     } else {
10412:                         if (!exists($domconfig{'quotas'}{$key})) {
10413:                             $changes{'defaultquota'}{$key} = 1;
10414:                         }
10415:                     }
10416:                 } else {
10417:                     $changes{'defaultquota'}{$key} = 1;
10418:                 }
10419:             }
10420:         }
10421:         if (ref($confhash{'authorquota'}) eq 'HASH') {
10422:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
10423:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10424:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10425:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
10426:                             $changes{'authorquota'}{$key} = 1;
10427:                         }
10428:                     } else {
10429:                         $changes{'authorquota'}{$key} = 1;
10430:                     }
10431:                 } else {
10432:                     $changes{'authorquota'}{$key} = 1;
10433:                 }
10434:             }
10435:         }
10436:     }
10437: 
10438:     if ($context eq 'requestauthor') {
10439:         $domdefaults{'requestauthor'} = \%confhash;
10440:     } else {
10441:         foreach my $key (keys(%confhash)) {
10442:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
10443:                 $domdefaults{$key} = $confhash{$key};
10444:             }
10445:         }
10446:     }
10447: 
10448:     my %quotahash = (
10449:                       $action => { %confhash }
10450:                     );
10451:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
10452:                                              $dom);
10453:     if ($putresult eq 'ok') {
10454:         if (keys(%changes) > 0) {
10455:             my $cachetime = 24*60*60;
10456:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10457:             if (ref($lastactref) eq 'HASH') {
10458:                 $lastactref->{'domdefaults'} = 1;
10459:             }
10460:             $resulttext = &mt('Changes made:').'<ul>';
10461:             unless (($context eq 'requestcourses') ||
10462:                     ($context eq 'requestauthor')) {
10463:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
10464:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
10465:                     foreach my $type (@{$types},'default') {
10466:                         if (defined($changes{'defaultquota'}{$type})) {
10467:                             my $typetitle = $usertypes->{$type};
10468:                             if ($type eq 'default') {
10469:                                 $typetitle = $othertitle;
10470:                             }
10471:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
10472:                         }
10473:                     }
10474:                     $resulttext .= '</ul></li>';
10475:                 }
10476:                 if (ref($changes{'authorquota'}) eq 'HASH') {
10477:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
10478:                     foreach my $type (@{$types},'default') {
10479:                         if (defined($changes{'authorquota'}{$type})) {
10480:                             my $typetitle = $usertypes->{$type};
10481:                             if ($type eq 'default') {
10482:                                 $typetitle = $othertitle;
10483:                             }
10484:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
10485:                         }
10486:                     }
10487:                     $resulttext .= '</ul></li>';
10488:                 }
10489:             }
10490:             my %newenv;
10491:             foreach my $item (@usertools) {
10492:                 my (%haschgs,%inconf);
10493:                 if ($context eq 'requestauthor') {
10494:                     %haschgs = %changes;
10495:                     %inconf = %confhash;
10496:                 } else {
10497:                     if (ref($changes{$item}) eq 'HASH') {
10498:                         %haschgs = %{$changes{$item}};
10499:                     }
10500:                     if (ref($confhash{$item}) eq 'HASH') {
10501:                         %inconf = %{$confhash{$item}};
10502:                     }
10503:                 }
10504:                 if (keys(%haschgs) > 0) {
10505:                     my $newacc = 
10506:                         &Apache::lonnet::usertools_access($env{'user.name'},
10507:                                                           $env{'user.domain'},
10508:                                                           $item,'reload',$context);
10509:                     if (($context eq 'requestcourses') ||
10510:                         ($context eq 'requestauthor')) {
10511:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
10512:                             $newenv{'environment.canrequest.'.$item} = $newacc;
10513:                         }
10514:                     } else {
10515:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
10516:                             $newenv{'environment.availabletools.'.$item} = $newacc;
10517:                         }
10518:                     }
10519:                     unless ($context eq 'requestauthor') {
10520:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
10521:                     }
10522:                     foreach my $type (@{$types},'default','_LC_adv') {
10523:                         if ($haschgs{$type}) {
10524:                             my $typetitle = $usertypes->{$type};
10525:                             if ($type eq 'default') {
10526:                                 $typetitle = $othertitle;
10527:                             } elsif ($type eq '_LC_adv') {
10528:                                 $typetitle = 'LON-CAPA Advanced Users'; 
10529:                             }
10530:                             if ($inconf{$type}) {
10531:                                 if ($context eq 'requestcourses') {
10532:                                     my $cond;
10533:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
10534:                                         if ($1 eq '') {
10535:                                             $cond = &mt('(Automatic processing of any request).');
10536:                                         } else {
10537:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
10538:                                         }
10539:                                     } else { 
10540:                                         $cond = $conditions{$inconf{$type}};
10541:                                     }
10542:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
10543:                                 } elsif ($context eq 'requestauthor') {
10544:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
10545:                                                              $titles{$inconf{$type}},$typetitle);
10546: 
10547:                                 } else {
10548:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
10549:                                 }
10550:                             } else {
10551:                                 if ($type eq '_LC_adv') {
10552:                                     if ($inconf{$type} eq '0') {
10553:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10554:                                     } else { 
10555:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
10556:                                     }
10557:                                 } else {
10558:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10559:                                 }
10560:                             }
10561:                         }
10562:                     }
10563:                     unless ($context eq 'requestauthor') {
10564:                         $resulttext .= '</ul></li>';
10565:                     }
10566:                 }
10567:             }
10568:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
10569:                 if (ref($changes{'notify'}) eq 'HASH') {
10570:                     if ($changes{'notify'}{'approval'}) {
10571:                         if (ref($confhash{'notify'}) eq 'HASH') {
10572:                             if ($confhash{'notify'}{'approval'}) {
10573:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
10574:                             } else {
10575:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
10576:                             }
10577:                         }
10578:                     }
10579:                 }
10580:             }
10581:             if ($action eq 'requestcourses') {
10582:                 my @offon = ('off','on');
10583:                 if ($changes{'uniquecode'}) {
10584:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
10585:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
10586:                         $resulttext .= '<li>'.
10587:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
10588:                                        '</li>';
10589:                     } else {
10590:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
10591:                                        '</li>';
10592:                     }
10593:                 }
10594:                 foreach my $type ('textbooks','templates') {
10595:                     if (ref($changes{$type}) eq 'HASH') {
10596:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
10597:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
10598:                             my %coursehash = &Apache::lonnet::coursedescription($key);
10599:                             my $coursetitle = $coursehash{'description'};
10600:                             my $position = $confhash{$type}{$key}{'order'} + 1;
10601:                             $resulttext .= '<li>';
10602:                             foreach my $item ('subject','title','publisher','author') {
10603:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
10604:                                          ($type eq 'templates'));
10605:                                 my $name = $item.':';
10606:                                 $name =~ s/^(\w)/\U$1/;
10607:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
10608:                             }
10609:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
10610:                             if ($type eq 'textbooks') {
10611:                                 if ($confhash{$type}{$key}{'image'}) {
10612:                                     $resulttext .= ' '.&mt('Image: [_1]',
10613:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
10614:                                                    ' alt="Textbook cover" />').'<br />';
10615:                                 }
10616:                             }
10617:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
10618:                         }
10619:                         $resulttext .= '</ul></li>';
10620:                     }
10621:                 }
10622:                 if (ref($changes{'validation'}) eq 'HASH') {
10623:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
10624:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
10625:                         foreach my $item (@{$validationitemsref}) {
10626:                             if (exists($changes{'validation'}{$item})) {
10627:                                 if ($item eq 'markup') {
10628:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10629:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
10630:                                 } else {
10631:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10632:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
10633:                                 }
10634:                             }
10635:                         }
10636:                         if (exists($changes{'validation'}{'dc'})) {
10637:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
10638:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
10639:                         }
10640:                     }
10641:                 }
10642:             }
10643:             $resulttext .= '</ul>';
10644:             if (keys(%newenv)) {
10645:                 &Apache::lonnet::appenv(\%newenv);
10646:             }
10647:         } else {
10648:             if ($context eq 'requestcourses') {
10649:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
10650:             } elsif ($context eq 'requestauthor') {
10651:                 $resulttext = &mt('No changes made to rights to request author space.');
10652:             } else {
10653:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
10654:             }
10655:         }
10656:     } else {
10657:         $resulttext = '<span class="LC_error">'.
10658: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10659:     }
10660:     if ($errors) {
10661:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
10662:                        '<ul>'.$errors.'</ul></p>';
10663:     }
10664:     return $resulttext;
10665: }
10666: 
10667: sub process_textbook_image {
10668:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
10669:     my $filename = $env{'form.'.$caller.'.filename'};
10670:     my ($error,$url);
10671:     my ($width,$height) = (50,50);
10672:     if ($configuserok eq 'ok') {
10673:         if ($switchserver) {
10674:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
10675:                          $switchserver);
10676:         } elsif ($author_ok eq 'ok') {
10677:             my ($result,$imageurl) =
10678:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10679:                              "$type/$cdom/$cnum/cover",$width,$height);
10680:             if ($result eq 'ok') {
10681:                 $url = $imageurl;
10682:             } else {
10683:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10684:             }
10685:         } else {
10686:             $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);
10687:         }
10688:     } else {
10689:         $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);
10690:     }
10691:     return ($url,$error);
10692: }
10693: 
10694: sub modify_ltitools {
10695:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10696:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10697:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
10698:     my $confname = $dom.'-domainconfig';
10699:     my $servadm = $r->dir_config('lonAdmEMail');
10700:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10701:     my (%posslti,%possfield);
10702:     my @courseroles = ('cc','in','ta','ep','st');
10703:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
10704:     map { $posslti{$_} = 1; } @ltiroles;
10705:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
10706:     map { $possfield{$_} = 1; } @allfields;
10707:     my %lt = &ltitools_names(); 
10708:     if ($env{'form.ltitools_add'}) {
10709:         my $title = $env{'form.ltitools_add_title'};
10710:         $title =~ s/(`)/'/g;
10711:         ($newid,my $error) = &get_ltitools_id($dom,$title);
10712:         if ($newid) {
10713:             my $position = $env{'form.ltitools_add_pos'};
10714:             $position =~ s/\D+//g;
10715:             if ($position ne '') {
10716:                 $allpos[$position] = $newid;
10717:             }
10718:             $changes{$newid} = 1;
10719:             foreach my $item ('title','url','key','secret','lifetime') {
10720:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
10721:                 if ($item eq 'lifetime') {
10722:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
10723:                 }
10724:                 if ($env{'form.ltitools_add_'.$item}) {
10725:                     if (($item eq 'key') || ($item eq 'secret')) {
10726:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10727:                     } else {
10728:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10729:                     }
10730:                 }
10731:             }
10732:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
10733:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
10734:             }
10735:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
10736:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
10737:             }
10738:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
10739:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
10740:             } else {
10741:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
10742:             }
10743:             foreach my $item ('width','height','linktext','explanation') {
10744:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
10745:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
10746:                 if (($item eq 'width') || ($item eq 'height')) {
10747:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
10748:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10749:                     }
10750:                 } else {
10751:                     if ($env{'form.ltitools_add_'.$item} ne '') {
10752:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
10753:                     }
10754:                 }
10755:             }
10756:             if ($env{'form.ltitools_add_target'} eq 'window') {
10757:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10758:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
10759:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10760:             } else {
10761:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
10762:             }
10763:             foreach my $item ('passback','roster') {
10764:                 if ($env{'form.ltitools_'.$item.'_add'}) {
10765:                     $confhash{$newid}{$item} = 1;
10766:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
10767:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
10768:                         $lifetime =~ s/^\s+|\s+$//g;
10769:                         if ($lifetime =~ /^\d+\.?\d*$/) {
10770:                             $confhash{$newid}{$item.'valid'} = $lifetime;
10771:                         }
10772:                     }
10773:                 }
10774:             }
10775:             if ($env{'form.ltitools_add_image.filename'} ne '') {
10776:                 my ($imageurl,$error) =
10777:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
10778:                                             $configuserok,$switchserver,$author_ok);
10779:                 if ($imageurl) {
10780:                     $confhash{$newid}{'image'} = $imageurl;
10781:                 }
10782:                 if ($error) {
10783:                     &Apache::lonnet::logthis($error);
10784:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10785:                 }
10786:             }
10787:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
10788:             foreach my $field (@fields) {
10789:                 if ($possfield{$field}) {
10790:                     if ($field eq 'roles') {
10791:                         foreach my $role (@courseroles) {
10792:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
10793:                             if (($choice ne '') && ($posslti{$choice})) {
10794:                                 $confhash{$newid}{'roles'}{$role} = $choice;
10795:                                 if ($role eq 'cc') {
10796:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
10797:                                 }
10798:                             }
10799:                         }
10800:                     } else {
10801:                         $confhash{$newid}{'fields'}{$field} = 1;
10802:                     }
10803:                 }
10804:             }
10805:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
10806:                 if ($confhash{$newid}{'fields'}{'user'}) {
10807:                     if ($env{'form.ltitools_userincdom_add'}) {
10808:                         $confhash{$newid}{'incdom'} = 1;
10809:                     }
10810:                 }
10811:             }
10812:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
10813:             foreach my $item (@courseconfig) {
10814:                 $confhash{$newid}{'crsconf'}{$item} = 1;
10815:             }
10816:             if ($env{'form.ltitools_add_custom'}) {
10817:                 my $name = $env{'form.ltitools_add_custom_name'};
10818:                 my $value = $env{'form.ltitools_add_custom_value'};
10819:                 $value =~ s/(`)/'/g;
10820:                 $name =~ s/(`)/'/g;
10821:                 $confhash{$newid}{'custom'}{$name} = $value;
10822:             }
10823:         } else {
10824:             my $error = &mt('Failed to acquire unique ID for new external tool');   
10825:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10826:         }
10827:     }
10828:     if (ref($domconfig{$action}) eq 'HASH') {
10829:         my %deletions;
10830:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
10831:         if (@todelete) {
10832:             map { $deletions{$_} = 1; } @todelete;
10833:         }
10834:         my %customadds;
10835:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
10836:         if (@newcustom) {
10837:             map { $customadds{$_} = 1; } @newcustom;
10838:         } 
10839:         my %imgdeletions;
10840:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
10841:         if (@todeleteimages) {
10842:             map { $imgdeletions{$_} = 1; } @todeleteimages;
10843:         }
10844:         my $maxnum = $env{'form.ltitools_maxnum'};
10845:         for (my $i=0; $i<=$maxnum; $i++) {
10846:             my $itemid = $env{'form.ltitools_id_'.$i};
10847:             $itemid =~ s/\D+//g;
10848:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10849:                 if ($deletions{$itemid}) {
10850:                     if ($domconfig{$action}{$itemid}{'image'}) {
10851:                         #FIXME need to obsolete item in RES space
10852:                     }
10853:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
10854:                     next;
10855:                 } else {
10856:                     my $newpos = $env{'form.ltitools_'.$itemid};
10857:                     $newpos =~ s/\D+//g;
10858:                     foreach my $item ('title','url','lifetime') {
10859:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10860:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
10861:                             $changes{$itemid} = 1;
10862:                         }
10863:                     }
10864:                     foreach my $item ('key','secret') {
10865:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10866:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
10867:                             $changes{$itemid} = 1;
10868:                         }
10869:                     }
10870:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
10871:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
10872:                     }
10873:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
10874:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
10875:                     }
10876:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
10877:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
10878:                     } else {
10879:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
10880:                     }
10881:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
10882:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
10883:                             $changes{$itemid} = 1;
10884:                         }
10885:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
10886:                         $changes{$itemid} = 1;
10887:                     }
10888:                     foreach my $size ('width','height') {
10889:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
10890:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
10891:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
10892:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
10893:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10894:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
10895:                                     $changes{$itemid} = 1;
10896:                                 }
10897:                             } else {
10898:                                 $changes{$itemid} = 1;
10899:                             }
10900:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10901:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
10902:                                 $changes{$itemid} = 1;
10903:                             }
10904:                         }
10905:                     }
10906:                     foreach my $item ('linktext','explanation') {
10907:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
10908:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
10909:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
10910:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10911:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10912:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
10913:                                     $changes{$itemid} = 1;
10914:                                 }
10915:                             } else {
10916:                                 $changes{$itemid} = 1;
10917:                             }
10918:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10919:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
10920:                                 $changes{$itemid} = 1;
10921:                             }
10922:                         }
10923:                     }
10924:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
10925:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10926:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
10927:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10928:                     } else {
10929:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
10930:                     }
10931:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10932:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
10933:                             $changes{$itemid} = 1;
10934:                         }
10935:                     } else {
10936:                         $changes{$itemid} = 1;
10937:                     }
10938:                     foreach my $extra ('passback','roster') {
10939:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
10940:                             $confhash{$itemid}{$extra} = 1;
10941:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
10942:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_add'};
10943:                                 $lifetime =~ s/^\s+|\s+$//g;
10944:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
10945:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
10946:                                 }
10947:                             }
10948:                         }
10949:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
10950:                             $changes{$itemid} = 1;
10951:                         }
10952:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
10953:                             $changes{$itemid} = 1;
10954:                         }
10955:                     }
10956:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
10957:                     foreach my $item ('label','title','target','linktext','explanation','append') {
10958:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
10959:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
10960:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
10961:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
10962:                                     $changes{$itemid} = 1;
10963:                                 }
10964:                             } else {
10965:                                 $changes{$itemid} = 1;
10966:                             }
10967:                         }
10968:                     }
10969:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
10970:                     foreach my $field (@fields) {
10971:                         if ($possfield{$field}) {
10972:                             if ($field eq 'roles') {
10973:                                 foreach my $role (@courseroles) {
10974:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
10975:                                     if (($choice ne '') && ($posslti{$choice})) {
10976:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
10977:                                         if ($role eq 'cc') {
10978:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
10979:                                         }
10980:                                     }
10981:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
10982:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
10983:                                             $changes{$itemid} = 1;
10984:                                         }
10985:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
10986:                                         $changes{$itemid} = 1;
10987:                                     }
10988:                                 }
10989:                             } else {
10990:                                 $confhash{$itemid}{'fields'}{$field} = 1;
10991:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
10992:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
10993:                                         $changes{$itemid} = 1;
10994:                                     }
10995:                                 } else {
10996:                                     $changes{$itemid} = 1;
10997:                                 }
10998:                             }
10999:                         }
11000:                     }
11001:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11002:                         if ($confhash{$itemid}{'fields'}{'user'}) {
11003:                             if ($env{'form.ltitools_userincdom_'.$i}) {
11004:                                 $confhash{$itemid}{'incdom'} = 1;
11005:                             }
11006:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
11007:                                 $changes{$itemid} = 1;
11008:                             }
11009:                         }
11010:                     }
11011:                     $allpos[$newpos] = $itemid;
11012:                 }
11013:                 if ($imgdeletions{$itemid}) {
11014:                     $changes{$itemid} = 1;
11015:                     #FIXME need to obsolete item in RES space
11016:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
11017:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
11018:                                                                  $itemid,$configuserok,$switchserver,
11019:                                                                  $author_ok);
11020:                     if ($imgurl) {
11021:                         $confhash{$itemid}{'image'} = $imgurl;
11022:                         $changes{$itemid} = 1;
11023:                     }
11024:                     if ($error) {
11025:                         &Apache::lonnet::logthis($error);
11026:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11027:                     }
11028:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
11029:                     $confhash{$itemid}{'image'} =
11030:                        $domconfig{$action}{$itemid}{'image'};
11031:                 }
11032:                 if ($customadds{$i}) {
11033:                     my $name = $env{'form.ltitools_custom_name_'.$i};
11034:                     $name =~ s/(`)/'/g;
11035:                     $name =~ s/^\s+//;
11036:                     $name =~ s/\s+$//;
11037:                     my $value = $env{'form.ltitools_custom_value_'.$i};
11038:                     $value =~ s/(`)/'/g;
11039:                     $value =~ s/^\s+//;
11040:                     $value =~ s/\s+$//;
11041:                     if ($name ne '') {
11042:                         $confhash{$itemid}{'custom'}{$name} = $value;
11043:                         $changes{$itemid} = 1;
11044:                     }
11045:                 }
11046:                 my %customdels;
11047:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
11048:                 if (@customdeletions) {
11049:                     $changes{$itemid} = 1;
11050:                 }
11051:                 map { $customdels{$_} = 1; } @customdeletions;
11052:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
11053:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
11054:                         unless ($customdels{$key}) {
11055:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
11056:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
11057:                             }
11058:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
11059:                                 $changes{$itemid} = 1;
11060:                             }
11061:                         }
11062:                     }
11063:                 }
11064:                 unless ($changes{$itemid}) {
11065:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
11066:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
11067:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
11068:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
11069:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
11070:                                         $changes{$itemid} = 1;
11071:                                         last;
11072:                                     }
11073:                                 }
11074:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
11075:                                 $changes{$itemid} = 1;
11076:                             }
11077:                         }
11078:                         last if ($changes{$itemid});
11079:                     }
11080:                 }
11081:             }
11082:         }
11083:     }
11084:     if (@allpos > 0) {
11085:         my $idx = 0;
11086:         foreach my $itemid (@allpos) {
11087:             if ($itemid ne '') {
11088:                 $confhash{$itemid}{'order'} = $idx;
11089:                 if (ref($domconfig{$action}) eq 'HASH') {
11090:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11091:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
11092:                             $changes{$itemid} = 1;
11093:                         }
11094:                     }
11095:                 }
11096:                 $idx ++;
11097:             }
11098:         }
11099:     }
11100:     my %ltitoolshash = (
11101:                           $action => { %confhash }
11102:                        );
11103:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
11104:                                              $dom);
11105:     if ($putresult eq 'ok') {
11106:         my %ltienchash = (
11107:                              $action => { %encconfig }
11108:                          );
11109:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
11110:         if (keys(%changes) > 0) {
11111:             my $cachetime = 24*60*60;
11112:             my %ltiall = %confhash;
11113:             foreach my $id (keys(%ltiall)) {
11114:                 if (ref($encconfig{$id}) eq 'HASH') {
11115:                     foreach my $item ('key','secret') {
11116:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
11117:                     }
11118:                 }
11119:             }
11120:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
11121:             if (ref($lastactref) eq 'HASH') {
11122:                 $lastactref->{'ltitools'} = 1;
11123:             }
11124:             $resulttext = &mt('Changes made:').'<ul>';
11125:             my %bynum;
11126:             foreach my $itemid (sort(keys(%changes))) {
11127:                 my $position = $confhash{$itemid}{'order'};
11128:                 $bynum{$position} = $itemid;
11129:             }
11130:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
11131:                 my $itemid = $bynum{$pos}; 
11132:                 if (ref($confhash{$itemid}) ne 'HASH') {
11133:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
11134:                 } else {
11135:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
11136:                     if ($confhash{$itemid}{'image'}) {
11137:                         $resulttext .= '&nbsp;'.
11138:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
11139:                                        ' alt="'.&mt('Tool Provider icon').'" />';
11140:                     }
11141:                     $resulttext .= '</li><ul>';
11142:                     my $position = $pos + 1;
11143:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
11144:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
11145:                         if ($confhash{$itemid}{$item} ne '') {
11146:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
11147:                         }
11148:                     }
11149:                     if ($encconfig{$itemid}{'key'} ne '') {
11150:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
11151:                     }
11152:                     if ($encconfig{$itemid}{'secret'} ne '') {
11153:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
11154:                         my $num = length($encconfig{$itemid}{'secret'});
11155:                         $resulttext .= ('*'x$num).'</li>';
11156:                     }
11157:                     $resulttext .= '<li>'.&mt('Configurable in course:');
11158:                     my @possconfig = ('label','title','target','linktext','explanation','append');
11159:                     my $numconfig = 0; 
11160:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
11161:                         foreach my $item (@possconfig) {
11162:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
11163:                                 $numconfig ++;
11164:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
11165:                             }
11166:                         }
11167:                     }
11168:                     if (!$numconfig) {
11169:                         $resulttext .= &mt('None');
11170:                     }
11171:                     $resulttext .= '</li>';
11172:                     foreach my $item ('passback','roster') {
11173:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
11174:                         if ($confhash{$itemid}{$item}) {
11175:                             $resulttext .= &mt('Yes');
11176:                             if ($confhash{$itemid}{$item.'valid'}) {
11177:                                 if ($item eq 'passback') {
11178:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
11179:                                                            $confhash{$itemid}{$item.'valid'});
11180:                                 } else {
11181:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
11182:                                                            $confhash{$itemid}{$item.'valid'});
11183:                                 }
11184:                             }
11185:                         } else {
11186:                             $resulttext .= &mt('No');
11187:                         }
11188:                         $resulttext .= '</li>';
11189:                     }
11190:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
11191:                         my $displaylist;
11192:                         if ($confhash{$itemid}{'display'}{'target'}) {
11193:                             $displaylist = &mt('Display target').':&nbsp;'.
11194:                                            $confhash{$itemid}{'display'}{'target'}.',';
11195:                         }
11196:                         foreach my $size ('width','height') { 
11197:                             if ($confhash{$itemid}{'display'}{$size}) {
11198:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
11199:                                                 $confhash{$itemid}{'display'}{$size}.',';
11200:                             }
11201:                         }
11202:                         if ($displaylist) {
11203:                             $displaylist =~ s/,$//;
11204:                             $resulttext .= '<li>'.$displaylist.'</li>';
11205:                         }
11206:                         foreach my $item ('linktext','explanation') {
11207:                             if ($confhash{$itemid}{'display'}{$item}) {
11208:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
11209:                             }
11210:                         }
11211:                     }
11212:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11213:                         my $fieldlist;
11214:                         foreach my $field (@allfields) {
11215:                             if ($confhash{$itemid}{'fields'}{$field}) {
11216:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
11217:                             }
11218:                         }
11219:                         if ($fieldlist) {
11220:                             $fieldlist =~ s/,$//;
11221:                             if ($confhash{$itemid}{'fields'}{'user'}) {
11222:                                 if ($confhash{$itemid}{'incdom'}) {
11223:                                     $fieldlist .= ' ('.&mt('username:domain').')';
11224:                                 } else {
11225:                                     $fieldlist .= ' ('.&mt('username').')';
11226:                                 }
11227:                             }
11228:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
11229:                         }
11230:                     }
11231:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
11232:                         my $rolemaps;
11233:                         foreach my $role (@courseroles) {
11234:                             if ($confhash{$itemid}{'roles'}{$role}) {
11235:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
11236:                                              $confhash{$itemid}{'roles'}{$role}.',';
11237:                             }
11238:                         }
11239:                         if ($rolemaps) {
11240:                             $rolemaps =~ s/,$//; 
11241:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
11242:                         }
11243:                     }
11244:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
11245:                         my $customlist;
11246:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
11247:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
11248:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
11249:                             } 
11250:                         }
11251:                         if ($customlist) {
11252:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
11253:                         }
11254:                     } 
11255:                     $resulttext .= '</ul></li>';
11256:                 }
11257:             }
11258:             $resulttext .= '</ul>';
11259:         } else {
11260:             $resulttext = &mt('No changes made.');
11261:         }
11262:     } else {
11263:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11264:     }
11265:     if ($errors) {
11266:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11267:                        $errors.'</ul>';
11268:     }
11269:     return $resulttext;
11270: }
11271: 
11272: sub process_ltitools_image {
11273:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
11274:     my $filename = $env{'form.'.$caller.'.filename'};
11275:     my ($error,$url);
11276:     my ($width,$height) = (21,21);
11277:     if ($configuserok eq 'ok') {
11278:         if ($switchserver) {
11279:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
11280:                          $switchserver);
11281:         } elsif ($author_ok eq 'ok') {
11282:             my ($result,$imageurl,$madethumb) =
11283:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11284:                              "ltitools/$itemid/icon",$width,$height);
11285:             if ($result eq 'ok') {
11286:                 if ($madethumb) {
11287:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
11288:                     my $imagethumb = "$path/tn-".$imagefile;
11289:                     $url = $imagethumb;
11290:                 } else {
11291:                     $url = $imageurl;
11292:                 }
11293:             } else {
11294:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11295:             }
11296:         } else {
11297:             $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);
11298:         }
11299:     } else {
11300:         $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);
11301:     }
11302:     return ($url,$error);
11303: }
11304: 
11305: sub get_ltitools_id {
11306:     my ($cdom,$title) = @_;
11307:     # get lock on ltitools db
11308:     my $lockhash = {
11309:                       lock => $env{'user.name'}.
11310:                               ':'.$env{'user.domain'},
11311:                    };
11312:     my $tries = 0;
11313:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11314:     my ($id,$error);
11315:  
11316:     while (($gotlock ne 'ok') && ($tries<10)) {
11317:         $tries ++;
11318:         sleep (0.1);
11319:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11320:     }
11321:     if ($gotlock eq 'ok') {
11322:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
11323:         if ($currids{'lock'}) {
11324:             delete($currids{'lock'});
11325:             if (keys(%currids)) {
11326:                 my @curr = sort { $a <=> $b } keys(%currids);
11327:                 if ($curr[-1] =~ /^\d+$/) {
11328:                     $id = 1 + $curr[-1];
11329:                 }
11330:             } else {
11331:                 $id = 1;
11332:             }
11333:             if ($id) {
11334:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
11335:                     $error = 'nostore';
11336:                 }
11337:             } else {
11338:                 $error = 'nonumber';
11339:             }
11340:         }
11341:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
11342:     } else {
11343:         $error = 'nolock';
11344:     }
11345:     return ($id,$error);
11346: }
11347: 
11348: sub modify_lti {
11349:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11350:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11351:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11352:     my (%posslti,%posslticrs,%posscrstype);
11353:     my @courseroles = ('cc','in','ta','ep','st');
11354:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
11355:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
11356:     my @coursetypes = ('official','unofficial','community','textbook','placement');
11357:     my %coursetypetitles = &Apache::lonlocal::texthash (
11358:                                official   => 'Official',
11359:                                unofficial => 'Unofficial',
11360:                                community  => 'Community',
11361:                                textbook   => 'Textbook',
11362:                                placement  => 'Placement Test',
11363:     );
11364:     my %lt = &lti_names();
11365:     map { $posslti{$_} = 1; } @ltiroles;
11366:     map { $posslticrs{$_} = 1; } @lticourseroles;
11367:     map { $posscrstype{$_} = 1; } @coursetypes;
11368:     
11369:     my (@items,%deletions,%itemids);
11370:     if ($env{'form.lti_add'}) {
11371:         my $consumer = $env{'form.lti_consumer_add'};
11372:         $consumer =~ s/(`)/'/g;
11373:         ($newid,my $error) = &get_lti_id($dom,$consumer);
11374:         if ($newid) {
11375:             $itemids{'add'} = $newid;
11376:             push(@items,'add');
11377:             $changes{$newid} = 1;
11378:         } else {
11379:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
11380:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11381:         }
11382:     }
11383:     if (ref($domconfig{$action}) eq 'HASH') {
11384:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
11385:         if (@todelete) {
11386:             map { $deletions{$_} = 1; } @todelete;
11387:         }
11388:         my $maxnum = $env{'form.lti_maxnum'};
11389:         for (my $i=0; $i<=$maxnum; $i++) {
11390:             my $itemid = $env{'form.lti_id_'.$i};
11391:             $itemid =~ s/\D+//g;
11392:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11393:                 if ($deletions{$itemid}) {
11394:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
11395:                 } else {
11396:                    push(@items,$i);
11397:                    $itemids{$i} = $itemid;
11398:                 }
11399:             }
11400:         }
11401:     }
11402:     foreach my $idx (@items) {
11403:         my $itemid = $itemids{$idx};
11404:         next unless ($itemid);
11405:         my $position = $env{'form.lti_pos_'.$idx};
11406:         $position =~ s/\D+//g;
11407:         if ($position ne '') {
11408:             $allpos[$position] = $itemid;
11409:         }
11410:         foreach my $item ('consumer','key','secret','lifetime') {
11411:             my $formitem = 'form.lti_'.$item.'_'.$idx;
11412:             $env{$formitem} =~ s/(`)/'/g;
11413:             if ($item eq 'lifetime') {
11414:                 $env{$formitem} =~ s/[^\d.]//g;
11415:             }
11416:             if ($env{$formitem} ne '') {
11417:                 if (($item eq 'key') || ($item eq 'secret')) {
11418:                     $encconfig{$itemid}{$item} = $env{$formitem};
11419:                 } else {
11420:                     $confhash{$itemid}{$item} = $env{$formitem};
11421:                     unless (($idx eq 'add') || ($changes{$itemid})) {
11422:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
11423:                             $changes{$itemid} = 1;
11424:                         }
11425:                     }
11426:                 }
11427:             }
11428:         }
11429:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
11430:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
11431:         }
11432:         if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
11433:             $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
11434:         } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
11435:             $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
11436:         } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
11437:             my $mapuser = $env{'form.lti_customuser_'.$idx};
11438:             $mapuser =~ s/(`)/'/g;
11439:             $mapuser =~ s/^\s+|\s+$//g; 
11440:             $confhash{$itemid}{'mapuser'} = $mapuser; 
11441:         }
11442:         foreach my $ltirole (@lticourseroles) {
11443:             my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
11444:             if (grep(/^\Q$possrole\E$/,@courseroles)) {
11445:                 $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
11446:             }
11447:         }
11448:         my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
11449:         my @makeuser;
11450:         foreach my $ltirole (sort(@possmakeuser)) {
11451:             if ($posslti{$ltirole}) {
11452:                 push(@makeuser,$ltirole);
11453:             }
11454:         }
11455:         $confhash{$itemid}{'makeuser'} = \@makeuser;
11456:         if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
11457:             ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
11458:             $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
11459:         } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
11460:             my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
11461:             $mapcrs =~ s/(`)/'/g;
11462:             $mapcrs =~ s/^\s+|\s+$//g;
11463:             $confhash{$itemid}{'mapcrs'} = $mapcrs;
11464:         }
11465:         my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
11466:         my @crstypes;
11467:         foreach my $type (sort(@posstypes)) {
11468:             if ($posscrstype{$type}) {
11469:                 push(@crstypes,$type);
11470:             }
11471:         }
11472:         $confhash{$itemid}{'mapcrstype'} = \@crstypes;
11473:         if ($env{'form.lti_makecrs_'.$idx}) {
11474:             $confhash{$itemid}{'makecrs'} = 1;
11475:         }
11476:         my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
11477:         my @selfenroll;
11478:         foreach my $type (sort(@possenroll)) {
11479:             if ($posslticrs{$type}) {
11480:                 push(@selfenroll,$type);
11481:             }
11482:         }
11483:         $confhash{$itemid}{'selfenroll'} = \@selfenroll;
11484:         if ($env{'form.lti_crssec_'.$idx}) {
11485:             if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
11486:                 $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
11487:             } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
11488:                 my $section = $env{'form.lti_customsection_'.$idx};
11489:                 $section =~ s/(`)/'/g;
11490:                 $section =~ s/^\s+|\s+$//g;
11491:                 if ($section ne '') {
11492:                     $confhash{$itemid}{'section'} = $section;
11493:                 }
11494:             }
11495:         }
11496:         foreach my $field ('passback','roster') {
11497:             if ($env{'form.lti_'.$field.'_'.$idx}) {
11498:                 $confhash{$itemid}{$field} = 1;
11499:             }
11500:         }
11501:         unless (($idx eq 'add') || ($changes{$itemid})) {
11502:             foreach my $field ('mapuser','mapcrs','section','passback','roster') {
11503:                 if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
11504:                     $changes{$itemid} = 1;
11505:                 }
11506:             }
11507:             foreach my $field ('makeuser','mapcrstype','selfenroll') {
11508:                 unless ($changes{$itemid}) {
11509:                     if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
11510:                         if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
11511:                             my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
11512:                                                                            $confhash{$itemid}{$field});
11513:                             if (@diffs) {
11514:                                 $changes{$itemid} = 1;
11515:                             }
11516:                         } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
11517:                             $changes{$itemid} = 1;
11518:                         }
11519:                     } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
11520:                         if (@{$confhash{$itemid}{$field}} > 0) {
11521:                             $changes{$itemid} = 1;
11522:                         }
11523:                     } 
11524:                 }
11525:             }
11526:             unless ($changes{$itemid}) {
11527:                 if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
11528:                     if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
11529:                         foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
11530:                             if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
11531:                                 $confhash{$itemid}{'maproles'}{$ltirole}) {
11532:                                 $changes{$itemid} = 1;
11533:                                 last;
11534:                             }
11535:                         }
11536:                         unless ($changes{$itemid}) {
11537:                             foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
11538:                                 if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
11539:                                     $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
11540:                                     $changes{$itemid} = 1;
11541:                                     last;
11542:                                 }
11543:                             }
11544:                         }
11545:                     } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
11546:                         $changes{$itemid} = 1;
11547:                     }
11548:                 } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
11549:                     unless ($changes{$itemid}) {
11550:                         if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
11551:                             $changes{$itemid} = 1;
11552:                         }
11553:                     }
11554:                 }
11555:             }
11556:         }
11557:     }
11558:     if (@allpos > 0) {
11559:         my $idx = 0;
11560:         foreach my $itemid (@allpos) {
11561:             if ($itemid ne '') {
11562:                 $confhash{$itemid}{'order'} = $idx;
11563:                 if (ref($domconfig{$action}) eq 'HASH') {
11564:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11565:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
11566:                             $changes{$itemid} = 1;
11567:                         }
11568:                     }
11569:                 }
11570:                 $idx ++;
11571:             }
11572:         }
11573:     }
11574:     my %ltihash = (
11575:                           $action => { %confhash }
11576:                        );
11577:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
11578:                                              $dom);
11579:     if ($putresult eq 'ok') {
11580:         my %ltienchash = (
11581:                              $action => { %encconfig }
11582:                          );
11583:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
11584:         if (keys(%changes) > 0) {
11585:             my $cachetime = 24*60*60;
11586:             my %ltiall = %confhash;
11587:             foreach my $id (keys(%ltiall)) {
11588:                 if (ref($encconfig{$id}) eq 'HASH') {
11589:                     foreach my $item ('key','secret') {
11590:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
11591:                     }
11592:                 }
11593:             }
11594:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
11595:             if (ref($lastactref) eq 'HASH') {
11596:                 $lastactref->{'lti'} = 1;
11597:             }
11598:             $resulttext = &mt('Changes made:').'<ul>';
11599:             my %bynum;
11600:             foreach my $itemid (sort(keys(%changes))) {
11601:                 my $position = $confhash{$itemid}{'order'};
11602:                 $bynum{$position} = $itemid;
11603:             }
11604:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
11605:                 my $itemid = $bynum{$pos};
11606:                 if (ref($confhash{$itemid}) ne 'HASH') {
11607:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
11608:                 } else {
11609:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
11610:                     my $position = $pos + 1;
11611:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
11612:                     foreach my $item ('version','lifetime') {
11613:                         if ($confhash{$itemid}{$item} ne '') {
11614:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
11615:                         }
11616:                     }
11617:                     if ($encconfig{$itemid}{'key'} ne '') {
11618:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
11619:                     }
11620:                     if ($encconfig{$itemid}{'secret'} ne '') {
11621:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
11622:                         my $num = length($encconfig{$itemid}{'secret'});
11623:                         $resulttext .= ('*'x$num).'</li>';
11624:                     }
11625:                     if ($confhash{$itemid}{'mapuser'}) {
11626:                         my $shownmapuser;
11627:                         if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
11628:                             $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
11629:                         } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
11630:                             $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
11631:                         } else {
11632:                             $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
11633:                         } 
11634:                         $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
11635:                     }
11636:                     if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
11637:                         my $rolemaps;
11638:                         foreach my $role (@ltiroles) {
11639:                             if ($confhash{$itemid}{'maproles'}{$role}) {
11640:                                 $rolemaps .= ('&nbsp;'x2).$role.'='.
11641:                                              &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
11642:                                                                         'Course').',';
11643:                             }
11644:                         }
11645:                         if ($rolemaps) {
11646:                             $rolemaps =~ s/,$//;
11647:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
11648:                         }
11649:                     }
11650:                     if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
11651:                         if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
11652:                             $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
11653:                                                       join(', ',@{$confhash{$itemid}{'makeuser'}})).'</li>';
11654:                         } else {
11655:                             $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
11656:                         }
11657:                     }
11658:                     if ($confhash{$itemid}{'mapcrs'}) {
11659:                         $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
11660:                     }
11661:                     if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
11662:                         if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
11663:                             $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
11664:                                            join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
11665:                                            '</li>';
11666:                         } else {
11667:                             $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
11668:                         }
11669:                     }
11670:                     if ($confhash{$itemid}{'makecrs'}) {
11671:                         $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
11672:                     } else {
11673:                         $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
11674:                     }
11675:                     if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
11676:                         if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
11677:                             $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
11678:                                                       join(', ',@{$confhash{$itemid}{'selfenroll'}})).
11679:                                            '</li>';
11680:                         } else {
11681:                             $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
11682:                         }
11683:                     }
11684:                     if ($confhash{$itemid}{'section'}) {
11685:                         if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
11686:                             $resulttext .= '<li>'.&mt('User section from standard field:').
11687:                                                  ' (course_section_sourcedid)'.'</li>';  
11688:                         } else {
11689:                             $resulttext .= '<li>'.&mt('User section from:').' '.
11690:                                                   $confhash{$itemid}{'section'}.'</li>';
11691:                         }
11692:                     } else {
11693:                         $resulttext .= '<li>'.&mt('No section assignment').'</li>';
11694:                     }
11695:                     foreach my $item ('passback','roster') {
11696:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
11697:                         if ($confhash{$itemid}{$item}) {
11698:                             $resulttext .= &mt('Yes');
11699:                         } else {
11700:                             $resulttext .= &mt('No');
11701:                         }
11702:                         $resulttext .= '</li>';
11703:                     }
11704:                     $resulttext .= '</ul></li>';
11705:                 }
11706:             }
11707:             $resulttext .= '</ul>';
11708:         } else {
11709:             $resulttext = &mt('No changes made.');
11710:         }
11711:     } else {
11712:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11713:     }
11714:     if ($errors) {
11715:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11716:                        $errors.'</ul>';
11717:     }
11718:     return $resulttext;
11719: }
11720: 
11721: sub get_lti_id {
11722:     my ($domain,$consumer) = @_;
11723:     # get lock on lti db
11724:     my $lockhash = {
11725:                       lock => $env{'user.name'}.
11726:                               ':'.$env{'user.domain'},
11727:                    };
11728:     my $tries = 0;
11729:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
11730:     my ($id,$error);
11731: 
11732:     while (($gotlock ne 'ok') && ($tries<10)) {
11733:         $tries ++;
11734:         sleep (0.1);
11735:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
11736:     }
11737:     if ($gotlock eq 'ok') {
11738:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
11739:         if ($currids{'lock'}) {
11740:             delete($currids{'lock'});
11741:             if (keys(%currids)) {
11742:                 my @curr = sort { $a <=> $b } keys(%currids);
11743:                 if ($curr[-1] =~ /^\d+$/) {
11744:                     $id = 1 + $curr[-1];
11745:                 }
11746:             } else {
11747:                 $id = 1;
11748:             }
11749:             if ($id) {
11750:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
11751:                     $error = 'nostore';
11752:                 }
11753:             } else {
11754:                 $error = 'nonumber';
11755:             }
11756:         }
11757:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
11758:     } else {
11759:         $error = 'nolock';
11760:     }
11761:     return ($id,$error);
11762: }
11763: 
11764: sub modify_autoenroll {
11765:     my ($dom,$lastactref,%domconfig) = @_;
11766:     my ($resulttext,%changes);
11767:     my %currautoenroll;
11768:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
11769:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
11770:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
11771:         }
11772:     }
11773:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
11774:     my %title = ( run => 'Auto-enrollment active',
11775:                   sender => 'Sender for notification messages',
11776:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
11777:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
11778:     my @offon = ('off','on');
11779:     my $sender_uname = $env{'form.sender_uname'};
11780:     my $sender_domain = $env{'form.sender_domain'};
11781:     if ($sender_domain eq '') {
11782:         $sender_uname = '';
11783:     } elsif ($sender_uname eq '') {
11784:         $sender_domain = '';
11785:     }
11786:     my $coowners = $env{'form.autoassign_coowners'};
11787:     my $failsafe = $env{'form.autoenroll_failsafe'};
11788:     $failsafe =~ s{^\s+|\s+$}{}g;
11789:     if ($failsafe =~ /\D/) {
11790:         undef($failsafe);
11791:     }
11792:     my %autoenrollhash =  (
11793:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
11794:                                        'sender_uname' => $sender_uname,
11795:                                        'sender_domain' => $sender_domain,
11796:                                        'co-owners' => $coowners,
11797:                                        'autofailsafe' => $failsafe,
11798:                                 }
11799:                      );
11800:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
11801:                                              $dom);
11802:     if ($putresult eq 'ok') {
11803:         if (exists($currautoenroll{'run'})) {
11804:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
11805:                  $changes{'run'} = 1;
11806:              }
11807:         } elsif ($autorun) {
11808:             if ($env{'form.autoenroll_run'} ne '1') {
11809:                  $changes{'run'} = 1;
11810:             }
11811:         }
11812:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
11813:             $changes{'sender'} = 1;
11814:         }
11815:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
11816:             $changes{'sender'} = 1;
11817:         }
11818:         if ($currautoenroll{'co-owners'} ne '') {
11819:             if ($currautoenroll{'co-owners'} ne $coowners) {
11820:                 $changes{'coowners'} = 1;
11821:             }
11822:         } elsif ($coowners) {
11823:             $changes{'coowners'} = 1;
11824:         }
11825:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
11826:             $changes{'autofailsafe'} = 1;
11827:         }
11828:         if (keys(%changes) > 0) {
11829:             $resulttext = &mt('Changes made:').'<ul>';
11830:             if ($changes{'run'}) {
11831:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
11832:             }
11833:             if ($changes{'sender'}) {
11834:                 if ($sender_uname eq '' || $sender_domain eq '') {
11835:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
11836:                 } else {
11837:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
11838:                 }
11839:             }
11840:             if ($changes{'coowners'}) {
11841:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
11842:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11843:                 if (ref($lastactref) eq 'HASH') {
11844:                     $lastactref->{'domainconfig'} = 1;
11845:                 }
11846:             }
11847:             if ($changes{'autofailsafe'}) {
11848:                 if ($failsafe ne '') {
11849:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
11850:                 } else {
11851:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
11852:                 }
11853:                 &Apache::lonnet::get_domain_defaults($dom,1);
11854:                 if (ref($lastactref) eq 'HASH') {
11855:                     $lastactref->{'domdefaults'} = 1;
11856:                 }
11857:             }
11858:             $resulttext .= '</ul>';
11859:         } else {
11860:             $resulttext = &mt('No changes made to auto-enrollment settings');
11861:         }
11862:     } else {
11863:         $resulttext = '<span class="LC_error">'.
11864: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11865:     }
11866:     return $resulttext;
11867: }
11868: 
11869: sub modify_autoupdate {
11870:     my ($dom,%domconfig) = @_;
11871:     my ($resulttext,%currautoupdate,%fields,%changes);
11872:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
11873:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
11874:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
11875:         }
11876:     }
11877:     my @offon = ('off','on');
11878:     my %title = &Apache::lonlocal::texthash (
11879:                    run => 'Auto-update:',
11880:                    classlists => 'Updates to user information in classlists?'
11881:                 );
11882:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11883:     my %fieldtitles = &Apache::lonlocal::texthash (
11884:                         id => 'Student/Employee ID',
11885:                         permanentemail => 'E-mail address',
11886:                         lastname => 'Last Name',
11887:                         firstname => 'First Name',
11888:                         middlename => 'Middle Name',
11889:                         generation => 'Generation',
11890:                       );
11891:     $othertitle = &mt('All users');
11892:     if (keys(%{$usertypes}) >  0) {
11893:         $othertitle = &mt('Other users');
11894:     }
11895:     foreach my $key (keys(%env)) {
11896:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
11897:             my ($usertype,$item) = ($1,$2);
11898:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
11899:                 if ($usertype eq 'default') {   
11900:                     push(@{$fields{$1}},$2);
11901:                 } elsif (ref($types) eq 'ARRAY') {
11902:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
11903:                         push(@{$fields{$1}},$2);
11904:                     }
11905:                 }
11906:             }
11907:         }
11908:     }
11909:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
11910:     @lockablenames = sort(@lockablenames);
11911:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
11912:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11913:         if (@changed) {
11914:             $changes{'lockablenames'} = 1;
11915:         }
11916:     } else {
11917:         if (@lockablenames) {
11918:             $changes{'lockablenames'} = 1;
11919:         }
11920:     }
11921:     my %updatehash = (
11922:                       autoupdate => { run => $env{'form.autoupdate_run'},
11923:                                       classlists => $env{'form.classlists'},
11924:                                       fields => {%fields},
11925:                                       lockablenames => \@lockablenames,
11926:                                     }
11927:                      );
11928:     foreach my $key (keys(%currautoupdate)) {
11929:         if (($key eq 'run') || ($key eq 'classlists')) {
11930:             if (exists($updatehash{autoupdate}{$key})) {
11931:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
11932:                     $changes{$key} = 1;
11933:                 }
11934:             }
11935:         } elsif ($key eq 'fields') {
11936:             if (ref($currautoupdate{$key}) eq 'HASH') {
11937:                 foreach my $item (@{$types},'default') {
11938:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
11939:                         my $change = 0;
11940:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
11941:                             if (!exists($fields{$item})) {
11942:                                 $change = 1;
11943:                                 last;
11944:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
11945:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
11946:                                     $change = 1;
11947:                                     last;
11948:                                 }
11949:                             }
11950:                         }
11951:                         if ($change) {
11952:                             push(@{$changes{$key}},$item);
11953:                         }
11954:                     } 
11955:                 }
11956:             }
11957:         } elsif ($key eq 'lockablenames') {
11958:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
11959:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11960:                 if (@changed) {
11961:                     $changes{'lockablenames'} = 1;
11962:                 }
11963:             } else {
11964:                 if (@lockablenames) {
11965:                     $changes{'lockablenames'} = 1;
11966:                 }
11967:             }
11968:         }
11969:     }
11970:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
11971:         if (@lockablenames) {
11972:             $changes{'lockablenames'} = 1;
11973:         }
11974:     }
11975:     foreach my $item (@{$types},'default') {
11976:         if (defined($fields{$item})) {
11977:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
11978:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
11979:                     my $change = 0;
11980:                     if (ref($fields{$item}) eq 'ARRAY') {
11981:                         foreach my $type (@{$fields{$item}}) {
11982:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
11983:                                 $change = 1;
11984:                                 last;
11985:                             }
11986:                         }
11987:                     }
11988:                     if ($change) {
11989:                         push(@{$changes{'fields'}},$item);
11990:                     }
11991:                 } else {
11992:                     push(@{$changes{'fields'}},$item);
11993:                 }
11994:             } else {
11995:                 push(@{$changes{'fields'}},$item);
11996:             }
11997:         }
11998:     }
11999:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
12000:                                              $dom);
12001:     if ($putresult eq 'ok') {
12002:         if (keys(%changes) > 0) {
12003:             $resulttext = &mt('Changes made:').'<ul>';
12004:             foreach my $key (sort(keys(%changes))) {
12005:                 if ($key eq 'lockablenames') {
12006:                     $resulttext .= '<li>';
12007:                     if (@lockablenames) {
12008:                         $usertypes->{'default'} = $othertitle;
12009:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
12010:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
12011:                     } else {
12012:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
12013:                     }
12014:                     $resulttext .= '</li>';
12015:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
12016:                     foreach my $item (@{$changes{$key}}) {
12017:                         my @newvalues;
12018:                         foreach my $type (@{$fields{$item}}) {
12019:                             push(@newvalues,$fieldtitles{$type});
12020:                         }
12021:                         my $newvaluestr;
12022:                         if (@newvalues > 0) {
12023:                             $newvaluestr = join(', ',@newvalues);
12024:                         } else {
12025:                             $newvaluestr = &mt('none');
12026:                         }
12027:                         if ($item eq 'default') {
12028:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
12029:                         } else {
12030:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
12031:                         }
12032:                     }
12033:                 } else {
12034:                     my $newvalue;
12035:                     if ($key eq 'run') {
12036:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
12037:                     } else {
12038:                         $newvalue = $offon[$env{'form.'.$key}];
12039:                     }
12040:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
12041:                 }
12042:             }
12043:             $resulttext .= '</ul>';
12044:         } else {
12045:             $resulttext = &mt('No changes made to autoupdates');
12046:         }
12047:     } else {
12048:         $resulttext = '<span class="LC_error">'.
12049: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12050:     }
12051:     return $resulttext;
12052: }
12053: 
12054: sub modify_autocreate {
12055:     my ($dom,%domconfig) = @_;
12056:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
12057:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
12058:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
12059:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
12060:         }
12061:     }
12062:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
12063:                  req => 'Auto-creation of validated requests for official courses',
12064:                  xmldc => 'Identity of course creator of courses from XML files',
12065:                );
12066:     my @types = ('xml','req');
12067:     foreach my $item (@types) {
12068:         $newvals{$item} = $env{'form.autocreate_'.$item};
12069:         $newvals{$item} =~ s/\D//g;
12070:         $newvals{$item} = 0 if ($newvals{$item} eq '');
12071:     }
12072:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
12073:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12074:     unless (exists($domcoords{$newvals{'xmldc'}})) {
12075:         $newvals{'xmldc'} = '';
12076:     } 
12077:     %autocreatehash =  (
12078:                         autocreate => { xml => $newvals{'xml'},
12079:                                         req => $newvals{'req'},
12080:                                       }
12081:                        );
12082:     if ($newvals{'xmldc'} ne '') {
12083:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
12084:     }
12085:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
12086:                                              $dom);
12087:     if ($putresult eq 'ok') {
12088:         my @items = @types;
12089:         if ($newvals{'xml'}) {
12090:             push(@items,'xmldc');
12091:         }
12092:         foreach my $item (@items) {
12093:             if (exists($currautocreate{$item})) {
12094:                 if ($currautocreate{$item} ne $newvals{$item}) {
12095:                     $changes{$item} = 1;
12096:                 }
12097:             } elsif ($newvals{$item}) {
12098:                 $changes{$item} = 1;
12099:             }
12100:         }
12101:         if (keys(%changes) > 0) {
12102:             my @offon = ('off','on'); 
12103:             $resulttext = &mt('Changes made:').'<ul>';
12104:             foreach my $item (@types) {
12105:                 if ($changes{$item}) {
12106:                     my $newtxt = $offon[$newvals{$item}];
12107:                     $resulttext .= '<li>'.
12108:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
12109:                                        '<b>','</b>').
12110:                                    '</li>';
12111:                 }
12112:             }
12113:             if ($changes{'xmldc'}) {
12114:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
12115:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
12116:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
12117:             }
12118:             $resulttext .= '</ul>';
12119:         } else {
12120:             $resulttext = &mt('No changes made to auto-creation settings');
12121:         }
12122:     } else {
12123:         $resulttext = '<span class="LC_error">'.
12124:             &mt('An error occurred: [_1]',$putresult).'</span>';
12125:     }
12126:     return $resulttext;
12127: }
12128: 
12129: sub modify_directorysrch {
12130:     my ($dom,$lastactref,%domconfig) = @_;
12131:     my ($resulttext,%changes);
12132:     my %currdirsrch;
12133:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
12134:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
12135:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
12136:         }
12137:     }
12138:     my %title = ( available => 'Institutional directory search available',
12139:                   localonly => 'Other domains can search institution',
12140:                   lcavailable => 'LON-CAPA directory search available',
12141:                   lclocalonly => 'Other domains can search LON-CAPA domain',
12142:                   searchby => 'Search types',
12143:                   searchtypes => 'Search latitude');
12144:     my @offon = ('off','on');
12145:     my @otherdoms = ('Yes','No');
12146: 
12147:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
12148:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
12149:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
12150: 
12151:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12152:     if (keys(%{$usertypes}) == 0) {
12153:         @cansearch = ('default');
12154:     } else {
12155:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
12156:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
12157:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
12158:                     push(@{$changes{'cansearch'}},$type);
12159:                 }
12160:             }
12161:             foreach my $type (@cansearch) {
12162:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
12163:                     push(@{$changes{'cansearch'}},$type);
12164:                 }
12165:             }
12166:         } else {
12167:             push(@{$changes{'cansearch'}},@cansearch);
12168:         }
12169:     }
12170: 
12171:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
12172:         foreach my $by (@{$currdirsrch{'searchby'}}) {
12173:             if (!grep(/^\Q$by\E$/,@searchby)) {
12174:                 push(@{$changes{'searchby'}},$by);
12175:             }
12176:         }
12177:         foreach my $by (@searchby) {
12178:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
12179:                 push(@{$changes{'searchby'}},$by);
12180:             }
12181:         }
12182:     } else {
12183:         push(@{$changes{'searchby'}},@searchby);
12184:     }
12185: 
12186:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
12187:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
12188:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
12189:                 push(@{$changes{'searchtypes'}},$type);
12190:             }
12191:         }
12192:         foreach my $type (@searchtypes) {
12193:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
12194:                 push(@{$changes{'searchtypes'}},$type);
12195:             }
12196:         }
12197:     } else {
12198:         if (exists($currdirsrch{'searchtypes'})) {
12199:             foreach my $type (@searchtypes) {  
12200:                 if ($type ne $currdirsrch{'searchtypes'}) { 
12201:                     push(@{$changes{'searchtypes'}},$type);
12202:                 }
12203:             }
12204:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
12205:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
12206:             }   
12207:         } else {
12208:             push(@{$changes{'searchtypes'}},@searchtypes); 
12209:         }
12210:     }
12211: 
12212:     my %dirsrch_hash =  (
12213:             directorysrch => { available => $env{'form.dirsrch_available'},
12214:                                cansearch => \@cansearch,
12215:                                localonly => $env{'form.dirsrch_instlocalonly'},
12216:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
12217:                                lcavailable => $env{'form.dirsrch_domavailable'},
12218:                                searchby => \@searchby,
12219:                                searchtypes => \@searchtypes,
12220:                              }
12221:             );
12222:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
12223:                                              $dom);
12224:     if ($putresult eq 'ok') {
12225:         if (exists($currdirsrch{'available'})) {
12226:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
12227:                  $changes{'available'} = 1;
12228:              }
12229:         } else {
12230:             if ($env{'form.dirsrch_available'} eq '1') {
12231:                 $changes{'available'} = 1;
12232:             }
12233:         }
12234:         if (exists($currdirsrch{'lcavailable'})) {
12235:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
12236:                 $changes{'lcavailable'} = 1;
12237:             }
12238:         } else {
12239:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
12240:                 $changes{'lcavailable'} = 1;
12241:             }
12242:         }
12243:         if (exists($currdirsrch{'localonly'})) {
12244:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
12245:                 $changes{'localonly'} = 1;
12246:             }
12247:         } else {
12248:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
12249:                 $changes{'localonly'} = 1;
12250:             }
12251:         }
12252:         if (exists($currdirsrch{'lclocalonly'})) {
12253:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
12254:                 $changes{'lclocalonly'} = 1;
12255:             }
12256:         } else {
12257:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
12258:                 $changes{'lclocalonly'} = 1;
12259:             }
12260:         }
12261:         if (keys(%changes) > 0) {
12262:             $resulttext = &mt('Changes made:').'<ul>';
12263:             if ($changes{'available'}) {
12264:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
12265:             }
12266:             if ($changes{'lcavailable'}) {
12267:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
12268:             }
12269:             if ($changes{'localonly'}) {
12270:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
12271:             }
12272:             if ($changes{'lclocalonly'}) {
12273:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
12274:             }
12275:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
12276:                 my $chgtext;
12277:                 if (ref($usertypes) eq 'HASH') {
12278:                     if (keys(%{$usertypes}) > 0) {
12279:                         foreach my $type (@{$types}) {
12280:                             if (grep(/^\Q$type\E$/,@cansearch)) {
12281:                                 $chgtext .= $usertypes->{$type}.'; ';
12282:                             }
12283:                         }
12284:                         if (grep(/^default$/,@cansearch)) {
12285:                             $chgtext .= $othertitle;
12286:                         } else {
12287:                             $chgtext =~ s/\; $//;
12288:                         }
12289:                         $resulttext .=
12290:                             '<li>'.
12291:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
12292:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
12293:                             '</li>';
12294:                     }
12295:                 }
12296:             }
12297:             if (ref($changes{'searchby'}) eq 'ARRAY') {
12298:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
12299:                 my $chgtext;
12300:                 foreach my $type (@{$titleorder}) {
12301:                     if (grep(/^\Q$type\E$/,@searchby)) {
12302:                         if (defined($searchtitles->{$type})) {
12303:                             $chgtext .= $searchtitles->{$type}.'; ';
12304:                         }
12305:                     }
12306:                 }
12307:                 $chgtext =~ s/\; $//;
12308:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
12309:             }
12310:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
12311:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
12312:                 my $chgtext;
12313:                 foreach my $type (@{$srchtypeorder}) {
12314:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
12315:                         if (defined($srchtypes_desc->{$type})) {
12316:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
12317:                         }
12318:                     }
12319:                 }
12320:                 $chgtext =~ s/\; $//;
12321:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
12322:             }
12323:             $resulttext .= '</ul>';
12324:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
12325:             if (ref($lastactref) eq 'HASH') {
12326:                 $lastactref->{'directorysrch'} = 1;
12327:             }
12328:         } else {
12329:             $resulttext = &mt('No changes made to directory search settings');
12330:         }
12331:     } else {
12332:         $resulttext = '<span class="LC_error">'.
12333:                       &mt('An error occurred: [_1]',$putresult).'</span>';
12334:     }
12335:     return $resulttext;
12336: }
12337: 
12338: sub modify_contacts {
12339:     my ($dom,$lastactref,%domconfig) = @_;
12340:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
12341:     if (ref($domconfig{'contacts'}) eq 'HASH') {
12342:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
12343:             $currsetting{$key} = $domconfig{'contacts'}{$key};
12344:         }
12345:     }
12346:     my (%others,%to,%bcc,%includestr,%includeloc);
12347:     my @contacts = ('supportemail','adminemail');
12348:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
12349:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
12350:     my @toggles = ('reporterrors','reportupdates');
12351:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
12352:     foreach my $type (@mailings) {
12353:         @{$newsetting{$type}} = 
12354:             &Apache::loncommon::get_env_multiple('form.'.$type);
12355:         foreach my $item (@contacts) {
12356:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
12357:                 $contacts_hash{contacts}{$type}{$item} = 1;
12358:             } else {
12359:                 $contacts_hash{contacts}{$type}{$item} = 0;
12360:             }
12361:         }
12362:         $others{$type} = $env{'form.'.$type.'_others'};
12363:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
12364:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12365:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
12366:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
12367:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
12368:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
12369:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
12370:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
12371:             }
12372:         }
12373:     }
12374:     foreach my $item (@contacts) {
12375:         $to{$item} = $env{'form.'.$item};
12376:         $contacts_hash{'contacts'}{$item} = $to{$item};
12377:     }
12378:     foreach my $item (@toggles) {
12379:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
12380:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
12381:         }
12382:     }
12383:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
12384:         foreach my $field (@{$fields}) {
12385:             if (ref($possoptions->{$field}) eq 'ARRAY') {
12386:                 my $value = $env{'form.helpform_'.$field};
12387:                 $value =~ s/^\s+|\s+$//g;
12388:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
12389:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
12390:                     if ($field eq 'screenshot') {
12391:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
12392:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
12393:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
12394:                         }
12395:                     }
12396:                 }
12397:             }
12398:         }
12399:     }
12400:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12401:     my (@statuses,%usertypeshash,@overrides);
12402:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
12403:         @statuses = @{$types};
12404:         if (ref($usertypes) eq 'HASH') {
12405:             %usertypeshash = %{$usertypes};
12406:         }
12407:     }
12408:     if (@statuses) {
12409:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
12410:         foreach my $type (@possoverrides) {
12411:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
12412:                 push(@overrides,$type);
12413:             }
12414:         }
12415:         if (@overrides) {
12416:             foreach my $type (@overrides) {
12417:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
12418:                 foreach my $item (@contacts) {
12419:                     if (grep(/^\Q$item\E$/,@standard)) {
12420:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
12421:                         $newsetting{'override_'.$type}{$item} = 1;
12422:                     } else {
12423:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
12424:                         $newsetting{'override_'.$type}{$item} = 0;
12425:                     }
12426:                 }
12427:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
12428:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
12429:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
12430:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
12431:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
12432:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
12433:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
12434:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
12435:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
12436:                 }
12437:             }     
12438:         }
12439:     }
12440:     if (keys(%currsetting) > 0) {
12441:         foreach my $item (@contacts) {
12442:             if ($to{$item} ne $currsetting{$item}) {
12443:                 $changes{$item} = 1;
12444:             }
12445:         }
12446:         foreach my $type (@mailings) {
12447:             foreach my $item (@contacts) {
12448:                 if (ref($currsetting{$type}) eq 'HASH') {
12449:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
12450:                         push(@{$changes{$type}},$item);
12451:                     }
12452:                 } else {
12453:                     push(@{$changes{$type}},@{$newsetting{$type}});
12454:                 }
12455:             }
12456:             if ($others{$type} ne $currsetting{$type}{'others'}) {
12457:                 push(@{$changes{$type}},'others');
12458:             }
12459:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12460:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
12461:                     push(@{$changes{$type}},'bcc'); 
12462:                 }
12463:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
12464:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
12465:                     push(@{$changes{$type}},'include');
12466:                 }
12467:             }
12468:         }
12469:         if (ref($fields) eq 'ARRAY') {
12470:             if (ref($currsetting{'helpform'}) eq 'HASH') {
12471:                 foreach my $field (@{$fields}) {
12472:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
12473:                         push(@{$changes{'helpform'}},$field);
12474:                     }
12475:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
12476:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
12477:                             push(@{$changes{'helpform'}},'maxsize');
12478:                         }
12479:                     }
12480:                 }
12481:             } else {
12482:                 foreach my $field (@{$fields}) {
12483:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
12484:                         push(@{$changes{'helpform'}},$field);
12485:                     }
12486:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
12487:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
12488:                             push(@{$changes{'helpform'}},'maxsize');
12489:                         }
12490:                     }
12491:                 }
12492:             }
12493:         }
12494:         if (@statuses) {
12495:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
12496:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
12497:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
12498:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
12499:                             foreach my $item (@contacts,'bcc','others','include') {
12500:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
12501:                                     push(@{$changes{'overrides'}},$key);
12502:                                     last;
12503:                                 }
12504:                             }
12505:                         } else {
12506:                             push(@{$changes{'overrides'}},$key);
12507:                         }
12508:                     }
12509:                 }
12510:                 foreach my $key (@overrides) {
12511:                     unless (exists($currsetting{'overrides'}{$key})) {
12512:                         push(@{$changes{'overrides'}},$key);
12513:                     }
12514:                 }
12515:             } else {
12516:                 foreach my $key (@overrides) {
12517:                     push(@{$changes{'overrides'}},$key); 
12518:                 }
12519:             }
12520:         }
12521:     } else {
12522:         my %default;
12523:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
12524:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
12525:         $default{'errormail'} = 'adminemail';
12526:         $default{'packagesmail'} = 'adminemail';
12527:         $default{'helpdeskmail'} = 'supportemail';
12528:         $default{'otherdomsmail'} = 'supportemail';
12529:         $default{'lonstatusmail'} = 'adminemail';
12530:         $default{'requestsmail'} = 'adminemail';
12531:         $default{'updatesmail'} = 'adminemail';
12532:         foreach my $item (@contacts) {
12533:            if ($to{$item} ne $default{$item}) {
12534:                $changes{$item} = 1;
12535:            }
12536:         }
12537:         foreach my $type (@mailings) {
12538:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
12539:                 push(@{$changes{$type}},@{$newsetting{$type}});
12540:             }
12541:             if ($others{$type} ne '') {
12542:                 push(@{$changes{$type}},'others');
12543:             }
12544:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12545:                 if ($bcc{$type} ne '') {
12546:                     push(@{$changes{$type}},'bcc');
12547:                 }
12548:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
12549:                     push(@{$changes{$type}},'include');
12550:                 }
12551:             }
12552:         }
12553:         if (ref($fields) eq 'ARRAY') {
12554:             foreach my $field (@{$fields}) {
12555:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
12556:                     push(@{$changes{'helpform'}},$field);
12557:                 }
12558:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
12559:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
12560:                         push(@{$changes{'helpform'}},'maxsize');
12561:                     }
12562:                 }
12563:             }
12564:         }
12565:     }
12566:     foreach my $item (@toggles) {
12567:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
12568:             $changes{$item} = 1;
12569:         } elsif ((!$env{'form.'.$item}) &&
12570:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
12571:             $changes{$item} = 1;
12572:         }
12573:     }
12574:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
12575:                                              $dom);
12576:     if ($putresult eq 'ok') {
12577:         if (keys(%changes) > 0) {
12578:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12579:             if (ref($lastactref) eq 'HASH') {
12580:                 $lastactref->{'domainconfig'} = 1;
12581:             }
12582:             my ($titles,$short_titles)  = &contact_titles();
12583:             $resulttext = &mt('Changes made:').'<ul>';
12584:             foreach my $item (@contacts) {
12585:                 if ($changes{$item}) {
12586:                     $resulttext .= '<li>'.$titles->{$item}.
12587:                                     &mt(' set to: ').
12588:                                     '<span class="LC_cusr_emph">'.
12589:                                     $to{$item}.'</span></li>';
12590:                 }
12591:             }
12592:             foreach my $type (@mailings) {
12593:                 if (ref($changes{$type}) eq 'ARRAY') {
12594:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12595:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
12596:                     } else {
12597:                         $resulttext .= '<li>'.$titles->{$type}.': ';
12598:                     }
12599:                     my @text;
12600:                     foreach my $item (@{$newsetting{$type}}) {
12601:                         push(@text,$short_titles->{$item});
12602:                     }
12603:                     if ($others{$type} ne '') {
12604:                         push(@text,$others{$type});
12605:                     }
12606:                     if (@text) {
12607:                         $resulttext .= '<span class="LC_cusr_emph">'.
12608:                                        join(', ',@text).'</span>';
12609:                     }
12610:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12611:                         if ($bcc{$type} ne '') {
12612:                             my $bcctext;
12613:                             if (@text) {
12614:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
12615:                             } else {
12616:                                 $bcctext = '(Bcc)';
12617:                             }
12618:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
12619:                         } elsif (!@text) {
12620:                             $resulttext .= &mt('No one');
12621:                         }   
12622:                         if ($includestr{$type} ne '') {
12623:                             if ($includeloc{$type} eq 'b') {
12624:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
12625:                             } elsif ($includeloc{$type} eq 's') {
12626:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
12627:                             }
12628:                         }
12629:                     } elsif (!@text) {
12630:                         $resulttext .= &mt('No recipients');
12631:                     }
12632:                     $resulttext .= '</li>';
12633:                 }
12634:             }
12635:             if (ref($changes{'overrides'}) eq 'ARRAY') {
12636:                 my @deletions;
12637:                 foreach my $type (@{$changes{'overrides'}}) {
12638:                     if ($usertypeshash{$type}) {
12639:                         if (grep(/^\Q$type\E/,@overrides)) {
12640:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
12641:                                                       $usertypeshash{$type}).'<ul><li>';
12642:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
12643:                                 my @text;
12644:                                 foreach my $item (@contacts) {
12645:                                     if ($newsetting{'override_'.$type}{$item}) { 
12646:                                         push(@text,$short_titles->{$item});
12647:                                     }
12648:                                 }
12649:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
12650:                                     push(@text,$newsetting{'override_'.$type}{'others'});
12651:                                 }
12652:   
12653:                                 if (@text) {
12654:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
12655:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
12656:                                 }
12657:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
12658:                                     my $bcctext;
12659:                                     if (@text) {
12660:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
12661:                                     } else {
12662:                                         $bcctext = '(Bcc)';
12663:                                     }
12664:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
12665:                                 } elsif (!@text) {
12666:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
12667:                                 }
12668:                                 $resulttext .= '</li>';
12669:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
12670:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
12671:                                     if ($loc eq 'b') {
12672:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
12673:                                     } elsif ($loc eq 's') {
12674:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
12675:                                     }
12676:                                 }
12677:                             }
12678:                             $resulttext .= '</li></ul></li>';
12679:                         } else {
12680:                             push(@deletions,$usertypeshash{$type});
12681:                         }
12682:                     }
12683:                 }
12684:                 if (@deletions) {
12685:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
12686:                                               join(', ',@deletions)).'</li>';
12687:                 }
12688:             }
12689:             my @offon = ('off','on');
12690:             if ($changes{'reporterrors'}) {
12691:                 $resulttext .= '<li>'.
12692:                                &mt('E-mail error reports to [_1] set to "'.
12693:                                    $offon[$env{'form.reporterrors'}].'".',
12694:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
12695:                                        &mt('LON-CAPA core group - MSU'),600,500)).
12696:                                '</li>';
12697:             }
12698:             if ($changes{'reportupdates'}) {
12699:                 $resulttext .= '<li>'.
12700:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
12701:                                     $offon[$env{'form.reportupdates'}].'".',
12702:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
12703:                                         &mt('LON-CAPA core group - MSU'),600,500)).
12704:                                 '</li>';
12705:             }
12706:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
12707:                 my (@optional,@required,@unused,$maxsizechg);
12708:                 foreach my $field (@{$changes{'helpform'}}) {
12709:                     if ($field eq 'maxsize') {
12710:                         $maxsizechg = 1;
12711:                         next;
12712:                     }
12713:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
12714:                         push(@optional,$field);
12715:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
12716:                         push(@unused,$field);
12717:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
12718:                         push(@required,$field);
12719:                     }
12720:                 }
12721:                 if (@optional) {
12722:                     $resulttext .= '<li>'.
12723:                                    &mt('Help form fields changed to "Optional": [_1].',
12724:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
12725:                                    '</li>';
12726:                 }
12727:                 if (@required) {
12728:                     $resulttext .= '<li>'.
12729:                                    &mt('Help form fields changed to "Required": [_1].',
12730:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
12731:                                    '</li>';
12732:                 }
12733:                 if (@unused) {
12734:                     $resulttext .= '<li>'.
12735:                                    &mt('Help form fields changed to "Not shown": [_1].',
12736:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
12737:                                    '</li>';
12738:                 }
12739:                 if ($maxsizechg) {
12740:                     $resulttext .= '<li>'.
12741:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
12742:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
12743:                                    '</li>';
12744:                 }
12745:             }
12746:             $resulttext .= '</ul>';
12747:         } else {
12748:             $resulttext = &mt('No changes made to contacts and form settings');
12749:         }
12750:     } else {
12751:         $resulttext = '<span class="LC_error">'.
12752:             &mt('An error occurred: [_1].',$putresult).'</span>';
12753:     }
12754:     return $resulttext;
12755: }
12756: 
12757: sub modify_usercreation {
12758:     my ($dom,%domconfig) = @_;
12759:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
12760:     my $warningmsg;
12761:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
12762:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
12763:             if ($key eq 'cancreate') {
12764:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
12765:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
12766:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
12767:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12768:                         } else {
12769:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12770:                         }
12771:                     }
12772:                 }
12773:             } elsif ($key eq 'email_rule') {
12774:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
12775:             } else {
12776:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
12777:             }
12778:         }
12779:     }
12780:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
12781:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
12782:     my @contexts = ('author','course','requestcrs');
12783:     foreach my $item(@contexts) {
12784:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
12785:     }
12786:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12787:         foreach my $item (@contexts) {
12788:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
12789:                 push(@{$changes{'cancreate'}},$item);
12790:             }
12791:         }
12792:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
12793:         foreach my $item (@contexts) {
12794:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
12795:                 if ($cancreate{$item} ne 'any') {
12796:                     push(@{$changes{'cancreate'}},$item);
12797:                 }
12798:             } else {
12799:                 if ($cancreate{$item} ne 'none') {
12800:                     push(@{$changes{'cancreate'}},$item);
12801:                 }
12802:             }
12803:         }
12804:     } else {
12805:         foreach my $item (@contexts)  {
12806:             push(@{$changes{'cancreate'}},$item);
12807:         }
12808:     }
12809: 
12810:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
12811:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
12812:             if (!grep(/^\Q$type\E$/,@username_rule)) {
12813:                 push(@{$changes{'username_rule'}},$type);
12814:             }
12815:         }
12816:         foreach my $type (@username_rule) {
12817:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
12818:                 push(@{$changes{'username_rule'}},$type);
12819:             }
12820:         }
12821:     } else {
12822:         push(@{$changes{'username_rule'}},@username_rule);
12823:     }
12824: 
12825:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
12826:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
12827:             if (!grep(/^\Q$type\E$/,@id_rule)) {
12828:                 push(@{$changes{'id_rule'}},$type);
12829:             }
12830:         }
12831:         foreach my $type (@id_rule) {
12832:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
12833:                 push(@{$changes{'id_rule'}},$type);
12834:             }
12835:         }
12836:     } else {
12837:         push(@{$changes{'id_rule'}},@id_rule);
12838:     }
12839: 
12840:     my @authen_contexts = ('author','course','domain');
12841:     my @authtypes = ('int','krb4','krb5','loc');
12842:     my %authhash;
12843:     foreach my $item (@authen_contexts) {
12844:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
12845:         foreach my $auth (@authtypes) {
12846:             if (grep(/^\Q$auth\E$/,@authallowed)) {
12847:                 $authhash{$item}{$auth} = 1;
12848:             } else {
12849:                 $authhash{$item}{$auth} = 0;
12850:             }
12851:         }
12852:     }
12853:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
12854:         foreach my $item (@authen_contexts) {
12855:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
12856:                 foreach my $auth (@authtypes) {
12857:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
12858:                         push(@{$changes{'authtypes'}},$item);
12859:                         last;
12860:                     }
12861:                 }
12862:             }
12863:         }
12864:     } else {
12865:         foreach my $item (@authen_contexts) {
12866:             push(@{$changes{'authtypes'}},$item);
12867:         }
12868:     }
12869: 
12870:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
12871:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
12872:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
12873:     $save_usercreate{'id_rule'} = \@id_rule;
12874:     $save_usercreate{'username_rule'} = \@username_rule,
12875:     $save_usercreate{'authtypes'} = \%authhash;
12876: 
12877:     my %usercreation_hash =  (
12878:         usercreation     => \%save_usercreate,
12879:     );
12880: 
12881:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
12882:                                              $dom);
12883: 
12884:     if ($putresult eq 'ok') {
12885:         if (keys(%changes) > 0) {
12886:             $resulttext = &mt('Changes made:').'<ul>';
12887:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12888:                 my %lt = &usercreation_types();
12889:                 foreach my $type (@{$changes{'cancreate'}}) {
12890:                     my $chgtext = $lt{$type}.', ';
12891:                     if ($cancreate{$type} eq 'none') {
12892:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
12893:                     } elsif ($cancreate{$type} eq 'any') {
12894:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
12895:                     } elsif ($cancreate{$type} eq 'official') {
12896:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
12897:                     } elsif ($cancreate{$type} eq 'unofficial') {
12898:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
12899:                     }
12900:                     $resulttext .= '<li>'.$chgtext.'</li>';
12901:                 }
12902:             }
12903:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
12904:                 my ($rules,$ruleorder) = 
12905:                     &Apache::lonnet::inst_userrules($dom,'username');
12906:                 my $chgtext = '<ul>';
12907:                 foreach my $type (@username_rule) {
12908:                     if (ref($rules->{$type}) eq 'HASH') {
12909:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
12910:                     }
12911:                 }
12912:                 $chgtext .= '</ul>';
12913:                 if (@username_rule > 0) {
12914:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
12915:                 } else {
12916:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
12917:                 }
12918:             }
12919:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
12920:                 my ($idrules,$idruleorder) = 
12921:                     &Apache::lonnet::inst_userrules($dom,'id');
12922:                 my $chgtext = '<ul>';
12923:                 foreach my $type (@id_rule) {
12924:                     if (ref($idrules->{$type}) eq 'HASH') {
12925:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
12926:                     }
12927:                 }
12928:                 $chgtext .= '</ul>';
12929:                 if (@id_rule > 0) {
12930:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
12931:                 } else {
12932:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
12933:                 }
12934:             }
12935:             my %authname = &authtype_names();
12936:             my %context_title = &context_names();
12937:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
12938:                 my $chgtext = '<ul>';
12939:                 foreach my $type (@{$changes{'authtypes'}}) {
12940:                     my @allowed;
12941:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
12942:                     foreach my $auth (@authtypes) {
12943:                         if ($authhash{$type}{$auth}) {
12944:                             push(@allowed,$authname{$auth});
12945:                         }
12946:                     }
12947:                     if (@allowed > 0) {
12948:                         $chgtext .= join(', ',@allowed).'</li>';
12949:                     } else {
12950:                         $chgtext .= &mt('none').'</li>';
12951:                     }
12952:                 }
12953:                 $chgtext .= '</ul>';
12954:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
12955:                 $resulttext .= '</li>';
12956:             }
12957:             $resulttext .= '</ul>';
12958:         } else {
12959:             $resulttext = &mt('No changes made to user creation settings');
12960:         }
12961:     } else {
12962:         $resulttext = '<span class="LC_error">'.
12963:             &mt('An error occurred: [_1]',$putresult).'</span>';
12964:     }
12965:     if ($warningmsg ne '') {
12966:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12967:     }
12968:     return $resulttext;
12969: }
12970: 
12971: sub modify_selfcreation {
12972:     my ($dom,$lastactref,%domconfig) = @_;
12973:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
12974:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
12975:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12976:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
12977:     if (ref($typesref) eq 'ARRAY') {
12978:         @types = @{$typesref};
12979:     }
12980:     if (ref($usertypesref) eq 'HASH') {
12981:         %usertypes = %{$usertypesref};
12982:     }
12983:     $usertypes{'default'} = $othertitle;
12984: #
12985: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
12986: #
12987:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
12988:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
12989:             if ($key eq 'cancreate') {
12990:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
12991:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
12992:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
12993:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
12994:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
12995:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
12996:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
12997:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
12998:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12999:                         } else {
13000:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13001:                         }
13002:                     }
13003:                 }
13004:             } elsif ($key eq 'email_rule') {
13005:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13006:             } else {
13007:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13008:             }
13009:         }
13010:     }
13011: #
13012: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
13013: #
13014:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13015:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13016:             if ($key eq 'selfcreate') {
13017:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
13018:             } else {
13019:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
13020:             }
13021:         }
13022:     }
13023: #
13024: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
13025: #
13026:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13027:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
13028:             if ($key eq 'inststatusguest') {
13029:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
13030:             } else {
13031:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
13032:             }
13033:         }
13034:     }
13035: 
13036:     my @contexts = ('selfcreate');
13037:     @{$cancreate{'selfcreate'}} = ();
13038:     %{$cancreate{'emailusername'}} = ();
13039:     if (@types) {
13040:         @{$cancreate{'statustocreate'}} = ();
13041:     }
13042:     %{$cancreate{'selfcreateprocessing'}} = ();
13043:     %{$cancreate{'shibenv'}} = ();
13044:     %{$cancreate{'emailverified'}} = ();
13045:     %{$cancreate{'emailoptions'}} = ();
13046:     %{$cancreate{'emaildomain'}} = ();
13047:     my %selfcreatetypes = (
13048:                              sso   => 'users authenticated by institutional single sign on',
13049:                              login => 'users authenticated by institutional log-in',
13050:                              email => 'users verified by e-mail',
13051:                           );
13052: #
13053: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
13054: # is permitted.
13055: #
13056:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
13057: 
13058:     my (@statuses,%email_rule);
13059:     foreach my $item ('login','sso','email') {
13060:         if ($item eq 'email') {
13061:             if ($env{'form.cancreate_email'}) {
13062:                 if (@types) {
13063:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
13064:                     foreach my $status (@poss_statuses) {
13065:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
13066:                             push(@statuses,$status);
13067:                         }
13068:                     }
13069:                     $save_inststatus{'inststatusguest'} = \@statuses;
13070:                 } else {
13071:                     push(@statuses,'default');
13072:                 }
13073:                 if (@statuses) {
13074:                     my %curr_rule;
13075:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
13076:                         foreach my $type (@statuses) {
13077:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
13078:                         }
13079:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
13080:                         foreach my $type (@statuses) {
13081:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
13082:                         }
13083:                     }
13084:                     push(@{$cancreate{'selfcreate'}},'email');
13085:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
13086:                     my %curremaildom;
13087:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
13088:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
13089:                     }
13090:                     foreach my $type (@statuses) {
13091:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
13092:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
13093:                         }
13094:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
13095:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
13096:                         }
13097:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
13098: #
13099: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
13100: #
13101:                             my $chosen = $1;
13102:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
13103:                                 my $emaildom;
13104:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
13105:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
13106:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
13107:                                     if (ref($curremaildom{$type}) eq 'HASH') {
13108:                                         if (exists($curremaildom{$type}{$chosen})) {
13109:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
13110:                                                 push(@{$changes{'cancreate'}},'emaildomain');
13111:                                             }
13112:                                         } elsif ($emaildom ne '') {
13113:                                             push(@{$changes{'cancreate'}},'emaildomain');
13114:                                         }
13115:                                     } elsif ($emaildom ne '') {
13116:                                         push(@{$changes{'cancreate'}},'emaildomain');
13117:                                     } 
13118:                                 }
13119:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13120:                             } elsif ($chosen eq 'custom') {
13121:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
13122:                                 $email_rule{$type} = [];
13123:                                 if (ref($emailrules) eq 'HASH') {
13124:                                     foreach my $rule (@possemail_rules) {
13125:                                         if (exists($emailrules->{$rule})) {
13126:                                             push(@{$email_rule{$type}},$rule);
13127:                                         }
13128:                                     }
13129:                                 }
13130:                                 if (@{$email_rule{$type}}) {
13131:                                     $cancreate{'emailoptions'}{$type} = 'custom';
13132:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
13133:                                         if (@{$curr_rule{$type}} > 0) {
13134:                                             foreach my $rule (@{$curr_rule{$type}}) {
13135:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
13136:                                                     push(@{$changes{'email_rule'}},$type);
13137:                                                 }
13138:                                             }
13139:                                         }
13140:                                         foreach my $type (@{$email_rule{$type}}) {
13141:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
13142:                                                 push(@{$changes{'email_rule'}},$type);
13143:                                             }
13144:                                         }
13145:                                     } else {
13146:                                         push(@{$changes{'email_rule'}},$type);
13147:                                     }
13148:                                 }
13149:                             } else {
13150:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13151:                             }
13152:                         }
13153:                     }
13154:                     if (@types) {
13155:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13156:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
13157:                             if (@changed) {
13158:                                 push(@{$changes{'inststatus'}},'inststatusguest');
13159:                             }
13160:                         } else {
13161:                             push(@{$changes{'inststatus'}},'inststatusguest');
13162:                         }
13163:                     }
13164:                 } else {
13165:                     delete($env{'form.cancreate_email'});
13166:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13167:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13168:                             push(@{$changes{'inststatus'}},'inststatusguest');
13169:                         }
13170:                     }
13171:                 }
13172:             } else {
13173:                 $save_inststatus{'inststatusguest'} = [];
13174:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13175:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13176:                         push(@{$changes{'inststatus'}},'inststatusguest');
13177:                     }
13178:                 }
13179:             }
13180:         } else {
13181:             if ($env{'form.cancreate_'.$item}) {
13182:                 push(@{$cancreate{'selfcreate'}},$item);
13183:             }
13184:         }
13185:     }
13186:     my (%userinfo,%savecaptcha);
13187:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
13188: #
13189: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
13190: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
13191: #
13192: 
13193:     if ($env{'form.cancreate_email'}) {
13194:         push(@contexts,'emailusername');
13195:         if (@statuses) {
13196:             foreach my $type (@statuses) {
13197:                 if (ref($infofields) eq 'ARRAY') {
13198:                     foreach my $field (@{$infofields}) {
13199:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
13200:                             $cancreate{'emailusername'}{$type}{$field} = $1;
13201:                         }
13202:                     }
13203:                 }
13204:             }
13205:         }
13206: #
13207: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
13208: # queued requests for self-creation of account verified by e-mail.
13209: #
13210: 
13211:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
13212:         @approvalnotify = sort(@approvalnotify);
13213:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
13214:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13215:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
13216:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
13217:                     push(@{$changes{'cancreate'}},'notify');
13218:                 }
13219:             } else {
13220:                 if ($cancreate{'notify'}{'approval'}) {
13221:                     push(@{$changes{'cancreate'}},'notify');
13222:                 }
13223:             }
13224:         } elsif ($cancreate{'notify'}{'approval'}) {
13225:             push(@{$changes{'cancreate'}},'notify');
13226:         }
13227: 
13228:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
13229:     }
13230: #  
13231: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
13232: # institutional log-in.
13233: #
13234:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
13235:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
13236:                ($domdefaults{'auth_def'} eq 'localauth'))) {
13237:             $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.').' '.
13238:                           &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.');
13239:         }
13240:     }
13241:     my @fields = ('lastname','firstname','middlename','generation',
13242:                   'permanentemail','id');
13243:     my @shibfields = (@fields,'inststatus');
13244:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13245: #
13246: # Where usernames may created for institutional log-in and/or institutional single sign on:
13247: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
13248: # may self-create accounts 
13249: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
13250: # which the user may supply, if institutional data is unavailable.
13251: #
13252:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
13253:         if (@types) {
13254:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
13255:             push(@contexts,'statustocreate');
13256:             foreach my $type (@types) {
13257:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
13258:                 foreach my $field (@fields) {
13259:                     if (grep(/^\Q$field\E$/,@modifiable)) {
13260:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
13261:                     } else {
13262:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
13263:                     }
13264:                 }
13265:             }
13266:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
13267:                 foreach my $type (@types) {
13268:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
13269:                         foreach my $field (@fields) {
13270:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
13271:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
13272:                                 push(@{$changes{'selfcreate'}},$type);
13273:                                 last;
13274:                             }
13275:                         }
13276:                     }
13277:                 }
13278:             } else {
13279:                 foreach my $type (@types) {
13280:                     push(@{$changes{'selfcreate'}},$type);
13281:                 }
13282:             }
13283:         }
13284:         foreach my $field (@shibfields) {
13285:             if ($env{'form.shibenv_'.$field} ne '') {
13286:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
13287:             }
13288:         }
13289:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13290:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
13291:                 foreach my $field (@shibfields) {
13292:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
13293:                         push(@{$changes{'cancreate'}},'shibenv');
13294:                     }
13295:                 }
13296:             } else {
13297:                 foreach my $field (@shibfields) {
13298:                     if ($env{'form.shibenv_'.$field}) {
13299:                         push(@{$changes{'cancreate'}},'shibenv');
13300:                         last;
13301:                     }
13302:                 }
13303:             }
13304:         }
13305:     }
13306:     foreach my $item (@contexts) {
13307:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
13308:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
13309:                 if (ref($cancreate{$item}) eq 'ARRAY') {
13310:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
13311:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13312:                             push(@{$changes{'cancreate'}},$item);
13313:                         }
13314:                     }
13315:                 }
13316:             }
13317:             if (ref($cancreate{$item}) eq 'ARRAY') {
13318:                 foreach my $type (@{$cancreate{$item}}) {
13319:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
13320:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13321:                             push(@{$changes{'cancreate'}},$item);
13322:                         }
13323:                     }
13324:                 }
13325:             }
13326:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
13327:             if (ref($cancreate{$item}) eq 'HASH') {
13328:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
13329:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13330:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
13331:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13332:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13333:                                     push(@{$changes{'cancreate'}},$item);
13334:                                 }
13335:                             }
13336:                         }
13337:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13338:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13339:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13340:                                 push(@{$changes{'cancreate'}},$item);
13341:                             }
13342:                         }
13343:                     }
13344:                 }
13345:                 foreach my $type (keys(%{$cancreate{$item}})) {
13346:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13347:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13348:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13349:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13350:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13351:                                         push(@{$changes{'cancreate'}},$item);
13352:                                     }
13353:                                 }
13354:                             } else {
13355:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13356:                                     push(@{$changes{'cancreate'}},$item);
13357:                                 }
13358:                             }
13359:                         }
13360:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13361:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13362:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13363:                                 push(@{$changes{'cancreate'}},$item);
13364:                             }
13365:                         }
13366:                     }
13367:                 }
13368:             }
13369:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
13370:             if (ref($cancreate{$item}) eq 'ARRAY') {
13371:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
13372:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13373:                         push(@{$changes{'cancreate'}},$item);
13374:                     }
13375:                 }
13376:             }
13377:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13378:             if (ref($cancreate{$item}) eq 'HASH') {
13379:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13380:                     push(@{$changes{'cancreate'}},$item);
13381:                 }
13382:             }
13383:         } elsif ($item eq 'emailusername') {
13384:             if (ref($cancreate{$item}) eq 'HASH') {
13385:                 foreach my $type (keys(%{$cancreate{$item}})) {
13386:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13387:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13388:                             if ($cancreate{$item}{$type}{$field}) {
13389:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13390:                                     push(@{$changes{'cancreate'}},$item);
13391:                                 }
13392:                                 last;
13393:                             }
13394:                         }
13395:                     }
13396:                 }
13397:             }
13398:         }
13399:     }
13400: #
13401: # Populate %save_usercreate hash with updates to self-creation configuration.
13402: #
13403:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
13404:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
13405:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
13406:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
13407:     if (ref($cancreate{'notify'}) eq 'HASH') {
13408:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
13409:     }
13410:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
13411:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
13412:     }
13413:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
13414:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
13415:     }
13416:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
13417:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
13418:     }
13419:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
13420:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
13421:     }
13422:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13423:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
13424:     }
13425:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
13426:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
13427:     }
13428:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
13429:     $save_usercreate{'email_rule'} = \%email_rule;
13430: 
13431:     my %userconfig_hash = (
13432:             usercreation     => \%save_usercreate,
13433:             usermodification => \%save_usermodify,
13434:             inststatus       => \%save_inststatus,
13435:     );
13436: 
13437:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
13438:                                              $dom);
13439: #
13440: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
13441: #
13442:     if ($putresult eq 'ok') {
13443:         if (keys(%changes) > 0) {
13444:             $resulttext = &mt('Changes made:').'<ul>';
13445:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13446:                 my %lt = &selfcreation_types();
13447:                 foreach my $type (@{$changes{'cancreate'}}) {
13448:                     my $chgtext = '';
13449:                     if ($type eq 'selfcreate') {
13450:                         if (@{$cancreate{$type}} == 0) {
13451:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
13452:                         } else {
13453:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
13454:                                         '<ul>';
13455:                             foreach my $case (@{$cancreate{$type}}) {
13456:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
13457:                             }
13458:                             $chgtext .= '</ul>';
13459:                             if (ref($cancreate{$type}) eq 'ARRAY') {
13460:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
13461:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13462:                                         if (@{$cancreate{'statustocreate'}} == 0) {
13463:                                             $chgtext .= '<span class="LC_warning">'.
13464:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
13465:                                                         '</span><br />';
13466:                                         }
13467:                                     }
13468:                                 }
13469:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
13470:                                     if (!@statuses) {
13471:                                         $chgtext .= '<span class="LC_warning">'.
13472:                                                     &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.").
13473:                                                     '</span><br />';
13474: 
13475:                                     }
13476:                                 }
13477:                             }
13478:                         }
13479:                     } elsif ($type eq 'shibenv') {
13480:                         if (keys(%{$cancreate{$type}}) == 0) {
13481:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
13482:                         } else {
13483:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
13484:                                         '<ul>';
13485:                             foreach my $field (@shibfields) {
13486:                                 next if ($cancreate{$type}{$field} eq '');
13487:                                 if ($field eq 'inststatus') {
13488:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
13489:                                 } else {
13490:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
13491:                                 }
13492:                             }
13493:                             $chgtext .= '</ul>';
13494:                         }
13495:                     } elsif ($type eq 'statustocreate') {
13496:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
13497:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
13498:                             if (@{$cancreate{'selfcreate'}} > 0) {
13499:                                 if (@{$cancreate{'statustocreate'}} == 0) {
13500:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
13501:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
13502:                                         $chgtext .= '<br />'.
13503:                                                     '<span class="LC_warning">'.
13504:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
13505:                                                     '</span>';
13506:                                     }
13507:                                 } elsif (keys(%usertypes) > 0) {
13508:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13509:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
13510:                                     } else {
13511:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
13512:                                     }
13513:                                     $chgtext .= '<ul>';
13514:                                     foreach my $case (@{$cancreate{$type}}) {
13515:                                         if ($case eq 'default') {
13516:                                             $chgtext .= '<li>'.$othertitle.'</li>';
13517:                                         } else {
13518:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
13519:                                         }
13520:                                     }
13521:                                     $chgtext .= '</ul>';
13522:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13523:                                         $chgtext .= '<span class="LC_warning">'.
13524:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
13525:                                                     '</span>';
13526:                                     }
13527:                                 }
13528:                             } else {
13529:                                 if (@{$cancreate{$type}} == 0) {
13530:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
13531:                                 } else {
13532:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
13533:                                 }
13534:                             }
13535:                             $chgtext .= '<br />';
13536:                         }
13537:                     } elsif ($type eq 'selfcreateprocessing') {
13538:                         my %choices = &Apache::lonlocal::texthash (
13539:                                                                     automatic => 'Automatic approval',
13540:                                                                     approval  => 'Queued for approval',
13541:                                                                   );
13542:                         if (@types) {
13543:                             if (@statuses) {
13544:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
13545:                                             '<ul>';
13546:                                 foreach my $status (@statuses) {
13547:                                     if ($status eq 'default') {
13548:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13549:                                     } else {
13550:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13551:                                     }
13552:                                 }
13553:                                 $chgtext .= '</ul>';
13554:                             }
13555:                         } else {
13556:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
13557:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
13558:                         }
13559:                     } elsif ($type eq 'emailverified') {
13560:                         my %options = &Apache::lonlocal::texthash (
13561:                                                                     all   => 'Same as e-mail',
13562:                                                                     first => 'Omit @domain',
13563:                                                                     free  => 'Free to choose',
13564:                                                                   );
13565:                         if (@types) {
13566:                             if (@statuses) {
13567:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
13568:                                             '<ul>';
13569:                                 foreach my $status (@statuses) {
13570:                                     if ($type eq 'default') {
13571:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13572:                                     } else {
13573:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13574:                                     }
13575:                                 }
13576:                                 $chgtext .= '</ul>';
13577:                             }
13578:                         } else {
13579:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
13580:                                             $options{$cancreate{'emailverified'}{'default'}});
13581:                         }
13582:                     } elsif ($type eq 'emailoptions') {
13583:                         my %options = &Apache::lonlocal::texthash (
13584:                                                                     any     => 'Any e-mail',
13585:                                                                     inst    => 'Institutional only',
13586:                                                                     noninst => 'Non-institutional only',
13587:                                                                     custom  => 'Custom restrictions',
13588:                                                                   );
13589:                         if (@types) {
13590:                             if (@statuses) {
13591:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
13592:                                             '<ul>';
13593:                                 foreach my $status (@statuses) {
13594:                                     if ($type eq 'default') {
13595:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13596:                                     } else {
13597:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13598:                                     }
13599:                                 }
13600:                                 $chgtext .= '</ul>';
13601:                             }
13602:                         } else {
13603:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
13604:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
13605:                             } else {
13606:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
13607:                                                 $options{$cancreate{'emailoptions'}{'default'}});
13608:                             }
13609:                         }
13610:                     } elsif ($type eq 'emaildomain') {
13611:                         my $output;
13612:                         if (@statuses) {
13613:                             foreach my $type (@statuses) {
13614:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
13615:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
13616:                                         if ($type eq 'default') {
13617:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13618:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13619:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13620:                                             } else {
13621:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
13622:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
13623:                                             }
13624:                                         } else {
13625:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13626:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13627:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13628:                                             } else {
13629:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
13630:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
13631:                                             }
13632:                                         }
13633:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
13634:                                         if ($type eq 'default') {
13635:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13636:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13637:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13638:                                             } else {
13639:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
13640:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
13641:                                             }
13642:                                         } else {
13643:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13644:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13645:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13646:                                             } else {
13647:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
13648:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
13649:                                             }
13650:                                         }
13651:                                     }
13652:                                 }
13653:                             }
13654:                         }
13655:                         if ($output ne '') {
13656:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
13657:                                         '<ul>'.$output.'</ul>';
13658:                         }
13659:                     } elsif ($type eq 'captcha') {
13660:                         if ($savecaptcha{$type} eq 'notused') {
13661:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
13662:                         } else {
13663:                             my %captchas = &captcha_phrases();
13664:                             if ($captchas{$savecaptcha{$type}}) {
13665:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
13666:                             } else {
13667:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
13668:                             }
13669:                         }
13670:                     } elsif ($type eq 'recaptchakeys') {
13671:                         my ($privkey,$pubkey);
13672:                         if (ref($savecaptcha{$type}) eq 'HASH') {
13673:                             $pubkey = $savecaptcha{$type}{'public'};
13674:                             $privkey = $savecaptcha{$type}{'private'};
13675:                         }
13676:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
13677:                         if (!$pubkey) {
13678:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
13679:                         } else {
13680:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13681:                         }
13682:                         if (!$privkey) {
13683:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
13684:                         } else {
13685:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
13686:                         }
13687:                         $chgtext .= '</ul>';
13688:                     } elsif ($type eq 'recaptchaversion') {
13689:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
13690:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
13691:                         }
13692:                     } elsif ($type eq 'emailusername') {
13693:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
13694:                             if (@statuses) {
13695:                                 foreach my $type (@statuses) {
13696:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
13697:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
13698:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
13699:                                                     '<ul>';
13700:                                             foreach my $field (@{$infofields}) {
13701:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
13702:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
13703:                                                 }
13704:                                             }
13705:                                             $chgtext .= '</ul>';
13706:                                         } else {
13707:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13708:                                         }
13709:                                     } else {
13710:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13711:                                     }
13712:                                 }
13713:                             }
13714:                         }
13715:                     } elsif ($type eq 'notify') {
13716:                         my $numapprove = 0;
13717:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
13718:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
13719:                                 if ($cancreate{'notify'}{'approval'}) {
13720:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
13721:                                     $numapprove ++;
13722:                                 }
13723:                             }
13724:                         }
13725:                         unless ($numapprove) {
13726:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
13727:                         }
13728:                     }
13729:                     if ($chgtext) {
13730:                         $resulttext .= '<li>'.$chgtext.'</li>';
13731:                     }
13732:                 }
13733:             }
13734:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
13735:                 my ($emailrules,$emailruleorder) =
13736:                     &Apache::lonnet::inst_userrules($dom,'email');
13737:                 foreach my $type (@{$changes{'email_rule'}}) {
13738:                     if (ref($email_rule{$type}) eq 'ARRAY') {
13739:                         my $chgtext = '<ul>';
13740:                         foreach my $rule (@{$email_rule{$type}}) {
13741:                             if (ref($emailrules->{$rule}) eq 'HASH') {
13742:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
13743:                             }
13744:                         }
13745:                         $chgtext .= '</ul>';
13746:                         my $typename;
13747:                         if (@types) {
13748:                             if ($type eq 'default') {
13749:                                 $typename = $othertitle;
13750:                             } else {
13751:                                 $typename = $usertypes{$type};
13752:                             } 
13753:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
13754:                         }
13755:                         if (@{$email_rule{$type}} > 0) {
13756:                             $resulttext .= '<li>'.
13757:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
13758:                                                $usertypes{$type}).
13759:                                            $chgtext.
13760:                                            '</li>';
13761:                         } else {
13762:                             $resulttext .= '<li>'.
13763:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
13764:                                            '</li>'.
13765:                                            &mt('(Affiliation: [_1])',$typename);
13766:                         }
13767:                     }
13768:                 }
13769:             }
13770:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
13771:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
13772:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
13773:                         my $chgtext = '<ul>';
13774:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
13775:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
13776:                         }
13777:                         $chgtext .= '</ul>';
13778:                         $resulttext .= '<li>'.
13779:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
13780:                                           $chgtext.
13781:                                        '</li>';
13782:                     } else {
13783:                         $resulttext .= '<li>'.
13784:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
13785:                                        '</li>';
13786:                     }
13787:                 }
13788:             }
13789:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
13790:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
13791:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13792:                 foreach my $type (@{$changes{'selfcreate'}}) {
13793:                     my $typename = $type;
13794:                     if (keys(%usertypes) > 0) {
13795:                         if ($usertypes{$type} ne '') {
13796:                             $typename = $usertypes{$type};
13797:                         }
13798:                     }
13799:                     my @modifiable;
13800:                     $resulttext .= '<li>'.
13801:                                     &mt('Self-creation of account by users with status: [_1]',
13802:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
13803:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
13804:                     foreach my $field (@fields) {
13805:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
13806:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
13807:                         }
13808:                     }
13809:                     if (@modifiable > 0) {
13810:                         $resulttext .= join(', ',@modifiable);
13811:                     } else {
13812:                         $resulttext .= &mt('none');
13813:                     }
13814:                     $resulttext .= '</li>';
13815:                 }
13816:                 $resulttext .= '</ul></li>';
13817:             }
13818:             $resulttext .= '</ul>';
13819:             my $cachetime = 24*60*60;
13820:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
13821:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13822:             if (ref($lastactref) eq 'HASH') {
13823:                 $lastactref->{'domdefaults'} = 1;
13824:             }
13825:         } else {
13826:             $resulttext = &mt('No changes made to self-creation settings');
13827:         }
13828:     } else {
13829:         $resulttext = '<span class="LC_error">'.
13830:             &mt('An error occurred: [_1]',$putresult).'</span>';
13831:     }
13832:     if ($warningmsg ne '') {
13833:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13834:     }
13835:     return $resulttext;
13836: }
13837: 
13838: sub process_captcha {
13839:     my ($container,$changes,$newsettings,$current) = @_;
13840:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
13841:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
13842:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
13843:         $newsettings->{'captcha'} = 'original';
13844:     }
13845:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
13846:         if ($container eq 'cancreate') {
13847:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13848:                 push(@{$changes->{'cancreate'}},'captcha');
13849:             } elsif (!defined($changes->{'cancreate'})) {
13850:                 $changes->{'cancreate'} = ['captcha'];
13851:             }
13852:         } else {
13853:             $changes->{'captcha'} = 1;
13854:         }
13855:     }
13856:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
13857:     if ($newsettings->{'captcha'} eq 'recaptcha') {
13858:         $newpub = $env{'form.'.$container.'_recaptchapub'};
13859:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
13860:         $newpub =~ s/[^\w\-]//g;
13861:         $newpriv =~ s/[^\w\-]//g;
13862:         $newsettings->{'recaptchakeys'} = {
13863:                                              public  => $newpub,
13864:                                              private => $newpriv,
13865:                                           };
13866:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
13867:         $newversion =~ s/\D//g;
13868:         if ($newversion ne '2') {
13869:             $newversion = 1;
13870:         }
13871:         $newsettings->{'recaptchaversion'} = $newversion;
13872:     }
13873:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
13874:         $currpub = $current->{'recaptchakeys'}{'public'};
13875:         $currpriv = $current->{'recaptchakeys'}{'private'};
13876:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
13877:             $newsettings->{'recaptchakeys'} = {
13878:                                                  public  => '',
13879:                                                  private => '',
13880:                                               }
13881:         }
13882:     }
13883:     if ($current->{'captcha'} eq 'recaptcha') {
13884:         $currversion = $current->{'recaptchaversion'};
13885:         if ($currversion ne '2') {
13886:             $currversion = 1;
13887:         }
13888:     }
13889:     if ($currversion ne $newversion) {
13890:         if ($container eq 'cancreate') {
13891:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13892:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
13893:             } elsif (!defined($changes->{'cancreate'})) {
13894:                 $changes->{'cancreate'} = ['recaptchaversion'];
13895:             }
13896:         } else {
13897:             $changes->{'recaptchaversion'} = 1;
13898:         }
13899:     }
13900:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
13901:         if ($container eq 'cancreate') {
13902:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13903:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
13904:             } elsif (!defined($changes->{'cancreate'})) {
13905:                 $changes->{'cancreate'} = ['recaptchakeys'];
13906:             }
13907:         } else {
13908:             $changes->{'recaptchakeys'} = 1;
13909:         }
13910:     }
13911:     return;
13912: }
13913: 
13914: sub modify_usermodification {
13915:     my ($dom,%domconfig) = @_;
13916:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
13917:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13918:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13919:             if ($key eq 'selfcreate') {
13920:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
13921:             } else {  
13922:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
13923:             }
13924:         }
13925:     }
13926:     my @contexts = ('author','course');
13927:     my %context_title = (
13928:                            author => 'In author context',
13929:                            course => 'In course context',
13930:                         );
13931:     my @fields = ('lastname','firstname','middlename','generation',
13932:                   'permanentemail','id');
13933:     my %roles = (
13934:                   author => ['ca','aa'],
13935:                   course => ['st','ep','ta','in','cr'],
13936:                 );
13937:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13938:     foreach my $context (@contexts) {
13939:         foreach my $role (@{$roles{$context}}) {
13940:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
13941:             foreach my $item (@fields) {
13942:                 if (grep(/^\Q$item\E$/,@modifiable)) {
13943:                     $modifyhash{$context}{$role}{$item} = 1;
13944:                 } else {
13945:                     $modifyhash{$context}{$role}{$item} = 0;
13946:                 }
13947:             }
13948:         }
13949:         if (ref($curr_usermodification{$context}) eq 'HASH') {
13950:             foreach my $role (@{$roles{$context}}) {
13951:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
13952:                     foreach my $field (@fields) {
13953:                         if ($modifyhash{$context}{$role}{$field} ne 
13954:                                 $curr_usermodification{$context}{$role}{$field}) {
13955:                             push(@{$changes{$context}},$role);
13956:                             last;
13957:                         }
13958:                     }
13959:                 }
13960:             }
13961:         } else {
13962:             foreach my $context (@contexts) {
13963:                 foreach my $role (@{$roles{$context}}) {
13964:                     push(@{$changes{$context}},$role);
13965:                 }
13966:             }
13967:         }
13968:     }
13969:     my %usermodification_hash =  (
13970:                                    usermodification => \%modifyhash,
13971:                                  );
13972:     my $putresult = &Apache::lonnet::put_dom('configuration',
13973:                                              \%usermodification_hash,$dom);
13974:     if ($putresult eq 'ok') {
13975:         if (keys(%changes) > 0) {
13976:             $resulttext = &mt('Changes made: ').'<ul>';
13977:             foreach my $context (@contexts) {
13978:                 if (ref($changes{$context}) eq 'ARRAY') {
13979:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
13980:                     if (ref($changes{$context}) eq 'ARRAY') {
13981:                         foreach my $role (@{$changes{$context}}) {
13982:                             my $rolename;
13983:                             if ($role eq 'cr') {
13984:                                 $rolename = &mt('Custom');
13985:                             } else {
13986:                                 $rolename = &Apache::lonnet::plaintext($role);
13987:                             }
13988:                             my @modifiable;
13989:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
13990:                             foreach my $field (@fields) {
13991:                                 if ($modifyhash{$context}{$role}{$field}) {
13992:                                     push(@modifiable,$fieldtitles{$field});
13993:                                 }
13994:                             }
13995:                             if (@modifiable > 0) {
13996:                                 $resulttext .= join(', ',@modifiable);
13997:                             } else {
13998:                                 $resulttext .= &mt('none'); 
13999:                             }
14000:                             $resulttext .= '</li>';
14001:                         }
14002:                         $resulttext .= '</ul></li>';
14003:                     }
14004:                 }
14005:             }
14006:             $resulttext .= '</ul>';
14007:         } else {
14008:             $resulttext = &mt('No changes made to user modification settings');
14009:         }
14010:     } else {
14011:         $resulttext = '<span class="LC_error">'.
14012:             &mt('An error occurred: [_1]',$putresult).'</span>';
14013:     }
14014:     return $resulttext;
14015: }
14016: 
14017: sub modify_defaults {
14018:     my ($dom,$lastactref,%domconfig) = @_;
14019:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
14020:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14021:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
14022:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
14023:     my @authtypes = ('internal','krb4','krb5','localauth');
14024:     foreach my $item (@items) {
14025:         $newvalues{$item} = $env{'form.'.$item};
14026:         if ($item eq 'auth_def') {
14027:             if ($newvalues{$item} ne '') {
14028:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
14029:                     push(@errors,$item);
14030:                 }
14031:             }
14032:         } elsif ($item eq 'lang_def') {
14033:             if ($newvalues{$item} ne '') {
14034:                 if ($newvalues{$item} =~ /^(\w+)/) {
14035:                     my $langcode = $1;
14036:                     if ($langcode ne 'x_chef') {
14037:                         if (code2language($langcode) eq '') {
14038:                             push(@errors,$item);
14039:                         }
14040:                     }
14041:                 } else {
14042:                     push(@errors,$item);
14043:                 }
14044:             }
14045:         } elsif ($item eq 'timezone_def') {
14046:             if ($newvalues{$item} ne '') {
14047:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
14048:                     push(@errors,$item);   
14049:                 }
14050:             }
14051:         } elsif ($item eq 'datelocale_def') {
14052:             if ($newvalues{$item} ne '') {
14053:                 my @datelocale_ids = DateTime::Locale->ids();
14054:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
14055:                     push(@errors,$item);
14056:                 }
14057:             }
14058:         } elsif ($item eq 'portal_def') {
14059:             if ($newvalues{$item} ne '') {
14060:                 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])\/?$/) {
14061:                     push(@errors,$item);
14062:                 }
14063:             }
14064:         } elsif ($item eq 'intauth_cost') {
14065:             if ($newvalues{$item} ne '') {
14066:                 if ($newvalues{$item} =~ /\D/) {
14067:                     push(@errors,$item);
14068:                 }
14069:             }
14070:         } elsif ($item eq 'intauth_check') {
14071:             if ($newvalues{$item} ne '') {
14072:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14073:                     push(@errors,$item);
14074:                 }
14075:             }
14076:         } elsif ($item eq 'intauth_switch') {
14077:             if ($newvalues{$item} ne '') {
14078:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14079:                     push(@errors,$item);
14080:                 }
14081:             }
14082:         }
14083:         if (grep(/^\Q$item\E$/,@errors)) {
14084:             $newvalues{$item} = $domdefaults{$item};
14085:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
14086:             $changes{$item} = 1;
14087:         }
14088:         $domdefaults{$item} = $newvalues{$item};
14089:     }
14090:     my %defaults_hash = (
14091:                          defaults => \%newvalues,
14092:                         );
14093:     my $title = &defaults_titles();
14094: 
14095:     my $currinststatus;
14096:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
14097:         $currinststatus = $domconfig{'inststatus'};
14098:     } else {
14099:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14100:         $currinststatus = {
14101:                              inststatustypes => $usertypes,
14102:                              inststatusorder => $types,
14103:                              inststatusguest => [],
14104:                           };
14105:     }
14106:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
14107:     my @allpos;
14108:     my %alltypes;
14109:     my @inststatusguest;
14110:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
14111:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
14112:             unless (grep(/^\Q$type\E$/,@todelete)) {
14113:                 push(@inststatusguest,$type);
14114:             }
14115:         }
14116:     }
14117:     my ($currtitles,$currorder);
14118:     if (ref($currinststatus) eq 'HASH') {
14119:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
14120:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
14121:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
14122:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
14123:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
14124:                     }
14125:                 }
14126:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
14127:                     my $position = $env{'form.inststatus_pos_'.$type};
14128:                     $position =~ s/\D+//g;
14129:                     $allpos[$position] = $type;
14130:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
14131:                     $alltypes{$type} =~ s/`//g;
14132:                 }
14133:             }
14134:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
14135:             $currtitles =~ s/,$//;
14136:         }
14137:     }
14138:     if ($env{'form.addinststatus'}) {
14139:         my $newtype = $env{'form.addinststatus'};
14140:         $newtype =~ s/\W//g;
14141:         unless (exists($alltypes{$newtype})) {
14142:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
14143:             $alltypes{$newtype} =~ s/`//g; 
14144:             my $position = $env{'form.addinststatus_pos'};
14145:             $position =~ s/\D+//g;
14146:             if ($position ne '') {
14147:                 $allpos[$position] = $newtype;
14148:             }
14149:         }
14150:     }
14151:     my @orderedstatus;
14152:     foreach my $type (@allpos) {
14153:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
14154:             push(@orderedstatus,$type);
14155:         }
14156:     }
14157:     foreach my $type (keys(%alltypes)) {
14158:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
14159:             delete($alltypes{$type});
14160:         }
14161:     }
14162:     $defaults_hash{'inststatus'} = {
14163:                                      inststatustypes => \%alltypes,
14164:                                      inststatusorder => \@orderedstatus,
14165:                                      inststatusguest => \@inststatusguest,
14166:                                    };
14167:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
14168:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
14169:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
14170:         }
14171:     }
14172:     if ($currorder ne join(',',@orderedstatus)) {
14173:         $changes{'inststatus'}{'inststatusorder'} = 1;
14174:     }
14175:     my $newtitles;
14176:     foreach my $item (@orderedstatus) {
14177:         $newtitles .= $alltypes{$item}.',';
14178:     }
14179:     $newtitles =~ s/,$//;
14180:     if ($currtitles ne $newtitles) {
14181:         $changes{'inststatus'}{'inststatustypes'} = 1;
14182:     }
14183:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
14184:                                              $dom);
14185:     if ($putresult eq 'ok') {
14186:         if (keys(%changes) > 0) {
14187:             $resulttext = &mt('Changes made:').'<ul>';
14188:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
14189:             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";
14190:             foreach my $item (sort(keys(%changes))) {
14191:                 if ($item eq 'inststatus') {
14192:                     if (ref($changes{'inststatus'}) eq 'HASH') {
14193:                         if (@orderedstatus) {
14194:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
14195:                             foreach my $type (@orderedstatus) { 
14196:                                 $resulttext .= $alltypes{$type}.', ';
14197:                             }
14198:                             $resulttext =~ s/, $//;
14199:                             $resulttext .= '</li>';
14200:                         } else {
14201:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
14202:                         }
14203:                     }
14204:                 } else {
14205:                     my $value = $env{'form.'.$item};
14206:                     if ($value eq '') {
14207:                         $value = &mt('none');
14208:                     } elsif ($item eq 'auth_def') {
14209:                         my %authnames = &authtype_names();
14210:                         my %shortauth = (
14211:                                           internal   => 'int',
14212:                                           krb4       => 'krb4',
14213:                                           krb5       => 'krb5',
14214:                                           localauth  => 'loc',
14215:                         );
14216:                         $value = $authnames{$shortauth{$value}};
14217:                     } elsif ($item eq 'intauth_switch') {
14218:                         my %optiondesc = &Apache::lonlocal::texthash (
14219:                                             0 => 'No',
14220:                                             1 => 'Yes',
14221:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
14222:                                          );
14223:                         if ($value =~ /^(0|1|2)$/) {
14224:                             $value = $optiondesc{$value};
14225:                         } else {
14226:                             $value = &mt('none -- defaults to No');
14227:                         }
14228:                     } elsif ($item eq 'intauth_check') {
14229:                         my %optiondesc = &Apache::lonlocal::texthash (
14230:                                              0 => 'No',
14231:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
14232:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
14233:                                          );
14234:                         if ($value =~ /^(0|1|2)$/) {
14235:                             $value = $optiondesc{$value};
14236:                         } else {
14237:                             $value = &mt('none -- defaults to No');
14238:                         }
14239:                     }
14240:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
14241:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
14242:                 }
14243:             }
14244:             $resulttext .= '</ul>';
14245:             $mailmsgtext .= "\n";
14246:             my $cachetime = 24*60*60;
14247:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14248:             if (ref($lastactref) eq 'HASH') {
14249:                 $lastactref->{'domdefaults'} = 1;
14250:             }
14251:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
14252:                 my $notify = 1;
14253:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
14254:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
14255:                         $notify = 0;
14256:                     }
14257:                 }
14258:                 if ($notify) {
14259:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
14260:                                                "LON-CAPA Domain Settings Change - $dom",
14261:                                                $mailmsgtext);
14262:                 }
14263:             }
14264:         } else {
14265:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
14266:         }
14267:     } else {
14268:         $resulttext = '<span class="LC_error">'.
14269:             &mt('An error occurred: [_1]',$putresult).'</span>';
14270:     }
14271:     if (@errors > 0) {
14272:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
14273:         foreach my $item (@errors) {
14274:             $resulttext .= ' "'.$title->{$item}.'",';
14275:         }
14276:         $resulttext =~ s/,$//;
14277:     }
14278:     return $resulttext;
14279: }
14280: 
14281: sub modify_scantron {
14282:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14283:     my ($resulttext,%confhash,%changes,$errors);
14284:     my $custom = 'custom.tab';
14285:     my $default = 'default.tab';
14286:     my $servadm = $r->dir_config('lonAdmEMail');
14287:     my ($configuserok,$author_ok,$switchserver) = 
14288:         &config_check($dom,$confname,$servadm);
14289:     if ($env{'form.scantronformat.filename'} ne '') {
14290:         my $error;
14291:         if ($configuserok eq 'ok') {
14292:             if ($switchserver) {
14293:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
14294:             } else {
14295:                 if ($author_ok eq 'ok') {
14296:                     my ($result,$scantronurl) =
14297:                         &publishlogo($r,'upload','scantronformat',$dom,
14298:                                      $confname,'scantron','','',$custom);
14299:                     if ($result eq 'ok') {
14300:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
14301:                         $changes{'scantronformat'} = 1;
14302:                     } else {
14303:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
14304:                     }
14305:                 } else {
14306:                     $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);
14307:                 }
14308:             }
14309:         } else {
14310:             $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);
14311:         }
14312:         if ($error) {
14313:             &Apache::lonnet::logthis($error);
14314:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14315:         }
14316:     }
14317:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14318:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
14319:             if ($env{'form.scantronformat_del'}) {
14320:                 $confhash{'scantron'}{'scantronformat'} = '';
14321:                 $changes{'scantronformat'} = 1;
14322:             }
14323:         }
14324:     }
14325:     if (keys(%confhash) > 0) {
14326:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
14327:                                                  $dom);
14328:         if ($putresult eq 'ok') {
14329:             if (keys(%changes) > 0) {
14330:                 if (ref($confhash{'scantron'}) eq 'HASH') {
14331:                     $resulttext = &mt('Changes made:').'<ul>';
14332:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
14333:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
14334:                     } else {
14335:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
14336:                     }
14337:                     $resulttext .= '</ul>';
14338:                 } else {
14339:                     $resulttext = &mt('Changes made to bubblesheet format file.');
14340:                 }
14341:                 $resulttext .= '</ul>';
14342:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14343:                 if (ref($lastactref) eq 'HASH') {
14344:                     $lastactref->{'domainconfig'} = 1;
14345:                 }
14346:             } else {
14347:                 $resulttext = &mt('No changes made to bubblesheet format file');
14348:             }
14349:         } else {
14350:             $resulttext = '<span class="LC_error">'.
14351:                 &mt('An error occurred: [_1]',$putresult).'</span>';
14352:         }
14353:     } else {
14354:         $resulttext = &mt('No changes made to bubblesheet format file'); 
14355:     }
14356:     if ($errors) {
14357:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14358:                        $errors.'</ul>';
14359:     }
14360:     return $resulttext;
14361: }
14362: 
14363: sub modify_coursecategories {
14364:     my ($dom,$lastactref,%domconfig) = @_;
14365:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
14366:         $cathash);
14367:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
14368:     my @catitems = ('unauth','auth');
14369:     my @cattypes = ('std','domonly','codesrch','none');
14370:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14371:         $cathash = $domconfig{'coursecategories'}{'cats'};
14372:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
14373:             $changes{'togglecats'} = 1;
14374:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
14375:         }
14376:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
14377:             $changes{'categorize'} = 1;
14378:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
14379:         }
14380:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
14381:             $changes{'togglecatscomm'} = 1;
14382:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
14383:         }
14384:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
14385:             $changes{'categorizecomm'} = 1;
14386:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
14387: 
14388:         }
14389:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
14390:             $changes{'togglecatsplace'} = 1;
14391:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
14392:         }
14393:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
14394:             $changes{'categorizeplace'} = 1;
14395:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
14396:         }
14397:         foreach my $item (@catitems) {
14398:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14399:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
14400:                     $changes{$item} = 1;
14401:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14402:                 }
14403:             }
14404:         }
14405:     } else {
14406:         $changes{'togglecats'} = 1;
14407:         $changes{'categorize'} = 1;
14408:         $changes{'togglecatscomm'} = 1;
14409:         $changes{'categorizecomm'} = 1;
14410:         $changes{'togglecatsplace'} = 1;
14411:         $changes{'categorizeplace'} = 1;
14412:         $domconfig{'coursecategories'} = {
14413:                                              togglecats => $env{'form.togglecats'},
14414:                                              categorize => $env{'form.categorize'},
14415:                                              togglecatscomm => $env{'form.togglecatscomm'},
14416:                                              categorizecomm => $env{'form.categorizecomm'},
14417:                                              togglecatsplace => $env{'form.togglecatsplace'},
14418:                                              categorizeplace => $env{'form.categorizeplace'},
14419:                                          };
14420:         foreach my $item (@catitems) {
14421:             if ($env{'form.coursecat_'.$item} ne 'std') {
14422:                 $changes{$item} = 1;
14423:             }
14424:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14425:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14426:             }
14427:         }
14428:     }
14429:     if (ref($cathash) eq 'HASH') {
14430:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
14431:             push (@deletecategory,'instcode::0');
14432:         }
14433:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
14434:             push(@deletecategory,'communities::0');
14435:         }
14436:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
14437:             push(@deletecategory,'placement::0');
14438:         }
14439:     }
14440:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
14441:     if (ref($cathash) eq 'HASH') {
14442:         if (@deletecategory > 0) {
14443:             #FIXME Need to remove category from all courses using a deleted category 
14444:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
14445:             foreach my $item (@deletecategory) {
14446:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
14447:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
14448:                     $deletions{$item} = 1;
14449:                     &recurse_cat_deletes($item,$cathash,\%deletions);
14450:                 }
14451:             }
14452:         }
14453:         foreach my $item (keys(%{$cathash})) {
14454:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14455:             if ($cathash->{$item} ne $env{'form.'.$item}) {
14456:                 $reorderings{$item} = 1;
14457:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
14458:             }
14459:             if ($env{'form.addcategory_name_'.$item} ne '') {
14460:                 my $newcat = $env{'form.addcategory_name_'.$item};
14461:                 my $newdepth = $depth+1;
14462:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14463:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
14464:                 $adds{$newitem} = 1; 
14465:             }
14466:             if ($env{'form.subcat_'.$item} ne '') {
14467:                 my $newcat = $env{'form.subcat_'.$item};
14468:                 my $newdepth = $depth+1;
14469:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14470:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
14471:                 $adds{$newitem} = 1;
14472:             }
14473:         }
14474:     }
14475:     if ($env{'form.instcode'} eq '1') {
14476:         if (ref($cathash) eq 'HASH') {
14477:             my $newitem = 'instcode::0';
14478:             if ($cathash->{$newitem} eq '') {  
14479:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14480:                 $adds{$newitem} = 1;
14481:             }
14482:         } else {
14483:             my $newitem = 'instcode::0';
14484:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14485:             $adds{$newitem} = 1;
14486:         }
14487:     }
14488:     if ($env{'form.communities'} eq '1') {
14489:         if (ref($cathash) eq 'HASH') {
14490:             my $newitem = 'communities::0';
14491:             if ($cathash->{$newitem} eq '') {
14492:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14493:                 $adds{$newitem} = 1;
14494:             }
14495:         } else {
14496:             my $newitem = 'communities::0';
14497:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14498:             $adds{$newitem} = 1;
14499:         }
14500:     }
14501:     if ($env{'form.placement'} eq '1') {
14502:         if (ref($cathash) eq 'HASH') {
14503:             my $newitem = 'placement::0';
14504:             if ($cathash->{$newitem} eq '') {
14505:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
14506:                 $adds{$newitem} = 1;
14507:             }
14508:         } else {
14509:             my $newitem = 'placement::0';
14510:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
14511:             $adds{$newitem} = 1;
14512:         }
14513:     }
14514:     if ($env{'form.addcategory_name'} ne '') {
14515:         if (($env{'form.addcategory_name'} ne 'instcode') &&
14516:             ($env{'form.addcategory_name'} ne 'communities') &&
14517:             ($env{'form.addcategory_name'} ne 'placement')) {
14518:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
14519:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
14520:             $adds{$newitem} = 1;
14521:         }
14522:     }
14523:     my $putresult;
14524:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14525:         if (keys(%deletions) > 0) {
14526:             foreach my $key (keys(%deletions)) {
14527:                 if ($predelallitems{$key} ne '') {
14528:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
14529:                 }
14530:             }
14531:         }
14532:         my (@chkcats,@chktrails,%chkallitems);
14533:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
14534:         if (ref($chkcats[0]) eq 'ARRAY') {
14535:             my $depth = 0;
14536:             my $chg = 0;
14537:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
14538:                 my $name = $chkcats[0][$i];
14539:                 my $item;
14540:                 if ($name eq '') {
14541:                     $chg ++;
14542:                 } else {
14543:                     $item = &escape($name).'::0';
14544:                     if ($chg) {
14545:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
14546:                     }
14547:                     $depth ++; 
14548:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
14549:                     $depth --;
14550:                 }
14551:             }
14552:         }
14553:     }
14554:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14555:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
14556:         if ($putresult eq 'ok') {
14557:             my %title = (
14558:                          togglecats     => 'Show/Hide a course in catalog',
14559:                          categorize     => 'Assign a category to a course',
14560:                          togglecatscomm => 'Show/Hide a community in catalog',
14561:                          categorizecomm => 'Assign a category to a community',
14562:                         );
14563:             my %level = (
14564:                          dom  => 'set in Domain ("Modify Course/Community")',
14565:                          crs  => 'set in Course ("Course Configuration")',
14566:                          comm => 'set in Community ("Community Configuration")',
14567:                          none     => 'No catalog',
14568:                          std      => 'Standard catalog',
14569:                          domonly  => 'Domain-only catalog',
14570:                          codesrch => 'Code search form',
14571:                         );
14572:             $resulttext = &mt('Changes made:').'<ul>';
14573:             if ($changes{'togglecats'}) {
14574:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
14575:             }
14576:             if ($changes{'categorize'}) {
14577:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
14578:             }
14579:             if ($changes{'togglecatscomm'}) {
14580:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
14581:             }
14582:             if ($changes{'categorizecomm'}) {
14583:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
14584:             }
14585:             if ($changes{'unauth'}) {
14586:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
14587:             }
14588:             if ($changes{'auth'}) {
14589:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
14590:             }
14591:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14592:                 my $cathash;
14593:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14594:                     $cathash = $domconfig{'coursecategories'}{'cats'};
14595:                 } else {
14596:                     $cathash = {};
14597:                 } 
14598:                 my (@cats,@trails,%allitems);
14599:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
14600:                 if (keys(%deletions) > 0) {
14601:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
14602:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
14603:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
14604:                     }
14605:                     $resulttext .= '</ul></li>';
14606:                 }
14607:                 if (keys(%reorderings) > 0) {
14608:                     my %sort_by_trail;
14609:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
14610:                     foreach my $key (keys(%reorderings)) {
14611:                         if ($allitems{$key} ne '') {
14612:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14613:                         }
14614:                     }
14615:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14616:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14617:                     }
14618:                     $resulttext .= '</ul></li>';
14619:                 }
14620:                 if (keys(%adds) > 0) {
14621:                     my %sort_by_trail;
14622:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
14623:                     foreach my $key (keys(%adds)) {
14624:                         if ($allitems{$key} ne '') {
14625:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14626:                         }
14627:                     }
14628:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14629:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14630:                     }
14631:                     $resulttext .= '</ul></li>';
14632:                 }
14633:             }
14634:             $resulttext .= '</ul>';
14635:             if ($changes{'unauth'} || $changes{'auth'}) {
14636:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
14637:                 if ($changes{'auth'}) {
14638:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
14639:                 }
14640:                 if ($changes{'unauth'}) {
14641:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
14642:                 }
14643:                 my $cachetime = 24*60*60;
14644:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14645:                 if (ref($lastactref) eq 'HASH') {
14646:                     $lastactref->{'domdefaults'} = 1;
14647:                 }
14648:             }
14649:         } else {
14650:             $resulttext = '<span class="LC_error">'.
14651:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14652:         }
14653:     } else {
14654:         $resulttext = &mt('No changes made to course and community categories');
14655:     }
14656:     return $resulttext;
14657: }
14658: 
14659: sub modify_serverstatuses {
14660:     my ($dom,%domconfig) = @_;
14661:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
14662:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
14663:         %currserverstatus = %{$domconfig{'serverstatuses'}};
14664:     }
14665:     my @pages = &serverstatus_pages();
14666:     foreach my $type (@pages) {
14667:         $newserverstatus{$type}{'namedusers'} = '';
14668:         $newserverstatus{$type}{'machines'} = '';
14669:         if (defined($env{'form.'.$type.'_namedusers'})) {
14670:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
14671:             my @okusers;
14672:             foreach my $user (@users) {
14673:                 my ($uname,$udom) = split(/:/,$user);
14674:                 if (($udom =~ /^$match_domain$/) &&   
14675:                     (&Apache::lonnet::domain($udom)) &&
14676:                     ($uname =~ /^$match_username$/)) {
14677:                     if (!grep(/^\Q$user\E/,@okusers)) {
14678:                         push(@okusers,$user);
14679:                     }
14680:                 }
14681:             }
14682:             if (@okusers > 0) {
14683:                  @okusers = sort(@okusers);
14684:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
14685:             }
14686:         }
14687:         if (defined($env{'form.'.$type.'_machines'})) {
14688:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
14689:             my @okmachines;
14690:             foreach my $ip (@machines) {
14691:                 my @parts = split(/\./,$ip);
14692:                 next if (@parts < 4);
14693:                 my $badip = 0;
14694:                 for (my $i=0; $i<4; $i++) {
14695:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
14696:                         $badip = 1;
14697:                         last;
14698:                     }
14699:                 }
14700:                 if (!$badip) {
14701:                     push(@okmachines,$ip);     
14702:                 }
14703:             }
14704:             @okmachines = sort(@okmachines);
14705:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
14706:         }
14707:     }
14708:     my %serverstatushash =  (
14709:                                 serverstatuses => \%newserverstatus,
14710:                             );
14711:     foreach my $type (@pages) {
14712:         foreach my $setting ('namedusers','machines') {
14713:             my (@current,@new);
14714:             if (ref($currserverstatus{$type}) eq 'HASH') {
14715:                 if ($currserverstatus{$type}{$setting} ne '') { 
14716:                     @current = split(/,/,$currserverstatus{$type}{$setting});
14717:                 }
14718:             }
14719:             if ($newserverstatus{$type}{$setting} ne '') {
14720:                 @new = split(/,/,$newserverstatus{$type}{$setting});
14721:             }
14722:             if (@current > 0) {
14723:                 if (@new > 0) {
14724:                     foreach my $item (@current) {
14725:                         if (!grep(/^\Q$item\E$/,@new)) {
14726:                             $changes{$type}{$setting} = 1;
14727:                             last;
14728:                         }
14729:                     }
14730:                     foreach my $item (@new) {
14731:                         if (!grep(/^\Q$item\E$/,@current)) {
14732:                             $changes{$type}{$setting} = 1;
14733:                             last;
14734:                         }
14735:                     }
14736:                 } else {
14737:                     $changes{$type}{$setting} = 1;
14738:                 }
14739:             } elsif (@new > 0) {
14740:                 $changes{$type}{$setting} = 1;
14741:             }
14742:         }
14743:     }
14744:     if (keys(%changes) > 0) {
14745:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
14746:         my $putresult = &Apache::lonnet::put_dom('configuration',
14747:                                                  \%serverstatushash,$dom);
14748:         if ($putresult eq 'ok') {
14749:             $resulttext .= &mt('Changes made:').'<ul>';
14750:             foreach my $type (@pages) {
14751:                 if (ref($changes{$type}) eq 'HASH') {
14752:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
14753:                     if ($changes{$type}{'namedusers'}) {
14754:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
14755:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
14756:                         } else {
14757:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
14758:                         }
14759:                     }
14760:                     if ($changes{$type}{'machines'}) {
14761:                         if ($newserverstatus{$type}{'machines'} eq '') {
14762:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
14763:                         } else {
14764:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
14765:                         }
14766: 
14767:                     }
14768:                     $resulttext .= '</ul></li>';
14769:                 }
14770:             }
14771:             $resulttext .= '</ul>';
14772:         } else {
14773:             $resulttext = '<span class="LC_error">'.
14774:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
14775: 
14776:         }
14777:     } else {
14778:         $resulttext = &mt('No changes made to access to server status pages');
14779:     }
14780:     return $resulttext;
14781: }
14782: 
14783: sub modify_helpsettings {
14784:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14785:     my ($resulttext,$errors,%changes,%helphash);
14786:     my %defaultchecked = ('submitbugs' => 'on');
14787:     my @offon = ('off','on');
14788:     my @toggles = ('submitbugs');
14789:     my %current = ('submitbugs' => '',
14790:                    'adhoc'      => {},
14791:                   );
14792:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
14793:         %current = %{$domconfig{'helpsettings'}};
14794:     }
14795:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14796:     foreach my $item (@toggles) {
14797:         if ($defaultchecked{$item} eq 'on') { 
14798:             if ($current{$item} eq '') {
14799:                 if ($env{'form.'.$item} eq '0') {
14800:                     $changes{$item} = 1;
14801:                 }
14802:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14803:                 $changes{$item} = 1;
14804:             }
14805:         } elsif ($defaultchecked{$item} eq 'off') {
14806:             if ($current{$item} eq '') {
14807:                 if ($env{'form.'.$item} eq '1') {
14808:                     $changes{$item} = 1;
14809:                 }
14810:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14811:                 $changes{$item} = 1;
14812:             }
14813:         }
14814:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
14815:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
14816:         }
14817:     }
14818:     my $maxnum = $env{'form.helproles_maxnum'};
14819:     my $confname = $dom.'-domainconfig';
14820:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
14821:     my (@allpos,%newsettings,%changedprivs,$newrole);
14822:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14823:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
14824:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
14825:     my %lt = &Apache::lonlocal::texthash(
14826:                     s      => 'system',
14827:                     d      => 'domain',
14828:                     order  => 'Display order',
14829:                     access => 'Role usage',
14830:                     all    => 'All with domain helpdesk or helpdesk assistant role',
14831:                     dh     => 'All with domain helpdesk role',
14832:                     da     => 'All with domain helpdesk assistant role',
14833:                     none   => 'None',
14834:                     status => 'Determined based on institutional status',
14835:                     inc    => 'Include all, but exclude specific personnel',
14836:                     exc    => 'Exclude all, but include specific personnel',
14837:     );
14838:     for (my $num=0; $num<=$maxnum; $num++) {
14839:         my ($prefix,$identifier,$rolename,%curr);
14840:         if ($num == $maxnum) {
14841:             next unless ($env{'form.newcusthelp'} == $maxnum);
14842:             $identifier = 'custhelp'.$num;
14843:             $prefix = 'helproles_'.$num;
14844:             $rolename = $env{'form.custhelpname'.$num};
14845:             $rolename=~s/[^A-Za-z0-9]//gs;
14846:             next if ($rolename eq '');
14847:             next if (exists($existing{'rolesdef_'.$rolename}));
14848:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
14849:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
14850:                                                      $newprivs{'c'},$confname,$dom);
14851:             if ($result ne 'ok') {
14852:                 $errors .= '<li><span class="LC_error">'.
14853:                            &mt('An error occurred storing the new custom role: [_1]',
14854:                            $result).'</span></li>';
14855:                 next;
14856:             } else {
14857:                 $changedprivs{$rolename} = \%newprivs;
14858:                 $newrole = $rolename;
14859:             }
14860:         } else {
14861:             $prefix = 'helproles_'.$num;
14862:             $rolename = $env{'form.'.$prefix};
14863:             next if ($rolename eq '');
14864:             next unless (exists($existing{'rolesdef_'.$rolename}));
14865:             $identifier = 'custhelp'.$num;
14866:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
14867:             my %currprivs;
14868:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
14869:                 split(/\_/,$existing{'rolesdef_'.$rolename});
14870:             foreach my $level ('c','d','s') {
14871:                 if ($newprivs{$level} ne $currprivs{$level}) {
14872:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
14873:                                                              $newprivs{'c'},$confname,$dom);
14874:                     if ($result ne 'ok') {
14875:                         $errors .= '<li><span class="LC_error">'.
14876:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
14877:                                        $rolename,$result).'</span></li>';
14878:                     } else {
14879:                         $changedprivs{$rolename} = \%newprivs;
14880:                     }
14881:                     last;
14882:                 }
14883:             }
14884:             if (ref($current{'adhoc'}) eq 'HASH') {
14885:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
14886:                     %curr = %{$current{'adhoc'}{$rolename}};
14887:                 }
14888:             }
14889:         }
14890:         my $newpos = $env{'form.'.$prefix.'_pos'};
14891:         $newpos =~ s/\D+//g;
14892:         $allpos[$newpos] = $rolename;
14893:         my $newdesc = $env{'form.'.$prefix.'_desc'};
14894:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
14895:         if ($curr{'desc'}) {
14896:             if ($curr{'desc'} ne $newdesc) {
14897:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
14898:                 $newsettings{$rolename}{'desc'} = $newdesc;
14899:             }
14900:         } elsif ($newdesc ne '') {
14901:             $changes{'customrole'}{$rolename}{'desc'} = 1;
14902:             $newsettings{$rolename}{'desc'} = $newdesc;
14903:         }
14904:         my $access = $env{'form.'.$prefix.'_access'};
14905:         if (grep(/^\Q$access\E$/,@accesstypes)) {
14906:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
14907:             if ($access eq 'status') {
14908:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
14909:                 if (scalar(@statuses) == 0) {
14910:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
14911:                 } else {
14912:                     my (@shownstatus,$numtypes);
14913:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
14914:                     if (ref($types) eq 'ARRAY') {
14915:                         $numtypes = scalar(@{$types});
14916:                         foreach my $type (sort(@statuses)) {
14917:                             if ($type eq 'default') {
14918:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
14919:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
14920:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
14921:                                 push(@shownstatus,$usertypes->{$type});
14922:                             }
14923:                         }
14924:                     }
14925:                     if (grep(/^default$/,@statuses)) {
14926:                         push(@shownstatus,$othertitle);
14927:                     }
14928:                     if (scalar(@shownstatus) == 1+$numtypes) {
14929:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
14930:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
14931:                     } else {
14932:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
14933:                         if (ref($curr{'status'}) eq 'ARRAY') {
14934:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
14935:                             if (@diffs) {
14936:                                 $changes{'customrole'}{$rolename}{$access} = 1;
14937:                             }
14938:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14939:                             $changes{'customrole'}{$rolename}{$access} = 1;
14940:                         }
14941:                     }
14942:                 }
14943:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
14944:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
14945:                 my @newspecstaff;
14946:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
14947:                 foreach my $person (sort(@personnel)) {
14948:                     if ($domhelpdesk{$person}) {
14949:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
14950:                     }
14951:                 }
14952:                 if (ref($curr{$access}) eq 'ARRAY') {
14953:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
14954:                     if (@diffs) {
14955:                         $changes{'customrole'}{$rolename}{$access} = 1;
14956:                     }
14957:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14958:                     $changes{'customrole'}{$rolename}{$access} = 1;
14959:                 }
14960:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14961:                     my ($uname,$udom) = split(/:/,$person);
14962:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
14963:                 }
14964:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
14965:             }
14966:         } else {
14967:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
14968:         }
14969:         unless ($curr{'access'} eq $access) {
14970:             $changes{'customrole'}{$rolename}{'access'} = 1;
14971:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
14972:         }
14973:     }
14974:     if (@allpos > 0) {
14975:         my $idx = 0;
14976:         foreach my $rolename (@allpos) {
14977:             if ($rolename ne '') {
14978:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
14979:                 if (ref($current{'adhoc'}) eq 'HASH') {
14980:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
14981:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
14982:                             $changes{'customrole'}{$rolename}{'order'} = 1;
14983:                             $newsettings{$rolename}{'order'} = $idx+1;
14984:                         }
14985:                     }
14986:                 }
14987:                 $idx ++;
14988:             }
14989:         }
14990:     }
14991:     my $putresult;
14992:     if (keys(%changes) > 0) {
14993:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
14994:         if ($putresult eq 'ok') {
14995:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
14996:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
14997:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
14998:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
14999:                 }
15000:             }
15001:             my $cachetime = 24*60*60;
15002:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15003:             if (ref($lastactref) eq 'HASH') {
15004:                 $lastactref->{'domdefaults'} = 1;
15005:             }
15006:         } else {
15007:             $errors .= '<li><span class="LC_error">'.
15008:                        &mt('An error occurred storing the settings: [_1]',
15009:                            $putresult).'</span></li>';
15010:         }
15011:     }
15012:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
15013:         $resulttext = &mt('Changes made:').'<ul>';
15014:         my (%shownprivs,@levelorder);
15015:         @levelorder = ('c','d','s');
15016:         if ((keys(%changes)) && ($putresult eq 'ok')) {
15017:             foreach my $item (sort(keys(%changes))) {
15018:                 if ($item eq 'submitbugs') {
15019:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
15020:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
15021:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
15022:                 } elsif ($item eq 'customrole') {
15023:                     if (ref($changes{'customrole'}) eq 'HASH') {
15024:                         my @keyorder = ('order','desc','access','status','exc','inc');
15025:                         my %keytext = &Apache::lonlocal::texthash(
15026:                                                                    order  => 'Order',
15027:                                                                    desc   => 'Role description',
15028:                                                                    access => 'Role usage',
15029:                                                                    status => 'Allowed institutional types',
15030:                                                                    exc    => 'Allowed personnel',
15031:                                                                    inc    => 'Disallowed personnel',
15032:                         );
15033:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
15034:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
15035:                                 if ($role eq $newrole) {
15036:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
15037:                                                               $role).'<ul>';
15038:                                 } else {
15039:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15040:                                                               $role).'<ul>';
15041:                                 }
15042:                                 foreach my $key (@keyorder) {
15043:                                     if ($changes{'customrole'}{$role}{$key}) {
15044:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
15045:                                                                   $keytext{$key},$newsettings{$role}{$key}).
15046:                                                        '</li>';
15047:                                     }
15048:                                 }
15049:                                 if (ref($changedprivs{$role}) eq 'HASH') {
15050:                                     $shownprivs{$role} = 1;
15051:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
15052:                                     foreach my $level (@levelorder) {
15053:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15054:                                             next if ($item eq '');
15055:                                             my ($priv) = split(/\&/,$item,2);
15056:                                             if (&Apache::lonnet::plaintext($priv)) {
15057:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15058:                                                 unless ($level eq 'c') {
15059:                                                     $resulttext .= ' ('.$lt{$level}.')';
15060:                                                 }
15061:                                                 $resulttext .= '</li>';
15062:                                             }
15063:                                         }
15064:                                     }
15065:                                     $resulttext .= '</ul>';
15066:                                 }
15067:                                 $resulttext .= '</ul></li>';
15068:                             }
15069:                         }
15070:                     }
15071:                 }
15072:             }
15073:         }
15074:         if (keys(%changedprivs)) {
15075:             foreach my $role (sort(keys(%changedprivs))) {
15076:                 unless ($shownprivs{$role}) {
15077:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15078:                                               $role).'<ul>'.
15079:                                    '<li>'.&mt('Privileges set to :').'<ul>';
15080:                     foreach my $level (@levelorder) {
15081:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15082:                             next if ($item eq '');
15083:                             my ($priv) = split(/\&/,$item,2);
15084:                             if (&Apache::lonnet::plaintext($priv)) {
15085:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15086:                                 unless ($level eq 'c') {
15087:                                     $resulttext .= ' ('.$lt{$level}.')';
15088:                                 }
15089:                                 $resulttext .= '</li>';
15090:                             }
15091:                         }
15092:                     }
15093:                     $resulttext .= '</ul></li></ul></li>';
15094:                 }
15095:             }
15096:         }
15097:         $resulttext .= '</ul>';
15098:     } else {
15099:         $resulttext = &mt('No changes made to help settings');
15100:     }
15101:     if ($errors) {
15102:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
15103:                                     $errors.'</ul>';
15104:     }
15105:     return $resulttext;
15106: }
15107: 
15108: sub modify_coursedefaults {
15109:     my ($dom,$lastactref,%domconfig) = @_;
15110:     my ($resulttext,$errors,%changes,%defaultshash);
15111:     my %defaultchecked = (
15112:                            'canuse_pdfforms' => 'off',
15113:                            'uselcmath'       => 'on',
15114:                            'usejsme'         => 'on'
15115:                          );
15116:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
15117:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
15118:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
15119:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
15120:                    'mysqltables_textbook','mysqltables_placement');
15121:     my @types = ('official','unofficial','community','textbook','placement');
15122:     my %staticdefaults = (
15123:                            anonsurvey_threshold => 10,
15124:                            uploadquota          => 500,
15125:                            postsubmit           => 60,
15126:                            mysqltables          => 172800,
15127:                          );
15128:     my %texoptions = (
15129:                         MathJax  => 'MathJax',
15130:                         mimetex  => &mt('Convert to Images'),
15131:                         tth      => &mt('TeX to HTML'),
15132:                      );
15133:     $defaultshash{'coursedefaults'} = {};
15134: 
15135:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
15136:         if ($domconfig{'coursedefaults'} eq '') {
15137:             $domconfig{'coursedefaults'} = {};
15138:         }
15139:     }
15140: 
15141:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
15142:         foreach my $item (@toggles) {
15143:             if ($defaultchecked{$item} eq 'on') {
15144:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15145:                     ($env{'form.'.$item} eq '0')) {
15146:                     $changes{$item} = 1;
15147:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15148:                     $changes{$item} = 1;
15149:                 }
15150:             } elsif ($defaultchecked{$item} eq 'off') {
15151:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15152:                     ($env{'form.'.$item} eq '1')) {
15153:                     $changes{$item} = 1;
15154:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15155:                     $changes{$item} = 1;
15156:                 }
15157:             }
15158:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
15159:         }
15160:         foreach my $item (@numbers) {
15161:             my ($currdef,$newdef);
15162:             $newdef = $env{'form.'.$item};
15163:             if ($item eq 'anonsurvey_threshold') {
15164:                 $currdef = $domconfig{'coursedefaults'}{$item};
15165:                 $newdef =~ s/\D//g;
15166:                 if ($newdef eq '' || $newdef < 1) {
15167:                     $newdef = 1;
15168:                 }
15169:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
15170:             } else {
15171:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
15172:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
15173:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
15174:                 }
15175:                 $newdef =~ s/[^\w.\-]//g;
15176:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
15177:             }
15178:             if ($currdef ne $newdef) {
15179:                 my $staticdef;
15180:                 if ($item eq 'anonsurvey_threshold') {
15181:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
15182:                         $changes{$item} = 1;
15183:                     }
15184:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
15185:                     my $setting = $1;
15186:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
15187:                         $changes{$setting} = 1;
15188:                     }
15189:                 }
15190:             }
15191:         }
15192:         my $texengine;
15193:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
15194:             $texengine = $env{'form.texengine'};
15195:             if ($defaultshash{'coursedefaults'}{'texengine'} eq '') {
15196:                 unless ($texengine eq 'MathJax') {
15197:                     $changes{'texengine'} = 1;
15198:                 }
15199:             } elsif ($defaultshash{'coursedefaults'}{'texengine'} ne $texengine) {
15200:                 $changes{'texengine'} = 1;
15201:             }
15202:         }
15203:         if ($texengine ne '') {
15204:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
15205:         }
15206:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
15207:         my @currclonecode;
15208:         if (ref($currclone) eq 'HASH') {
15209:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
15210:                 @currclonecode = @{$currclone->{'instcode'}};
15211:             }
15212:         }
15213:         my $newclone;
15214:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
15215:             $newclone = $env{'form.canclone'};
15216:         }
15217:         if ($newclone eq 'instcode') {
15218:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
15219:             my (%codedefaults,@code_order,@clonecode);
15220:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
15221:                                                     \@code_order);
15222:             foreach my $item (@code_order) {
15223:                 if (grep(/^\Q$item\E$/,@newcodes)) {
15224:                     push(@clonecode,$item);
15225:                 }
15226:             }
15227:             if (@clonecode) {
15228:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
15229:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
15230:                 if (@diffs) {
15231:                     $changes{'canclone'} = 1;
15232:                 }
15233:             } else {
15234:                 $newclone eq '';
15235:             }
15236:         } elsif ($newclone ne '') {
15237:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
15238:         }
15239:         if ($newclone ne $currclone) {
15240:             $changes{'canclone'} = 1;
15241:         }
15242:         my %credits;
15243:         foreach my $type (@types) {
15244:             unless ($type eq 'community') {
15245:                 $credits{$type} = $env{'form.'.$type.'_credits'};
15246:                 $credits{$type} =~ s/[^\d.]+//g;
15247:             }
15248:         }
15249:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
15250:             ($env{'form.coursecredits'} eq '1')) {
15251:             $changes{'coursecredits'} = 1;
15252:             foreach my $type (keys(%credits)) {
15253:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15254:             }
15255:         } else {
15256:             if ($env{'form.coursecredits'} eq '1') {
15257:                 foreach my $type (@types) {
15258:                     unless ($type eq 'community') {
15259:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
15260:                             $changes{'coursecredits'} = 1;
15261:                         }
15262:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15263:                     }
15264:                 }
15265:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15266:                 foreach my $type (@types) {
15267:                     unless ($type eq 'community') {
15268:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
15269:                             $changes{'coursecredits'} = 1;
15270:                             last;
15271:                         }
15272:                     }
15273:                 }
15274:             }
15275:         }
15276:         if ($env{'form.postsubmit'} eq '1') {
15277:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
15278:             my %currtimeout;
15279:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15280:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
15281:                     $changes{'postsubmit'} = 1;
15282:                 }
15283:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15284:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
15285:                 }
15286:             } else {
15287:                 $changes{'postsubmit'} = 1;
15288:             }
15289:             foreach my $type (@types) {
15290:                 my $timeout = $env{'form.'.$type.'_timeout'};
15291:                 $timeout =~ s/\D//g;
15292:                 if ($timeout == $staticdefaults{'postsubmit'}) {
15293:                     $timeout = '';
15294:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
15295:                     $timeout = '0';
15296:                 }
15297:                 unless ($timeout eq '') {
15298:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
15299:                 }
15300:                 if (exists($currtimeout{$type})) {
15301:                     if ($timeout ne $currtimeout{$type}) {
15302:                         $changes{'postsubmit'} = 1;
15303:                     }
15304:                 } elsif ($timeout ne '') {
15305:                     $changes{'postsubmit'} = 1;
15306:                 }
15307:             }
15308:         } else {
15309:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
15310:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15311:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
15312:                     $changes{'postsubmit'} = 1;
15313:                 }
15314:             } else {
15315:                 $changes{'postsubmit'} = 1;
15316:             }
15317:         }
15318:     }
15319:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15320:                                              $dom);
15321:     if ($putresult eq 'ok') {
15322:         if (keys(%changes) > 0) {
15323:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15324:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
15325:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
15326:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
15327:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
15328:                     if ($changes{$item}) {
15329:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
15330:                     }
15331:                 }
15332:                 if ($changes{'coursecredits'}) {
15333:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15334:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
15335:                             $domdefaults{$type.'credits'} =
15336:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
15337:                         }
15338:                     }
15339:                 }
15340:                 if ($changes{'postsubmit'}) {
15341:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15342:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
15343:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15344:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
15345:                                 $domdefaults{$type.'postsubtimeout'} =
15346:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15347:                             }
15348:                         }
15349:                     }
15350:                 }
15351:                 if ($changes{'uploadquota'}) {
15352:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15353:                         foreach my $type (@types) {
15354:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
15355:                         }
15356:                     }
15357:                 }
15358:                 if ($changes{'canclone'}) {
15359:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15360:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15361:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
15362:                             if (@clonecodes) {
15363:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
15364:                             }
15365:                         }
15366:                     } else {
15367:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
15368:                     }
15369:                 }
15370:                 my $cachetime = 24*60*60;
15371:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15372:                 if (ref($lastactref) eq 'HASH') {
15373:                     $lastactref->{'domdefaults'} = 1;
15374:                 }
15375:             }
15376:             $resulttext = &mt('Changes made:').'<ul>';
15377:             foreach my $item (sort(keys(%changes))) {
15378:                 if ($item eq 'canuse_pdfforms') {
15379:                     if ($env{'form.'.$item} eq '1') {
15380:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
15381:                     } else {
15382:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
15383:                     }
15384:                 } elsif ($item eq 'uselcmath') {
15385:                     if ($env{'form.'.$item} eq '1') {
15386:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
15387:                     } else {
15388:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
15389:                     }
15390:                 } elsif ($item eq 'usejsme') {
15391:                     if ($env{'form.'.$item} eq '1') {
15392:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
15393:                     } else {
15394:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
15395:                     }
15396:                 } elsif ($item eq 'texengine') {
15397:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
15398:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
15399:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
15400:                     }
15401:                 } elsif ($item eq 'anonsurvey_threshold') {
15402:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
15403:                 } elsif ($item eq 'uploadquota') {
15404:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15405:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
15406:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
15407:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
15408:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
15409:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
15410:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
15411:                                        '</ul>'.
15412:                                        '</li>';
15413:                     } else {
15414:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
15415:                     }
15416:                 } elsif ($item eq 'mysqltables') {
15417:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
15418:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
15419:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
15420:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
15421:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
15422:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
15423:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
15424:                                        '</ul>'.
15425:                                        '</li>';
15426:                     } else {
15427:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
15428:                     }
15429:                 } elsif ($item eq 'postsubmit') {
15430:                     if ($domdefaults{'postsubmit'} eq 'off') {
15431:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
15432:                     } else {
15433:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
15434:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15435:                             $resulttext .= &mt('durations:').'<ul>';
15436:                             foreach my $type (@types) {
15437:                                 $resulttext .= '<li>';
15438:                                 my $timeout;
15439:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15440:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15441:                                 }
15442:                                 my $display;
15443:                                 if ($timeout eq '0') {
15444:                                     $display = &mt('unlimited');
15445:                                 } elsif ($timeout eq '') {
15446:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
15447:                                 } else {
15448:                                     $display = &mt('[quant,_1,second]',$timeout);
15449:                                 }
15450:                                 if ($type eq 'community') {
15451:                                     $resulttext .= &mt('Communities');
15452:                                 } elsif ($type eq 'official') {
15453:                                     $resulttext .= &mt('Official courses');
15454:                                 } elsif ($type eq 'unofficial') {
15455:                                     $resulttext .= &mt('Unofficial courses');
15456:                                 } elsif ($type eq 'textbook') {
15457:                                     $resulttext .= &mt('Textbook courses');
15458:                                 } elsif ($type eq 'placement') {
15459:                                     $resulttext .= &mt('Placement tests');
15460:                                 }
15461:                                 $resulttext .= ' -- '.$display.'</li>';
15462:                             }
15463:                             $resulttext .= '</ul>';
15464:                         }
15465:                         $resulttext .= '</li>';
15466:                     }
15467:                 } elsif ($item eq 'coursecredits') {
15468:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15469:                         if (($domdefaults{'officialcredits'} eq '') &&
15470:                             ($domdefaults{'unofficialcredits'} eq '') &&
15471:                             ($domdefaults{'textbookcredits'} eq '')) {
15472:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15473:                         } else {
15474:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
15475:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
15476:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
15477:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
15478:                                            '</ul>'.
15479:                                            '</li>';
15480:                         }
15481:                     } else {
15482:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15483:                     }
15484:                 } elsif ($item eq 'canclone') {
15485:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15486:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15487:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
15488:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
15489:                         }
15490:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
15491:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
15492:                     } else {
15493:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
15494:                     }
15495:                 }
15496:             }
15497:             $resulttext .= '</ul>';
15498:         } else {
15499:             $resulttext = &mt('No changes made to course defaults');
15500:         }
15501:     } else {
15502:         $resulttext = '<span class="LC_error">'.
15503:             &mt('An error occurred: [_1]',$putresult).'</span>';
15504:     }
15505:     return $resulttext;
15506: }
15507: 
15508: sub modify_selfenrollment {
15509:     my ($dom,$lastactref,%domconfig) = @_;
15510:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
15511:     my @types = ('official','unofficial','community','textbook','placement');
15512:     my %titles = &tool_titles();
15513:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
15514:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
15515:     $ordered{'default'} = ['types','registered','approval','limit'];
15516: 
15517:     my (%roles,%shown,%toplevel);
15518:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
15519: 
15520:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
15521:         if ($domconfig{'selfenrollment'} eq '') {
15522:             $domconfig{'selfenrollment'} = {};
15523:         }
15524:     }
15525:     %toplevel = (
15526:                   admin      => 'Configuration Rights',
15527:                   default    => 'Default settings',
15528:                   validation => 'Validation of self-enrollment requests',
15529:                 );
15530:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
15531: 
15532:     if (ref($ordered{'admin'}) eq 'ARRAY') {
15533:         foreach my $item (@{$ordered{'admin'}}) {
15534:             foreach my $type (@types) {
15535:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
15536:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
15537:                 } else {
15538:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
15539:                 }
15540:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
15541:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
15542:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
15543:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
15544:                             push(@{$changes{'admin'}{$type}},$item);
15545:                         }
15546:                     } else {
15547:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
15548:                             push(@{$changes{'admin'}{$type}},$item);
15549:                         }
15550:                     }
15551:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
15552:                     push(@{$changes{'admin'}{$type}},$item);
15553:                 }
15554:             }
15555:         }
15556:     }
15557: 
15558:     foreach my $item (@{$ordered{'default'}}) {
15559:         foreach my $type (@types) {
15560:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
15561:             if ($item eq 'types') {
15562:                 unless (($value eq 'all') || ($value eq 'dom')) {
15563:                     $value = '';
15564:                 }
15565:             } elsif ($item eq 'registered') {
15566:                 unless ($value eq '1') {
15567:                     $value = 0;
15568:                 }
15569:             } elsif ($item eq 'approval') {
15570:                 unless ($value =~ /^[012]$/) {
15571:                     $value = 0;
15572:                 }
15573:             } else {
15574:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15575:                     $value = 'none';
15576:                 }
15577:             }
15578:             $selfenrollhash{'default'}{$type}{$item} = $value;
15579:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
15580:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15581:                     if ($selfenrollhash{'default'}{$type}{$item} ne
15582:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
15583:                          push(@{$changes{'default'}{$type}},$item);
15584:                     }
15585:                 } else {
15586:                     push(@{$changes{'default'}{$type}},$item);
15587:                 }
15588:             } else {
15589:                 push(@{$changes{'default'}{$type}},$item);
15590:             }
15591:             if ($item eq 'limit') {
15592:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15593:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
15594:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
15595:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
15596:                     }
15597:                 } else {
15598:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
15599:                 }
15600:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15601:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
15602:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
15603:                          push(@{$changes{'default'}{$type}},'cap');
15604:                     }
15605:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
15606:                     push(@{$changes{'default'}{$type}},'cap');
15607:                 }
15608:             }
15609:         }
15610:     }
15611: 
15612:     foreach my $item (@{$itemsref}) {
15613:         if ($item eq 'fields') {
15614:             my @changed;
15615:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
15616:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
15617:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
15618:             }
15619:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15620:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
15621:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
15622:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
15623:                 } else {
15624:                     @changed = @{$selfenrollhash{'validation'}{$item}};
15625:                 }
15626:             } else {
15627:                 @changed = @{$selfenrollhash{'validation'}{$item}};
15628:             }
15629:             if (@changed) {
15630:                 if ($selfenrollhash{'validation'}{$item}) { 
15631:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
15632:                 } else {
15633:                     $changes{'validation'}{$item} = &mt('None');
15634:                 }
15635:             }
15636:         } else {
15637:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
15638:             if ($item eq 'markup') {
15639:                if ($env{'form.selfenroll_validation_'.$item}) {
15640:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
15641:                }
15642:             }
15643:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15644:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
15645:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
15646:                 }
15647:             }
15648:         }
15649:     }
15650: 
15651:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
15652:                                              $dom);
15653:     if ($putresult eq 'ok') {
15654:         if (keys(%changes) > 0) {
15655:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15656:             $resulttext = &mt('Changes made:').'<ul>';
15657:             foreach my $key ('admin','default','validation') {
15658:                 if (ref($changes{$key}) eq 'HASH') {
15659:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
15660:                     if ($key eq 'validation') {
15661:                         foreach my $item (@{$itemsref}) {
15662:                             if (exists($changes{$key}{$item})) {
15663:                                 if ($item eq 'markup') {
15664:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15665:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
15666:                                 } else {  
15667:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15668:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
15669:                                 }
15670:                             }
15671:                         }
15672:                     } else {
15673:                         foreach my $type (@types) {
15674:                             if ($type eq 'community') {
15675:                                 $roles{'1'} = &mt('Community personnel');
15676:                             } else {
15677:                                 $roles{'1'} = &mt('Course personnel');
15678:                             }
15679:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
15680:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
15681:                                     if ($key eq 'admin') {
15682:                                         my @mgrdc = ();
15683:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15684:                                             foreach my $item (@{$ordered{'admin'}}) {
15685:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
15686:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
15687:                                                         push(@mgrdc,$item);
15688:                                                     }
15689:                                                 }
15690:                                             }
15691:                                             if (@mgrdc) {
15692:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
15693:                                             } else {
15694:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
15695:                                             }
15696:                                         }
15697:                                     } else {
15698:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15699:                                             foreach my $item (@{$ordered{$key}}) {
15700:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15701:                                                     $domdefaults{$type.'selfenroll'.$item} =
15702:                                                         $selfenrollhash{$key}{$type}{$item};
15703:                                                 }
15704:                                             }
15705:                                         }
15706:                                     }
15707:                                 }
15708:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
15709:                                 foreach my $item (@{$ordered{$key}}) {
15710:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15711:                                         $resulttext .= '<li>';
15712:                                         if ($key eq 'admin') {
15713:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
15714:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
15715:                                         } else {
15716:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
15717:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
15718:                                         }
15719:                                         $resulttext .= '</li>';
15720:                                     }
15721:                                 }
15722:                                 $resulttext .= '</ul></li>';
15723:                             }
15724:                         }
15725:                         $resulttext .= '</ul></li>'; 
15726:                     }
15727:                 }
15728:             }
15729:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
15730:                 my $cachetime = 24*60*60;
15731:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15732:                 if (ref($lastactref) eq 'HASH') {
15733:                     $lastactref->{'domdefaults'} = 1;
15734:                 }
15735:             }
15736:             $resulttext .= '</ul>';
15737:         } else {
15738:             $resulttext = &mt('No changes made to self-enrollment settings');
15739:         }
15740:     } else {
15741:         $resulttext = '<span class="LC_error">'.
15742:             &mt('An error occurred: [_1]',$putresult).'</span>';
15743:     }
15744:     return $resulttext;
15745: }
15746: 
15747: sub modify_usersessions {
15748:     my ($dom,$lastactref,%domconfig) = @_;
15749:     my @hostingtypes = ('version','excludedomain','includedomain');
15750:     my @offloadtypes = ('primary','default');
15751:     my %types = (
15752:                   remote => \@hostingtypes,
15753:                   hosted => \@hostingtypes,
15754:                   spares => \@offloadtypes,
15755:                 );
15756:     my @prefixes = ('remote','hosted','spares');
15757:     my @lcversions = &Apache::lonnet::all_loncaparevs();
15758:     my (%by_ip,%by_location,@intdoms,@instdoms);
15759:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
15760:     my @locations = sort(keys(%by_location));
15761:     my (%defaultshash,%changes);
15762:     foreach my $prefix (@prefixes) {
15763:         $defaultshash{'usersessions'}{$prefix} = {};
15764:     }
15765:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15766:     my $resulttext;
15767:     my %iphost = &Apache::lonnet::get_iphost();
15768:     foreach my $prefix (@prefixes) {
15769:         next if ($prefix eq 'spares');
15770:         foreach my $type (@{$types{$prefix}}) {
15771:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
15772:             if ($type eq 'version') {
15773:                 my $value = $env{'form.'.$prefix.'_'.$type};
15774:                 my $okvalue;
15775:                 if ($value ne '') {
15776:                     if (grep(/^\Q$value\E$/,@lcversions)) {
15777:                         $okvalue = $value;
15778:                     }
15779:                 }
15780:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15781:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15782:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
15783:                             if ($inuse == 0) {
15784:                                 $changes{$prefix}{$type} = 1;
15785:                             } else {
15786:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
15787:                                     $changes{$prefix}{$type} = 1;
15788:                                 }
15789:                                 if ($okvalue ne '') {
15790:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15791:                                 } 
15792:                             }
15793:                         } else {
15794:                             if (($inuse == 1) && ($okvalue ne '')) {
15795:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15796:                                 $changes{$prefix}{$type} = 1;
15797:                             }
15798:                         }
15799:                     } else {
15800:                         if (($inuse == 1) && ($okvalue ne '')) {
15801:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15802:                             $changes{$prefix}{$type} = 1;
15803:                         }
15804:                     }
15805:                 } else {
15806:                     if (($inuse == 1) && ($okvalue ne '')) {
15807:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15808:                         $changes{$prefix}{$type} = 1;
15809:                     }
15810:                 }
15811:             } else {
15812:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
15813:                 my @okvals;
15814:                 foreach my $val (@vals) {
15815:                     if ($val =~ /:/) {
15816:                         my @items = split(/:/,$val);
15817:                         foreach my $item (@items) {
15818:                             if (ref($by_location{$item}) eq 'ARRAY') {
15819:                                 push(@okvals,$item);
15820:                             }
15821:                         }
15822:                     } else {
15823:                         if (ref($by_location{$val}) eq 'ARRAY') {
15824:                             push(@okvals,$val);
15825:                         }
15826:                     }
15827:                 }
15828:                 @okvals = sort(@okvals);
15829:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15830:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15831:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
15832:                             if ($inuse == 0) {
15833:                                 $changes{$prefix}{$type} = 1; 
15834:                             } else {
15835:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15836:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
15837:                                 if (@changed > 0) {
15838:                                     $changes{$prefix}{$type} = 1;
15839:                                 }
15840:                             }
15841:                         } else {
15842:                             if ($inuse == 1) {
15843:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15844:                                 $changes{$prefix}{$type} = 1;
15845:                             }
15846:                         } 
15847:                     } else {
15848:                         if ($inuse == 1) {
15849:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15850:                             $changes{$prefix}{$type} = 1;
15851:                         }
15852:                     }
15853:                 } else {
15854:                     if ($inuse == 1) {
15855:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15856:                         $changes{$prefix}{$type} = 1;
15857:                     }
15858:                 }
15859:             }
15860:         }
15861:     }
15862: 
15863:     my @alldoms = &Apache::lonnet::all_domains();
15864:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15865:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
15866:     my $savespares;
15867: 
15868:     foreach my $lonhost (sort(keys(%servers))) {
15869:         my $serverhomeID =
15870:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
15871:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
15872:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
15873:         my %spareschg;
15874:         foreach my $type (@{$types{'spares'}}) {
15875:             my @okspares;
15876:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
15877:             foreach my $server (@checked) {
15878:                 if (&Apache::lonnet::hostname($server) ne '') {
15879:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
15880:                         unless (grep(/^\Q$server\E$/,@okspares)) {
15881:                             push(@okspares,$server);
15882:                         }
15883:                     }
15884:                 }
15885:             }
15886:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
15887:             my $newspare;
15888:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
15889:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
15890:                     $newspare = $new;
15891:                 }
15892:             }
15893:             my @spares;
15894:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
15895:                 @spares = sort(@okspares,$newspare);
15896:             } else {
15897:                 @spares = sort(@okspares);
15898:             }
15899:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
15900:             if (ref($spareid{$lonhost}) eq 'HASH') {
15901:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
15902:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
15903:                     if (@diffs > 0) {
15904:                         $spareschg{$type} = 1;
15905:                     }
15906:                 }
15907:             }
15908:         }
15909:         if (keys(%spareschg) > 0) {
15910:             $changes{'spares'}{$lonhost} = \%spareschg;
15911:         }
15912:     }
15913:     $defaultshash{'usersessions'}{'offloadnow'} = {};
15914:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
15915:     my @okoffload;
15916:     if (@offloadnow) {
15917:         foreach my $server (@offloadnow) {
15918:             if (&Apache::lonnet::hostname($server) ne '') {
15919:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
15920:                     push(@okoffload,$server);
15921:                 }
15922:             }
15923:         }
15924:         if (@okoffload) {
15925:             foreach my $lonhost (@okoffload) {
15926:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
15927:             }
15928:         }
15929:     }
15930:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
15931:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
15932:             if (ref($changes{'spares'}) eq 'HASH') {
15933:                 if (keys(%{$changes{'spares'}}) > 0) {
15934:                     $savespares = 1;
15935:                 }
15936:             }
15937:         } else {
15938:             $savespares = 1;
15939:         }
15940:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
15941:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
15942:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
15943:                     $changes{'offloadnow'} = 1;
15944:                     last;
15945:                 }
15946:             }
15947:             unless ($changes{'offloadnow'}) {
15948:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
15949:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
15950:                         $changes{'offloadnow'} = 1;
15951:                         last;
15952:                     }
15953:                 }
15954:             }
15955:         } elsif (@okoffload) {
15956:             $changes{'offloadnow'} = 1;
15957:         }
15958:     } elsif (@okoffload) {
15959:         $changes{'offloadnow'} = 1;
15960:     }
15961:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
15962:     if ((keys(%changes) > 0) || ($savespares)) {
15963:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15964:                                                  $dom);
15965:         if ($putresult eq 'ok') {
15966:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
15967:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
15968:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
15969:                 }
15970:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
15971:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
15972:                 }
15973:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
15974:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
15975:                 }
15976:             }
15977:             my $cachetime = 24*60*60;
15978:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15979:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
15980:             if (ref($lastactref) eq 'HASH') {
15981:                 $lastactref->{'domdefaults'} = 1;
15982:                 $lastactref->{'usersessions'} = 1;
15983:             }
15984:             if (keys(%changes) > 0) {
15985:                 my %lt = &usersession_titles();
15986:                 $resulttext = &mt('Changes made:').'<ul>';
15987:                 foreach my $prefix (@prefixes) {
15988:                     if (ref($changes{$prefix}) eq 'HASH') {
15989:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
15990:                         if ($prefix eq 'spares') {
15991:                             if (ref($changes{$prefix}) eq 'HASH') {
15992:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
15993:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
15994:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
15995:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
15996:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15997:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
15998:                                         foreach my $type (@{$types{$prefix}}) {
15999:                                             if ($changes{$prefix}{$lonhost}{$type}) {
16000:                                                 my $offloadto = &mt('None');
16001:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
16002:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
16003:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
16004:                                                     }
16005:                                                 }
16006:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
16007:                                             }
16008:                                         }
16009:                                     }
16010:                                     $resulttext .= '</li>';
16011:                                 }
16012:                             }
16013:                         } else {
16014:                             foreach my $type (@{$types{$prefix}}) {
16015:                                 if (defined($changes{$prefix}{$type})) {
16016:                                     my $newvalue;
16017:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16018:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
16019:                                             if ($type eq 'version') {
16020:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
16021:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16022:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
16023:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
16024:                                                 }
16025:                                             }
16026:                                         }
16027:                                     }
16028:                                     if ($newvalue eq '') {
16029:                                         if ($type eq 'version') {
16030:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
16031:                                         } else {
16032:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
16033:                                         }
16034:                                     } else {
16035:                                         if ($type eq 'version') {
16036:                                             $newvalue .= ' '.&mt('(or later)'); 
16037:                                         }
16038:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
16039:                                     }
16040:                                 }
16041:                             }
16042:                         }
16043:                         $resulttext .= '</ul>';
16044:                     }
16045:                 }
16046:                 if ($changes{'offloadnow'}) {
16047:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16048:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
16049:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
16050:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
16051:                                 $resulttext .= '<li>'.$lonhost.'</li>';
16052:                             }
16053:                             $resulttext .= '</ul>';
16054:                         } else {
16055:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
16056:                         }
16057:                     } else {
16058:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
16059:                     }
16060:                 }
16061:                 $resulttext .= '</ul>';
16062:             } else {
16063:                 $resulttext = $nochgmsg;
16064:             }
16065:         } else {
16066:             $resulttext = '<span class="LC_error">'.
16067:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16068:         }
16069:     } else {
16070:         $resulttext = $nochgmsg;
16071:     }
16072:     return $resulttext;
16073: }
16074: 
16075: sub modify_ssl {
16076:     my ($dom,$lastactref,%domconfig) = @_;
16077:     my (%by_ip,%by_location,@intdoms,@instdoms);
16078:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16079:     my @locations = sort(keys(%by_location));
16080:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16081:     my (%defaultshash,%changes);
16082:     my $action = 'ssl';
16083:     my @prefixes = ('connto','connfrom','replication');
16084:     foreach my $prefix (@prefixes) {
16085:         $defaultshash{$action}{$prefix} = {};
16086:     }
16087:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16088:     my $resulttext;
16089:     my %iphost = &Apache::lonnet::get_iphost();
16090:     my @reptypes = ('certreq','nocertreq');
16091:     my @connecttypes = ('dom','intdom','other');
16092:     my %types = (
16093:                   connto      => \@connecttypes,
16094:                   connfrom    => \@connecttypes,
16095:                   replication => \@reptypes,
16096:                 );
16097:     foreach my $prefix (sort(keys(%types))) {
16098:         foreach my $type (@{$types{$prefix}}) {
16099:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
16100:                 my $value = 'yes';
16101:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
16102:                     $value = $env{'form.'.$prefix.'_'.$type};
16103:                 }
16104:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
16105:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
16106:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
16107:                             $changes{$prefix}{$type} = 1;
16108:                         }
16109:                         $defaultshash{$action}{$prefix}{$type} = $value;
16110:                     } else {
16111:                         $defaultshash{$action}{$prefix}{$type} = $value;
16112:                         $changes{$prefix}{$type} = 1;
16113:                     }
16114:                 } else {
16115:                     $defaultshash{$action}{$prefix}{$type} = $value;
16116:                     $changes{$prefix}{$type} = 1;
16117:                 }
16118:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
16119:                     delete($changes{$prefix}{$type});
16120:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
16121:                     delete($changes{$prefix}{$type});
16122:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
16123:                     delete($changes{$prefix}{$type});
16124:                 }
16125:             } elsif ($prefix eq 'replication') {
16126:                 if (@locations > 0) {
16127:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16128:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16129:                     my @okvals;
16130:                     foreach my $val (@vals) {
16131:                         if ($val =~ /:/) {
16132:                             my @items = split(/:/,$val);
16133:                             foreach my $item (@items) {
16134:                                 if (ref($by_location{$item}) eq 'ARRAY') {
16135:                                     push(@okvals,$item);
16136:                                 }
16137:                             }
16138:                         } else {
16139:                             if (ref($by_location{$val}) eq 'ARRAY') {
16140:                                 push(@okvals,$val);
16141:                             }
16142:                         }
16143:                     }
16144:                     @okvals = sort(@okvals);
16145:                     if (ref($domconfig{$action}) eq 'HASH') {
16146:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
16147:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
16148:                                 if ($inuse == 0) {
16149:                                     $changes{$prefix}{$type} = 1;
16150:                                 } else {
16151:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
16152:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
16153:                                     if (@changed > 0) {
16154:                                         $changes{$prefix}{$type} = 1;
16155:                                     }
16156:                                 }
16157:                             } else {
16158:                                 if ($inuse == 1) {
16159:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
16160:                                     $changes{$prefix}{$type} = 1;
16161:                                 }
16162:                             }
16163:                         } else {
16164:                             if ($inuse == 1) {
16165:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
16166:                                 $changes{$prefix}{$type} = 1;
16167:                             }
16168:                         }
16169:                     } else {
16170:                         if ($inuse == 1) {
16171:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
16172:                             $changes{$prefix}{$type} = 1;
16173:                         }
16174:                     }
16175:                 }
16176:             }
16177:         }
16178:     }
16179:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
16180:     if (keys(%changes) > 0) {
16181:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16182:                                                  $dom);
16183:         if ($putresult eq 'ok') {
16184:             if (ref($defaultshash{$action}) eq 'HASH') {
16185:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
16186:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
16187:                 }
16188:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
16189:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
16190:                 }
16191:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
16192:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
16193:                 }
16194:             }
16195:             my $cachetime = 24*60*60;
16196:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16197:             if (ref($lastactref) eq 'HASH') {
16198:                 $lastactref->{'domdefaults'} = 1;
16199:             }
16200:             if (keys(%changes) > 0) {
16201:                 my %titles = &ssl_titles();
16202:                 $resulttext = &mt('Changes made:').'<ul>';
16203:                 foreach my $prefix (@prefixes) {
16204:                     if (ref($changes{$prefix}) eq 'HASH') {
16205:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
16206:                         foreach my $type (@{$types{$prefix}}) {
16207:                             if (defined($changes{$prefix}{$type})) {
16208:                                 my $newvalue;
16209:                                 if (ref($defaultshash{$action}) eq 'HASH') {
16210:                                     if (ref($defaultshash{$action}{$prefix})) {
16211:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
16212:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
16213:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
16214:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
16215:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
16216:                                             }
16217:                                         }
16218:                                     }
16219:                                     if ($newvalue eq '') {
16220:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
16221:                                     } else {
16222:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
16223:                                     }
16224:                                 }
16225:                             }
16226:                         }
16227:                         $resulttext .= '</ul>';
16228:                     }
16229:                 }
16230:             } else {
16231:                 $resulttext = $nochgmsg;
16232:             }
16233:         } else {
16234:             $resulttext = '<span class="LC_error">'.
16235:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16236:         }
16237:     } else {
16238:         $resulttext = $nochgmsg;
16239:     }
16240:     return $resulttext;
16241: }
16242: 
16243: sub modify_trust {
16244:     my ($dom,$lastactref,%domconfig) = @_;
16245:     my (%by_ip,%by_location,@intdoms,@instdoms);
16246:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16247:     my @locations = sort(keys(%by_location));
16248:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
16249:     my @types = ('exc','inc');
16250:     my (%defaultshash,%changes);
16251:     foreach my $prefix (@prefixes) {
16252:         $defaultshash{'trust'}{$prefix} = {};
16253:     }
16254:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16255:     my $resulttext;
16256:     foreach my $prefix (@prefixes) {
16257:         foreach my $type (@types) {
16258:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16259:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16260:             my @okvals;
16261:             foreach my $val (@vals) {
16262:                 if ($val =~ /:/) {
16263:                     my @items = split(/:/,$val);
16264:                     foreach my $item (@items) {
16265:                         if (ref($by_location{$item}) eq 'ARRAY') {
16266:                             push(@okvals,$item);
16267:                         }
16268:                     }
16269:                 } else {
16270:                     if (ref($by_location{$val}) eq 'ARRAY') {
16271:                         push(@okvals,$val);
16272:                     }
16273:                 }
16274:             }
16275:             @okvals = sort(@okvals);
16276:             if (ref($domconfig{'trust'}) eq 'HASH') {
16277:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
16278:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
16279:                         if ($inuse == 0) {
16280:                             $changes{$prefix}{$type} = 1;
16281:                         } else {
16282:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
16283:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
16284:                             if (@changed > 0) {
16285:                                 $changes{$prefix}{$type} = 1;
16286:                             }
16287:                         }
16288:                     } else {
16289:                         if ($inuse == 1) {
16290:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
16291:                             $changes{$prefix}{$type} = 1;
16292:                         }
16293:                     }
16294:                 } else {
16295:                     if ($inuse == 1) {
16296:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
16297:                         $changes{$prefix}{$type} = 1;
16298:                     }
16299:                 }
16300:             } else {
16301:                 if ($inuse == 1) {
16302:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
16303:                     $changes{$prefix}{$type} = 1;
16304:                 }
16305:             }
16306:         }
16307:     }
16308:     my $nochgmsg = &mt('No changes made to trust settings.');
16309:     if (keys(%changes) > 0) {
16310:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16311:                                                  $dom);
16312:         if ($putresult eq 'ok') {
16313:             if (ref($defaultshash{'trust'}) eq 'HASH') {
16314:                 foreach my $prefix (@prefixes) {
16315:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
16316:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
16317:                     }
16318:                 }
16319:             }
16320:             my $cachetime = 24*60*60;
16321:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16322:             if (ref($lastactref) eq 'HASH') {
16323:                 $lastactref->{'domdefaults'} = 1;
16324:             }
16325:             if (keys(%changes) > 0) {
16326:                 my %lt = &trust_titles();
16327:                 $resulttext = &mt('Changes made:').'<ul>';
16328:                 foreach my $prefix (@prefixes) {
16329:                     if (ref($changes{$prefix}) eq 'HASH') {
16330:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
16331:                         foreach my $type (@types) {
16332:                             if (defined($changes{$prefix}{$type})) {
16333:                                 my $newvalue;
16334:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
16335:                                     if (ref($defaultshash{'trust'}{$prefix})) {
16336:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
16337:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
16338:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
16339:                                             }
16340:                                         }
16341:                                     }
16342:                                 }
16343:                                 if ($newvalue eq '') {
16344:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
16345:                                 } else {
16346:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
16347:                                 }
16348:                             }
16349:                         }
16350:                         $resulttext .= '</ul>';
16351:                     }
16352:                 }
16353:                 $resulttext .= '</ul>';
16354:             } else {
16355:                 $resulttext = $nochgmsg;
16356:             }
16357:         } else {
16358:             $resulttext = '<span class="LC_error">'.
16359:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16360:         }
16361:     } else {
16362:         $resulttext = $nochgmsg;
16363:     }
16364:     return $resulttext;
16365: }
16366: 
16367: sub modify_loadbalancing {
16368:     my ($dom,%domconfig) = @_;
16369:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
16370:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
16371:     my ($othertitle,$usertypes,$types) =
16372:         &Apache::loncommon::sorted_inst_types($dom);
16373:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16374:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
16375:     my @sparestypes = ('primary','default');
16376:     my %typetitles = &sparestype_titles();
16377:     my $resulttext;
16378:     my (%currbalancer,%currtargets,%currrules,%existing);
16379:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
16380:         %existing = %{$domconfig{'loadbalancing'}};
16381:     }
16382:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
16383:                               \%currtargets,\%currrules);
16384:     my ($saveloadbalancing,%defaultshash,%changes);
16385:     my ($alltypes,$othertypes,$titles) =
16386:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
16387:     my %ruletitles = &offloadtype_text();
16388:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
16389:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
16390:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
16391:         if ($balancer eq '') {
16392:             next;
16393:         }
16394:         if (!exists($servers{$balancer})) {
16395:             if (exists($currbalancer{$balancer})) {
16396:                 push(@{$changes{'delete'}},$balancer);
16397:             }
16398:             next;
16399:         }
16400:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
16401:             push(@{$changes{'delete'}},$balancer);
16402:             next;
16403:         }
16404:         if (!exists($currbalancer{$balancer})) {
16405:             push(@{$changes{'add'}},$balancer);
16406:         }
16407:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
16408:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
16409:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
16410:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
16411:             $saveloadbalancing = 1;
16412:         }
16413:         foreach my $sparetype (@sparestypes) {
16414:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
16415:             my @offloadto;
16416:             foreach my $target (@targets) {
16417:                 if (($servers{$target}) && ($target ne $balancer)) {
16418:                     if ($sparetype eq 'default') {
16419:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
16420:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
16421:                         }
16422:                     }
16423:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
16424:                         push(@offloadto,$target);
16425:                     }
16426:                 }
16427:             }
16428:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
16429:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
16430:                     push(@offloadto,$balancer);
16431:                 }
16432:             }
16433:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
16434:         }
16435:         if (ref($currtargets{$balancer}) eq 'HASH') {
16436:             foreach my $sparetype (@sparestypes) {
16437:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
16438:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
16439:                     if (@targetdiffs > 0) {
16440:                         $changes{'curr'}{$balancer}{'targets'} = 1;
16441:                     }
16442:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16443:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
16444:                         $changes{'curr'}{$balancer}{'targets'} = 1;
16445:                     }
16446:                 }
16447:             }
16448:         } else {
16449:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
16450:                 foreach my $sparetype (@sparestypes) {
16451:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16452:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
16453:                             $changes{'curr'}{$balancer}{'targets'} = 1;
16454:                         }
16455:                     }
16456:                 }
16457:             }
16458:         }
16459:         my $ishomedom;
16460:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
16461:             $ishomedom = 1;
16462:         }
16463:         if (ref($alltypes) eq 'ARRAY') {
16464:             foreach my $type (@{$alltypes}) {
16465:                 my $rule;
16466:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
16467:                          (!$ishomedom)) {
16468:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
16469:                 }
16470:                 if ($rule eq 'specific') {
16471:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
16472:                     if (exists($servers{$specifiedhost})) {
16473:                         $rule = $specifiedhost;
16474:                     }
16475:                 }
16476:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
16477:                 if (ref($currrules{$balancer}) eq 'HASH') {
16478:                     if ($rule ne $currrules{$balancer}{$type}) {
16479:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
16480:                     }
16481:                 } elsif ($rule ne '') {
16482:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
16483:                 }
16484:             }
16485:         }
16486:     }
16487:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
16488:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
16489:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
16490:             $defaultshash{'loadbalancing'} = {};
16491:         }
16492:         my $putresult = &Apache::lonnet::put_dom('configuration',
16493:                                                  \%defaultshash,$dom);
16494:         if ($putresult eq 'ok') {
16495:             if (keys(%changes) > 0) {
16496:                 my %toupdate;
16497:                 if (ref($changes{'delete'}) eq 'ARRAY') {
16498:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
16499:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
16500:                         $toupdate{$balancer} = 1;
16501:                     }
16502:                 }
16503:                 if (ref($changes{'add'}) eq 'ARRAY') {
16504:                     foreach my $balancer (sort(@{$changes{'add'}})) {
16505:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
16506:                         $toupdate{$balancer} = 1;
16507:                     }
16508:                 }
16509:                 if (ref($changes{'curr'}) eq 'HASH') {
16510:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
16511:                         $toupdate{$balancer} = 1;
16512:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
16513:                             if ($changes{'curr'}{$balancer}{'targets'}) {
16514:                                 my %offloadstr;
16515:                                 foreach my $sparetype (@sparestypes) {
16516:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16517:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
16518:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
16519:                                         }
16520:                                     }
16521:                                 }
16522:                                 if (keys(%offloadstr) == 0) {
16523:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
16524:                                 } else {
16525:                                     my $showoffload;
16526:                                     foreach my $sparetype (@sparestypes) {
16527:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
16528:                                         if (defined($offloadstr{$sparetype})) {
16529:                                             $showoffload .= $offloadstr{$sparetype};
16530:                                         } else {
16531:                                             $showoffload .= &mt('None');
16532:                                         }
16533:                                         $showoffload .= ('&nbsp;'x3);
16534:                                     }
16535:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
16536:                                 }
16537:                             }
16538:                         }
16539:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
16540:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
16541:                                 foreach my $type (@{$alltypes}) {
16542:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
16543:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16544:                                         my $balancetext;
16545:                                         if ($rule eq '') {
16546:                                             $balancetext =  $ruletitles{'default'};
16547:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
16548:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16549:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16550:                                                 foreach my $sparetype (@sparestypes) {
16551:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16552:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
16553:                                                     }
16554:                                                 }
16555:                                                 foreach my $item (@{$alltypes}) {
16556:                                                     next if ($item =~  /^_LC_ipchange/);
16557:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
16558:                                                     if ($hasrule eq 'homeserver') {
16559:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
16560:                                                     } else {
16561:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
16562:                                                             if ($servers{$hasrule}) {
16563:                                                                 $toupdate{$hasrule} = 1;
16564:                                                             }
16565:                                                         }
16566:                                                     }
16567:                                                 }
16568:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
16569:                                                     $balancetext =  $ruletitles{$rule};
16570:                                                 } else {
16571:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16572:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
16573:                                                     if ($receiver) {
16574:                                                         $toupdate{$receiver};
16575:                                                     }
16576:                                                 }
16577:                                             } else {
16578:                                                 $balancetext =  $ruletitles{$rule};
16579:                                             }
16580:                                         } else {
16581:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
16582:                                         }
16583:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
16584:                                     }
16585:                                 }
16586:                             }
16587:                         }
16588:                         if (keys(%toupdate)) {
16589:                             my %thismachine;
16590:                             my $updatedhere;
16591:                             my $cachetime = 60*60*24;
16592:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
16593:                             foreach my $lonhost (keys(%toupdate)) {
16594:                                 if ($thismachine{$lonhost}) {
16595:                                     unless ($updatedhere) {
16596:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
16597:                                                                       $defaultshash{'loadbalancing'},
16598:                                                                       $cachetime);
16599:                                         $updatedhere = 1;
16600:                                     }
16601:                                 } else {
16602:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
16603:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16604:                                 }
16605:                             }
16606:                         }
16607:                     }
16608:                 }
16609:                 if ($resulttext ne '') {
16610:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
16611:                 } else {
16612:                     $resulttext = $nochgmsg;
16613:                 }
16614:             } else {
16615:                 $resulttext = $nochgmsg;
16616:             }
16617:         } else {
16618:             $resulttext = '<span class="LC_error">'.
16619:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16620:         }
16621:     } else {
16622:         $resulttext = $nochgmsg;
16623:     }
16624:     return $resulttext;
16625: }
16626: 
16627: sub recurse_check {
16628:     my ($chkcats,$categories,$depth,$name) = @_;
16629:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
16630:         my $chg = 0;
16631:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
16632:             my $category = $chkcats->[$depth]{$name}[$j];
16633:             my $item;
16634:             if ($category eq '') {
16635:                 $chg ++;
16636:             } else {
16637:                 my $deeper = $depth + 1;
16638:                 $item = &escape($category).':'.&escape($name).':'.$depth;
16639:                 if ($chg) {
16640:                     $categories->{$item} -= $chg;
16641:                 }
16642:                 &recurse_check($chkcats,$categories,$deeper,$category);
16643:                 $deeper --;
16644:             }
16645:         }
16646:     }
16647:     return;
16648: }
16649: 
16650: sub recurse_cat_deletes {
16651:     my ($item,$coursecategories,$deletions) = @_;
16652:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
16653:     my $subdepth = $depth + 1;
16654:     if (ref($coursecategories) eq 'HASH') {
16655:         foreach my $subitem (keys(%{$coursecategories})) {
16656:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
16657:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
16658:                 delete($coursecategories->{$subitem});
16659:                 $deletions->{$subitem} = 1;
16660:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
16661:             }
16662:         }
16663:     }
16664:     return;
16665: }
16666: 
16667: sub active_dc_picker {
16668:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
16669:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16670:     my @domcoord = keys(%domcoords);
16671:     if (keys(%currhash)) {
16672:         foreach my $dc (keys(%currhash)) {
16673:             unless (exists($domcoords{$dc})) {
16674:                 push(@domcoord,$dc);
16675:             }
16676:         }
16677:     }
16678:     @domcoord = sort(@domcoord);
16679:     my $numdcs = scalar(@domcoord);
16680:     my $rows = 0;
16681:     my $table;
16682:     if ($numdcs > 1) {
16683:         $table = '<table>';
16684:         for (my $i=0; $i<@domcoord; $i++) {
16685:             my $rem = $i%($numinrow);
16686:             if ($rem == 0) {
16687:                 if ($i > 0) {
16688:                     $table .= '</tr>';
16689:                 }
16690:                 $table .= '<tr>';
16691:                 $rows ++;
16692:             }
16693:             my $check = '';
16694:             if ($inputtype eq 'radio') {
16695:                 if (keys(%currhash) == 0) {
16696:                     if (!$i) {
16697:                         $check = ' checked="checked"';
16698:                     }
16699:                 } elsif (exists($currhash{$domcoord[$i]})) {
16700:                     $check = ' checked="checked"';
16701:                 }
16702:             } else {
16703:                 if (exists($currhash{$domcoord[$i]})) {
16704:                     $check = ' checked="checked"';
16705:                 }
16706:             }
16707:             if ($i == @domcoord - 1) {
16708:                 my $colsleft = $numinrow - $rem;
16709:                 if ($colsleft > 1) {
16710:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
16711:                 } else {
16712:                     $table .= '<td class="LC_left_item">';
16713:                 }
16714:             } else {
16715:                 $table .= '<td class="LC_left_item">';
16716:             }
16717:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
16718:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16719:             $table .= '<span class="LC_nobreak"><label>'.
16720:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
16721:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
16722:             if ($user ne $dcname.':'.$dcdom) {
16723:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16724:             }
16725:             $table .= '</label></span></td>';
16726:         }
16727:         $table .= '</tr></table>';
16728:     } elsif ($numdcs == 1) {
16729:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
16730:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16731:         if ($inputtype eq 'radio') {
16732:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
16733:             if ($user ne $dcname.':'.$dcdom) {
16734:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16735:             }
16736:         } else {
16737:             my $check;
16738:             if (exists($currhash{$domcoord[0]})) {
16739:                 $check = ' checked="checked"';
16740:             }
16741:             $table = '<span class="LC_nobreak"><label>'.
16742:                      '<input type="checkbox" name="'.$name.'" '.
16743:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
16744:             if ($user ne $dcname.':'.$dcdom) {
16745:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16746:             }
16747:             $table .= '</label></span>';
16748:             $rows ++;
16749:         }
16750:     }
16751:     return ($numdcs,$table,$rows);
16752: }
16753: 
16754: sub usersession_titles {
16755:     return &Apache::lonlocal::texthash(
16756:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
16757:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
16758:                spares => 'Servers offloaded to, when busy',
16759:                version => 'LON-CAPA version requirement',
16760:                excludedomain => 'Allow all, but exclude specific domains',
16761:                includedomain => 'Deny all, but include specific domains',
16762:                primary => 'Primary (checked first)',
16763:                default => 'Default',
16764:            );
16765: }
16766: 
16767: sub id_for_thisdom {
16768:     my (%servers) = @_;
16769:     my %altids;
16770:     foreach my $server (keys(%servers)) {
16771:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
16772:         if ($serverhome ne $server) {
16773:             $altids{$serverhome} = $server;
16774:         }
16775:     }
16776:     return %altids;
16777: }
16778: 
16779: sub count_servers {
16780:     my ($currbalancer,%servers) = @_;
16781:     my (@spares,$numspares);
16782:     foreach my $lonhost (sort(keys(%servers))) {
16783:         next if ($currbalancer eq $lonhost);
16784:         push(@spares,$lonhost);
16785:     }
16786:     if ($currbalancer) {
16787:         $numspares = scalar(@spares);
16788:     } else {
16789:         $numspares = scalar(@spares) - 1;
16790:     }
16791:     return ($numspares,@spares);
16792: }
16793: 
16794: sub lonbalance_targets_js {
16795:     my ($dom,$types,$servers,$settings) = @_;
16796:     my $select = &mt('Select');
16797:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
16798:     if (ref($servers) eq 'HASH') {
16799:         $alltargets = join("','",sort(keys(%{$servers})));
16800:         my @homedoms;
16801:         foreach my $server (sort(keys(%{$servers}))) {
16802:             if (&Apache::lonnet::host_domain($server) eq $dom) {
16803:                 push(@homedoms,'1');
16804:             } else {
16805:                 push(@homedoms,'0');
16806:             }
16807:         }
16808:         $allishome = join("','",@homedoms);
16809:     }
16810:     if (ref($types) eq 'ARRAY') {
16811:         if (@{$types} > 0) {
16812:             @alltypes = @{$types};
16813:         }
16814:     }
16815:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
16816:     $allinsttypes = join("','",@alltypes);
16817:     my (%currbalancer,%currtargets,%currrules,%existing);
16818:     if (ref($settings) eq 'HASH') {
16819:         %existing = %{$settings};
16820:     }
16821:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
16822:                               \%currtargets,\%currrules);
16823:     my $balancers = join("','",sort(keys(%currbalancer)));
16824:     return <<"END";
16825: 
16826: <script type="text/javascript">
16827: // <![CDATA[
16828: 
16829: currBalancers = new Array('$balancers');
16830: 
16831: function toggleTargets(balnum) {
16832:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
16833:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
16834:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
16835:     var prevbalancer = prevhostitem.value;
16836:     var baltotal = document.getElementById('loadbalancing_total').value;
16837:     prevhostitem.value = balancer;
16838:     if (prevbalancer != '') {
16839:         var prevIdx = currBalancers.indexOf(prevbalancer);
16840:         if (prevIdx != -1) {
16841:             currBalancers.splice(prevIdx,1);
16842:         }
16843:     }
16844:     if (balancer == '') {
16845:         hideSpares(balnum);
16846:     } else {
16847:         var currIdx = currBalancers.indexOf(balancer);
16848:         if (currIdx == -1) {
16849:             currBalancers.push(balancer);
16850:         }
16851:         var homedoms = new Array('$allishome');
16852:         var ishomedom = homedoms[lonhostitem.selectedIndex];
16853:         showSpares(balancer,ishomedom,balnum);
16854:     }
16855:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
16856:     return;
16857: }
16858: 
16859: function showSpares(balancer,ishomedom,balnum) {
16860:     var alltargets = new Array('$alltargets');
16861:     var insttypes = new Array('$allinsttypes');
16862:     var offloadtypes = new Array('primary','default');
16863: 
16864:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
16865:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
16866:  
16867:     for (var i=0; i<offloadtypes.length; i++) {
16868:         var count = 0;
16869:         for (var j=0; j<alltargets.length; j++) {
16870:             if (alltargets[j] != balancer) {
16871:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
16872:                 item.value = alltargets[j];
16873:                 item.style.textAlign='left';
16874:                 item.style.textFace='normal';
16875:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
16876:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
16877:                     item.disabled = '';
16878:                 } else {
16879:                     item.disabled = 'disabled';
16880:                     item.checked = false;
16881:                 }
16882:                 count ++;
16883:             }
16884:         }
16885:     }
16886:     for (var k=0; k<insttypes.length; k++) {
16887:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
16888:             if (ishomedom == 1) {
16889:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16890:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16891:             } else {
16892:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16893:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16894:             }
16895:         } else {
16896:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16897:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16898:         }
16899:         if ((insttypes[k] != '_LC_external') && 
16900:             ((insttypes[k] != '_LC_internetdom') ||
16901:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
16902:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
16903:             item.options.length = 0;
16904:             item.options[0] = new Option("","",true,true);
16905:             var idx = 0;
16906:             for (var m=0; m<alltargets.length; m++) {
16907:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
16908:                     idx ++;
16909:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
16910:                 }
16911:             }
16912:         }
16913:     }
16914:     return;
16915: }
16916: 
16917: function hideSpares(balnum) {
16918:     var alltargets = new Array('$alltargets');
16919:     var insttypes = new Array('$allinsttypes');
16920:     var offloadtypes = new Array('primary','default');
16921: 
16922:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
16923:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
16924: 
16925:     var total = alltargets.length - 1;
16926:     for (var i=0; i<offloadtypes; i++) {
16927:         for (var j=0; j<total; j++) {
16928:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
16929:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
16930:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
16931:         }
16932:     }
16933:     for (var k=0; k<insttypes.length; k++) {
16934:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16935:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16936:         if (insttypes[k] != '_LC_external') {
16937:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
16938:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
16939:         }
16940:     }
16941:     return;
16942: }
16943: 
16944: function checkOffloads(item,balnum,type) {
16945:     var alltargets = new Array('$alltargets');
16946:     var offloadtypes = new Array('primary','default');
16947:     if (item.checked) {
16948:         var total = alltargets.length - 1;
16949:         var other;
16950:         if (type == offloadtypes[0]) {
16951:             other = offloadtypes[1];
16952:         } else {
16953:             other = offloadtypes[0];
16954:         }
16955:         for (var i=0; i<total; i++) {
16956:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
16957:             if (server == item.value) {
16958:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
16959:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
16960:                 }
16961:             }
16962:         }
16963:     }
16964:     return;
16965: }
16966: 
16967: function singleServerToggle(balnum,type) {
16968:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
16969:     if (offloadtoSelIdx == 0) {
16970:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
16971:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16972: 
16973:     } else {
16974:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
16975:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16976:     }
16977:     return;
16978: }
16979: 
16980: function balanceruleChange(formname,balnum,type) {
16981:     if (type == '_LC_external') {
16982:         return;
16983:     }
16984:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
16985:     for (var i=0; i<typesRules.length; i++) {
16986:         if (formname.elements[typesRules[i]].checked) {
16987:             if (formname.elements[typesRules[i]].value != 'specific') {
16988:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
16989:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16990:             } else {
16991:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16992:             }
16993:         }
16994:     }
16995:     return;
16996: }
16997: 
16998: function balancerDeleteChange(balnum) {
16999:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17000:     var baltotal = document.getElementById('loadbalancing_total').value;
17001:     var addtarget;
17002:     var removetarget;
17003:     var action = 'delete';
17004:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
17005:         var lonhost = hostitem.value;
17006:         var currIdx = currBalancers.indexOf(lonhost);
17007:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
17008:             if (currIdx != -1) {
17009:                 currBalancers.splice(currIdx,1);
17010:             }
17011:             addtarget = lonhost;
17012:         } else {
17013:             if (currIdx == -1) {
17014:                 currBalancers.push(lonhost);
17015:             }
17016:             removetarget = lonhost;
17017:             action = 'undelete';
17018:         }
17019:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
17020:     }
17021:     return;
17022: }
17023: 
17024: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
17025:     if (baltotal > 1) {
17026:         var offloadtypes = new Array('primary','default');
17027:         var alltargets = new Array('$alltargets');
17028:         var insttypes = new Array('$allinsttypes');
17029:         for (var i=0; i<baltotal; i++) {
17030:             if (i != balnum) {
17031:                 for (var j=0; j<offloadtypes.length; j++) {
17032:                     var total = alltargets.length - 1;
17033:                     for (var k=0; k<total; k++) {
17034:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
17035:                         var server = serveritem.value;
17036:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
17037:                             if (server == addtarget) {
17038:                                 serveritem.disabled = '';
17039:                             }
17040:                         }
17041:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
17042:                             if (server == removetarget) {
17043:                                 serveritem.disabled = 'disabled';
17044:                                 serveritem.checked = false;
17045:                             }
17046:                         }
17047:                     }
17048:                 }
17049:                 for (var j=0; j<insttypes.length; j++) {
17050:                     if (insttypes[j] != '_LC_external') {
17051:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
17052:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
17053:                             var currSel = singleserver.selectedIndex;
17054:                             var currVal = singleserver.options[currSel].value;
17055:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
17056:                                 var numoptions = singleserver.options.length;
17057:                                 var needsnew = 1;
17058:                                 for (var k=0; k<numoptions; k++) {
17059:                                     if (singleserver.options[k] == addtarget) {
17060:                                         needsnew = 0;
17061:                                         break;
17062:                                     }
17063:                                 }
17064:                                 if (needsnew == 1) {
17065:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
17066:                                 }
17067:                             }
17068:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
17069:                                 singleserver.options.length = 0;
17070:                                 if ((currVal) && (currVal != removetarget)) {
17071:                                     singleserver.options[0] = new Option("","",false,false);
17072:                                 } else {
17073:                                     singleserver.options[0] = new Option("","",true,true);
17074:                                 }
17075:                                 var idx = 0;
17076:                                 for (var m=0; m<alltargets.length; m++) {
17077:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
17078:                                         idx ++;
17079:                                         if (currVal == alltargets[m]) {
17080:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
17081:                                         } else {
17082:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17083:                                         }
17084:                                     }
17085:                                 }
17086:                             }
17087:                         }
17088:                     }
17089:                 }
17090:             }
17091:         }
17092:     }
17093:     return;
17094: }
17095: 
17096: // ]]>
17097: </script>
17098: 
17099: END
17100: }
17101: 
17102: sub new_spares_js {
17103:     my @sparestypes = ('primary','default');
17104:     my $types = join("','",@sparestypes);
17105:     my $select = &mt('Select');
17106:     return <<"END";
17107: 
17108: <script type="text/javascript">
17109: // <![CDATA[
17110: 
17111: function updateNewSpares(formname,lonhost) {
17112:     var types = new Array('$types');
17113:     var include = new Array();
17114:     var exclude = new Array();
17115:     for (var i=0; i<types.length; i++) {
17116:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
17117:         for (var j=0; j<spareboxes.length; j++) {
17118:             if (formname.elements[spareboxes[j]].checked) {
17119:                 exclude.push(formname.elements[spareboxes[j]].value);
17120:             } else {
17121:                 include.push(formname.elements[spareboxes[j]].value);
17122:             }
17123:         }
17124:     }
17125:     for (var i=0; i<types.length; i++) {
17126:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
17127:         var selIdx = newSpare.selectedIndex;
17128:         var currnew = newSpare.options[selIdx].value;
17129:         var okSpares = new Array();
17130:         for (var j=0; j<newSpare.options.length; j++) {
17131:             var possible = newSpare.options[j].value;
17132:             if (possible != '') {
17133:                 if (exclude.indexOf(possible) == -1) {
17134:                     okSpares.push(possible);
17135:                 } else {
17136:                     if (currnew == possible) {
17137:                         selIdx = 0;
17138:                     }
17139:                 }
17140:             }
17141:         }
17142:         for (var k=0; k<include.length; k++) {
17143:             if (okSpares.indexOf(include[k]) == -1) {
17144:                 okSpares.push(include[k]);
17145:             }
17146:         }
17147:         okSpares.sort();
17148:         newSpare.options.length = 0;
17149:         if (selIdx == 0) {
17150:             newSpare.options[0] = new Option("$select","",true,true);
17151:         } else {
17152:             newSpare.options[0] = new Option("$select","",false,false);
17153:         }
17154:         for (var m=0; m<okSpares.length; m++) {
17155:             var idx = m+1;
17156:             var selThis = 0;
17157:             if (selIdx != 0) {
17158:                 if (okSpares[m] == currnew) {
17159:                     selThis = 1;
17160:                 }
17161:             }
17162:             if (selThis == 1) {
17163:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
17164:             } else {
17165:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
17166:             }
17167:         }
17168:     }
17169:     return;
17170: }
17171: 
17172: function checkNewSpares(lonhost,type) {
17173:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
17174:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
17175:     if (chosen != '') { 
17176:         var othertype;
17177:         var othernewSpare;
17178:         if (type == 'primary') {
17179:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
17180:         }
17181:         if (type == 'default') {
17182:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
17183:         }
17184:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
17185:             othernewSpare.selectedIndex = 0;
17186:         }
17187:     }
17188:     return;
17189: }
17190: 
17191: // ]]>
17192: </script>
17193: 
17194: END
17195: 
17196: }
17197: 
17198: sub common_domprefs_js {
17199:     return <<"END";
17200: 
17201: <script type="text/javascript">
17202: // <![CDATA[
17203: 
17204: function getIndicesByName(formname,item) {
17205:     var group = new Array();
17206:     for (var i=0;i<formname.elements.length;i++) {
17207:         if (formname.elements[i].name == item) {
17208:             group.push(formname.elements[i].id);
17209:         }
17210:     }
17211:     return group;
17212: }
17213: 
17214: // ]]>
17215: </script>
17216: 
17217: END
17218: 
17219: }
17220: 
17221: sub recaptcha_js {
17222:     my %lt = &captcha_phrases();
17223:     return <<"END";
17224: 
17225: <script type="text/javascript">
17226: // <![CDATA[
17227: 
17228: function updateCaptcha(caller,context) {
17229:     var privitem;
17230:     var pubitem;
17231:     var privtext;
17232:     var pubtext;
17233:     var versionitem;
17234:     var versiontext;
17235:     if (document.getElementById(context+'_recaptchapub')) {
17236:         pubitem = document.getElementById(context+'_recaptchapub');
17237:     } else {
17238:         return;
17239:     }
17240:     if (document.getElementById(context+'_recaptchapriv')) {
17241:         privitem = document.getElementById(context+'_recaptchapriv');
17242:     } else {
17243:         return;
17244:     }
17245:     if (document.getElementById(context+'_recaptchapubtxt')) {
17246:         pubtext = document.getElementById(context+'_recaptchapubtxt');
17247:     } else {
17248:         return;
17249:     }
17250:     if (document.getElementById(context+'_recaptchaprivtxt')) {
17251:         privtext = document.getElementById(context+'_recaptchaprivtxt');
17252:     } else {
17253:         return;
17254:     }
17255:     if (document.getElementById(context+'_recaptchaversion')) {
17256:         versionitem = document.getElementById(context+'_recaptchaversion');
17257:     } else {
17258:         return;
17259:     }
17260:     if (document.getElementById(context+'_recaptchavertxt')) {
17261:         versiontext = document.getElementById(context+'_recaptchavertxt');
17262:     } else {
17263:         return;
17264:     }
17265:     if (caller.checked) {
17266:         if (caller.value == 'recaptcha') {
17267:             pubitem.type = 'text';
17268:             privitem.type = 'text';
17269:             pubitem.size = '40';
17270:             privitem.size = '40';
17271:             pubtext.innerHTML = "$lt{'pub'}";
17272:             privtext.innerHTML = "$lt{'priv'}";
17273:             versionitem.type = 'text';
17274:             versionitem.size = '3';
17275:             versiontext.innerHTML = "$lt{'ver'}";
17276:         } else {
17277:             pubitem.type = 'hidden';
17278:             privitem.type = 'hidden';
17279:             versionitem.type = 'hidden';
17280:             pubtext.innerHTML = '';
17281:             privtext.innerHTML = '';
17282:             versiontext.innerHTML = '';
17283:         }
17284:     }
17285:     return;
17286: }
17287: 
17288: // ]]>
17289: </script>
17290: 
17291: END
17292: 
17293: }
17294: 
17295: sub toggle_display_js {
17296:     return <<"END";
17297: 
17298: <script type="text/javascript">
17299: // <![CDATA[
17300: 
17301: function toggleDisplay(domForm,caller) {
17302:     if (document.getElementById(caller)) {
17303:         var divitem = document.getElementById(caller);
17304:         var optionsElement = domForm.coursecredits;
17305:         var checkval = 1;
17306:         var dispval = 'block';
17307:         var selfcreateRegExp = /^cancreate_emailverified/;
17308:         if (caller == 'emailoptions') {
17309:             optionsElement = domForm.cancreate_email; 
17310:         }
17311:         if (caller == 'studentsubmission') {
17312:             optionsElement = domForm.postsubmit;
17313:         }
17314:         if (caller == 'cloneinstcode') {
17315:             optionsElement = domForm.canclone;
17316:             checkval = 'instcode';
17317:         }
17318:         if (selfcreateRegExp.test(caller)) {
17319:             optionsElement = domForm.elements[caller];
17320:             checkval = 'other';
17321:             dispval = 'inline'
17322:         }
17323:         if (optionsElement.length) {
17324:             var currval;
17325:             for (var i=0; i<optionsElement.length; i++) {
17326:                 if (optionsElement[i].checked) {
17327:                    currval = optionsElement[i].value;
17328:                 }
17329:             }
17330:             if (currval == checkval) {
17331:                 divitem.style.display = dispval;
17332:             } else {
17333:                 divitem.style.display = 'none';
17334:             }
17335:         }
17336:     }
17337:     return;
17338: }
17339: 
17340: // ]]>
17341: </script>
17342: 
17343: END
17344: 
17345: }
17346: 
17347: sub captcha_phrases {
17348:     return &Apache::lonlocal::texthash (
17349:                  priv => 'Private key',
17350:                  pub  => 'Public key',
17351:                  original  => 'original (CAPTCHA)',
17352:                  recaptcha => 'successor (ReCAPTCHA)',
17353:                  notused   => 'unused',
17354:                  ver => 'ReCAPTCHA version (1 or 2)',
17355:     );
17356: }
17357: 
17358: sub devalidate_remote_domconfs {
17359:     my ($dom,$cachekeys) = @_;
17360:     return unless (ref($cachekeys) eq 'HASH');
17361:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17362:     my %thismachine;
17363:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
17364:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
17365:     if (keys(%servers)) {
17366:         foreach my $server (keys(%servers)) {
17367:             next if ($thismachine{$server});
17368:             my @cached;
17369:             foreach my $name (@posscached) {
17370:                 if ($cachekeys->{$name}) {
17371:                     push(@cached,&escape($name).':'.&escape($dom));
17372:                 }
17373:             }
17374:             if (@cached) {
17375:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
17376:             }
17377:         }
17378:     }
17379:     return;
17380: }
17381: 
17382: 1;

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