File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.346: download - view: text, annotated - select for diffs
Sun Jan 27 14:39:48 2019 UTC (5 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Uploaded bubblesheet data can consist of comma separated values.
  - Data will be converted to one of formats specified in bubblesheet
    format file, prior to saving in scantron_orig_* file.
  - Domain configuration for bubblesheet data format allows order of
     columns in csv data to be specified.
  - &get_scantronformat_file and &get_scantron_config moved from grades.pm
    to lonnet.pm to facilitate re-use.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.346 2019/01/27 14:39:48 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, placement, and lti).  
  108: In each case the radio buttons allow the selection of one of four values:  
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: 
  180: my $registered_cleanup;
  181: my $modified_urls;
  182: 
  183: sub handler {
  184:     my $r=shift;
  185:     if ($r->header_only) {
  186:         &Apache::loncommon::content_type($r,'text/html');
  187:         $r->send_http_header;
  188:         return OK;
  189:     }
  190: 
  191:     my $context = 'domain';
  192:     my $dom = $env{'request.role.domain'};
  193:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  194:     if (&Apache::lonnet::allowed('mau',$dom)) {
  195:         &Apache::loncommon::content_type($r,'text/html');
  196:         $r->send_http_header;
  197:     } else {
  198:         $env{'user.error.msg'}=
  199:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  200:         return HTTP_NOT_ACCEPTABLE;
  201:     }
  202: 
  203:     $registered_cleanup=0;
  204:     @{$modified_urls}=();
  205: 
  206:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  208:                                             ['phase','actions']);
  209:     my $phase = 'pickactions';
  210:     if ( exists($env{'form.phase'}) ) {
  211:         $phase = $env{'form.phase'};
  212:     }
  213:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  214:     my %domconfig =
  215:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  216:                 'quotas','autoenroll','autoupdate','autocreate',
  217:                 'directorysrch','usercreation','usermodification',
  218:                 'contacts','defaults','scantron','coursecategories',
  219:                 'serverstatuses','requestcourses','helpsettings',
  220:                 'coursedefaults','usersessions','loadbalancing',
  221:                 'requestauthor','selfenrollment','inststatus',
  222:                 'ltitools','ssl','trust','lti'],$dom);
  223:     my %encconfig =
  224:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti'],$dom);
  225:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  226:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  227:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  228:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  229:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  230:                     foreach my $item ('key','secret') {
  231:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  232:                     }
  233:                 }
  234:             }
  235:         }
  236:     }
  237:     if (ref($domconfig{'lti'}) eq 'HASH') {
  238:         if (ref($encconfig{'lti'}) eq 'HASH') {
  239:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  240:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  241:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  242:                     foreach my $item ('key','secret') {
  243:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  244:                     }
  245:                 }
  246:             }
  247:         }
  248:     }
  249:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  250:                        'autoupdate','autocreate','directorysrch','contacts',
  251:                        'usercreation','selfcreation','usermodification','scantron',
  252:                        'requestcourses','requestauthor','coursecategories',
  253:                        'serverstatuses','helpsettings','coursedefaults',
  254:                        'ltitools','selfenrollment','usersessions','ssl','trust','lti');
  255:     my %existing;
  256:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  257:         %existing = %{$domconfig{'loadbalancing'}};
  258:     }
  259:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  260:         push(@prefs_order,'loadbalancing');
  261:     }
  262:     my %prefs = (
  263:         'rolecolors' =>
  264:                    { text => 'Default color schemes',
  265:                      help => 'Domain_Configuration_Color_Schemes',
  266:                      header => [{col1 => 'Student Settings',
  267:                                  col2 => '',},
  268:                                 {col1 => 'Coordinator Settings',
  269:                                  col2 => '',},
  270:                                 {col1 => 'Author Settings',
  271:                                  col2 => '',},
  272:                                 {col1 => 'Administrator Settings',
  273:                                  col2 => '',}],
  274:                       print => \&print_rolecolors,
  275:                       modify => \&modify_rolecolors,
  276:                     },
  277:         'login' =>
  278:                     { text => 'Log-in page options',
  279:                       help => 'Domain_Configuration_Login_Page',
  280:                       header => [{col1 => 'Log-in Page Items',
  281:                                   col2 => '',},
  282:                                  {col1 => 'Log-in Help',
  283:                                   col2 => 'Value'},
  284:                                  {col1 => 'Custom HTML in document head',
  285:                                   col2 => 'Value'}],
  286:                       print => \&print_login,
  287:                       modify => \&modify_login,
  288:                     },
  289:         'defaults' => 
  290:                     { text => 'Default authentication/language/timezone/portal/types',
  291:                       help => 'Domain_Configuration_LangTZAuth',
  292:                       header => [{col1 => 'Setting',
  293:                                   col2 => 'Value'},
  294:                                  {col1 => 'Internal Authentication',
  295:                                   col2 => 'Value'},
  296:                                  {col1 => 'Institutional user types',
  297:                                   col2 => 'Name displayed'}],
  298:                       print => \&print_defaults,
  299:                       modify => \&modify_defaults,
  300:                     },
  301:         'quotas' => 
  302:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  303:                       help => 'Domain_Configuration_Quotas',
  304:                       header => [{col1 => 'User affiliation',
  305:                                   col2 => 'Available tools',
  306:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  307:                       print => \&print_quotas,
  308:                       modify => \&modify_quotas,
  309:                     },
  310:         'autoenroll' =>
  311:                    { text => 'Auto-enrollment settings',
  312:                      help => 'Domain_Configuration_Auto_Enrollment',
  313:                      header => [{col1 => 'Configuration setting',
  314:                                  col2 => 'Value(s)'}],
  315:                      print => \&print_autoenroll,
  316:                      modify => \&modify_autoenroll,
  317:                    },
  318:         'autoupdate' => 
  319:                    { text => 'Auto-update settings',
  320:                      help => 'Domain_Configuration_Auto_Updates',
  321:                      header => [{col1 => 'Setting',
  322:                                  col2 => 'Value',},
  323:                                 {col1 => 'Setting',
  324:                                  col2 => 'Affiliation'},
  325:                                 {col1 => 'User population',
  326:                                  col2 => 'Updatable user data'}],
  327:                      print => \&print_autoupdate,
  328:                      modify => \&modify_autoupdate,
  329:                   },
  330:         'autocreate' => 
  331:                   { text => 'Auto-course creation settings',
  332:                      help => 'Domain_Configuration_Auto_Creation',
  333:                      header => [{col1 => 'Configuration Setting',
  334:                                  col2 => 'Value',}],
  335:                      print => \&print_autocreate,
  336:                      modify => \&modify_autocreate,
  337:                   },
  338:         'directorysrch' => 
  339:                   { text => 'Directory searches',
  340:                     help => 'Domain_Configuration_InstDirectory_Search',
  341:                     header => [{col1 => 'Institutional Directory Setting',
  342:                                 col2 => 'Value',},
  343:                                {col1 => 'LON-CAPA Directory Setting',
  344:                                 col2 => 'Value',}],
  345:                     print => \&print_directorysrch,
  346:                     modify => \&modify_directorysrch,
  347:                   },
  348:         'contacts' =>
  349:                   { text => 'E-mail addresses and helpform',
  350:                     help => 'Domain_Configuration_Contact_Info',
  351:                     header => [{col1 => 'Default e-mail addresses',
  352:                                 col2 => 'Value',},
  353:                                {col1 => 'Recipient(s) for notifications',
  354:                                 col2 => 'Value',},
  355:                                {col1 => 'Nightly status check e-mail',
  356:                                 col2 => 'Settings',},
  357:                                {col1 => 'Ask helpdesk form settings',
  358:                                 col2 => 'Value',},],
  359:                     print => \&print_contacts,
  360:                     modify => \&modify_contacts,
  361:                   },
  362:         'usercreation' => 
  363:                   { text => 'User creation',
  364:                     help => 'Domain_Configuration_User_Creation',
  365:                     header => [{col1 => 'Format rule type',
  366:                                 col2 => 'Format rules in force'},
  367:                                {col1 => 'User account creation',
  368:                                 col2 => 'Usernames which may be created',},
  369:                                {col1 => 'Context',
  370:                                 col2 => 'Assignable authentication types'}],
  371:                     print => \&print_usercreation,
  372:                     modify => \&modify_usercreation,
  373:                   },
  374:         'selfcreation' => 
  375:                   { text => 'Users self-creating accounts',
  376:                     help => 'Domain_Configuration_Self_Creation', 
  377:                     header => [{col1 => 'Self-creation with institutional username',
  378:                                 col2 => 'Enabled?'},
  379:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  380:                                 col2 => 'Information user can enter'},
  381:                                {col1 => 'Self-creation with e-mail verification',
  382:                                 col2 => 'Settings'}],
  383:                     print => \&print_selfcreation,
  384:                     modify => \&modify_selfcreation,
  385:                   },
  386:         'usermodification' =>
  387:                   { text => 'User modification',
  388:                     help => 'Domain_Configuration_User_Modification',
  389:                     header => [{col1 => 'Target user has role',
  390:                                 col2 => 'User information updatable in author context'},
  391:                                {col1 => 'Target user has role',
  392:                                 col2 => 'User information updatable in course context'}],
  393:                     print => \&print_usermodification,
  394:                     modify => \&modify_usermodification,
  395:                   },
  396:         'scantron' =>
  397:                   { text => 'Bubblesheet format',
  398:                     help => 'Domain_Configuration_Scantron_Format',
  399:                     header => [ {col1 => 'Bubblesheet format file',
  400:                                  col2 => ''},
  401:                                 {col1 => 'Bubblesheet data upload formats',
  402:                                  col2 => 'Settings'}],
  403:                     print => \&print_scantron,
  404:                     modify => \&modify_scantron,
  405:                   },
  406:         'requestcourses' => 
  407:                  {text => 'Request creation of courses',
  408:                   help => 'Domain_Configuration_Request_Courses',
  409:                   header => [{col1 => 'User affiliation',
  410:                               col2 => 'Availability/Processing of requests',},
  411:                              {col1 => 'Setting',
  412:                               col2 => 'Value'},
  413:                              {col1 => 'Available textbooks',
  414:                               col2 => ''},
  415:                              {col1 => 'Available templates',
  416:                               col2 => ''},
  417:                              {col1 => 'Validation (not official courses)',
  418:                               col2 => 'Value'},],
  419:                   print => \&print_quotas,
  420:                   modify => \&modify_quotas,
  421:                  },
  422:         'requestauthor' =>
  423:                  {text => 'Request Authoring Space',
  424:                   help => 'Domain_Configuration_Request_Author',
  425:                   header => [{col1 => 'User affiliation',
  426:                               col2 => 'Availability/Processing of requests',},
  427:                              {col1 => 'Setting',
  428:                               col2 => 'Value'}],
  429:                   print => \&print_quotas,
  430:                   modify => \&modify_quotas,
  431:                  },
  432:         'coursecategories' =>
  433:                   { text => 'Cataloging of courses/communities',
  434:                     help => 'Domain_Configuration_Cataloging_Courses',
  435:                     header => [{col1 => 'Catalog type/availability',
  436:                                 col2 => '',},
  437:                                {col1 => 'Category settings for standard catalog',
  438:                                 col2 => '',},
  439:                                {col1 => 'Categories',
  440:                                 col2 => '',
  441:                                }],
  442:                     print => \&print_coursecategories,
  443:                     modify => \&modify_coursecategories,
  444:                   },
  445:         'serverstatuses' =>
  446:                  {text   => 'Access to server status pages',
  447:                   help   => 'Domain_Configuration_Server_Status',
  448:                   header => [{col1 => 'Status Page',
  449:                               col2 => 'Other named users',
  450:                               col3 => 'Specific IPs',
  451:                             }],
  452:                   print => \&print_serverstatuses,
  453:                   modify => \&modify_serverstatuses,
  454:                  },
  455:         'helpsettings' =>
  456:                  {text   => 'Support settings',
  457:                   help   => 'Domain_Configuration_Help_Settings',
  458:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  459:                               col2 => 'Value'},
  460:                              {col1 => 'Helpdesk Roles',
  461:                               col2 => 'Settings'},],
  462:                   print  => \&print_helpsettings,
  463:                   modify => \&modify_helpsettings,
  464:                  },
  465:         'coursedefaults' => 
  466:                  {text => 'Course/Community defaults',
  467:                   help => 'Domain_Configuration_Course_Defaults',
  468:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  469:                               col2 => 'Value',},
  470:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  471:                               col2 => 'Value',},],
  472:                   print => \&print_coursedefaults,
  473:                   modify => \&modify_coursedefaults,
  474:                  },
  475:         'selfenrollment' => 
  476:                  {text   => 'Self-enrollment in Course/Community',
  477:                   help   => 'Domain_Configuration_Selfenrollment',
  478:                   header => [{col1 => 'Configuration Rights',
  479:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  480:                              {col1 => 'Defaults',
  481:                               col2 => 'Value'},
  482:                              {col1 => 'Self-enrollment validation (optional)',
  483:                               col2 => 'Value'},],
  484:                   print => \&print_selfenrollment,
  485:                   modify => \&modify_selfenrollment,
  486:                  },
  487:         'privacy' => 
  488:                  {text   => 'User Privacy',
  489:                   help   => 'Domain_Configuration_User_Privacy',
  490:                   header => [{col1 => 'Setting',
  491:                               col2 => 'Value',}],
  492:                   print => \&print_privacy,
  493:                   modify => \&modify_privacy,
  494:                  },
  495:         'usersessions' =>
  496:                  {text  => 'User session hosting/offloading',
  497:                   help  => 'Domain_Configuration_User_Sessions',
  498:                   header => [{col1 => 'Domain server',
  499:                               col2 => 'Servers to offload sessions to when busy'},
  500:                              {col1 => 'Hosting of users from other domains',
  501:                               col2 => 'Rules'},
  502:                              {col1 => "Hosting domain's own users elsewhere",
  503:                               col2 => 'Rules'}],
  504:                   print => \&print_usersessions,
  505:                   modify => \&modify_usersessions,
  506:                  },
  507:         'loadbalancing' =>
  508:                  {text  => 'Dedicated Load Balancer(s)',
  509:                   help  => 'Domain_Configuration_Load_Balancing',
  510:                   header => [{col1 => 'Balancers',
  511:                               col2 => 'Default destinations',
  512:                               col3 => 'User affiliation',
  513:                               col4 => 'Overrides'},
  514:                             ],
  515:                   print => \&print_loadbalancing,
  516:                   modify => \&modify_loadbalancing,
  517:                  },
  518:         'ltitools' => 
  519:                  {text => 'External Tools (LTI)',
  520:                   help => 'Domain_Configuration_LTI_Tools',
  521:                   header => [{col1 => 'Setting',
  522:                               col2 => 'Value',}],
  523:                   print => \&print_ltitools,
  524:                   modify => \&modify_ltitools,
  525:                  },
  526:         'ssl' =>
  527:                  {text  => 'LON-CAPA Network (SSL)',
  528:                   help  => 'Domain_Configuration_Network_SSL',
  529:                   header => [{col1 => 'Server',
  530:                               col2 => 'Certificate Status'},
  531:                              {col1 => 'Connections to other servers',
  532:                               col2 => 'Rules'},
  533:                              {col1 => 'Connections from other servers',
  534:                               col2 => 'Rules'},
  535:                              {col1 => "Replicating domain's published content",
  536:                               col2 => 'Rules'}],
  537:                   print => \&print_ssl,
  538:                   modify => \&modify_ssl,
  539:                  },
  540:         'trust' =>
  541:                  {text   => 'Trust Settings',
  542:                   help   => 'Domain_Configuration_Trust',
  543:                   header => [{col1 => "Access to this domain's content by others",
  544:                               col2 => 'Rules'},
  545:                              {col1 => "Access to other domain's content by this domain",
  546:                               col2 => 'Rules'},
  547:                              {col1 => "Enrollment in this domain's courses by others",
  548:                               col2 => 'Rules',},
  549:                              {col1 => "Co-author roles in this domain for others",
  550:                               col2 => 'Rules',},
  551:                              {col1 => "Co-author roles for this domain's users elsewhere",
  552:                               col2 => 'Rules',},
  553:                              {col1 => "Domain roles in this domain assignable to others",
  554:                               col2 => 'Rules'},
  555:                              {col1 => "Course catalog for this domain displayed elsewhere",
  556:                               col2 => 'Rules'},
  557:                              {col1 => "Requests for creation of courses in this domain by others",
  558:                               col2 => 'Rules'},
  559:                              {col1 => "Users in other domains can send messages to this domain",
  560:                               col2 => 'Rules'},],
  561:                   print => \&print_trust,
  562:                   modify => \&modify_trust,
  563:                  },
  564:           'lti' =>
  565:                  {text => 'LTI Provider',
  566:                   help => 'Domain_Configuration_LTI_Provider',
  567:                   header => [{col1 => 'Setting',
  568:                               col2 => 'Value',}],
  569:                   print => \&print_lti,
  570:                   modify => \&modify_lti,
  571:                  },
  572:     );
  573:     if (keys(%servers) > 1) {
  574:         $prefs{'login'}  = { text   => 'Log-in page options',
  575:                              help   => 'Domain_Configuration_Login_Page',
  576:                             header => [{col1 => 'Log-in Service',
  577:                                         col2 => 'Server Setting',},
  578:                                        {col1 => 'Log-in Page Items',
  579:                                         col2 => ''},
  580:                                        {col1 => 'Log-in Help',
  581:                                         col2 => 'Value'},
  582:                                        {col1 => 'Custom HTML in document head',
  583:                                         col2 => 'Value'}],
  584:                             print => \&print_login,
  585:                             modify => \&modify_login,
  586:                            };
  587:     }
  588: 
  589:     my @roles = ('student','coordinator','author','admin');
  590:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  591:     &Apache::lonhtmlcommon::add_breadcrumb
  592:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  593:       text=>"Settings to display/modify"});
  594:     my $confname = $dom.'-domainconfig';
  595: 
  596:     if ($phase eq 'process') {
  597:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  598:                                                               \%prefs,\%domconfig,$confname,\@roles);
  599:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  600:             $r->rflush();
  601:             &devalidate_remote_domconfs($dom,$result);
  602:         }
  603:     } elsif ($phase eq 'display') {
  604:         my $js = &recaptcha_js().
  605:                  &toggle_display_js();
  606:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  607:             my ($othertitle,$usertypes,$types) =
  608:                 &Apache::loncommon::sorted_inst_types($dom);
  609:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  610:                                           $domconfig{'loadbalancing'}).
  611:                    &new_spares_js().
  612:                    &common_domprefs_js().
  613:                    &Apache::loncommon::javascript_array_indexof();
  614:         }
  615:         if (grep(/^requestcourses$/,@actions)) {
  616:             my $javascript_validations;
  617:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  618:             $js .= <<END;
  619: <script type="text/javascript">
  620: $javascript_validations
  621: </script>
  622: $coursebrowserjs
  623: END
  624:         }
  625:         if (grep(/^selfcreation$/,@actions)) {
  626:             $js .= &selfcreate_javascript();
  627:         }
  628:         if (grep(/^contacts$/,@actions)) {
  629:             $js .= &contacts_javascript();
  630:         }
  631:         if (grep(/^scantron$/,@actions)) {
  632:             $js .= &scantron_javascript();
  633:         }
  634:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  635:     } else {
  636: # check if domconfig user exists for the domain.
  637:         my $servadm = $r->dir_config('lonAdmEMail');
  638:         my ($configuserok,$author_ok,$switchserver) =
  639:             &config_check($dom,$confname,$servadm);
  640:         unless ($configuserok eq 'ok') {
  641:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  642:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  643:                           $confname).
  644:                       '<br />'
  645:             );
  646:             if ($switchserver) {
  647:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  648:                           '<br />'.
  649:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  650:                           '<br />'.
  651:                           &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).
  652:                           '<br />'.
  653:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  654:                 );
  655:             } else {
  656:                 $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.').
  657:                           '<br />'.
  658:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  659:                 );
  660:             }
  661:             $r->print(&Apache::loncommon::end_page());
  662:             return OK;
  663:         }
  664:         if (keys(%domconfig) == 0) {
  665:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  666:             my @ids=&Apache::lonnet::current_machine_ids();
  667:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  668:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  669:                 my @loginimages = ('img','logo','domlogo','login');
  670:                 my $custom_img_count = 0;
  671:                 foreach my $img (@loginimages) {
  672:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  673:                         $custom_img_count ++;
  674:                     }
  675:                 }
  676:                 foreach my $role (@roles) {
  677:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  678:                         $custom_img_count ++;
  679:                     }
  680:                 }
  681:                 if ($custom_img_count > 0) {
  682:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  683:                     my $switch_server = &check_switchserver($dom,$confname);
  684:                     $r->print(
  685:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  686:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  687:     &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 />'.
  688:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  689:                     if ($switch_server) {
  690:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  691:                     }
  692:                     $r->print(&Apache::loncommon::end_page());
  693:                     return OK;
  694:                 }
  695:             }
  696:         }
  697:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  698:     }
  699:     return OK;
  700: }
  701: 
  702: sub process_changes {
  703:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  704:     my %domconfig;
  705:     if (ref($values) eq 'HASH') {
  706:         %domconfig = %{$values};
  707:     }
  708:     my $output;
  709:     if ($action eq 'login') {
  710:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  711:     } elsif ($action eq 'rolecolors') {
  712:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  713:                                      $lastactref,%domconfig);
  714:     } elsif ($action eq 'quotas') {
  715:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  716:     } elsif ($action eq 'autoenroll') {
  717:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  718:     } elsif ($action eq 'autoupdate') {
  719:         $output = &modify_autoupdate($dom,%domconfig);
  720:     } elsif ($action eq 'autocreate') {
  721:         $output = &modify_autocreate($dom,%domconfig);
  722:     } elsif ($action eq 'directorysrch') {
  723:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  724:     } elsif ($action eq 'usercreation') {
  725:         $output = &modify_usercreation($dom,%domconfig);
  726:     } elsif ($action eq 'selfcreation') {
  727:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  728:     } elsif ($action eq 'usermodification') {
  729:         $output = &modify_usermodification($dom,%domconfig);
  730:     } elsif ($action eq 'contacts') {
  731:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  732:     } elsif ($action eq 'defaults') {
  733:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  734:     } elsif ($action eq 'scantron') {
  735:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  736:     } elsif ($action eq 'coursecategories') {
  737:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  738:     } elsif ($action eq 'serverstatuses') {
  739:         $output = &modify_serverstatuses($dom,%domconfig);
  740:     } elsif ($action eq 'requestcourses') {
  741:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  742:     } elsif ($action eq 'requestauthor') {
  743:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  744:     } elsif ($action eq 'helpsettings') {
  745:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  746:     } elsif ($action eq 'coursedefaults') {
  747:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  748:     } elsif ($action eq 'selfenrollment') {
  749:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  750:     } elsif ($action eq 'usersessions') {
  751:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  752:     } elsif ($action eq 'loadbalancing') {
  753:         $output = &modify_loadbalancing($dom,%domconfig);
  754:     } elsif ($action eq 'ltitools') {
  755:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  756:     } elsif ($action eq 'ssl') {
  757:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  758:     } elsif ($action eq 'trust') {
  759:         $output = &modify_trust($dom,$lastactref,%domconfig);
  760:     } elsif ($action eq 'lti') {
  761:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  762:     }
  763:     return $output;
  764: }
  765: 
  766: sub print_config_box {
  767:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  768:     my $rowtotal = 0;
  769:     my $output;
  770:     if ($action eq 'coursecategories') {
  771:         $output = &coursecategories_javascript($settings);
  772:     } elsif ($action eq 'defaults') {
  773:         $output = &defaults_javascript($settings); 
  774:     } elsif ($action eq 'helpsettings') {
  775:         my (%privs,%levelscurrent);
  776:         my %full=();
  777:         my %levels=(
  778:                      course => {},
  779:                      domain => {},
  780:                      system => {},
  781:                    );
  782:         my $context = 'domain';
  783:         my $crstype = 'Course';
  784:         my $formname = 'display';
  785:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  786:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  787:         $output =
  788:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  789:                                                       \@templateroles);
  790:     } elsif ($action eq 'ltitools') {
  791:         $output .= &ltitools_javascript($settings);
  792:     } elsif ($action eq 'lti') {
  793:         $output .= &lti_javascript($settings);
  794:     }
  795:     $output .=
  796:          '<table class="LC_nested_outer">
  797:           <tr>
  798:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  799:            &mt($item->{text}).'&nbsp;'.
  800:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  801:           '</tr>';
  802:     $rowtotal ++;
  803:     my $numheaders = 1;
  804:     if (ref($item->{'header'}) eq 'ARRAY') {
  805:         $numheaders = scalar(@{$item->{'header'}});
  806:     }
  807:     if ($numheaders > 1) {
  808:         my $colspan = '';
  809:         my $rightcolspan = '';
  810:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  811:             ($action eq 'directorysrch') ||
  812:             (($action eq 'login') && ($numheaders < 4))) {
  813:             $colspan = ' colspan="2"';
  814:         }
  815:         if ($action eq 'usersessions') {
  816:             $rightcolspan = ' colspan="3"'; 
  817:         }
  818:         $output .= '
  819:           <tr>
  820:            <td>
  821:             <table class="LC_nested">
  822:              <tr class="LC_info_row">
  823:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  824:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  825:              </tr>';
  826:         $rowtotal ++;
  827:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  828:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  829:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  830:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  831:             ($action eq 'contacts')) {
  832:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  833:         } elsif ($action eq 'coursecategories') {
  834:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  835:         } elsif ($action eq 'scantron') {
  836:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  837:         } elsif ($action eq 'login') {
  838:             if ($numheaders == 4) {
  839:                 $colspan = ' colspan="2"';
  840:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  841:             } else {
  842:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  843:             }
  844:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  845:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  846:         } elsif ($action eq 'rolecolors') {
  847:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  848:         }
  849:         $output .= '
  850:            </table>
  851:           </td>
  852:          </tr>
  853:          <tr>
  854:            <td>
  855:             <table class="LC_nested">
  856:              <tr class="LC_info_row">
  857:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  858:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  859:              </tr>';
  860:             $rowtotal ++;
  861:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  862:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  863:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  864:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults')) {
  865:             if ($action eq 'coursecategories') {
  866:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  867:                 $colspan = ' colspan="2"';
  868:             } elsif ($action eq 'trust') {
  869:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  870:             } else {
  871:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  872:             }
  873:             if ($action eq 'trust') {
  874:                 $output .= '
  875:             </table>
  876:           </td>
  877:          </tr>';
  878:                 my @trusthdrs = qw(2 3 4 5 6 7);
  879:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  880:                 for (my $i=0; $i<@trusthdrs; $i++) {
  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'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  887:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  888:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  889:             </table>
  890:           </td>
  891:          </tr>';
  892:                 }
  893:                 $output .= '
  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'}->[8]->{'col1'}).'</td>
  899:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  900:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  901:             } else {
  902:                 $output .= '
  903:            </table>
  904:           </td>
  905:          </tr>
  906:          <tr>
  907:            <td>
  908:             <table class="LC_nested">
  909:              <tr class="LC_info_row">
  910:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  911:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  912:              </tr>'."\n";
  913:                 if ($action eq 'coursecategories') {
  914:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  915:                 } elsif ($action eq 'contacts') {
  916:                     $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal).'
  917:              </tr>
  918:             </table>
  919:            </td>
  920:           </tr>
  921:           <tr>
  922:            <td>
  923:             <table class="LC_nested">
  924:              <tr class="LC_info_row">
  925:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  926:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  927:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal).'
  928:             </table>
  929:           </td>
  930:          </tr>
  931:          <tr>';
  932:                 } else {
  933:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  934:                 }
  935:             }
  936:             $rowtotal ++;
  937:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  938:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  939:                  ($action eq 'helpsettings')) {
  940:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  941:         } elsif ($action eq 'scantron') {
  942:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  943:         } elsif ($action eq 'ssl') {
  944:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  945:             </table>
  946:           </td>
  947:          </tr>
  948:          <tr>
  949:            <td>
  950:             <table class="LC_nested">
  951:              <tr class="LC_info_row">
  952:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  953:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  954:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  955:             </table>
  956:           </td>
  957:          </tr>
  958:          <tr>
  959:            <td>
  960:             <table class="LC_nested">
  961:              <tr class="LC_info_row">
  962:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  963:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  964:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  965:         } elsif ($action eq 'login') {
  966:             if ($numheaders == 4) {
  967:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  968:            </table>
  969:           </td>
  970:          </tr>
  971:          <tr>
  972:            <td>
  973:             <table class="LC_nested">
  974:              <tr class="LC_info_row">
  975:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  976:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  977:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  978:                 $rowtotal ++;
  979:             } else {
  980:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  981:             }
  982:             $output .= '
  983:            </table>
  984:           </td>
  985:          </tr>
  986:          <tr>
  987:            <td>
  988:             <table class="LC_nested">
  989:              <tr class="LC_info_row">';
  990:             if ($numheaders == 4) {
  991:                 $output .= '
  992:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  993:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  994:              </tr>';
  995:             } else {
  996:                 $output .= '
  997:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  998:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  999:              </tr>';
 1000:             }
 1001:             $rowtotal ++;
 1002:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
 1003:         } elsif ($action eq 'requestcourses') {
 1004:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1005:             $rowtotal ++;
 1006:             $output .= &print_studentcode($settings,\$rowtotal).'
 1007:            </table>
 1008:           </td>
 1009:          </tr>
 1010:          <tr>
 1011:            <td>
 1012:             <table class="LC_nested">
 1013:              <tr class="LC_info_row">
 1014:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1015:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1016:                        &textbookcourses_javascript($settings).
 1017:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1018:             </table>
 1019:            </td>
 1020:           </tr>
 1021:          <tr>
 1022:            <td>
 1023:             <table class="LC_nested">
 1024:              <tr class="LC_info_row">
 1025:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1026:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1027:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1028:             </table>
 1029:            </td>
 1030:           </tr>
 1031:           <tr>
 1032:            <td>
 1033:             <table class="LC_nested">
 1034:              <tr class="LC_info_row">
 1035:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1036:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1037:              </tr>'.
 1038:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1039:         } elsif ($action eq 'requestauthor') {
 1040:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1041:             $rowtotal ++;
 1042:         } elsif ($action eq 'rolecolors') {
 1043:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1044:            </table>
 1045:           </td>
 1046:          </tr>
 1047:          <tr>
 1048:            <td>
 1049:             <table class="LC_nested">
 1050:              <tr class="LC_info_row">
 1051:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1052:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1053:               <td class="LC_right_item" style="vertical-align: top">'.
 1054:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1055:              </tr>'.
 1056:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1057:            </table>
 1058:           </td>
 1059:          </tr>
 1060:          <tr>
 1061:            <td>
 1062:             <table class="LC_nested">
 1063:              <tr class="LC_info_row">
 1064:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1065:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1066:              </tr>'.
 1067:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1068:             $rowtotal += 2;
 1069:         }
 1070:     } else {
 1071:         $output .= '
 1072:           <tr>
 1073:            <td>
 1074:             <table class="LC_nested">
 1075:              <tr class="LC_info_row">';
 1076:         if ($action eq 'login') {
 1077:             $output .= '  
 1078:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1079:         } elsif ($action eq 'serverstatuses') {
 1080:             $output .= '
 1081:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1082:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1083: 
 1084:         } else {
 1085:             $output .= '
 1086:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1087:         }
 1088:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1089:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1090:                        &mt($item->{'header'}->[0]->{'col2'});
 1091:             if ($action eq 'serverstatuses') {
 1092:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1093:             } 
 1094:         } else {
 1095:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1096:                        &mt($item->{'header'}->[0]->{'col2'});
 1097:         }
 1098:         $output .= '</td>';
 1099:         if ($item->{'header'}->[0]->{'col3'}) {
 1100:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1101:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1102:                             &mt($item->{'header'}->[0]->{'col3'});
 1103:             } else {
 1104:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1105:                            &mt($item->{'header'}->[0]->{'col3'});
 1106:             }
 1107:             if ($action eq 'serverstatuses') {
 1108:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1109:             }
 1110:             $output .= '</td>';
 1111:         }
 1112:         if ($item->{'header'}->[0]->{'col4'}) {
 1113:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1114:                        &mt($item->{'header'}->[0]->{'col4'});
 1115:         }
 1116:         $output .= '</tr>';
 1117:         $rowtotal ++;
 1118:         if ($action eq 'quotas') {
 1119:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1120:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1121:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1122:                  ($action eq 'ltitools') || ($action eq 'lti')) {
 1123:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1124:         }
 1125:     }
 1126:     $output .= '
 1127:    </table>
 1128:   </td>
 1129:  </tr>
 1130: </table><br />';
 1131:     return ($output,$rowtotal);
 1132: }
 1133: 
 1134: sub print_login {
 1135:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1136:     my ($css_class,$datatable);
 1137:     my %choices = &login_choices();
 1138: 
 1139:     if ($caller eq 'service') {
 1140:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1141:         my $choice = $choices{'disallowlogin'};
 1142:         $css_class = ' class="LC_odd_row"';
 1143:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1144:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1145:                       '<th>'.$choices{'server'}.'</th>'.
 1146:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1147:                       '<th>'.$choices{'custompath'}.'</th>'.
 1148:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1149:         my %disallowed;
 1150:         if (ref($settings) eq 'HASH') {
 1151:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1152:                %disallowed = %{$settings->{'loginvia'}};
 1153:             }
 1154:         }
 1155:         foreach my $lonhost (sort(keys(%servers))) {
 1156:             my $direct = 'selected="selected"';
 1157:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1158:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1159:                     $direct = '';
 1160:                 }
 1161:             }
 1162:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1163:                           '<td><select name="'.$lonhost.'_server">'.
 1164:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1165:                           '</option>';
 1166:             foreach my $hostid (sort(keys(%servers))) {
 1167:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1168:                 my $selected = '';
 1169:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1170:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1171:                         $selected = 'selected="selected"';
 1172:                     }
 1173:                 }
 1174:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1175:                               $servers{$hostid}.'</option>';
 1176:             }
 1177:             $datatable .= '</select></td>'.
 1178:                           '<td><select name="'.$lonhost.'_serverpath">';
 1179:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1180:                 my $pathname = $path;
 1181:                 if ($path eq 'custom') {
 1182:                     $pathname = &mt('Custom Path').' ->';
 1183:                 }
 1184:                 my $selected = '';
 1185:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1186:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1187:                         $selected = 'selected="selected"';
 1188:                     }
 1189:                 } elsif ($path eq '') {
 1190:                     $selected = 'selected="selected"';
 1191:                 }
 1192:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1193:             }
 1194:             $datatable .= '</select></td>';
 1195:             my ($custom,$exempt);
 1196:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1197:                 $custom = $disallowed{$lonhost}{'custompath'};
 1198:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1199:             }
 1200:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1201:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1202:                           '</tr>';
 1203:         }
 1204:         $datatable .= '</table></td></tr>';
 1205:         return $datatable;
 1206:     } elsif ($caller eq 'page') {
 1207:         my %defaultchecked = ( 
 1208:                                'coursecatalog' => 'on',
 1209:                                'helpdesk'      => 'on',
 1210:                                'adminmail'     => 'off',
 1211:                                'newuser'       => 'off',
 1212:                              );
 1213:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1214:         my (%checkedon,%checkedoff);
 1215:         foreach my $item (@toggles) {
 1216:             if ($defaultchecked{$item} eq 'on') { 
 1217:                 $checkedon{$item} = ' checked="checked" ';
 1218:                 $checkedoff{$item} = ' ';
 1219:             } elsif ($defaultchecked{$item} eq 'off') {
 1220:                 $checkedoff{$item} = ' checked="checked" ';
 1221:                 $checkedon{$item} = ' ';
 1222:             }
 1223:         }
 1224:         my @images = ('img','logo','domlogo','login');
 1225:         my @logintext = ('textcol','bgcol');
 1226:         my @bgs = ('pgbg','mainbg','sidebg');
 1227:         my @links = ('link','alink','vlink');
 1228:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1229:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1230:         my (%is_custom,%designs);
 1231:         my %defaults = (
 1232:                        font => $defaultdesign{'login.font'},
 1233:                        );
 1234:         foreach my $item (@images) {
 1235:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1236:             $defaults{'showlogo'}{$item} = 1;
 1237:         }
 1238:         foreach my $item (@bgs) {
 1239:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1240:         }
 1241:         foreach my $item (@logintext) {
 1242:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1243:         }
 1244:         foreach my $item (@links) {
 1245:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1246:         }
 1247:         if (ref($settings) eq 'HASH') {
 1248:             foreach my $item (@toggles) {
 1249:                 if ($settings->{$item} eq '1') {
 1250:                     $checkedon{$item} =  ' checked="checked" ';
 1251:                     $checkedoff{$item} = ' ';
 1252:                 } elsif ($settings->{$item} eq '0') {
 1253:                     $checkedoff{$item} =  ' checked="checked" ';
 1254:                     $checkedon{$item} = ' ';
 1255:                 }
 1256:             }
 1257:             foreach my $item (@images) {
 1258:                 if (defined($settings->{$item})) {
 1259:                     $designs{$item} = $settings->{$item};
 1260:                     $is_custom{$item} = 1;
 1261:                 }
 1262:                 if (defined($settings->{'showlogo'}{$item})) {
 1263:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1264:                 }
 1265:             }
 1266:             foreach my $item (@logintext) {
 1267:                 if ($settings->{$item} ne '') {
 1268:                     $designs{'logintext'}{$item} = $settings->{$item};
 1269:                     $is_custom{$item} = 1;
 1270:                 }
 1271:             }
 1272:             if ($settings->{'font'} ne '') {
 1273:                 $designs{'font'} = $settings->{'font'};
 1274:                 $is_custom{'font'} = 1;
 1275:             }
 1276:             foreach my $item (@bgs) {
 1277:                 if ($settings->{$item} ne '') {
 1278:                     $designs{'bgs'}{$item} = $settings->{$item};
 1279:                     $is_custom{$item} = 1;
 1280:                 }
 1281:             }
 1282:             foreach my $item (@links) {
 1283:                 if ($settings->{$item} ne '') {
 1284:                     $designs{'links'}{$item} = $settings->{$item};
 1285:                     $is_custom{$item} = 1;
 1286:                 }
 1287:             }
 1288:         } else {
 1289:             if ($designhash{$dom.'.login.font'} ne '') {
 1290:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1291:                 $is_custom{'font'} = 1;
 1292:             }
 1293:             foreach my $item (@images) {
 1294:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1295:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1296:                     $is_custom{$item} = 1;
 1297:                 }
 1298:             }
 1299:             foreach my $item (@bgs) {
 1300:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1301:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1302:                     $is_custom{$item} = 1;
 1303:                 }
 1304:             }
 1305:             foreach my $item (@links) {
 1306:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1307:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1308:                     $is_custom{$item} = 1;
 1309:                 }
 1310:             }
 1311:         }
 1312:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1313:                                                       logo => 'Institution Logo',
 1314:                                                       domlogo => 'Domain Logo',
 1315:                                                       login => 'Login box');
 1316:         my $itemcount = 1;
 1317:         foreach my $item (@toggles) {
 1318:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1319:             $datatable .=  
 1320:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1321:                 '</td><td>'.
 1322:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1323:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1324:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1325:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1326:                 '</tr>';
 1327:             $itemcount ++;
 1328:         }
 1329:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1330:         $datatable .= '</tr></table></td></tr>';
 1331:     } elsif ($caller eq 'help') {
 1332:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1333:         my $switchserver = &check_switchserver($dom,$confname);
 1334:         my $itemcount = 1;
 1335:         $defaulturl = '/adm/loginproblems.html';
 1336:         $defaulttype = 'default';
 1337:         %lt = &Apache::lonlocal::texthash (
 1338:                      del     => 'Delete?',
 1339:                      rep     => 'Replace:',
 1340:                      upl     => 'Upload:',
 1341:                      default => 'Default',
 1342:                      custom  => 'Custom',
 1343:                                              );
 1344:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1345:         my @currlangs;
 1346:         if (ref($settings) eq 'HASH') {
 1347:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1348:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1349:                     next if ($settings->{'helpurl'}{$key} eq '');
 1350:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1351:                     $type{$key} = 'custom';
 1352:                     unless ($key eq 'nolang') {
 1353:                         push(@currlangs,$key);
 1354:                     }
 1355:                 }
 1356:             } elsif ($settings->{'helpurl'} ne '') {
 1357:                 $type{'nolang'} = 'custom';
 1358:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1359:             }
 1360:         }
 1361:         foreach my $lang ('nolang',sort(@currlangs)) {
 1362:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1363:             $datatable .= '<tr'.$css_class.'>';
 1364:             if ($url{$lang} eq '') {
 1365:                 $url{$lang} = $defaulturl;
 1366:             }
 1367:             if ($type{$lang} eq '') {
 1368:                 $type{$lang} = $defaulttype;
 1369:             }
 1370:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1371:             if ($lang eq 'nolang') {
 1372:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1373:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1374:             } else {
 1375:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1376:                                   $langchoices{$lang},
 1377:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1378:             }
 1379:             $datatable .= '</span></td>'."\n".
 1380:                           '<td class="LC_left_item">';
 1381:             if ($type{$lang} eq 'custom') {
 1382:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1383:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1384:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1385:             } else {
 1386:                 $datatable .= $lt{'upl'};
 1387:             }
 1388:             $datatable .='<br />';
 1389:             if ($switchserver) {
 1390:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1391:             } else {
 1392:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1393:             }
 1394:             $datatable .= '</td></tr>';
 1395:             $itemcount ++;
 1396:         }
 1397:         my @addlangs;
 1398:         foreach my $lang (sort(keys(%langchoices))) {
 1399:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1400:             push(@addlangs,$lang);
 1401:         }
 1402:         if (@addlangs > 0) {
 1403:             my %toadd;
 1404:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1405:             $toadd{''} = &mt('Select');
 1406:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1407:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1408:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1409:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1410:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1411:             if ($switchserver) {
 1412:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1413:             } else {
 1414:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1415:             }
 1416:             $datatable .= '</td></tr>';
 1417:             $itemcount ++;
 1418:         }
 1419:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1420:     } elsif ($caller eq 'headtag') {
 1421:         my %domservers = &Apache::lonnet::get_servers($dom);
 1422:         my $choice = $choices{'headtag'};
 1423:         $css_class = ' class="LC_odd_row"';
 1424:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1425:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1426:                       '<th>'.$choices{'current'}.'</th>'.
 1427:                       '<th>'.$choices{'action'}.'</th>'.
 1428:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1429:         my (%currurls,%currexempt);
 1430:         if (ref($settings) eq 'HASH') {
 1431:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1432:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1433:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1434:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1435:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1436:                     }
 1437:                 }
 1438:             }
 1439:         }
 1440:         my %lt = &Apache::lonlocal::texthash(
 1441:                                                del  => 'Delete?',
 1442:                                                rep  => 'Replace:',
 1443:                                                upl  => 'Upload:',
 1444:                                                curr => 'View contents',
 1445:                                                none => 'None',
 1446:         );
 1447:         my $switchserver = &check_switchserver($dom,$confname);
 1448:         foreach my $lonhost (sort(keys(%domservers))) {
 1449:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1450:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1451:             if ($currurls{$lonhost}) {
 1452:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1453:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1454:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1455:                               '">'.$lt{'curr'}.'</a></td>'.
 1456:                               '<td><span class="LC_nobreak"><label>'.
 1457:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1458:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1459:             } else {
 1460:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1461:             }
 1462:             $datatable .='<br />';
 1463:             if ($switchserver) {
 1464:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1465:             } else {
 1466:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1467:             }
 1468:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1469:         }
 1470:         $datatable .= '</table></td></tr>';
 1471:     }
 1472:     return $datatable;
 1473: }
 1474: 
 1475: sub login_choices {
 1476:     my %choices =
 1477:         &Apache::lonlocal::texthash (
 1478:             coursecatalog => 'Display Course/Community Catalog link?',
 1479:             adminmail     => "Display Administrator's E-mail Address?",
 1480:             helpdesk      => 'Display "Contact Helpdesk" link',
 1481:             disallowlogin => "Login page requests redirected",
 1482:             hostid        => "Server",
 1483:             server        => "Redirect to:",
 1484:             serverpath    => "Path",
 1485:             custompath    => "Custom", 
 1486:             exempt        => "Exempt IP(s)",
 1487:             directlogin   => "No redirect",
 1488:             newuser       => "Link to create a user account",
 1489:             img           => "Header",
 1490:             logo          => "Main Logo",
 1491:             domlogo       => "Domain Logo",
 1492:             login         => "Log-in Header", 
 1493:             textcol       => "Text color",
 1494:             bgcol         => "Box color",
 1495:             bgs           => "Background colors",
 1496:             links         => "Link colors",
 1497:             font          => "Font color",
 1498:             pgbg          => "Header",
 1499:             mainbg        => "Page",
 1500:             sidebg        => "Login box",
 1501:             link          => "Link",
 1502:             alink         => "Active link",
 1503:             vlink         => "Visited link",
 1504:             headtag       => "Custom markup",
 1505:             action        => "Action",
 1506:             current       => "Current",
 1507:         );
 1508:     return %choices;
 1509: }
 1510: 
 1511: sub print_rolecolors {
 1512:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1513:     my %choices = &color_font_choices();
 1514:     my @bgs = ('pgbg','tabbg','sidebg');
 1515:     my @links = ('link','alink','vlink');
 1516:     my @images = ('img');
 1517:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1518:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1519:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1520:     my (%is_custom,%designs);
 1521:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1522:     if (ref($settings) eq 'HASH') {
 1523:         if (ref($settings->{$role}) eq 'HASH') {
 1524:             if ($settings->{$role}->{'img'} ne '') {
 1525:                 $designs{'img'} = $settings->{$role}->{'img'};
 1526:                 $is_custom{'img'} = 1;
 1527:             }
 1528:             if ($settings->{$role}->{'font'} ne '') {
 1529:                 $designs{'font'} = $settings->{$role}->{'font'};
 1530:                 $is_custom{'font'} = 1;
 1531:             }
 1532:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1533:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1534:                 $is_custom{'fontmenu'} = 1;
 1535:             }
 1536:             foreach my $item (@bgs) {
 1537:                 if ($settings->{$role}->{$item} ne '') {
 1538:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1539:                     $is_custom{$item} = 1;
 1540:                 }
 1541:             }
 1542:             foreach my $item (@links) {
 1543:                 if ($settings->{$role}->{$item} ne '') {
 1544:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1545:                     $is_custom{$item} = 1;
 1546:                 }
 1547:             }
 1548:         }
 1549:     } else {
 1550:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1551:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1552:             $is_custom{'img'} = 1;
 1553:         }
 1554:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1555:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1556:             $is_custom{'fontmenu'} = 1; 
 1557:         }
 1558:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1559:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1560:             $is_custom{'font'} = 1;
 1561:         }
 1562:         foreach my $item (@bgs) {
 1563:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1564:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1565:                 $is_custom{$item} = 1;
 1566:             
 1567:             }
 1568:         }
 1569:         foreach my $item (@links) {
 1570:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1571:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1572:                 $is_custom{$item} = 1;
 1573:             }
 1574:         }
 1575:     }
 1576:     my $itemcount = 1;
 1577:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1578:     $datatable .= '</tr></table></td></tr>';
 1579:     return $datatable;
 1580: }
 1581: 
 1582: sub role_defaults {
 1583:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1584:     my %defaults;
 1585:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1586:         return %defaults;
 1587:     }
 1588:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1589:     if ($role eq 'login') {
 1590:         %defaults = (
 1591:                        font => $defaultdesign{$role.'.font'},
 1592:                     );
 1593:         if (ref($logintext) eq 'ARRAY') {
 1594:             foreach my $item (@{$logintext}) {
 1595:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1596:             }
 1597:         }
 1598:         foreach my $item (@{$images}) {
 1599:             $defaults{'showlogo'}{$item} = 1;
 1600:         }
 1601:     } else {
 1602:         %defaults = (
 1603:                        img => $defaultdesign{$role.'.img'},
 1604:                        font => $defaultdesign{$role.'.font'},
 1605:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1606:                     );
 1607:     }
 1608:     foreach my $item (@{$bgs}) {
 1609:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1610:     }
 1611:     foreach my $item (@{$links}) {
 1612:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1613:     }
 1614:     foreach my $item (@{$images}) {
 1615:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1616:     }
 1617:     return %defaults;
 1618: }
 1619: 
 1620: sub display_color_options {
 1621:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1622:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1623:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1624:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1625:     my $datatable = '<tr'.$css_class.'>'.
 1626:         '<td>'.$choices->{'font'}.'</td>';
 1627:     if (!$is_custom->{'font'}) {
 1628:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1629:     } else {
 1630:         $datatable .= '<td>&nbsp;</td>';
 1631:     }
 1632:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1633: 
 1634:     $datatable .= '<td><span class="LC_nobreak">'.
 1635:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1636:                   ' value="'.$current_color.'" />&nbsp;'.
 1637:                   '&nbsp;</span></td></tr>';
 1638:     unless ($role eq 'login') { 
 1639:         $datatable .= '<tr'.$css_class.'>'.
 1640:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1641:         if (!$is_custom->{'fontmenu'}) {
 1642:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1643:         } else {
 1644:             $datatable .= '<td>&nbsp;</td>';
 1645:         }
 1646: 	$current_color = $designs->{'fontmenu'} ?
 1647: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1648:         $datatable .= '<td><span class="LC_nobreak">'.
 1649:                       '<input class="colorchooser" type="text" size="10" name="'
 1650: 		      .$role.'_fontmenu"'.
 1651:                       ' value="'.$current_color.'" />&nbsp;'.
 1652:                       '&nbsp;</span></td></tr>';
 1653:     }
 1654:     my $switchserver = &check_switchserver($dom,$confname);
 1655:     foreach my $img (@{$images}) {
 1656: 	$itemcount ++;
 1657:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1658:         $datatable .= '<tr'.$css_class.'>'.
 1659:                       '<td>'.$choices->{$img};
 1660:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1661:         if ($role eq 'login') {
 1662:             if ($img eq 'login') {
 1663:                 $login_hdr_pick =
 1664:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1665:                 $logincolors =
 1666:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1667:                                        $designs,$defaults);
 1668:             } elsif ($img ne 'domlogo') {
 1669:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1670:             }
 1671:         }
 1672:         $datatable .= '</td>';
 1673:         if ($designs->{$img} ne '') {
 1674:             $imgfile = $designs->{$img};
 1675: 	    $img_import = ($imgfile =~ m{^/adm/});
 1676:         } else {
 1677:             $imgfile = $defaults->{$img};
 1678:         }
 1679:         if ($imgfile) {
 1680:             my ($showfile,$fullsize);
 1681:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1682:                 my $urldir = $1;
 1683:                 my $filename = $2;
 1684:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1685:                 if (@info) {
 1686:                     my $thumbfile = 'tn-'.$filename;
 1687:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1688:                     if (@thumb) {
 1689:                         $showfile = $urldir.'/'.$thumbfile;
 1690:                     } else {
 1691:                         $showfile = $imgfile;
 1692:                     }
 1693:                 } else {
 1694:                     $showfile = '';
 1695:                 }
 1696:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1697:                 $showfile = $imgfile;
 1698:                 my $imgdir = $1;
 1699:                 my $filename = $2;
 1700:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1701:                     $showfile = "/$imgdir/tn-".$filename;
 1702:                 } else {
 1703:                     my $input = $londocroot.$imgfile;
 1704:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1705:                     if (!-e $output) {
 1706:                         my ($width,$height) = &thumb_dimensions();
 1707:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1708:                         if ($fullwidth ne '' && $fullheight ne '') {
 1709:                             if ($fullwidth > $width && $fullheight > $height) { 
 1710:                                 my $size = $width.'x'.$height;
 1711:                                 my @args = ('convert','-sample',$size,$input,$output);
 1712:                                 system({$args[0]} @args);
 1713:                                 $showfile = "/$imgdir/tn-".$filename;
 1714:                             }
 1715:                         }
 1716:                     }
 1717:                 }
 1718:             }
 1719:             if ($showfile) {
 1720:                 if ($showfile =~ m{^/(adm|res)/}) {
 1721:                     if ($showfile =~ m{^/res/}) {
 1722:                         my $local_showfile =
 1723:                             &Apache::lonnet::filelocation('',$showfile);
 1724:                         &Apache::lonnet::repcopy($local_showfile);
 1725:                     }
 1726:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1727:                 }
 1728:                 if ($imgfile) {
 1729:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1730:                         if ($imgfile =~ m{^/res/}) {
 1731:                             my $local_imgfile =
 1732:                                 &Apache::lonnet::filelocation('',$imgfile);
 1733:                             &Apache::lonnet::repcopy($local_imgfile);
 1734:                         }
 1735:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1736:                     } else {
 1737:                         $fullsize = $imgfile;
 1738:                     }
 1739:                 }
 1740:                 $datatable .= '<td>';
 1741:                 if ($img eq 'login') {
 1742:                     $datatable .= $login_hdr_pick;
 1743:                 } 
 1744:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1745:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1746:             } else {
 1747:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1748:                               &mt('Upload:').'<br />';
 1749:             }
 1750:         } else {
 1751:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1752:                           &mt('Upload:').'<br />';
 1753:         }
 1754:         if ($switchserver) {
 1755:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1756:         } else {
 1757:             if ($img ne 'login') { # suppress file selection for Log-in header
 1758:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1759:             }
 1760:         }
 1761:         $datatable .= '</td></tr>';
 1762:     }
 1763:     $itemcount ++;
 1764:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1765:     $datatable .= '<tr'.$css_class.'>'.
 1766:                   '<td>'.$choices->{'bgs'}.'</td>';
 1767:     my $bgs_def;
 1768:     foreach my $item (@{$bgs}) {
 1769:         if (!$is_custom->{$item}) {
 1770:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span class="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1771:         }
 1772:     }
 1773:     if ($bgs_def) {
 1774:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1775:     } else {
 1776:         $datatable .= '<td>&nbsp;</td>';
 1777:     }
 1778:     $datatable .= '<td class="LC_right_item">'.
 1779:                   '<table border="0"><tr>';
 1780: 
 1781:     foreach my $item (@{$bgs}) {
 1782:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1783: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1784:         if ($designs->{'bgs'}{$item}) {
 1785:             $datatable .= '&nbsp;';
 1786:         }
 1787:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1788:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1789:     }
 1790:     $datatable .= '</tr></table></td></tr>';
 1791:     $itemcount ++;
 1792:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1793:     $datatable .= '<tr'.$css_class.'>'.
 1794:                   '<td>'.$choices->{'links'}.'</td>';
 1795:     my $links_def;
 1796:     foreach my $item (@{$links}) {
 1797:         if (!$is_custom->{$item}) {
 1798:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1799:         }
 1800:     }
 1801:     if ($links_def) {
 1802:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1803:     } else {
 1804:         $datatable .= '<td>&nbsp;</td>';
 1805:     }
 1806:     $datatable .= '<td class="LC_right_item">'.
 1807:                   '<table border="0"><tr>';
 1808:     foreach my $item (@{$links}) {
 1809: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1810:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1811:         if ($designs->{'links'}{$item}) {
 1812:             $datatable.='&nbsp;';
 1813:         }
 1814:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1815:                       '" /></td>';
 1816:     }
 1817:     $$rowtotal += $itemcount;
 1818:     return $datatable;
 1819: }
 1820: 
 1821: sub logo_display_options {
 1822:     my ($img,$defaults,$designs) = @_;
 1823:     my $checkedon;
 1824:     if (ref($defaults) eq 'HASH') {
 1825:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1826:             if ($defaults->{'showlogo'}{$img}) {
 1827:                 $checkedon = 'checked="checked" ';     
 1828:             }
 1829:         } 
 1830:     }
 1831:     if (ref($designs) eq 'HASH') {
 1832:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1833:             if (defined($designs->{'showlogo'}{$img})) {
 1834:                 if ($designs->{'showlogo'}{$img} == 0) {
 1835:                     $checkedon = '';
 1836:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1837:                     $checkedon = 'checked="checked" ';
 1838:                 }
 1839:             }
 1840:         }
 1841:     }
 1842:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1843:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1844:            &mt('show').'</label>'."\n";
 1845: }
 1846: 
 1847: sub login_header_options  {
 1848:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1849:     my $output = '';
 1850:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1851:         $output .= &mt('Text default(s):').'<br />';
 1852:         if (!$is_custom->{'textcol'}) {
 1853:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1854:                        '&nbsp;&nbsp;&nbsp;';
 1855:         }
 1856:         if (!$is_custom->{'bgcol'}) {
 1857:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1858:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1859:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1860:         }
 1861:         $output .= '<br />';
 1862:     }
 1863:     $output .='<br />';
 1864:     return $output;
 1865: }
 1866: 
 1867: sub login_text_colors {
 1868:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1869:     my $color_menu = '<table border="0"><tr>';
 1870:     foreach my $item (@{$logintext}) {
 1871:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1872:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1873:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1874:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1875:     }
 1876:     $color_menu .= '</tr></table><br />';
 1877:     return $color_menu;
 1878: }
 1879: 
 1880: sub image_changes {
 1881:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1882:     my $output;
 1883:     if ($img eq 'login') {
 1884:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1885:     } elsif (!$is_custom) {
 1886:         if ($img ne 'domlogo') {
 1887:             $output = &mt('Default image:').'<br />';
 1888:         } else {
 1889:             $output = &mt('Default in use:').'<br />';
 1890:         }
 1891:     }
 1892:     if ($img ne 'login') {
 1893:         if ($img_import) {
 1894:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1895:         }
 1896:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1897:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1898:         if ($is_custom) {
 1899:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1900:                        '<input type="checkbox" name="'.
 1901:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1902:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1903:         } else {
 1904:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1905:         }
 1906:     }
 1907:     return $output;
 1908: }
 1909: 
 1910: sub print_quotas {
 1911:     my ($dom,$settings,$rowtotal,$action) = @_;
 1912:     my $context;
 1913:     if ($action eq 'quotas') {
 1914:         $context = 'tools';
 1915:     } else {
 1916:         $context = $action;
 1917:     }
 1918:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1919:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1920:     my $typecount = 0;
 1921:     my ($css_class,%titles);
 1922:     if ($context eq 'requestcourses') {
 1923:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 1924:         @options =('norequest','approval','validate','autolimit');
 1925:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1926:         %titles = &courserequest_titles();
 1927:     } elsif ($context eq 'requestauthor') {
 1928:         @usertools = ('author');
 1929:         @options = ('norequest','approval','automatic');
 1930:         %titles = &authorrequest_titles();
 1931:     } else {
 1932:         @usertools = ('aboutme','blog','webdav','portfolio');
 1933:         %titles = &tool_titles();
 1934:     }
 1935:     if (ref($types) eq 'ARRAY') {
 1936:         foreach my $type (@{$types}) {
 1937:             my ($currdefquota,$currauthorquota);
 1938:             unless (($context eq 'requestcourses') ||
 1939:                     ($context eq 'requestauthor')) {
 1940:                 if (ref($settings) eq 'HASH') {
 1941:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1942:                         $currdefquota = $settings->{defaultquota}->{$type};
 1943:                     } else {
 1944:                         $currdefquota = $settings->{$type};
 1945:                     }
 1946:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1947:                         $currauthorquota = $settings->{authorquota}->{$type};
 1948:                     }
 1949:                 }
 1950:             }
 1951:             if (defined($usertypes->{$type})) {
 1952:                 $typecount ++;
 1953:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1954:                 $datatable .= '<tr'.$css_class.'>'.
 1955:                               '<td>'.$usertypes->{$type}.'</td>'.
 1956:                               '<td class="LC_left_item">';
 1957:                 if ($context eq 'requestcourses') {
 1958:                     $datatable .= '<table><tr>';
 1959:                 }
 1960:                 my %cell;  
 1961:                 foreach my $item (@usertools) {
 1962:                     if ($context eq 'requestcourses') {
 1963:                         my ($curroption,$currlimit);
 1964:                         if (ref($settings) eq 'HASH') {
 1965:                             if (ref($settings->{$item}) eq 'HASH') {
 1966:                                 $curroption = $settings->{$item}->{$type};
 1967:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1968:                                     $currlimit = $1; 
 1969:                                 }
 1970:                             }
 1971:                         }
 1972:                         if (!$curroption) {
 1973:                             $curroption = 'norequest';
 1974:                         }
 1975:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1976:                         foreach my $option (@options) {
 1977:                             my $val = $option;
 1978:                             if ($option eq 'norequest') {
 1979:                                 $val = 0;  
 1980:                             }
 1981:                             if ($option eq 'validate') {
 1982:                                 my $canvalidate = 0;
 1983:                                 if (ref($validations{$item}) eq 'HASH') { 
 1984:                                     if ($validations{$item}{$type}) {
 1985:                                         $canvalidate = 1;
 1986:                                     }
 1987:                                 }
 1988:                                 next if (!$canvalidate);
 1989:                             }
 1990:                             my $checked = '';
 1991:                             if ($option eq $curroption) {
 1992:                                 $checked = ' checked="checked"';
 1993:                             } elsif ($option eq 'autolimit') {
 1994:                                 if ($curroption =~ /^autolimit/) {
 1995:                                     $checked = ' checked="checked"';
 1996:                                 }                       
 1997:                             } 
 1998:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1999:                                   '<input type="radio" name="crsreq_'.$item.
 2000:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2001:                                   $titles{$option}.'</label>';
 2002:                             if ($option eq 'autolimit') {
 2003:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2004:                                                 $item.'_limit_'.$type.'" size="1" '.
 2005:                                                 'value="'.$currlimit.'" />';
 2006:                             }
 2007:                             $cell{$item} .= '</span> ';
 2008:                             if ($option eq 'autolimit') {
 2009:                                 $cell{$item} .= $titles{'unlimited'};
 2010:                             }
 2011:                         }
 2012:                     } elsif ($context eq 'requestauthor') {
 2013:                         my $curroption;
 2014:                         if (ref($settings) eq 'HASH') {
 2015:                             $curroption = $settings->{$type};
 2016:                         }
 2017:                         if (!$curroption) {
 2018:                             $curroption = 'norequest';
 2019:                         }
 2020:                         foreach my $option (@options) {
 2021:                             my $val = $option;
 2022:                             if ($option eq 'norequest') {
 2023:                                 $val = 0;
 2024:                             }
 2025:                             my $checked = '';
 2026:                             if ($option eq $curroption) {
 2027:                                 $checked = ' checked="checked"';
 2028:                             }
 2029:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2030:                                   '<input type="radio" name="authorreq_'.$type.
 2031:                                   '" value="'.$val.'"'.$checked.' />'.
 2032:                                   $titles{$option}.'</label></span>&nbsp; ';
 2033:                         }
 2034:                     } else {
 2035:                         my $checked = 'checked="checked" ';
 2036:                         if (ref($settings) eq 'HASH') {
 2037:                             if (ref($settings->{$item}) eq 'HASH') {
 2038:                                 if ($settings->{$item}->{$type} == 0) {
 2039:                                     $checked = '';
 2040:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2041:                                     $checked =  'checked="checked" ';
 2042:                                 }
 2043:                             }
 2044:                         }
 2045:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2046:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2047:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2048:                                       '</label></span>&nbsp; ';
 2049:                     }
 2050:                 }
 2051:                 if ($context eq 'requestcourses') {
 2052:                     $datatable .= '</tr><tr>';
 2053:                     foreach my $item (@usertools) {
 2054:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2055:                     }
 2056:                     $datatable .= '</tr></table>';
 2057:                 }
 2058:                 $datatable .= '</td>';
 2059:                 unless (($context eq 'requestcourses') ||
 2060:                         ($context eq 'requestauthor')) {
 2061:                     $datatable .= 
 2062:                               '<td class="LC_right_item">'.
 2063:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2064:                               '<input type="text" name="quota_'.$type.
 2065:                               '" value="'.$currdefquota.
 2066:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2067:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2068:                               '<input type="text" name="authorquota_'.$type.
 2069:                               '" value="'.$currauthorquota.
 2070:                               '" size="5" /></span></td>';
 2071:                 }
 2072:                 $datatable .= '</tr>';
 2073:             }
 2074:         }
 2075:     }
 2076:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2077:         $defaultquota = '20';
 2078:         $authorquota = '500';
 2079:         if (ref($settings) eq 'HASH') {
 2080:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2081:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2082:             } elsif (defined($settings->{'default'})) {
 2083:                 $defaultquota = $settings->{'default'};
 2084:             }
 2085:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2086:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2087:             }
 2088:         }
 2089:     }
 2090:     $typecount ++;
 2091:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2092:     $datatable .= '<tr'.$css_class.'>'.
 2093:                   '<td>'.$othertitle.'</td>'.
 2094:                   '<td class="LC_left_item">';
 2095:     if ($context eq 'requestcourses') {
 2096:         $datatable .= '<table><tr>';
 2097:     }
 2098:     my %defcell;
 2099:     foreach my $item (@usertools) {
 2100:         if ($context eq 'requestcourses') {
 2101:             my ($curroption,$currlimit);
 2102:             if (ref($settings) eq 'HASH') {
 2103:                 if (ref($settings->{$item}) eq 'HASH') {
 2104:                     $curroption = $settings->{$item}->{'default'};
 2105:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2106:                         $currlimit = $1;
 2107:                     }
 2108:                 }
 2109:             }
 2110:             if (!$curroption) {
 2111:                 $curroption = 'norequest';
 2112:             }
 2113:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2114:             foreach my $option (@options) {
 2115:                 my $val = $option;
 2116:                 if ($option eq 'norequest') {
 2117:                     $val = 0;
 2118:                 }
 2119:                 if ($option eq 'validate') {
 2120:                     my $canvalidate = 0;
 2121:                     if (ref($validations{$item}) eq 'HASH') {
 2122:                         if ($validations{$item}{'default'}) {
 2123:                             $canvalidate = 1;
 2124:                         }
 2125:                     }
 2126:                     next if (!$canvalidate);
 2127:                 }
 2128:                 my $checked = '';
 2129:                 if ($option eq $curroption) {
 2130:                     $checked = ' checked="checked"';
 2131:                 } elsif ($option eq 'autolimit') {
 2132:                     if ($curroption =~ /^autolimit/) {
 2133:                         $checked = ' checked="checked"';
 2134:                     }
 2135:                 }
 2136:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2137:                                   '<input type="radio" name="crsreq_'.$item.
 2138:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2139:                                   $titles{$option}.'</label>';
 2140:                 if ($option eq 'autolimit') {
 2141:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2142:                                        $item.'_limit_default" size="1" '.
 2143:                                        'value="'.$currlimit.'" />';
 2144:                 }
 2145:                 $defcell{$item} .= '</span> ';
 2146:                 if ($option eq 'autolimit') {
 2147:                     $defcell{$item} .= $titles{'unlimited'};
 2148:                 }
 2149:             }
 2150:         } elsif ($context eq 'requestauthor') {
 2151:             my $curroption;
 2152:             if (ref($settings) eq 'HASH') {
 2153:                 $curroption = $settings->{'default'};
 2154:             }
 2155:             if (!$curroption) {
 2156:                 $curroption = 'norequest';
 2157:             }
 2158:             foreach my $option (@options) {
 2159:                 my $val = $option;
 2160:                 if ($option eq 'norequest') {
 2161:                     $val = 0;
 2162:                 }
 2163:                 my $checked = '';
 2164:                 if ($option eq $curroption) {
 2165:                     $checked = ' checked="checked"';
 2166:                 }
 2167:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2168:                               '<input type="radio" name="authorreq_default"'.
 2169:                               ' value="'.$val.'"'.$checked.' />'.
 2170:                               $titles{$option}.'</label></span>&nbsp; ';
 2171:             }
 2172:         } else {
 2173:             my $checked = 'checked="checked" ';
 2174:             if (ref($settings) eq 'HASH') {
 2175:                 if (ref($settings->{$item}) eq 'HASH') {
 2176:                     if ($settings->{$item}->{'default'} == 0) {
 2177:                         $checked = '';
 2178:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2179:                         $checked = 'checked="checked" ';
 2180:                     }
 2181:                 }
 2182:             }
 2183:             $datatable .= '<span class="LC_nobreak"><label>'.
 2184:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2185:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2186:                           '</label></span>&nbsp; ';
 2187:         }
 2188:     }
 2189:     if ($context eq 'requestcourses') {
 2190:         $datatable .= '</tr><tr>';
 2191:         foreach my $item (@usertools) {
 2192:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2193:         }
 2194:         $datatable .= '</tr></table>';
 2195:     }
 2196:     $datatable .= '</td>';
 2197:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2198:         $datatable .= '<td class="LC_right_item">'.
 2199:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2200:                       '<input type="text" name="defaultquota" value="'.
 2201:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2202:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2203:                       '<input type="text" name="authorquota" value="'.
 2204:                       $authorquota.'" size="5" /></span></td>';
 2205:     }
 2206:     $datatable .= '</tr>';
 2207:     $typecount ++;
 2208:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2209:     $datatable .= '<tr'.$css_class.'>'.
 2210:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2211:     if ($context eq 'requestcourses') {
 2212:         $datatable .= &mt('(overrides affiliation, if set)').
 2213:                       '</td>'.
 2214:                       '<td class="LC_left_item">'.
 2215:                       '<table><tr>';
 2216:     } else {
 2217:         $datatable .= &mt('(overrides affiliation, if checked)').
 2218:                       '</td>'.
 2219:                       '<td class="LC_left_item" colspan="2">'.
 2220:                       '<br />';
 2221:     }
 2222:     my %advcell;
 2223:     foreach my $item (@usertools) {
 2224:         if ($context eq 'requestcourses') {
 2225:             my ($curroption,$currlimit);
 2226:             if (ref($settings) eq 'HASH') {
 2227:                 if (ref($settings->{$item}) eq 'HASH') {
 2228:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2229:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2230:                         $currlimit = $1;
 2231:                     }
 2232:                 }
 2233:             }
 2234:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2235:             my $checked = '';
 2236:             if ($curroption eq '') {
 2237:                 $checked = ' checked="checked"';
 2238:             }
 2239:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2240:                                '<input type="radio" name="crsreq_'.$item.
 2241:                                '__LC_adv" value=""'.$checked.' />'.
 2242:                                &mt('No override set').'</label></span>&nbsp; ';
 2243:             foreach my $option (@options) {
 2244:                 my $val = $option;
 2245:                 if ($option eq 'norequest') {
 2246:                     $val = 0;
 2247:                 }
 2248:                 if ($option eq 'validate') {
 2249:                     my $canvalidate = 0;
 2250:                     if (ref($validations{$item}) eq 'HASH') {
 2251:                         if ($validations{$item}{'_LC_adv'}) {
 2252:                             $canvalidate = 1;
 2253:                         }
 2254:                     }
 2255:                     next if (!$canvalidate);
 2256:                 }
 2257:                 my $checked = '';
 2258:                 if ($val eq $curroption) {
 2259:                     $checked = ' checked="checked"';
 2260:                 } elsif ($option eq 'autolimit') {
 2261:                     if ($curroption =~ /^autolimit/) {
 2262:                         $checked = ' checked="checked"';
 2263:                     }
 2264:                 }
 2265:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2266:                                   '<input type="radio" name="crsreq_'.$item.
 2267:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2268:                                   $titles{$option}.'</label>';
 2269:                 if ($option eq 'autolimit') {
 2270:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2271:                                        $item.'_limit__LC_adv" size="1" '.
 2272:                                        'value="'.$currlimit.'" />';
 2273:                 }
 2274:                 $advcell{$item} .= '</span> ';
 2275:                 if ($option eq 'autolimit') {
 2276:                     $advcell{$item} .= $titles{'unlimited'};
 2277:                 }
 2278:             }
 2279:         } elsif ($context eq 'requestauthor') {
 2280:             my $curroption;
 2281:             if (ref($settings) eq 'HASH') {
 2282:                 $curroption = $settings->{'_LC_adv'};
 2283:             }
 2284:             my $checked = '';
 2285:             if ($curroption eq '') {
 2286:                 $checked = ' checked="checked"';
 2287:             }
 2288:             $datatable .= '<span class="LC_nobreak"><label>'.
 2289:                           '<input type="radio" name="authorreq__LC_adv"'.
 2290:                           ' value=""'.$checked.' />'.
 2291:                           &mt('No override set').'</label></span>&nbsp; ';
 2292:             foreach my $option (@options) {
 2293:                 my $val = $option;
 2294:                 if ($option eq 'norequest') {
 2295:                     $val = 0;
 2296:                 }
 2297:                 my $checked = '';
 2298:                 if ($val eq $curroption) {
 2299:                     $checked = ' checked="checked"';
 2300:                 }
 2301:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2302:                               '<input type="radio" name="authorreq__LC_adv"'.
 2303:                               ' value="'.$val.'"'.$checked.' />'.
 2304:                               $titles{$option}.'</label></span>&nbsp; ';
 2305:             }
 2306:         } else {
 2307:             my $checked = 'checked="checked" ';
 2308:             if (ref($settings) eq 'HASH') {
 2309:                 if (ref($settings->{$item}) eq 'HASH') {
 2310:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2311:                         $checked = '';
 2312:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2313:                         $checked = 'checked="checked" ';
 2314:                     }
 2315:                 }
 2316:             }
 2317:             $datatable .= '<span class="LC_nobreak"><label>'.
 2318:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2319:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2320:                           '</label></span>&nbsp; ';
 2321:         }
 2322:     }
 2323:     if ($context eq 'requestcourses') {
 2324:         $datatable .= '</tr><tr>';
 2325:         foreach my $item (@usertools) {
 2326:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2327:         }
 2328:         $datatable .= '</tr></table>';
 2329:     }
 2330:     $datatable .= '</td></tr>';
 2331:     $$rowtotal += $typecount;
 2332:     return $datatable;
 2333: }
 2334: 
 2335: sub print_requestmail {
 2336:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2337:     my ($now,$datatable,%currapp);
 2338:     $now = time;
 2339:     if (ref($settings) eq 'HASH') {
 2340:         if (ref($settings->{'notify'}) eq 'HASH') {
 2341:             if ($settings->{'notify'}{'approval'} ne '') {
 2342:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2343:             }
 2344:         }
 2345:     }
 2346:     my $numinrow = 2;
 2347:     my $css_class;
 2348:     if ($$rowtotal%2) {
 2349:         $css_class = 'LC_odd_row';
 2350:     }
 2351:     if ($customcss) {
 2352:         $css_class .= " $customcss";
 2353:     }
 2354:     $css_class =~ s/^\s+//;
 2355:     if ($css_class) {
 2356:         $css_class = ' class="'.$css_class.'"';
 2357:     }
 2358:     if ($rowstyle) {
 2359:         $css_class .= ' style="'.$rowstyle.'"';
 2360:     }
 2361:     my $text;
 2362:     if ($action eq 'requestcourses') {
 2363:         $text = &mt('Receive notification of course requests requiring approval');
 2364:     } elsif ($action eq 'requestauthor') {
 2365:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2366:     } else {
 2367:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2368:     }
 2369:     $datatable = '<tr'.$css_class.'>'.
 2370:                  ' <td>'.$text.'</td>'.
 2371:                  ' <td class="LC_left_item">';
 2372:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2373:                                                  $action.'notifyapproval',%currapp);
 2374:     if ($numdc > 0) {
 2375:         $datatable .= $table;
 2376:     } else {
 2377:         $datatable .= &mt('There are no active Domain Coordinators');
 2378:     }
 2379:     $datatable .='</td></tr>';
 2380:     return $datatable;
 2381: }
 2382: 
 2383: sub print_studentcode {
 2384:     my ($settings,$rowtotal) = @_;
 2385:     my $rownum = 0; 
 2386:     my ($output,%current);
 2387:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2388:     if (ref($settings) eq 'HASH') {
 2389:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2390:             foreach my $type (@crstypes) {
 2391:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2392:             }
 2393:         }
 2394:     }
 2395:     $output .= '<tr>'.
 2396:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2397:                '<td class="LC_left_item">';
 2398:     foreach my $type (@crstypes) {
 2399:         my $check = ' ';
 2400:         if ($current{$type}) {
 2401:             $check = ' checked="checked" ';
 2402:         }
 2403:         $output .= '<span class="LC_nobreak"><label>'.
 2404:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2405:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2406:     }
 2407:     $output .= '</td></tr>';
 2408:     $$rowtotal ++;
 2409:     return $output;
 2410: }
 2411: 
 2412: sub print_textbookcourses {
 2413:     my ($dom,$type,$settings,$rowtotal) = @_;
 2414:     my $rownum = 0;
 2415:     my $css_class;
 2416:     my $itemcount = 1;
 2417:     my $maxnum = 0;
 2418:     my $bookshash;
 2419:     if (ref($settings) eq 'HASH') {
 2420:         $bookshash = $settings->{$type};
 2421:     }
 2422:     my %ordered;
 2423:     if (ref($bookshash) eq 'HASH') {
 2424:         foreach my $item (keys(%{$bookshash})) {
 2425:             if (ref($bookshash->{$item}) eq 'HASH') {
 2426:                 my $num = $bookshash->{$item}{'order'};
 2427:                 $ordered{$num} = $item;
 2428:             }
 2429:         }
 2430:     }
 2431:     my $confname = $dom.'-domainconfig';
 2432:     my $switchserver = &check_switchserver($dom,$confname);
 2433:     my $maxnum = scalar(keys(%ordered));
 2434:     my $datatable;
 2435:     if (keys(%ordered)) {
 2436:         my @items = sort { $a <=> $b } keys(%ordered);
 2437:         for (my $i=0; $i<@items; $i++) {
 2438:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2439:             my $key = $ordered{$items[$i]};
 2440:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2441:             my $coursetitle = $coursehash{'description'};
 2442:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2443:             if (ref($bookshash->{$key}) eq 'HASH') {
 2444:                 $subject = $bookshash->{$key}->{'subject'};
 2445:                 $title = $bookshash->{$key}->{'title'};
 2446:                 if ($type eq 'textbooks') {
 2447:                     $publisher = $bookshash->{$key}->{'publisher'};
 2448:                     $author = $bookshash->{$key}->{'author'};
 2449:                     $image = $bookshash->{$key}->{'image'};
 2450:                     if ($image ne '') {
 2451:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2452:                         my $imagethumb = "$path/tn-".$imagefile;
 2453:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2454:                     }
 2455:                 }
 2456:             }
 2457:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2458:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2459:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2460:             for (my $k=0; $k<=$maxnum; $k++) {
 2461:                 my $vpos = $k+1;
 2462:                 my $selstr;
 2463:                 if ($k == $i) {
 2464:                     $selstr = ' selected="selected" ';
 2465:                 }
 2466:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2467:             }
 2468:             $datatable .= '</select>'.('&nbsp;'x2).
 2469:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2470:                 &mt('Delete?').'</label></span></td>'.
 2471:                 '<td colspan="2">'.
 2472:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2473:                 ('&nbsp;'x2).
 2474:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2475:             if ($type eq 'textbooks') {
 2476:                 $datatable .= ('&nbsp;'x2).
 2477:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2478:                               ('&nbsp;'x2).
 2479:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2480:                               ('&nbsp;'x2).
 2481:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2482:                 if ($image) {
 2483:                     $datatable .= $imgsrc.
 2484:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2485:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2486:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2487:                 }
 2488:                 if ($switchserver) {
 2489:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2490:                 } else {
 2491:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2492:                 }
 2493:             }
 2494:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2495:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2496:                           $coursetitle.'</span></td></tr>'."\n";
 2497:             $itemcount ++;
 2498:         }
 2499:     }
 2500:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2501:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2502:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2503:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2504:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2505:     for (my $k=0; $k<$maxnum+1; $k++) {
 2506:         my $vpos = $k+1;
 2507:         my $selstr;
 2508:         if ($k == $maxnum) {
 2509:             $selstr = ' selected="selected" ';
 2510:         }
 2511:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2512:     }
 2513:     $datatable .= '</select>&nbsp;'."\n".
 2514:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2515:                   '<td colspan="2">'.
 2516:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2517:                   ('&nbsp;'x2).
 2518:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2519:                   ('&nbsp;'x2);
 2520:     if ($type eq 'textbooks') {
 2521:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2522:                       ('&nbsp;'x2).
 2523:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2524:                       ('&nbsp;'x2).
 2525:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2526:         if ($switchserver) {
 2527:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2528:         } else {
 2529:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2530:         }
 2531:         $datatable .= '</span>'."\n";
 2532:     }
 2533:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2534:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2535:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2536:                   &Apache::loncommon::selectcourse_link
 2537:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2538:                   '</span></td>'."\n".
 2539:                   '</tr>'."\n";
 2540:     $itemcount ++;
 2541:     return $datatable;
 2542: }
 2543: 
 2544: sub textbookcourses_javascript {
 2545:     my ($settings) = @_;
 2546:     return unless(ref($settings) eq 'HASH');
 2547:     my (%ordered,%total,%jstext);
 2548:     foreach my $type ('textbooks','templates') {
 2549:         $total{$type} = 0;
 2550:         if (ref($settings->{$type}) eq 'HASH') {
 2551:             foreach my $item (keys(%{$settings->{$type}})) {
 2552:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2553:                     my $num = $settings->{$type}->{$item}{'order'};
 2554:                     $ordered{$type}{$num} = $item;
 2555:                 }
 2556:             }
 2557:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2558:         }
 2559:         my @jsarray = ();
 2560:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2561:             push(@jsarray,$ordered{$type}{$item});
 2562:         }
 2563:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2564:     }
 2565:     return <<"ENDSCRIPT";
 2566: <script type="text/javascript">
 2567: // <![CDATA[
 2568: function reorderBooks(form,item,caller) {
 2569:     var changedVal;
 2570: $jstext{'textbooks'};
 2571: $jstext{'templates'};
 2572:     var newpos;
 2573:     var maxh;
 2574:     if (caller == 'textbooks') {  
 2575:         newpos = 'textbooks_addbook_pos';
 2576:         maxh = 1 + $total{'textbooks'};
 2577:     } else {
 2578:         newpos = 'templates_addbook_pos';
 2579:         maxh = 1 + $total{'templates'};
 2580:     }
 2581:     var current = new Array;
 2582:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2583:     if (item == newpos) {
 2584:         changedVal = newitemVal;
 2585:     } else {
 2586:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2587:         current[newitemVal] = newpos;
 2588:     }
 2589:     if (caller == 'textbooks') {
 2590:         for (var i=0; i<textbooks.length; i++) {
 2591:             var elementName = 'textbooks_'+textbooks[i];
 2592:             if (elementName != item) {
 2593:                 if (form.elements[elementName]) {
 2594:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2595:                     current[currVal] = elementName;
 2596:                 }
 2597:             }
 2598:         }
 2599:     }
 2600:     if (caller == 'templates') {
 2601:         for (var i=0; i<templates.length; i++) {
 2602:             var elementName = 'templates_'+templates[i];
 2603:             if (elementName != item) {
 2604:                 if (form.elements[elementName]) {
 2605:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2606:                     current[currVal] = elementName;
 2607:                 }
 2608:             }
 2609:         }
 2610:     }
 2611:     var oldVal;
 2612:     for (var j=0; j<maxh; j++) {
 2613:         if (current[j] == undefined) {
 2614:             oldVal = j;
 2615:         }
 2616:     }
 2617:     if (oldVal < changedVal) {
 2618:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2619:            var elementName = current[k];
 2620:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2621:         }
 2622:     } else {
 2623:         for (var k=changedVal; k<oldVal; k++) {
 2624:             var elementName = current[k];
 2625:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2626:         }
 2627:     }
 2628:     return;
 2629: }
 2630: 
 2631: // ]]>
 2632: </script>
 2633: 
 2634: ENDSCRIPT
 2635: }
 2636: 
 2637: sub ltitools_javascript {
 2638:     my ($settings) = @_;
 2639:     my $togglejs = &ltitools_toggle_js();
 2640:     unless (ref($settings) eq 'HASH') {
 2641:         return $togglejs;
 2642:     }
 2643:     my (%ordered,$total,%jstext);
 2644:     $total = 0;
 2645:     foreach my $item (keys(%{$settings})) {
 2646:         if (ref($settings->{$item}) eq 'HASH') {
 2647:             my $num = $settings->{$item}{'order'};
 2648:             $ordered{$num} = $item;
 2649:         }
 2650:     }
 2651:     $total = scalar(keys(%{$settings}));
 2652:     my @jsarray = ();
 2653:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2654:         push(@jsarray,$ordered{$item});
 2655:     }
 2656:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2657:     return <<"ENDSCRIPT";
 2658: <script type="text/javascript">
 2659: // <![CDATA[
 2660: function reorderLTITools(form,item) {
 2661:     var changedVal;
 2662: $jstext
 2663:     var newpos = 'ltitools_add_pos';
 2664:     var maxh = 1 + $total;
 2665:     var current = new Array;
 2666:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2667:     if (item == newpos) {
 2668:         changedVal = newitemVal;
 2669:     } else {
 2670:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2671:         current[newitemVal] = newpos;
 2672:     }
 2673:     for (var i=0; i<ltitools.length; i++) {
 2674:         var elementName = 'ltitools_'+ltitools[i];
 2675:         if (elementName != item) {
 2676:             if (form.elements[elementName]) {
 2677:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2678:                 current[currVal] = elementName;
 2679:             }
 2680:         }
 2681:     }
 2682:     var oldVal;
 2683:     for (var j=0; j<maxh; j++) {
 2684:         if (current[j] == undefined) {
 2685:             oldVal = j;
 2686:         }
 2687:     }
 2688:     if (oldVal < changedVal) {
 2689:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2690:            var elementName = current[k];
 2691:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2692:         }
 2693:     } else {
 2694:         for (var k=changedVal; k<oldVal; k++) {
 2695:             var elementName = current[k];
 2696:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2697:         }
 2698:     }
 2699:     return;
 2700: }
 2701: 
 2702: // ]]>
 2703: </script>
 2704: 
 2705: $togglejs
 2706: 
 2707: ENDSCRIPT
 2708: }
 2709: 
 2710: sub ltitools_toggle_js {
 2711:     return <<"ENDSCRIPT";
 2712: <script type="text/javascript">
 2713: // <![CDATA[
 2714: 
 2715: function toggleLTITools(form,setting,item) {
 2716:     var radioname = '';
 2717:     var divid = '';
 2718:     if ((setting == 'passback') || (setting == 'roster')) {
 2719:         radioname = 'ltitools_'+setting+'_'+item;
 2720:         divid = 'ltitools_'+setting+'time_'+item;
 2721:         var num = form.elements[radioname].length;
 2722:         if (num) {
 2723:             var setvis = '';
 2724:             for (var i=0; i<num; i++) {
 2725:                 if (form.elements[radioname][i].checked) {
 2726:                     if (form.elements[radioname][i].value == '1') {
 2727:                         if (document.getElementById(divid)) {
 2728:                             document.getElementById(divid).style.display = 'inline-block';
 2729:                         }
 2730:                         setvis = 1;
 2731:                     }
 2732:                     break;
 2733:                 }
 2734:             }
 2735:         }
 2736:         if (!setvis) {
 2737:             if (document.getElementById(divid)) {
 2738:                 document.getElementById(divid).style.display = 'none';
 2739:             }
 2740:         }
 2741:     }
 2742:     if (setting == 'user') {
 2743:         divid = 'ltitools_'+setting+'_div_'+item;
 2744:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2745:         if (document.getElementById(divid)) {
 2746:             if (document.getElementById(checkid)) {
 2747:                 if (document.getElementById(checkid).checked) {
 2748:                     document.getElementById(divid).style.display = 'inline-block';
 2749:                 } else {
 2750:                     document.getElementById(divid).style.display = 'none';
 2751:                 }
 2752:             }
 2753:         }
 2754:     }
 2755:     return;
 2756: }
 2757: // ]]>
 2758: </script>
 2759: 
 2760: ENDSCRIPT
 2761: }
 2762: 
 2763: sub lti_javascript {
 2764:     my ($settings) = @_;
 2765:     my $togglejs = &lti_toggle_js();
 2766:     unless (ref($settings) eq 'HASH') {
 2767:         return $togglejs;
 2768:     }
 2769:     my (%ordered,$total,%jstext);
 2770:     $total = 0;
 2771:     foreach my $item (keys(%{$settings})) {
 2772:         if (ref($settings->{$item}) eq 'HASH') {
 2773:             my $num = $settings->{$item}{'order'};
 2774:             $ordered{$num} = $item;
 2775:         }
 2776:     }
 2777:     $total = scalar(keys(%{$settings}));
 2778:     my @jsarray = ();
 2779:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2780:         push(@jsarray,$ordered{$item});
 2781:     }
 2782:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 2783:     return <<"ENDSCRIPT";
 2784: <script type="text/javascript">
 2785: // <![CDATA[
 2786: function reorderLTI(form,item) {
 2787:     var changedVal;
 2788: $jstext
 2789:     var newpos = 'lti_pos_add';
 2790:     var maxh = 1 + $total;
 2791:     var current = new Array;
 2792:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2793:     if (item == newpos) {
 2794:         changedVal = newitemVal;
 2795:     } else {
 2796:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2797:         current[newitemVal] = newpos;
 2798:     }
 2799:     for (var i=0; i<lti.length; i++) {
 2800:         var elementName = 'lti_pos_'+lti[i];
 2801:         if (elementName != item) {
 2802:             if (form.elements[elementName]) {
 2803:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2804:                 current[currVal] = elementName;
 2805:             }
 2806:         }
 2807:     }
 2808:     var oldVal;
 2809:     for (var j=0; j<maxh; j++) {
 2810:         if (current[j] == undefined) {
 2811:             oldVal = j;
 2812:         }
 2813:     }
 2814:     if (oldVal < changedVal) {
 2815:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2816:            var elementName = current[k];
 2817:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2818:         }
 2819:     } else {
 2820:         for (var k=changedVal; k<oldVal; k++) {
 2821:             var elementName = current[k];
 2822:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2823:         }
 2824:     }
 2825:     return;
 2826: }
 2827: // ]]>
 2828: </script>
 2829: 
 2830: $togglejs
 2831: 
 2832: ENDSCRIPT
 2833: }
 2834: 
 2835: sub lti_toggle_js {
 2836:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 2837:                             localauth => 'Local auth argument',
 2838:                             krb       => 'Kerberos domain',
 2839:                          );
 2840:     return <<"ENDSCRIPT";
 2841: <script type="text/javascript">
 2842: // <![CDATA[
 2843: 
 2844: function toggleLTI(form,setting,item) {
 2845:     if (setting == 'requser') {
 2846:         var fieldsets = document.getElementsByClassName('ltioption_'+item);
 2847:         if (fieldsets.length) {
 2848:             var radioname = 'lti_'+setting+'_'+item;
 2849:             var num = form.elements[radioname].length;
 2850:             if (num) {
 2851:                 var setvis = '';
 2852:                 for (var i=0; i<num; i++) {
 2853:                     if (form.elements[radioname][i].checked) {
 2854:                         if (form.elements[radioname][i].value == '1') {
 2855:                            setvis = 1;
 2856:                            break;
 2857:                         }
 2858:                     }
 2859:                 }
 2860:                 for (var j=0; j<fieldsets.length; j++) { 
 2861:                     if (setvis) {
 2862:                         fieldsets[j].style.display = 'block';
 2863:                     } else {
 2864:                         fieldsets[j].style.display = 'none';
 2865:                     }
 2866:                 }
 2867:             }
 2868:         }
 2869:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback')) {
 2870:         var radioname = '';
 2871:         var divid = '';
 2872:         if (setting == 'user') {
 2873:             radioname = 'lti_mapuser_'+item;
 2874:             divid = 'lti_userfield_'+item;
 2875:         } else if (setting == 'crs') {
 2876:             radioname = 'lti_mapcrs_'+item;
 2877:             divid = 'lti_crsfield_'+item;
 2878:         } else {
 2879:             radioname = 'lti_passbackformat_'+item;
 2880:             divid =  'lti_passback_'+item;
 2881:         }
 2882:         var num = form.elements[radioname].length;
 2883:         if (num) {
 2884:             var setvis = '';
 2885:             for (var i=0; i<num; i++) {
 2886:                if (form.elements[radioname][i].checked) {
 2887:                    if (setting == 'passback') {
 2888:                        if (form.elements[radioname][i].value == '1') {
 2889:                            if (document.getElementById(divid)) {
 2890:                                document.getElementById(divid).style.display = 'inline-block';
 2891:                            }
 2892:                            setvis = 1;
 2893:                            break;
 2894:                        }
 2895:                    } else {
 2896:                        if (form.elements[radioname][i].value == 'other') {
 2897:                            if (document.getElementById(divid)) {
 2898:                                document.getElementById(divid).style.display = 'inline-block';
 2899:                            }
 2900:                            setvis = 1;
 2901:                            break;
 2902:                        }
 2903:                    }
 2904:                } 
 2905:             }
 2906:             if (!setvis) {
 2907:                 if (document.getElementById(divid)) {
 2908:                     document.getElementById(divid).style.display = 'none';
 2909:                 }
 2910:             }
 2911:         }
 2912:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 2913:         var numsec = form.elements['lti_crssec_'+item].length;
 2914:         if (numsec) {
 2915:             var setvis = '';
 2916:             for (var i=0; i<numsec; i++) {
 2917:                 if (form.elements['lti_crssec_'+item][i].checked) {
 2918:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 2919:                         if (document.getElementById('lti_crssecfield_'+item)) {
 2920:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 2921:                             setvis = 1;
 2922:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 2923:                             if (numsrcsec) {
 2924:                                 var setsrcvis = '';
 2925:                                 for (var j=0; j<numsrcsec; j++) {
 2926:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 2927:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 2928:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 2929:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 2930:                                                 setsrcvis = 1;
 2931:                                             }
 2932:                                         }
 2933:                                     }
 2934:                                 }
 2935:                                 if (!setsrcvis) {
 2936:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 2937:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2938:                                     }
 2939:                                 }
 2940:                             }
 2941:                         }
 2942:                     }
 2943:                 }
 2944:             }
 2945:             if (!setvis) {
 2946:                 if (document.getElementById('lti_crssecfield_'+item)) {
 2947:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 2948:                 }
 2949:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 2950:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2951:                 }
 2952:             }
 2953:         }
 2954:     } else if (setting == 'lcauth') {
 2955:         var numauth = form.elements['lti_lcauth_'+item].length;
 2956:         if (numauth) {
 2957:             for (var i=0; i<numauth; i++) {
 2958:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 2959:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 2960:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 2961:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 2962:                         } else {
 2963:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 2964:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 2965:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 2966:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 2967:                                 } else {
 2968:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 2969:                                 }
 2970:                             }
 2971:                         }
 2972:                     }
 2973:                 }
 2974:             }
 2975:         }
 2976:     } else if (setting == 'lcmenu') {
 2977:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 2978:         var divid = 'lti_menufield_'+item;
 2979:         var setvis = '';
 2980:         for (var i=0; i<menus.length; i++) {
 2981:             var radioname = menus[i];  
 2982:             var num = form.elements[radioname].length;
 2983:             if (num) {
 2984:                 for (var j=0; j<num; j++) {
 2985:                     if (form.elements[radioname][j].checked) {
 2986:                         if (form.elements[radioname][j].value == '1') {
 2987:                             if (document.getElementById(divid)) {
 2988:                                 document.getElementById(divid).style.display = 'inline-block';
 2989:                             }
 2990:                             setvis = 1;
 2991:                             break;
 2992:                         }
 2993:                     }
 2994:                 }
 2995:             }
 2996:             if (setvis == 1) {
 2997:                 break;
 2998:             }
 2999:         }
 3000:         if (!setvis) {
 3001:             if (document.getElementById(divid)) {
 3002:                 document.getElementById(divid).style.display = 'none';
 3003:             }
 3004:         }
 3005:     }
 3006:     return;
 3007: }
 3008: // ]]>
 3009: </script>
 3010: 
 3011: ENDSCRIPT
 3012: }
 3013: 
 3014: sub print_autoenroll {
 3015:     my ($dom,$settings,$rowtotal) = @_;
 3016:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3017:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3018:     if (ref($settings) eq 'HASH') {
 3019:         if (exists($settings->{'run'})) {
 3020:             if ($settings->{'run'} eq '0') {
 3021:                 $runoff = ' checked="checked" ';
 3022:                 $runon = ' ';
 3023:             } else {
 3024:                 $runon = ' checked="checked" ';
 3025:                 $runoff = ' ';
 3026:             }
 3027:         } else {
 3028:             if ($autorun) {
 3029:                 $runon = ' checked="checked" ';
 3030:                 $runoff = ' ';
 3031:             } else {
 3032:                 $runoff = ' checked="checked" ';
 3033:                 $runon = ' ';
 3034:             }
 3035:         }
 3036:         if (exists($settings->{'co-owners'})) {
 3037:             if ($settings->{'co-owners'} eq '0') {
 3038:                 $coownersoff = ' checked="checked" ';
 3039:                 $coownerson = ' ';
 3040:             } else {
 3041:                 $coownerson = ' checked="checked" ';
 3042:                 $coownersoff = ' ';
 3043:             }
 3044:         } else {
 3045:             $coownersoff = ' checked="checked" ';
 3046:             $coownerson = ' ';
 3047:         }
 3048:         if (exists($settings->{'sender_domain'})) {
 3049:             $defdom = $settings->{'sender_domain'};
 3050:         }
 3051:         if (exists($settings->{'autofailsafe'})) {
 3052:             $failsafe = $settings->{'autofailsafe'};
 3053:         }
 3054:     } else {
 3055:         if ($autorun) {
 3056:             $runon = ' checked="checked" ';
 3057:             $runoff = ' ';
 3058:         } else {
 3059:             $runoff = ' checked="checked" ';
 3060:             $runon = ' ';
 3061:         }
 3062:     }
 3063:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3064:     my $notif_sender;
 3065:     if (ref($settings) eq 'HASH') {
 3066:         $notif_sender = $settings->{'sender_uname'};
 3067:     }
 3068:     my $datatable='<tr class="LC_odd_row">'.
 3069:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3070:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3071:                   '<input type="radio" name="autoenroll_run"'.
 3072:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3073:                   '<label><input type="radio" name="autoenroll_run"'.
 3074:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3075:                   '</tr><tr>'.
 3076:                   '<td>'.&mt('Notification messages - sender').
 3077:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3078:                   &mt('username').':&nbsp;'.
 3079:                   '<input type="text" name="sender_uname" value="'.
 3080:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3081:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3082:                   '<tr class="LC_odd_row">'.
 3083:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3084:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3085:                   '<input type="radio" name="autoassign_coowners"'.
 3086:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3087:                   '<label><input type="radio" name="autoassign_coowners"'.
 3088:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3089:                   '</tr><tr>'.
 3090:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3091:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3092:                   '<input type="text" name="autoenroll_failsafe"'.
 3093:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3094:     $$rowtotal += 4;
 3095:     return $datatable;
 3096: }
 3097: 
 3098: sub print_autoupdate {
 3099:     my ($position,$dom,$settings,$rowtotal) = @_;
 3100:     my $datatable;
 3101:     if ($position eq 'top') {
 3102:         my $updateon = ' ';
 3103:         my $updateoff = ' checked="checked" ';
 3104:         my $classlistson = ' ';
 3105:         my $classlistsoff = ' checked="checked" ';
 3106:         if (ref($settings) eq 'HASH') {
 3107:             if ($settings->{'run'} eq '1') {
 3108:                 $updateon = $updateoff;
 3109:                 $updateoff = ' ';
 3110:             }
 3111:             if ($settings->{'classlists'} eq '1') {
 3112:                 $classlistson = $classlistsoff;
 3113:                 $classlistsoff = ' ';
 3114:             }
 3115:         }
 3116:         my %title = (
 3117:                    run => 'Auto-update active?',
 3118:                    classlists => 'Update information in classlists?',
 3119:                     );
 3120:         $datatable = '<tr class="LC_odd_row">'. 
 3121:                   '<td>'.&mt($title{'run'}).'</td>'.
 3122:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3123:                   '<input type="radio" name="autoupdate_run"'.
 3124:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3125:                   '<label><input type="radio" name="autoupdate_run"'.
 3126:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3127:                   '</tr><tr>'.
 3128:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 3129:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3130:                   '<label><input type="radio" name="classlists"'.
 3131:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3132:                   '<label><input type="radio" name="classlists"'.
 3133:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3134:                   '</tr>';
 3135:         $$rowtotal += 2;
 3136:     } elsif ($position eq 'middle') {
 3137:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3138:         my $numinrow = 3;
 3139:         my $locknamesettings;
 3140:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3141:                                      $dom,$numinrow,$othertitle,
 3142:                                     'lockablenames',$rowtotal);
 3143:         $$rowtotal ++;
 3144:     } else {
 3145:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3146:         my @fields = ('lastname','firstname','middlename','generation',
 3147:                       'permanentemail','id');
 3148:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3149:         my $numrows = 0;
 3150:         if (ref($types) eq 'ARRAY') {
 3151:             if (@{$types} > 0) {
 3152:                 $datatable = 
 3153:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3154:                                          \@fields,$types,\$numrows);
 3155:                     $$rowtotal += @{$types}; 
 3156:             }
 3157:         }
 3158:         $datatable .= 
 3159:             &usertype_update_row($settings,{'default' => $othertitle},
 3160:                                  \%fieldtitles,\@fields,['default'],
 3161:                                  \$numrows);
 3162:         $$rowtotal ++;     
 3163:     }
 3164:     return $datatable;
 3165: }
 3166: 
 3167: sub print_autocreate {
 3168:     my ($dom,$settings,$rowtotal) = @_;
 3169:     my (%createon,%createoff,%currhash);
 3170:     my @types = ('xml','req');
 3171:     if (ref($settings) eq 'HASH') {
 3172:         foreach my $item (@types) {
 3173:             $createoff{$item} = ' checked="checked" ';
 3174:             $createon{$item} = ' ';
 3175:             if (exists($settings->{$item})) {
 3176:                 if ($settings->{$item}) {
 3177:                     $createon{$item} = ' checked="checked" ';
 3178:                     $createoff{$item} = ' ';
 3179:                 }
 3180:             }
 3181:         }
 3182:         if ($settings->{'xmldc'} ne '') {
 3183:             $currhash{$settings->{'xmldc'}} = 1;
 3184:         }
 3185:     } else {
 3186:         foreach my $item (@types) {
 3187:             $createoff{$item} = ' checked="checked" ';
 3188:             $createon{$item} = ' ';
 3189:         }
 3190:     }
 3191:     $$rowtotal += 2;
 3192:     my $numinrow = 2;
 3193:     my $datatable='<tr class="LC_odd_row">'.
 3194:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3195:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3196:                   '<input type="radio" name="autocreate_xml"'.
 3197:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3198:                   '<label><input type="radio" name="autocreate_xml"'.
 3199:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3200:                   '</td></tr><tr>'.
 3201:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3202:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3203:                   '<input type="radio" name="autocreate_req"'.
 3204:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3205:                   '<label><input type="radio" name="autocreate_req"'.
 3206:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3207:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3208:                                                    'autocreate_xmldc',%currhash);
 3209:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3210:     if ($numdc > 1) {
 3211:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3212:                       '</td><td class="LC_left_item">';
 3213:     } else {
 3214:         $datatable .= &mt('Course creation processed as:').
 3215:                       '</td><td class="LC_right_item">';
 3216:     }
 3217:     $datatable .= $dctable.'</td></tr>';
 3218:     $$rowtotal += $rows;
 3219:     return $datatable;
 3220: }
 3221: 
 3222: sub print_directorysrch {
 3223:     my ($position,$dom,$settings,$rowtotal) = @_;
 3224:     my $datatable;
 3225:     if ($position eq 'top') {
 3226:         my $instsrchon = ' ';
 3227:         my $instsrchoff = ' checked="checked" ';
 3228:         my ($exacton,$containson,$beginson);
 3229:         my $instlocalon = ' ';
 3230:         my $instlocaloff = ' checked="checked" ';
 3231:         if (ref($settings) eq 'HASH') {
 3232:             if ($settings->{'available'} eq '1') {
 3233:                 $instsrchon = $instsrchoff;
 3234:                 $instsrchoff = ' ';
 3235:             }
 3236:             if ($settings->{'localonly'} eq '1') {
 3237:                 $instlocalon = $instlocaloff;
 3238:                 $instlocaloff = ' ';
 3239:             }
 3240:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3241:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3242:                     if ($type eq 'exact') {
 3243:                         $exacton = ' checked="checked" ';
 3244:                     } elsif ($type eq 'contains') {
 3245:                         $containson = ' checked="checked" ';
 3246:                     } elsif ($type eq 'begins') {
 3247:                         $beginson = ' checked="checked" ';
 3248:                     }
 3249:                 }
 3250:             } else {
 3251:                 if ($settings->{'searchtypes'} eq 'exact') {
 3252:                     $exacton = ' checked="checked" ';
 3253:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3254:                     $containson = ' checked="checked" ';
 3255:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3256:                     $exacton = ' checked="checked" ';
 3257:                     $containson = ' checked="checked" ';
 3258:                 }
 3259:             }
 3260:         }
 3261:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3262:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3263: 
 3264:         my $numinrow = 4;
 3265:         my $cansrchrow = 0;
 3266:         $datatable='<tr class="LC_odd_row">'.
 3267:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3268:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3269:                    '<input type="radio" name="dirsrch_available"'.
 3270:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3271:                    '<label><input type="radio" name="dirsrch_available"'.
 3272:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3273:                    '</tr><tr>'.
 3274:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3275:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3276:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3277:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3278:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3279:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3280:                    '</tr>';
 3281:         $$rowtotal += 2;
 3282:         if (ref($usertypes) eq 'HASH') {
 3283:             if (keys(%{$usertypes}) > 0) {
 3284:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3285:                                              $numinrow,$othertitle,'cansearch',
 3286:                                              $rowtotal);
 3287:                 $cansrchrow = 1;
 3288:             }
 3289:         }
 3290:         if ($cansrchrow) {
 3291:             $$rowtotal ++;
 3292:             $datatable .= '<tr>';
 3293:         } else {
 3294:             $datatable .= '<tr class="LC_odd_row">';
 3295:         }
 3296:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3297:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3298:         foreach my $title (@{$titleorder}) {
 3299:             if (defined($searchtitles->{$title})) {
 3300:                 my $check = ' ';
 3301:                 if (ref($settings) eq 'HASH') {
 3302:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3303:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3304:                             $check = ' checked="checked" ';
 3305:                         }
 3306:                     }
 3307:                 }
 3308:                 $datatable .= '<td class="LC_left_item">'.
 3309:                               '<span class="LC_nobreak"><label>'.
 3310:                               '<input type="checkbox" name="searchby" '.
 3311:                               'value="'.$title.'"'.$check.'/>'.
 3312:                               $searchtitles->{$title}.'</label></span></td>';
 3313:             }
 3314:         }
 3315:         $datatable .= '</tr></table></td></tr>';
 3316:         $$rowtotal ++;
 3317:         if ($cansrchrow) {
 3318:             $datatable .= '<tr class="LC_odd_row">';
 3319:         } else {
 3320:             $datatable .= '<tr>';
 3321:         }
 3322:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3323:                       '<td class="LC_left_item" colspan="2">'.
 3324:                       '<span class="LC_nobreak"><label>'.
 3325:                       '<input type="checkbox" name="searchtypes" '.
 3326:                       $exacton.' value="exact" />'.&mt('Exact match').
 3327:                       '</label>&nbsp;'.
 3328:                       '<label><input type="checkbox" name="searchtypes" '.
 3329:                       $beginson.' value="begins" />'.&mt('Begins with').
 3330:                       '</label>&nbsp;'.
 3331:                       '<label><input type="checkbox" name="searchtypes" '.
 3332:                       $containson.' value="contains" />'.&mt('Contains').
 3333:                       '</label></span></td></tr>';
 3334:         $$rowtotal ++;
 3335:     } else {
 3336:         my $domsrchon = ' checked="checked" ';
 3337:         my $domsrchoff = ' ';
 3338:         my $domlocalon = ' ';
 3339:         my $domlocaloff = ' checked="checked" ';
 3340:         if (ref($settings) eq 'HASH') {
 3341:             if ($settings->{'lclocalonly'} eq '1') {
 3342:                 $domlocalon = $domlocaloff;
 3343:                 $domlocaloff = ' ';
 3344:             }
 3345:             if ($settings->{'lcavailable'} eq '0') {
 3346:                 $domsrchoff = $domsrchon;
 3347:                 $domsrchon = ' ';
 3348:             }
 3349:         }
 3350:         $datatable='<tr class="LC_odd_row">'.
 3351:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3352:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3353:                       '<input type="radio" name="dirsrch_domavailable"'.
 3354:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3355:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3356:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3357:                       '</tr><tr>'.
 3358:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3359:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3360:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3361:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3362:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3363:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3364:                       '</tr>';
 3365:         $$rowtotal += 2;
 3366:     }
 3367:     return $datatable;
 3368: }
 3369: 
 3370: sub print_contacts {
 3371:     my ($position,$dom,$settings,$rowtotal) = @_;
 3372:     my $datatable;
 3373:     my @contacts = ('adminemail','supportemail');
 3374:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3375:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3376:     if ($position eq 'top') {
 3377:         if (ref($settings) eq 'HASH') {
 3378:             foreach my $item (@contacts) {
 3379:                 if (exists($settings->{$item})) {
 3380:                     $to{$item} = $settings->{$item};
 3381:                 }
 3382:             }
 3383:         }
 3384:     } elsif ($position eq 'middle') {
 3385:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3386:                      'updatesmail','idconflictsmail');
 3387:         foreach my $type (@mailings) {
 3388:             $otheremails{$type} = '';
 3389:         }
 3390:     } elsif ($position eq 'lower') {
 3391:         if (ref($settings) eq 'HASH') {
 3392:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3393:                 %lonstatus = %{$settings->{'lonstatus'}};
 3394:             }
 3395:         }
 3396:     } else {
 3397:         @mailings = ('helpdeskmail','otherdomsmail');
 3398:         foreach my $type (@mailings) {
 3399:             $otheremails{$type} = '';
 3400:         }
 3401:         $bccemails{'helpdeskmail'} = '';
 3402:         $bccemails{'otherdomsmail'} = '';
 3403:         $includestr{'helpdeskmail'} = '';
 3404:         $includestr{'otherdomsmail'} = '';
 3405:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3406:     }
 3407:     if (ref($settings) eq 'HASH') {
 3408:         unless (($position eq 'top') || ($position eq 'lower')) {
 3409:             foreach my $type (@mailings) {
 3410:                 if (exists($settings->{$type})) {
 3411:                     if (ref($settings->{$type}) eq 'HASH') {
 3412:                         foreach my $item (@contacts) {
 3413:                             if ($settings->{$type}{$item}) {
 3414:                                 $checked{$type}{$item} = ' checked="checked" ';
 3415:                             }
 3416:                         }
 3417:                         $otheremails{$type} = $settings->{$type}{'others'};
 3418:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3419:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3420:                             if ($settings->{$type}{'include'} ne '') {
 3421:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3422:                                 $includestr{$type} = &unescape($includestr{$type});
 3423:                             }
 3424:                         }
 3425:                     }
 3426:                 } elsif ($type eq 'lonstatusmail') {
 3427:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3428:                 }
 3429:             }
 3430:         }
 3431:         if ($position eq 'bottom') {
 3432:             foreach my $type (@mailings) {
 3433:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3434:                 if ($settings->{$type}{'include'} ne '') {
 3435:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3436:                     $includestr{$type} = &unescape($includestr{$type});
 3437:                 }
 3438:             }
 3439:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3440:                 if (ref($fields) eq 'ARRAY') {
 3441:                     foreach my $field (@{$fields}) {
 3442:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3443:                     }
 3444:                 }
 3445:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3446:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3447:                 } else {
 3448:                     $maxsize = '1.0';
 3449:                 }
 3450:             } else {
 3451:                 if (ref($fields) eq 'ARRAY') {
 3452:                     foreach my $field (@{$fields}) {
 3453:                         $currfield{$field} = 'yes';
 3454:                     }
 3455:                 }
 3456:                 $maxsize = '1.0';
 3457:             }
 3458:         }
 3459:     } else {
 3460:         if ($position eq 'top') {
 3461:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3462:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3463:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3464:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3465:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3466:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3467:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3468:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3469:         } elsif ($position eq 'bottom') {
 3470:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3471:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3472:             if (ref($fields) eq 'ARRAY') {
 3473:                 foreach my $field (@{$fields}) {
 3474:                     $currfield{$field} = 'yes';
 3475:                 }
 3476:             }
 3477:             $maxsize = '1.0';
 3478:         }
 3479:     }
 3480:     my ($titles,$short_titles) = &contact_titles();
 3481:     my $rownum = 0;
 3482:     my $css_class;
 3483:     if ($position eq 'top') {
 3484:         foreach my $item (@contacts) {
 3485:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3486:             $datatable .= '<tr'.$css_class.'>'. 
 3487:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3488:                           '</span></td><td class="LC_right_item">'.
 3489:                           '<input type="text" name="'.$item.'" value="'.
 3490:                           $to{$item}.'" /></td></tr>';
 3491:             $rownum ++;
 3492:         }
 3493:     } elsif ($position eq 'bottom') {
 3494:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3495:         $datatable .= '<tr'.$css_class.'>'.
 3496:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3497:                       &mt('(e-mail, subject, and description always shown)').
 3498:                       '</td><td class="LC_left_item">';
 3499:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3500:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3501:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3502:             foreach my $field (@{$fields}) {
 3503:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3504:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3505:                     $datatable .= ' '.&mt('(logged-in users)');
 3506:                 }
 3507:                 $datatable .='</td><td>';
 3508:                 my $clickaction;
 3509:                 if ($field eq 'screenshot') {
 3510:                     $clickaction = ' onclick="screenshotSize(this);"';
 3511:                 }
 3512:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3513:                     foreach my $option (@{$possoptions->{$field}}) {
 3514:                         my $checked;
 3515:                         if ($currfield{$field} eq $option) {
 3516:                             $checked = ' checked="checked"';
 3517:                         }
 3518:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3519:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3520:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3521:                                       '</label></span>'.('&nbsp;'x2);
 3522:                     }
 3523:                 }
 3524:                 if ($field eq 'screenshot') {
 3525:                     my $display;
 3526:                     if ($currfield{$field} eq 'no') {
 3527:                         $display = ' style="display:none"';
 3528:                     }
 3529:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3530:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3531:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3532:                 }
 3533:                 $datatable .= '</td></tr>';
 3534:             }
 3535:             $datatable .= '</table>';
 3536:         }
 3537:         $datatable .= '</td></tr>'."\n";
 3538:         $rownum ++;
 3539:     }
 3540:     unless (($position eq 'top') || ($position eq 'lower')) {
 3541:         foreach my $type (@mailings) {
 3542:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3543:             $datatable .= '<tr'.$css_class.'>'.
 3544:                           '<td><span class="LC_nobreak">'.
 3545:                           $titles->{$type}.': </span></td>'.
 3546:                           '<td class="LC_left_item">';
 3547:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3548:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3549:             }
 3550:             $datatable .= '<span class="LC_nobreak">';
 3551:             foreach my $item (@contacts) {
 3552:                 $datatable .= '<label>'.
 3553:                               '<input type="checkbox" name="'.$type.'"'.
 3554:                               $checked{$type}{$item}.
 3555:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3556:                               '</label>&nbsp;';
 3557:             }
 3558:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3559:                           '<input type="text" name="'.$type.'_others" '.
 3560:                           'value="'.$otheremails{$type}.'"  />';
 3561:             my %locchecked;
 3562:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3563:                 foreach my $loc ('s','b') {
 3564:                     if ($includeloc{$type} eq $loc) {
 3565:                         $locchecked{$loc} = ' checked="checked"';
 3566:                         last;
 3567:                     }
 3568:                 }
 3569:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3570:                               '<input type="text" name="'.$type.'_bcc" '.
 3571:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3572:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3573:                               &mt('Text automatically added to e-mail:').' '.
 3574:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3575:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3576:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3577:                               ('&nbsp;'x2).
 3578:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3579:                               '</span></fieldset>';
 3580:             }
 3581:             $datatable .= '</td></tr>'."\n";
 3582:             $rownum ++;
 3583:         }
 3584:     }
 3585:     if ($position eq 'middle') {
 3586:         my %choices;
 3587:         my $corelink = &core_link_msu();
 3588:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3589:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3590:                                         $corelink);
 3591:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3592:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3593:         my %defaultchecked = ('reporterrors'  => 'on',
 3594:                               'reportupdates' => 'on',
 3595:                               'reportstatus'  => 'on');
 3596:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3597:                                                    \%choices,$rownum);
 3598:         $datatable .= $reports;
 3599:     } elsif ($position eq 'lower') {
 3600:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3601:         my ($threshold,$sysmail,%excluded,%weights);
 3602:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3603:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3604:             $threshold = $lonstatus{'threshold'};
 3605:         } else {
 3606:             $threshold = $defaults->{'threshold'};
 3607:         }
 3608:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3609:             $sysmail = $lonstatus{'sysmail'};
 3610:         } else {
 3611:             $sysmail = $defaults->{'sysmail'};
 3612:         }
 3613:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3614:             foreach my $type ('E','W','N','U') {
 3615:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3616:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3617:                 } else {
 3618:                     $weights{$type} = $defaults->{$type};
 3619:                 }
 3620:             }
 3621:         } else {
 3622:             foreach my $type ('E','W','N','U') {
 3623:                 $weights{$type} = $defaults->{$type};
 3624:             }
 3625:         }
 3626:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3627:             if (@{$lonstatus{'excluded'}} > 0) {
 3628:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3629:             }
 3630:         }
 3631:         $datatable .= '<tr'.$css_class.'>'.
 3632:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3633:                       $titles->{'errorthreshold'}.
 3634:                       '</span></td><td class="LC_left_item">'.
 3635:                       '<input type="text" name="errorthreshold" value="'.
 3636:                       $threshold.'" size="5" /></td></tr>';
 3637:         $rownum ++;
 3638:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3639:         $datatable .= '<tr'.$css_class.'>'.
 3640:                       '<td class="LC_left_item">'.
 3641:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3642:                       '</span></td><td class="LC_left_item"><table><tr>';
 3643:         foreach my $type ('E','W','N','U') {
 3644:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3645:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3646:                           $weights{$type}.'" size="5" /></td>';
 3647:         }
 3648:         $datatable .= '</tr></table></tr>';
 3649:         $rownum ++;
 3650:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3651:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3652:                       $titles->{'errorexcluded'}.'</td>'.
 3653:                       '<td class="LC_left_item"><table>';
 3654:         my $numinrow = 4;
 3655:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3656:         for (my $i=0; $i<@ids; $i++) {
 3657:             my $rem = $i%($numinrow);
 3658:             if ($rem == 0) {
 3659:                 if ($i > 0) {
 3660:                     $datatable .= '</tr>';
 3661:                 }
 3662:                 $datatable .= '<tr>';
 3663:             }
 3664:             my $check;
 3665:             if ($excluded{$ids[$i]}) {
 3666:                 $check = ' checked="checked" ';
 3667:             }
 3668:             $datatable .= '<td class="LC_left_item">'.
 3669:                           '<span class="LC_nobreak"><label>'.
 3670:                           '<input type="checkbox" name="errorexcluded" '.
 3671:                           'value="'.$ids[$i].'"'.$check.' />'.
 3672:                           $ids[$i].'</label></span></td>';
 3673:         }
 3674:         my $colsleft = $numinrow - @ids%($numinrow);
 3675:         if ($colsleft > 1 ) {
 3676:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3677:                           '&nbsp;</td>';
 3678:         } elsif ($colsleft == 1) {
 3679:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3680:         }
 3681:         $datatable .= '</tr></table></td></tr>';
 3682:         $rownum ++;
 3683:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3684:         $datatable .= '<tr'.$css_class.'>'.
 3685:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3686:                       $titles->{'errorsysmail'}.
 3687:                       '</span></td><td class="LC_left_item">'.
 3688:                       '<input type="text" name="errorsysmail" value="'.
 3689:                       $sysmail.'" size="5" /></td></tr>';
 3690:         $rownum ++;
 3691:     } elsif ($position eq 'bottom') {
 3692:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3693:         my (@posstypes,%usertypeshash);
 3694:         if (ref($types) eq 'ARRAY') {
 3695:             @posstypes = @{$types};
 3696:         }
 3697:         if (@posstypes) {
 3698:             if (ref($usertypes) eq 'HASH') {
 3699:                 %usertypeshash = %{$usertypes};
 3700:             }
 3701:             my @overridden;
 3702:             my $numinrow = 4;
 3703:             if (ref($settings) eq 'HASH') {
 3704:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3705:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3706:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3707:                             push(@overridden,$key);
 3708:                             foreach my $item (@contacts) {
 3709:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3710:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3711:                                 }
 3712:                             }
 3713:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3714:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3715:                             $includeloc{'override_'.$key} = '';
 3716:                             $includestr{'override_'.$key} = '';
 3717:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3718:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3719:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3720:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3721:                             }
 3722:                         }
 3723:                     }
 3724:                 }
 3725:             }
 3726:             my $customclass = 'LC_helpdesk_override';
 3727:             my $optionsprefix = 'LC_options_helpdesk_';
 3728: 
 3729:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3730:  
 3731:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3732:                                          $numinrow,$othertitle,'overrides',
 3733:                                          \$rownum,$onclicktypes,$customclass);
 3734:             $rownum ++;
 3735:             $usertypeshash{'default'} = $othertitle;
 3736:             foreach my $status (@posstypes) {
 3737:                 my $css_class;
 3738:                 if ($rownum%2) {
 3739:                     $css_class = 'LC_odd_row ';
 3740:                 }
 3741:                 $css_class .= $customclass;
 3742:                 my $rowid = $optionsprefix.$status;
 3743:                 my $hidden = 1;
 3744:                 my $currstyle = 'display:none';
 3745:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3746:                     $currstyle = 'display:table-row';
 3747:                     $hidden = 0;
 3748:                 }
 3749:                 my $key = 'override_'.$status;
 3750:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3751:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3752:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3753:                                                   \@contacts,$short_titles);
 3754:                 unless ($hidden) {
 3755:                     $rownum ++;
 3756:                 }
 3757:             }
 3758:         }
 3759:     }
 3760:     $$rowtotal += $rownum;
 3761:     return $datatable;
 3762: }
 3763: 
 3764: sub core_link_msu {
 3765:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3766:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3767: }
 3768: 
 3769: sub overridden_helpdesk {
 3770:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3771:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3772:     my $class = 'LC_left_item';
 3773:     if ($css_class) {
 3774:         $css_class = ' class="'.$css_class.'"';
 3775:     }
 3776:     if ($rowid) {
 3777:         $rowid = ' id="'.$rowid.'"';
 3778:     }
 3779:     if ($rowstyle) {
 3780:         $rowstyle = ' style="'.$rowstyle.'"';
 3781:     }
 3782:     my ($output,$description);
 3783:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3784:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3785:               "<td>$description</td>\n".
 3786:               '<td class="'.$class.'" colspan="2">'.
 3787:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3788:               '<span class="LC_nobreak">';
 3789:     if (ref($contacts) eq 'ARRAY') {
 3790:         foreach my $item (@{$contacts}) {
 3791:             my $check;
 3792:             if (ref($checked) eq 'HASH') {
 3793:                $check = $checked->{$item};
 3794:             }
 3795:             my $title;
 3796:             if (ref($short_titles) eq 'HASH') {
 3797:                 $title = $short_titles->{$item}; 
 3798:             }
 3799:             $output .= '<label>'.
 3800:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3801:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3802:         }
 3803:     }
 3804:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3805:                '<input type="text" name="override_'.$type.'_others" '.
 3806:                'value="'.$otheremails.'"  />';
 3807:     my %locchecked;
 3808:     foreach my $loc ('s','b') {
 3809:         if ($includeloc eq $loc) {
 3810:             $locchecked{$loc} = ' checked="checked"';
 3811:             last;
 3812:         }
 3813:     }
 3814:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3815:                '<input type="text" name="override_'.$type.'_bcc" '.
 3816:                'value="'.$bccemails.'"  /></fieldset>'.
 3817:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3818:                &mt('Text automatically added to e-mail:').' '.
 3819:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3820:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3821:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3822:                ('&nbsp;'x2).
 3823:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3824:                '</span></fieldset>'.
 3825:                '</td></tr>'."\n";
 3826:     return $output;
 3827: }
 3828: 
 3829: sub contacts_javascript {
 3830:     return <<"ENDSCRIPT";
 3831: 
 3832: <script type="text/javascript">
 3833: // <![CDATA[
 3834: 
 3835: function screenshotSize(field) {
 3836:     if (document.getElementById('help_screenshotsize')) {
 3837:         if (field.value == 'no') {
 3838:             document.getElementById('help_screenshotsize').style.display="none";
 3839:         } else {
 3840:             document.getElementById('help_screenshotsize').style.display="";
 3841:         }
 3842:     }
 3843:     return;
 3844: }
 3845: 
 3846: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3847:     if (form.elements[checkbox].length != undefined) {
 3848:         var count = 0;
 3849:         if (docount) {
 3850:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3851:                 if (form.elements[checkbox][i].checked) {
 3852:                     count ++;
 3853:                 }
 3854:             }
 3855:         }
 3856:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3857:             var type = form.elements[checkbox][i].value;
 3858:             if (document.getElementById(prefix+type)) {
 3859:                 if (form.elements[checkbox][i].checked) {
 3860:                     document.getElementById(prefix+type).style.display = 'table-row';
 3861:                     if (count % 2 == 1) {
 3862:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3863:                     } else {
 3864:                         document.getElementById(prefix+type).className = target;
 3865:                     }
 3866:                     count ++;
 3867:                 } else {
 3868:                     document.getElementById(prefix+type).style.display = 'none';
 3869:                 }
 3870:             }
 3871:         }
 3872:     }
 3873:     return;
 3874: }
 3875: 
 3876: 
 3877: // ]]>
 3878: </script>
 3879: 
 3880: ENDSCRIPT
 3881: }
 3882: 
 3883: sub print_helpsettings {
 3884:     my ($position,$dom,$settings,$rowtotal) = @_;
 3885:     my $confname = $dom.'-domainconfig';
 3886:     my $formname = 'display';
 3887:     my ($datatable,$itemcount);
 3888:     if ($position eq 'top') {
 3889:         $itemcount = 1;
 3890:         my (%choices,%defaultchecked,@toggles);
 3891:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3892:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3893:                                      &mt('LON-CAPA bug tracker'),600,500));
 3894:         %defaultchecked = ('submitbugs' => 'on');
 3895:         @toggles = ('submitbugs');
 3896:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3897:                                                      \%choices,$itemcount);
 3898:         $$rowtotal ++;
 3899:     } else {
 3900:         my $css_class;
 3901:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3902:         my (%customroles,%ordered,%current);
 3903:         if (ref($settings) eq 'HASH') {
 3904:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3905:                 %current = %{$settings->{'adhoc'}};
 3906:             }
 3907:         }
 3908:         my $count = 0;
 3909:         foreach my $key (sort(keys(%existing))) {
 3910:             if ($key=~/^rolesdef\_(\w+)$/) {
 3911:                 my $rolename = $1;
 3912:                 my (%privs,$order);
 3913:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3914:                 $customroles{$rolename} = \%privs;
 3915:                 if (ref($current{$rolename}) eq 'HASH') {
 3916:                     $order = $current{$rolename}{'order'};
 3917:                 }
 3918:                 if ($order eq '') {
 3919:                     $order = $count;
 3920:                 }
 3921:                 $ordered{$order} = $rolename;
 3922:                 $count++;
 3923:             }
 3924:         }
 3925:         my $maxnum = scalar(keys(%ordered));
 3926:         my @roles_by_num = ();
 3927:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3928:             push(@roles_by_num,$item);
 3929:         }
 3930:         my $context = 'domprefs';
 3931:         my $crstype = 'Course';
 3932:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3933:         my @accesstypes = ('all','dh','da','none');
 3934:         my ($numstatustypes,@jsarray);
 3935:         if (ref($types) eq 'ARRAY') {
 3936:             if (@{$types} > 0) {
 3937:                 $numstatustypes = scalar(@{$types});
 3938:                 push(@accesstypes,'status');
 3939:                 @jsarray = ('bystatus');
 3940:             }
 3941:         }
 3942:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3943:         if (keys(%domhelpdesk)) {
 3944:             push(@accesstypes,('inc','exc'));
 3945:             push(@jsarray,('notinc','notexc'));
 3946:         }
 3947:         my $hiddenstr = join("','",@jsarray);
 3948:         my $context = 'domprefs';
 3949:         my $crstype = 'Course';
 3950:         my $prefix = 'helproles_';
 3951:         my $add_class = 'LC_hidden';
 3952:         foreach my $num (@roles_by_num) {
 3953:             my $role = $ordered{$num};
 3954:             my ($desc,$access,@statuses);
 3955:             if (ref($current{$role}) eq 'HASH') {
 3956:                 $desc = $current{$role}{'desc'};
 3957:                 $access = $current{$role}{'access'};
 3958:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3959:                     @statuses = @{$current{$role}{'insttypes'}};
 3960:                 }
 3961:             }
 3962:             if ($desc eq '') {
 3963:                 $desc = $role;
 3964:             }
 3965:             my $identifier = 'custhelp'.$num;
 3966:             my %full=();
 3967:             my %levels= (
 3968:                          course => {},
 3969:                          domain => {},
 3970:                          system => {},
 3971:                         );
 3972:             my %levelscurrent=(
 3973:                                course => {},
 3974:                                domain => {},
 3975:                                system => {},
 3976:                               );
 3977:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3978:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3979:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3980:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3981:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 3982:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3983:             for (my $k=0; $k<=$maxnum; $k++) {
 3984:                 my $vpos = $k+1;
 3985:                 my $selstr;
 3986:                 if ($k == $num) {
 3987:                     $selstr = ' selected="selected" ';
 3988:                 }
 3989:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3990:             }
 3991:             $datatable .= '</select>'.('&nbsp;'x2).
 3992:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3993:                           '</td>'.
 3994:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3995:                           &mt('Name shown to users:').
 3996:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3997:                           '</fieldset>'.
 3998:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3999:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4000:                           '<fieldset>'.
 4001:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4002:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4003:                                                                    \%levelscurrent,$identifier,
 4004:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4005:                           '</fieldset></td>';
 4006:             $itemcount ++;
 4007:         }
 4008:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4009:         my $newcust = 'custhelp'.$count;
 4010:         my (%privs,%levelscurrent);
 4011:         my %full=();
 4012:         my %levels= (
 4013:                      course => {},
 4014:                      domain => {},
 4015:                      system => {},
 4016:                     );
 4017:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4018:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4019:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4020:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4021:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4022:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4023:         for (my $k=0; $k<$maxnum+1; $k++) {
 4024:             my $vpos = $k+1;
 4025:             my $selstr;
 4026:             if ($k == $maxnum) {
 4027:                 $selstr = ' selected="selected" ';
 4028:             }
 4029:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4030:         }
 4031:         $datatable .= '</select>&nbsp;'."\n".
 4032:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4033:                       '</label></span></td>'.
 4034:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4035:                       '<span class="LC_nobreak">'.
 4036:                       &mt('Internal name:').
 4037:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4038:                       '</span>'.('&nbsp;'x4).
 4039:                       '<span class="LC_nobreak">'.
 4040:                       &mt('Name shown to users:').
 4041:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4042:                       '</span></fieldset>'.
 4043:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4044:                                              $usertypes,$types,\%domhelpdesk).
 4045:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4046:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4047:                                                                 \@templateroles,$newcust).
 4048:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4049:                                                                \%levelscurrent,$newcust).
 4050:                       '</fieldset>'.
 4051:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4052:                       '</td></tr>';
 4053:         $count ++;
 4054:         $$rowtotal += $count;
 4055:     }
 4056:     return $datatable;
 4057: }
 4058: 
 4059: sub adhocbutton {
 4060:     my ($prefix,$num,$field,$visibility) = @_;
 4061:     my %lt = &Apache::lonlocal::texthash(
 4062:                                           show => 'Show details',
 4063:                                           hide => 'Hide details',
 4064:                                         );
 4065:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4066:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4067:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4068:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4069: }
 4070: 
 4071: sub helpsettings_javascript {
 4072:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4073:     return unless(ref($roles_by_num) eq 'ARRAY');
 4074:     my %html_js_lt = &Apache::lonlocal::texthash(
 4075:                                           show => 'Show details',
 4076:                                           hide => 'Hide details',
 4077:                                         );
 4078:     &html_escape(\%html_js_lt);
 4079:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4080:     return <<"ENDSCRIPT";
 4081: <script type="text/javascript">
 4082: // <![CDATA[
 4083: 
 4084: function reorderHelpRoles(form,item) {
 4085:     var changedVal;
 4086: $jstext
 4087:     var newpos = 'helproles_${total}_pos';
 4088:     var maxh = 1 + $total;
 4089:     var current = new Array();
 4090:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4091:     if (item == newpos) {
 4092:         changedVal = newitemVal;
 4093:     } else {
 4094:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4095:         current[newitemVal] = newpos;
 4096:     }
 4097:     for (var i=0; i<helproles.length; i++) {
 4098:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4099:         if (elementName != item) {
 4100:             if (form.elements[elementName]) {
 4101:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4102:                 current[currVal] = elementName;
 4103:             }
 4104:         }
 4105:     }
 4106:     var oldVal;
 4107:     for (var j=0; j<maxh; j++) {
 4108:         if (current[j] == undefined) {
 4109:             oldVal = j;
 4110:         }
 4111:     }
 4112:     if (oldVal < changedVal) {
 4113:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4114:            var elementName = current[k];
 4115:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4116:         }
 4117:     } else {
 4118:         for (var k=changedVal; k<oldVal; k++) {
 4119:             var elementName = current[k];
 4120:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4121:         }
 4122:     }
 4123:     return;
 4124: }
 4125: 
 4126: function helpdeskAccess(num) {
 4127:     var curraccess = null;
 4128:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4129:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4130:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4131:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4132:             }
 4133:         }
 4134:     }
 4135:     var shown = Array();
 4136:     var hidden = Array();
 4137:     if (curraccess == 'none') {
 4138:         hidden = Array('$hiddenstr');
 4139:     } else {
 4140:         if (curraccess == 'status') {
 4141:             shown = Array('bystatus');
 4142:             hidden = Array('notinc','notexc');
 4143:         } else {
 4144:             if (curraccess == 'exc') {
 4145:                 shown = Array('notexc');
 4146:                 hidden = Array('notinc','bystatus');
 4147:             }
 4148:             if (curraccess == 'inc') {
 4149:                 shown = Array('notinc');
 4150:                 hidden = Array('notexc','bystatus');
 4151:             }
 4152:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4153:                 hidden = Array('notinc','notexc','bystatus');
 4154:             }
 4155:         }
 4156:     }
 4157:     if (hidden.length > 0) {
 4158:         for (var i=0; i<hidden.length; i++) {
 4159:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4160:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4161:             }
 4162:         }
 4163:     }
 4164:     if (shown.length > 0) {
 4165:         for (var i=0; i<shown.length; i++) {
 4166:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4167:                 if (shown[i] == 'privs') {
 4168:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4169:                 } else {
 4170:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4171:                 }
 4172:             }
 4173:         }
 4174:     }
 4175:     return;
 4176: }
 4177: 
 4178: function toggleHelpdeskItem(num,field) {
 4179:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4180:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4181:             document.getElementById('helproles_'+num+'_'+field).className =
 4182:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4183:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4184:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4185:             }
 4186:         } else {
 4187:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4188:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4189:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4190:             }
 4191:         }
 4192:     }
 4193:     return;
 4194: }
 4195: 
 4196: // ]]>
 4197: </script>
 4198: 
 4199: ENDSCRIPT
 4200: }
 4201: 
 4202: sub helpdeskroles_access {
 4203:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4204:         $usertypes,$types,$domhelpdesk) = @_;
 4205:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4206:     my %lt = &Apache::lonlocal::texthash(
 4207:                     'rou'    => 'Role usage',
 4208:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4209:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4210:                     'dh'     => 'All with domain helpdesk role',
 4211:                     'da'     => 'All with domain helpdesk assistant role',
 4212:                     'none'   => 'None',
 4213:                     'status' => 'Determined based on institutional status',
 4214:                     'inc'    => 'Include all, but exclude specific personnel',
 4215:                     'exc'    => 'Exclude all, but include specific personnel',
 4216:                   );
 4217:     my %usecheck = (
 4218:                      all => ' checked="checked"',
 4219:                    );
 4220:     my %displaydiv = (
 4221:                       status => 'none',
 4222:                       inc    => 'none',
 4223:                       exc    => 'none',
 4224:                       priv   => 'block',
 4225:                      );
 4226:     my $output;
 4227:     if (ref($current) eq 'HASH') {
 4228:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4229:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4230:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4231:                 delete($usecheck{'all'});
 4232:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4233:                     my $access = $1;
 4234:                     $displaydiv{$access} = 'inline';
 4235:                 } elsif ($current->{access} eq 'none') {
 4236:                     $displaydiv{'priv'} = 'none';
 4237:                 }
 4238:             }
 4239:         }
 4240:     }
 4241:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4242:               '<p>'.$lt{'whi'}.'</p>';
 4243:     foreach my $access (@{$accesstypes}) {
 4244:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4245:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4246:                    $lt{$access}.'</label>';
 4247:         if ($access eq 'status') {
 4248:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4249:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4250:                                                                  $othertitle,$usertypes,$types).
 4251:                        '</div>';
 4252:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4253:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4254:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4255:                        '</div>';
 4256:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4257:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4258:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4259:                        '</div>';
 4260:         }
 4261:         $output .= '</p>';
 4262:     }
 4263:     $output .= '</fieldset>';
 4264:     return $output;
 4265: }
 4266: 
 4267: sub radiobutton_prefs {
 4268:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4269:         $additional,$align) = @_;
 4270:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4271:                    (ref($choices) eq 'HASH'));
 4272: 
 4273:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4274: 
 4275:     foreach my $item (@{$toggles}) {
 4276:         if ($defaultchecked->{$item} eq 'on') {
 4277:             $checkedon{$item} = ' checked="checked" ';
 4278:             $checkedoff{$item} = ' ';
 4279:         } elsif ($defaultchecked->{$item} eq 'off') {
 4280:             $checkedoff{$item} = ' checked="checked" ';
 4281:             $checkedon{$item} = ' ';
 4282:         }
 4283:     }
 4284:     if (ref($settings) eq 'HASH') {
 4285:         foreach my $item (@{$toggles}) {
 4286:             if ($settings->{$item} eq '1') {
 4287:                 $checkedon{$item} =  ' checked="checked" ';
 4288:                 $checkedoff{$item} = ' ';
 4289:             } elsif ($settings->{$item} eq '0') {
 4290:                 $checkedoff{$item} =  ' checked="checked" ';
 4291:                 $checkedon{$item} = ' ';
 4292:             }
 4293:         }
 4294:     }
 4295:     if ($onclick) {
 4296:         $onclick = ' onclick="'.$onclick.'"';
 4297:     }
 4298:     foreach my $item (@{$toggles}) {
 4299:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4300:         $datatable .=
 4301:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4302:             '<span class="LC_nobreak">'.$choices->{$item}.
 4303:             '</span></td>';
 4304:         if ($align eq 'left') {
 4305:             $datatable .= '<td class="LC_left_item">';
 4306:         } else {
 4307:             $datatable .= '<td class="LC_right_item">';
 4308:         }
 4309:         $datatable .=
 4310:             '<span class="LC_nobreak">'.
 4311:             '<label><input type="radio" name="'.
 4312:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4313:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4314:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4315:             '</span>'.$additional.
 4316:             '</td>'.
 4317:             '</tr>';
 4318:         $itemcount ++;
 4319:     }
 4320:     return ($datatable,$itemcount);
 4321: }
 4322: 
 4323: sub print_ltitools {
 4324:     my ($dom,$settings,$rowtotal) = @_;
 4325:     my $rownum = 0;
 4326:     my $css_class;
 4327:     my $itemcount = 1;
 4328:     my $maxnum = 0;
 4329:     my %ordered;
 4330:     if (ref($settings) eq 'HASH') {
 4331:         foreach my $item (keys(%{$settings})) {
 4332:             if (ref($settings->{$item}) eq 'HASH') {
 4333:                 my $num = $settings->{$item}{'order'};
 4334:                 $ordered{$num} = $item;
 4335:             }
 4336:         }
 4337:     }
 4338:     my $confname = $dom.'-domainconfig';
 4339:     my $switchserver = &check_switchserver($dom,$confname);
 4340:     my $maxnum = scalar(keys(%ordered));
 4341:     my $datatable;
 4342:     my %lt = &ltitools_names();
 4343:     my @courseroles = ('cc','in','ta','ep','st');
 4344:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4345:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4346:     if (keys(%ordered)) {
 4347:         my @items = sort { $a <=> $b } keys(%ordered);
 4348:         for (my $i=0; $i<@items; $i++) {
 4349:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4350:             my $item = $ordered{$items[$i]};
 4351:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4352:             if (ref($settings->{$item}) eq 'HASH') {
 4353:                 $title = $settings->{$item}->{'title'};
 4354:                 $url = $settings->{$item}->{'url'};
 4355:                 $key = $settings->{$item}->{'key'};
 4356:                 $secret = $settings->{$item}->{'secret'};
 4357:                 $lifetime = $settings->{$item}->{'lifetime'};
 4358:                 my $image = $settings->{$item}->{'image'};
 4359:                 if ($image ne '') {
 4360:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4361:                 }
 4362:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4363:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4364:                 } else {
 4365:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4366:                 }
 4367:             }
 4368:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4369:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4370:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4371:             for (my $k=0; $k<=$maxnum; $k++) {
 4372:                 my $vpos = $k+1;
 4373:                 my $selstr;
 4374:                 if ($k == $i) {
 4375:                     $selstr = ' selected="selected" ';
 4376:                 }
 4377:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4378:             }
 4379:             $datatable .= '</select>'.('&nbsp;'x2).
 4380:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4381:                 &mt('Delete?').'</label></span></td>'.
 4382:                 '<td colspan="2">'.
 4383:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4384:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4385:                 ('&nbsp;'x2).
 4386:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4387:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4388:                 ('&nbsp;'x2).
 4389:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4390:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4391:                 ('&nbsp;'x2).
 4392:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4393:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4394:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4395:                 '<br /><br />'.
 4396:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4397:                 ' value="'.$url.'" /></span>'.
 4398:                 ('&nbsp;'x2).
 4399:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4400:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4401:                 ('&nbsp;'x2).
 4402:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4403:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4404:                 ('&nbsp;'x2).
 4405:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4406:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4407:                 '<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>'.
 4408:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4409:                 '</fieldset>'.
 4410:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4411:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4412:             my %currdisp;
 4413:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4414:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4415:                     $currdisp{'window'} = ' checked="checked"';
 4416:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4417:                     $currdisp{'tab'} = ' checked="checked"';
 4418:                 } else {
 4419:                     $currdisp{'iframe'} = ' checked="checked"';
 4420:                 }
 4421:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4422:                     $currdisp{'width'} = $1;
 4423:                 }
 4424:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4425:                      $currdisp{'height'} = $1;
 4426:                 }
 4427:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4428:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4429:             } else {
 4430:                 $currdisp{'iframe'} = ' checked="checked"';
 4431:             }
 4432:             foreach my $disp ('iframe','tab','window') {
 4433:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4434:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4435:             }
 4436:             $datatable .= ('&nbsp;'x4);
 4437:             foreach my $dimen ('width','height') {
 4438:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4439:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4440:                               ('&nbsp;'x2);
 4441:             }
 4442:             $datatable .= '</span><br />'.
 4443:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4444:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4445:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4446:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4447:                           '</textarea></div><div style=""></div><br />';
 4448:             my %units = (
 4449:                           'passback' => 'days',
 4450:                           'roster'   => 'seconds',
 4451:                         );
 4452:             foreach my $extra ('passback','roster') {
 4453:                 my $validsty = 'none';
 4454:                 my $currvalid;
 4455:                 my $checkedon = '';
 4456:                 my $checkedoff = ' checked="checked"';
 4457:                 if ($settings->{$item}->{$extra}) {
 4458:                     $checkedon = $checkedoff;
 4459:                     $checkedoff = '';
 4460:                     $validsty = 'inline-block';
 4461:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4462:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4463:                     }
 4464:                 }
 4465:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4466:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4467:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4468:                               &mt('No').'</label>'.('&nbsp;'x2).
 4469:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4470:                               &mt('Yes').'</label></span></div>'.
 4471:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4472:                               '<span class="LC_nobreak">'.
 4473:                               &mt("at least [_1] $units{$extra} after launch",
 4474:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4475:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4476:             }
 4477:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4478:             if ($imgsrc) {
 4479:                 $datatable .= $imgsrc.
 4480:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4481:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4482:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4483:             } else {
 4484:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4485:             }
 4486:             if ($switchserver) {
 4487:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4488:             } else {
 4489:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4490:             }
 4491:             $datatable .= '</span></fieldset>';
 4492:             my (%checkedfields,%rolemaps,$userincdom);
 4493:             if (ref($settings->{$item}) eq 'HASH') {
 4494:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4495:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4496:                 }
 4497:                 $userincdom = $settings->{$item}->{'incdom'};
 4498:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4499:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4500:                     $checkedfields{'roles'} = 1;
 4501:                 }
 4502:             }
 4503:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4504:                           '<span class="LC_nobreak">';
 4505:             my $userfieldstyle = 'display:none;';
 4506:             my $seluserdom = '';
 4507:             my $unseluserdom = ' selected="selected"';
 4508:             foreach my $field (@fields) {
 4509:                 my ($checked,$onclick,$id,$spacer);
 4510:                 if ($checkedfields{$field}) {
 4511:                     $checked = ' checked="checked"';
 4512:                 }
 4513:                 if ($field eq 'user') {
 4514:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4515:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4516:                     if ($checked) {
 4517:                         $userfieldstyle = 'display:inline-block';
 4518:                         if ($userincdom) {
 4519:                             $seluserdom = $unseluserdom;
 4520:                             $unseluserdom = '';
 4521:                         }
 4522:                     }
 4523:                 } else {
 4524:                     $spacer = ('&nbsp;' x2);
 4525:                 }
 4526:                 $datatable .= '<label>'.
 4527:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4528:                               $lt{$field}.'</label>'.$spacer;
 4529:             }
 4530:             $datatable .= '</span>';
 4531:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4532:                           '<span class="LC_nobreak"> : '.
 4533:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4534:                           '<option value="">'.&mt('Select').'</option>'.
 4535:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4536:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4537:                           '</select></span></div>';
 4538:             $datatable .= '</fieldset>'.
 4539:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4540:             foreach my $role (@courseroles) {
 4541:                 my ($selected,$selectnone);
 4542:                 if (!$rolemaps{$role}) {
 4543:                     $selectnone = ' selected="selected"';
 4544:                 }
 4545:                 $datatable .= '<td style="text-align: center">'. 
 4546:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4547:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4548:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4549:                 foreach my $ltirole (@ltiroles) {
 4550:                     unless ($selectnone) {
 4551:                         if ($rolemaps{$role} eq $ltirole) {
 4552:                             $selected = ' selected="selected"';
 4553:                         } else {
 4554:                             $selected = '';
 4555:                         }
 4556:                     }
 4557:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4558:                 }
 4559:                 $datatable .= '</select></td>';
 4560:             }
 4561:             $datatable .= '</tr></table></fieldset>';
 4562:             my %courseconfig;
 4563:             if (ref($settings->{$item}) eq 'HASH') {
 4564:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4565:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4566:                 }
 4567:             }
 4568:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4569:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4570:                 my $checked;
 4571:                 if ($courseconfig{$item}) {
 4572:                     $checked = ' checked="checked"';
 4573:                 }
 4574:                 $datatable .= '<label>'.
 4575:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4576:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4577:             }
 4578:             $datatable .= '</span></fieldset>'.
 4579:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4580:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4581:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4582:                 my %custom = %{$settings->{$item}->{'custom'}};
 4583:                 if (keys(%custom) > 0) {
 4584:                     foreach my $key (sort(keys(%custom))) {
 4585:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4586:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4587:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4588:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4589:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4590:                     }
 4591:                 }
 4592:             }
 4593:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4594:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4595:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4596:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4597:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4598:             $itemcount ++;
 4599:         }
 4600:     }
 4601:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4602:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4603:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4604:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4605:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4606:     for (my $k=0; $k<$maxnum+1; $k++) {
 4607:         my $vpos = $k+1;
 4608:         my $selstr;
 4609:         if ($k == $maxnum) {
 4610:             $selstr = ' selected="selected" ';
 4611:         }
 4612:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4613:     }
 4614:     $datatable .= '</select>&nbsp;'."\n".
 4615:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4616:                   '<td colspan="2">'.
 4617:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4618:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4619:                   ('&nbsp;'x2).
 4620:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4621:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4622:                   ('&nbsp;'x2).
 4623:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4624:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4625:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4626:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4627:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4628:                   '<br />'.
 4629:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4630:                   ('&nbsp;'x2).
 4631:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4632:                   ('&nbsp;'x2).
 4633:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4634:                   ('&nbsp;'x2).
 4635:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4636:                   '<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".
 4637:                   '</fieldset>'.
 4638:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4639:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4640:     my %defaultdisp;
 4641:     $defaultdisp{'iframe'} = ' checked="checked"';
 4642:     foreach my $disp ('iframe','tab','window') {
 4643:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4644:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4645:     }
 4646:     $datatable .= ('&nbsp;'x4);
 4647:     foreach my $dimen ('width','height') {
 4648:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4649:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4650:                       ('&nbsp;'x2);
 4651:     }
 4652:     $datatable .= '</span><br />'.
 4653:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4654:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4655:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4656:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4657:                   '</div><div style=""></div><br />';
 4658:     my %units = (
 4659:                   'passback' => 'days',
 4660:                   'roster'   => 'seconds',
 4661:                 );
 4662:     my %defaulttimes = (
 4663:                      'passback' => '7',
 4664:                      'roster'   => '300',
 4665:                    );
 4666:     foreach my $extra ('passback','roster') {
 4667:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4668:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4669:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4670:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4671:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4672:                       &mt('Yes').'</label></span></div>'.
 4673:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4674:                       '<span class="LC_nobreak">'.
 4675:                       &mt("at least [_1] $units{$extra} after launch",
 4676:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4677:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4678:     }
 4679:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4680:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4681:     if ($switchserver) {
 4682:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4683:     } else {
 4684:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4685:     }
 4686:     $datatable .= '</span></fieldset>'.
 4687:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4688:                   '<span class="LC_nobreak">';
 4689:     foreach my $field (@fields) {
 4690:         my ($id,$onclick,$spacer);
 4691:         if ($field eq 'user') {
 4692:             $id = ' id="ltitools_user_field_add"';
 4693:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4694:         } else {
 4695:             $spacer = ('&nbsp;' x2);
 4696:         }
 4697:         $datatable .= '<label>'.
 4698:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4699:                       $lt{$field}.'</label>'.$spacer;
 4700:     }
 4701:     $datatable .= '</span>'.
 4702:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4703:                   '<span class="LC_nobreak"> : '.
 4704:                   '<select name="ltitools_userincdom_add">'.
 4705:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4706:                   '<option value="0">'.&mt('username').'</option>'.
 4707:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4708:                   '</select></span></div></fieldset>';
 4709:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4710:     foreach my $role (@courseroles) {
 4711:         my ($checked,$checkednone);
 4712:         $datatable .= '<td style="text-align: center">'.
 4713:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4714:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4715:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4716:         foreach my $ltirole (@ltiroles) {
 4717:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4718:         }
 4719:         $datatable .= '</select></td>';
 4720:     }
 4721:     $datatable .= '</tr></table></fieldset>'.
 4722:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4723:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4724:         $datatable .= '<label>'.
 4725:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4726:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4727:     }
 4728:     $datatable .= '</span></fieldset>'.
 4729:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4730:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4731:                   '<tr><td><span class="LC_nobreak">'.
 4732:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4733:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4734:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4735:                   '</table></fieldset>'."\n".
 4736:                   '</td>'."\n".
 4737:                   '</tr>'."\n";
 4738:     $itemcount ++;
 4739:     return $datatable;
 4740: }
 4741: 
 4742: sub ltitools_names {
 4743:     my %lt = &Apache::lonlocal::texthash(
 4744:                                           'title'          => 'Title',
 4745:                                           'version'        => 'Version',
 4746:                                           'msgtype'        => 'Message Type',
 4747:                                           'sigmethod'      => 'Signature Method',
 4748:                                           'url'            => 'URL',
 4749:                                           'key'            => 'Key',
 4750:                                           'lifetime'       => 'Nonce lifetime (s)',
 4751:                                           'secret'         => 'Secret',
 4752:                                           'icon'           => 'Icon',   
 4753:                                           'user'           => 'User',
 4754:                                           'fullname'       => 'Full Name',
 4755:                                           'firstname'      => 'First Name',
 4756:                                           'lastname'       => 'Last Name',
 4757:                                           'email'          => 'E-mail',
 4758:                                           'roles'          => 'Role',
 4759:                                           'window'         => 'Window',
 4760:                                           'tab'            => 'Tab',
 4761:                                           'iframe'         => 'iFrame',
 4762:                                           'height'         => 'Height',
 4763:                                           'width'          => 'Width',
 4764:                                           'linktext'       => 'Default Link Text',
 4765:                                           'explanation'    => 'Default Explanation',
 4766:                                           'passback'       => 'Tool can return grades:',
 4767:                                           'roster'         => 'Tool can retrieve roster:',
 4768:                                           'crstarget'      => 'Display target',
 4769:                                           'crslabel'       => 'Course label',
 4770:                                           'crstitle'       => 'Course title', 
 4771:                                           'crslinktext'    => 'Link Text',
 4772:                                           'crsexplanation' => 'Explanation',
 4773:                                           'crsappend'      => 'Provider URL',
 4774:                                         );
 4775:     return %lt;
 4776: }
 4777: 
 4778: sub print_lti {
 4779:     my ($dom,$settings,$rowtotal) = @_;
 4780:     my $itemcount = 1;
 4781:     my $maxnum = 0;
 4782:     my $css_class;
 4783:     my %ordered;
 4784:     if (ref($settings) eq 'HASH') {
 4785:         foreach my $item (keys(%{$settings})) {
 4786:             if (ref($settings->{$item}) eq 'HASH') {
 4787:                 my $num = $settings->{$item}{'order'};
 4788:                 $ordered{$num} = $item;
 4789:             }
 4790:         }
 4791:     }
 4792:     my $maxnum = scalar(keys(%ordered));
 4793:     my $datatable;
 4794:     my %lt = &lti_names();
 4795:     if (keys(%ordered)) {
 4796:         my @items = sort { $a <=> $b } keys(%ordered);
 4797:         for (my $i=0; $i<@items; $i++) {
 4798:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4799:             my $item = $ordered{$items[$i]};
 4800:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 4801:             if (ref($settings->{$item}) eq 'HASH') {
 4802:                 $key = $settings->{$item}->{'key'};
 4803:                 $secret = $settings->{$item}->{'secret'};
 4804:                 $lifetime = $settings->{$item}->{'lifetime'};
 4805:                 $consumer = $settings->{$item}->{'consumer'};
 4806:                 $requser = $settings->{$item}->{'requser'};
 4807:                 $current = $settings->{$item};
 4808:             }
 4809:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 4810:             my %checkedrequser = (
 4811:                                    yes => ' checked="checked"',
 4812:                                    no  => '',
 4813:                                  );
 4814:             if (!$requser) {
 4815:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 4816:                 $checkedrequser{'yes'} = '';
 4817:             } 
 4818:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 4819:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4820:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 4821:             for (my $k=0; $k<=$maxnum; $k++) {
 4822:                 my $vpos = $k+1;
 4823:                 my $selstr;
 4824:                 if ($k == $i) {
 4825:                     $selstr = ' selected="selected" ';
 4826:                 }
 4827:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4828:             }
 4829:             $datatable .= '</select>'.('&nbsp;'x2).
 4830:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 4831:                 &mt('Delete?').'</label></span></td>'.
 4832:                 '<td colspan="2">'.
 4833:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4834:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 4835:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 4836:                 ('&nbsp;'x2).
 4837:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 4838:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4839:                 ('&nbsp;'x2).
 4840:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 4841:                 'value="'.$lifetime.'" size="3" /></span>'.
 4842:                 ('&nbsp;'x2).
 4843:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4844:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 4845:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 4846:                 '<br /><br />'.
 4847:                 '<span class="LC_nobreak">'.$lt{'key'}.
 4848:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 4849:                 ('&nbsp;'x2).
 4850:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4851:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 4852:                 '<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>'.
 4853:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 4854:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 4855:             $itemcount ++;
 4856:         }
 4857:     }
 4858:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4859:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 4860:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4861:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 4862:                   '<select name="lti_pos_add"'.$chgstr.'>';
 4863:     for (my $k=0; $k<$maxnum+1; $k++) {
 4864:         my $vpos = $k+1;
 4865:         my $selstr;
 4866:         if ($k == $maxnum) {
 4867:             $selstr = ' selected="selected" ';
 4868:         }
 4869:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4870:     }
 4871:     $datatable .= '</select>&nbsp;'."\n".
 4872:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4873:                   '<td colspan="2">'.
 4874:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4875:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 4876:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 4877:                   ('&nbsp;'x2).
 4878:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 4879:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4880:                   ('&nbsp;'x2).
 4881:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 4882:                   ('&nbsp;'x2).
 4883:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4884:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 4885:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 4886:                   '<br /><br />'.
 4887:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 4888:                   ('&nbsp;'x2).
 4889:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 4890:                   '<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".
 4891:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 4892:                   '</td>'."\n".
 4893:                   '</tr>'."\n";
 4894:     $$rowtotal ++;
 4895:     return $datatable;;
 4896: }
 4897: 
 4898: sub lti_names {
 4899:     my %lt = &Apache::lonlocal::texthash(
 4900:                                           'version'   => 'LTI Version',
 4901:                                           'url'       => 'URL',
 4902:                                           'key'       => 'Key',
 4903:                                           'lifetime'  => 'Nonce lifetime (s)',
 4904:                                           'consumer'  => 'Consumer',
 4905:                                           'secret'    => 'Secret',
 4906:                                           'requser'   => "User's identity sent",
 4907:                                           'email'     => 'Email address',
 4908:                                           'sourcedid' => 'User ID',
 4909:                                           'other'     => 'Other',
 4910:                                           'passback'  => 'Can return grades to Consumer:',
 4911:                                           'roster'    => 'Can retrieve roster from Consumer:',
 4912:                                           'topmenu'   => 'Display LON-CAPA page header',
 4913:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 4914:                                         );
 4915:     return %lt;
 4916: }
 4917: 
 4918: sub lti_options {
 4919:     my ($num,$current,$itemcount,%lt) = @_;
 4920:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield);
 4921:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 4922:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 4923:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 4924:     $checked{'mapcrstype'} = {};
 4925:     $checked{'makeuser'} = {};
 4926:     $checked{'selfenroll'} = {};
 4927:     $checked{'crssec'} = {};
 4928:     $checked{'crssecsrc'} = {};
 4929:     $checked{'lcauth'} = {};
 4930:     $checked{'menuitem'} = {};
 4931:     if ($num eq 'add') {
 4932:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 4933:     }
 4934:     my $userfieldsty = 'none';
 4935:     my $crsfieldsty = 'none';
 4936:     my $crssecfieldsty = 'none';
 4937:     my $secsrcfieldsty = 'none';
 4938:     my $passbacksty = 'none';
 4939:     my $optionsty = 'block';
 4940:     my $lcauthparm;
 4941:     my $lcauthparmstyle = 'display:none';
 4942:     my $lcauthparmtext;
 4943:     my $menusty;
 4944:     my $numinrow = 4;
 4945:     my %menutitles = &ltimenu_titles();
 4946: 
 4947:     if (ref($current) eq 'HASH') {
 4948:         if (!$current->{'requser'}) {
 4949:             $optionsty = 'none';
 4950:         }
 4951:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 4952:             $checked{'mapuser'}{'sourcedid'} = '';
 4953:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 4954:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 4955:             } else {
 4956:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 4957:                 $userfield = $current->{'mapuser'};
 4958:                 $userfieldsty = 'inline-block';
 4959:             }
 4960:         }
 4961:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 4962:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 4963:             if ($current->{'mapcrs'} eq 'context_id') {
 4964:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 4965:             } else {
 4966:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 4967:                 $cidfield = $current->{'mapcrs'};
 4968:                 $crsfieldsty = 'inline-block';
 4969:             }
 4970:         }
 4971:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 4972:             foreach my $type (@{$current->{'mapcrstype'}}) {
 4973:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 4974:             }
 4975:         }
 4976:         if ($current->{'makecrs'}) {
 4977:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 4978:         }
 4979:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 4980:             foreach my $role (@{$current->{'makeuser'}}) {
 4981:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 4982:             }
 4983:         }
 4984:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 4985:             $checked{'lcauth'}{$1} = ' checked="checked"';
 4986:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 4987:                 $lcauthparm = $current->{'lcauthparm'};
 4988:                 $lcauthparmstyle = 'display:table-row'; 
 4989:                 if ($current->{'lcauth'} eq 'localauth') {
 4990:                     $lcauthparmtext = &mt('Local auth argument');
 4991:                 } else {
 4992:                     $lcauthparmtext = &mt('Kerberos domain');
 4993:                 }
 4994:             }
 4995:         }
 4996:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 4997:             foreach my $role (@{$current->{'selfenroll'}}) {
 4998:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 4999:             }
 5000:         }
 5001:         if (ref($current->{'maproles'}) eq 'HASH') {
 5002:             %rolemaps = %{$current->{'maproles'}};
 5003:         }
 5004:         if ($current->{'section'} ne '') {
 5005:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 5006:             $crssecfieldsty = 'inline-block';
 5007:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5008:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5009:             } else {
 5010:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5011:                 $crssecsrc = $current->{'section'};
 5012:                 $secsrcfieldsty = 'inline-block';
 5013:             }
 5014:         } else {
 5015:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5016:         }
 5017:         if ($current->{'topmenu'}) {
 5018:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5019:         } else {
 5020:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5021:         }
 5022:         if ($current->{'inlinemenu'}) {
 5023:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5024:         } else {
 5025:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5026:         }
 5027:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5028:             $menusty = 'inline-block';
 5029:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5030:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5031:                     if (exists($menutitles{$item})) {
 5032:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5033:                     }
 5034:                 }
 5035:             }
 5036:         } else {
 5037:             $menusty = 'none';
 5038:         }
 5039:     } else {
 5040:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5041:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5042:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5043:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 5044:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5045:         $menusty = 'inline-block'; 
 5046:     }
 5047:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 5048:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5049:                                official   => 'Official',
 5050:                                unofficial => 'Unofficial',
 5051:                                community  => 'Community',
 5052:                                textbook   => 'Textbook',
 5053:                                placement  => 'Placement Test',
 5054:                                lti        => 'LTI Provider',
 5055:     );
 5056:     my @authtypes = ('internal','krb4','krb5','localauth');
 5057:     my %shortauth = (
 5058:                      internal => 'int',
 5059:                      krb4 => 'krb4',
 5060:                      krb5 => 'krb5',
 5061:                      localauth  => 'loc'
 5062:                     );
 5063:     my %authnames = &authtype_names();
 5064:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5065:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5066:     my @courseroles = ('cc','in','ta','ep','st');
 5067:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5068:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5069:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5070:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5071:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5072:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5073:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5074:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5075:     foreach my $option ('sourcedid','email','other') {
 5076:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5077:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5078:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5079:     }
 5080:     $output .= '</span></div>'.
 5081:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5082:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5083:                'value="'.$userfield.'" /></div></fieldset>'. 
 5084:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5085:     foreach my $ltirole (@lticourseroles) {
 5086:         my ($selected,$selectnone);
 5087:         if ($rolemaps{$ltirole} eq '') {
 5088:             $selectnone = ' selected="selected"';
 5089:         }
 5090:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5091:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5092:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5093:         foreach my $role (@courseroles) {
 5094:             unless ($selectnone) {
 5095:                 if ($rolemaps{$ltirole} eq $role) {
 5096:                     $selected = ' selected="selected"';
 5097:                 } else {
 5098:                     $selected = '';
 5099:                 }
 5100:             }
 5101:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5102:                        &Apache::lonnet::plaintext($role,'Course').
 5103:                        '</option>';
 5104:         }
 5105:         $output .= '</select></td>';
 5106:     }
 5107:     $output .= '</tr></table></fieldset>'.
 5108:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5109:     foreach my $ltirole (@ltiroles) {
 5110:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5111:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 5112:     }
 5113:     $output .= '</fieldset>'.
 5114:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5115:                '<table>'.
 5116:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5117:                '</table>'.
 5118:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5119:                '<td class="LC_left_item">';
 5120:     foreach my $auth ('lti',@authtypes) {
 5121:         my $authtext;
 5122:         if ($auth eq 'lti') {
 5123:             $authtext = &mt('None');
 5124:         } else {
 5125:             $authtext = $authnames{$shortauth{$auth}};
 5126:         }
 5127:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5128:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5129:                    $authtext.'</label></span> &nbsp;';
 5130:     }
 5131:     $output .= '</td></tr>'.
 5132:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5133:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5134:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5135:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5136:                '</table></fieldset>'.
 5137:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5138:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5139:                &mt('Unique course identifier').':&nbsp;';
 5140:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5141:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5142:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5143:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5144:     }
 5145:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5146:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5147:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5148:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5149:     foreach my $type (@coursetypes) {
 5150:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5151:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5152:                    ('&nbsp;'x2);
 5153:     }
 5154:     $output .= '</span></fieldset>'.
 5155:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5156:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5157:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5158:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5159:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5160:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5161:                '</fieldset>'.
 5162:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5163:     foreach my $lticrsrole (@lticourseroles) {
 5164:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5165:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5166:     }
 5167:     $output .= '</fieldset>'.
 5168:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 5169:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5170:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5171:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5172:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5173:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5174:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5175:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5176:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5177:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5178:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5179:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5180:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5181:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5182:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5183:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5184:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5185:     foreach my $extra ('roster','passback') {
 5186:         my $checkedon = '';
 5187:         my $checkedoff = ' checked="checked"';
 5188:         if ($extra eq 'passback') {
 5189:             $pb1p1chk = ' checked="checked"';
 5190:             $pb1p0chk = '';
 5191:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 5192:         } else {
 5193:             $onclickpb = ''; 
 5194:         }
 5195:         if (ref($current) eq 'HASH') {
 5196:             if (($current->{$extra})) {
 5197:                 $checkedon = $checkedoff;
 5198:                 $checkedoff = '';
 5199:                 if ($extra eq 'passback') {
 5200:                     $passbacksty = 'inline-block';
 5201:                 }
 5202:                 if ($current->{'passbackformat'} eq '1.0') {
 5203:                     $pb1p0chk =  ' checked="checked"';
 5204:                     $pb1p1chk = '';
 5205:                 }
 5206:             }
 5207:         }
 5208:         $output .= $lt{$extra}.'&nbsp;'.
 5209:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 5210:                    &mt('No').'</label>'.('&nbsp;'x2).
 5211:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 5212:                    &mt('Yes').'</label><br />';
 5213:     }
 5214:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 5215:                '<span class="LC_nobreak">'.&mt('Grade format').
 5216:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 5217:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 5218:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 5219:                &mt('Outcomes Extension (1.0)').'</label></span></div></fieldset>'.
 5220:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 5221:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 5222:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 5223:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5224:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 5225:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 5226:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5227:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 5228:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 5229:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5230:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 5231:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 5232:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 5233:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 5234:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 5235:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 5236:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 5237:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 5238:                    ('&nbsp;'x2);
 5239:     }
 5240:     $output .= '</span></div></fieldset>';
 5241: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 5242: #
 5243: #    $output .= '</fieldset>'.
 5244: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 5245:     return $output;
 5246: }
 5247: 
 5248: sub ltimenu_titles {
 5249:     return &Apache::lonlocal::texthash(
 5250:                                         fullname    => 'Full name',
 5251:                                         coursetitle => 'Course title',
 5252:                                         role        => 'Role',
 5253:                                         logout      => 'Logout',
 5254:                                         grades      => 'Grades',
 5255:     );
 5256: }
 5257: 
 5258: sub print_coursedefaults {
 5259:     my ($position,$dom,$settings,$rowtotal) = @_;
 5260:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 5261:     my $itemcount = 1;
 5262:     my %choices =  &Apache::lonlocal::texthash (
 5263:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 5264:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 5265:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 5266:         coursecredits        => 'Credits can be specified for courses',
 5267:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 5268:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 5269:         texengine            => 'Default method to display mathematics',
 5270:         postsubmit           => 'Disable submit button/keypress following student submission',
 5271:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 5272:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 5273:     );
 5274:     my %staticdefaults = (
 5275:                            texengine            => 'MathJax',
 5276:                            anonsurvey_threshold => 10,
 5277:                            uploadquota          => 500,
 5278:                            postsubmit           => 60,
 5279:                            mysqltables          => 172800,
 5280:                          );
 5281:     if ($position eq 'top') {
 5282:         %defaultchecked = (
 5283:                             'canuse_pdfforms' => 'off',
 5284:                             'uselcmath'       => 'on',
 5285:                             'usejsme'         => 'on',
 5286:                             'canclone'        => 'none',
 5287:                           );
 5288:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 5289:         my $deftex = $staticdefaults{'texengine'};
 5290:         if (ref($settings) eq 'HASH') {
 5291:             if ($settings->{'texengine'}) {
 5292:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 5293:                     $deftex = $settings->{'texengine'};
 5294:                 }
 5295:             }
 5296:         }
 5297:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5298:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 5299:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 5300:                        '</span></td><td class="LC_right_item">'.
 5301:                        '<select name="texengine">'."\n";
 5302:         my %texoptions = (
 5303:                             MathJax  => 'MathJax',
 5304:                             mimetex  => &mt('Convert to Images'),
 5305:                             tth      => &mt('TeX to HTML'),
 5306:                          );
 5307:         foreach my $renderer ('MathJax','mimetex','tth') {
 5308:             my $selected = '';
 5309:             if ($renderer eq $deftex) {
 5310:                 $selected = ' selected="selected"';
 5311:             }
 5312:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 5313:         }
 5314:         $mathdisp .= '</select></td></tr>'."\n";
 5315:         $itemcount ++;
 5316:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 5317:                                                      \%choices,$itemcount);
 5318:         $datatable = $mathdisp.$datatable;
 5319:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5320:         $datatable .=
 5321:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5322:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 5323:             '</span></td><td class="LC_left_item">';
 5324:         my $currcanclone = 'none';
 5325:         my $onclick;
 5326:         my @cloneoptions = ('none','domain');
 5327:         my %clonetitles = (
 5328:                              none     => 'No additional course requesters',
 5329:                              domain   => "Any course requester in course's domain",
 5330:                              instcode => 'Course requests for official courses ...',
 5331:                           );
 5332:         my (%codedefaults,@code_order,@posscodes);
 5333:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 5334:                                                     \@code_order) eq 'ok') {
 5335:             if (@code_order > 0) {
 5336:                 push(@cloneoptions,'instcode');
 5337:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 5338:             }
 5339:         }
 5340:         if (ref($settings) eq 'HASH') {
 5341:             if ($settings->{'canclone'}) {
 5342:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 5343:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 5344:                         if (@code_order > 0) {
 5345:                             $currcanclone = 'instcode';
 5346:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 5347:                         }
 5348:                     }
 5349:                 } elsif ($settings->{'canclone'} eq 'domain') {
 5350:                     $currcanclone = $settings->{'canclone'};
 5351:                 }
 5352:             }
 5353:         }
 5354:         foreach my $option (@cloneoptions) {
 5355:             my ($checked,$additional);
 5356:             if ($currcanclone eq $option) {
 5357:                 $checked = ' checked="checked"';
 5358:             }
 5359:             if ($option eq 'instcode') {
 5360:                 if (@code_order) {
 5361:                     my $show = 'none';
 5362:                     if ($checked) {
 5363:                         $show = 'block';
 5364:                     }
 5365:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 5366:                                   &mt('Institutional codes for new and cloned course have identical:').
 5367:                                   '<br />';
 5368:                     foreach my $item (@code_order) {
 5369:                         my $codechk;
 5370:                         if ($checked) {
 5371:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 5372:                                 $codechk = ' checked="checked"';
 5373:                             }
 5374:                         }
 5375:                         $additional .= '<label>'.
 5376:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 5377:                                        $item.'</label>';
 5378:                     }
 5379:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 5380:                 }
 5381:             }
 5382:             $datatable .=
 5383:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 5384:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 5385:                 '</label>&nbsp;'.$additional.'</span><br />';
 5386:         }
 5387:         $datatable .= '</td>'.
 5388:                       '</tr>';
 5389:         $itemcount ++;
 5390:     } else {
 5391:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5392:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 5393:         my $currusecredits = 0;
 5394:         my $postsubmitclient = 1;
 5395:         my @types = ('official','unofficial','community','textbook','placement');
 5396:         if (ref($settings) eq 'HASH') {
 5397:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 5398:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 5399:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 5400:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 5401:                 }
 5402:             }
 5403:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 5404:                 foreach my $type (@types) {
 5405:                     next if ($type eq 'community');
 5406:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 5407:                     if ($defcredits{$type} ne '') {
 5408:                         $currusecredits = 1;
 5409:                     }
 5410:                 }
 5411:             }
 5412:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 5413:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 5414:                     $postsubmitclient = 0;
 5415:                     foreach my $type (@types) {
 5416:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5417:                     }
 5418:                 } else {
 5419:                     foreach my $type (@types) {
 5420:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 5421:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 5422:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 5423:                             } else {
 5424:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5425:                             }
 5426:                         } else {
 5427:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5428:                         }
 5429:                     }
 5430:                 }
 5431:             } else {
 5432:                 foreach my $type (@types) {
 5433:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5434:                 }
 5435:             }
 5436:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 5437:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 5438:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 5439:                 }
 5440:             } else {
 5441:                 foreach my $type (@types) {
 5442:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 5443:                 }
 5444:             }
 5445:         } else {
 5446:             foreach my $type (@types) {
 5447:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5448:             }
 5449:         }
 5450:         if (!$currdefresponder) {
 5451:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 5452:         } elsif ($currdefresponder < 1) {
 5453:             $currdefresponder = 1;
 5454:         }
 5455:         foreach my $type (@types) {
 5456:             if ($curruploadquota{$type} eq '') {
 5457:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 5458:             }
 5459:         }
 5460:         $datatable .=
 5461:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5462:                 $choices{'anonsurvey_threshold'}.
 5463:                 '</span></td>'.
 5464:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 5465:                 '<input type="text" name="anonsurvey_threshold"'.
 5466:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 5467:                 '</td></tr>'."\n";
 5468:         $itemcount ++;
 5469:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5470:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5471:                       $choices{'uploadquota'}.
 5472:                       '</span></td>'.
 5473:                       '<td style="text-align: right" class="LC_right_item">'.
 5474:                       '<table><tr>';
 5475:         foreach my $type (@types) {
 5476:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5477:                            '<input type="text" name="uploadquota_'.$type.'"'.
 5478:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 5479:         }
 5480:         $datatable .= '</tr></table></td></tr>'."\n";
 5481:         $itemcount ++;
 5482:         my $onclick = "toggleDisplay(this.form,'credits');";
 5483:         my $display = 'none';
 5484:         if ($currusecredits) {
 5485:             $display = 'block';
 5486:         }
 5487:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 5488:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 5489:         foreach my $type (@types) {
 5490:             next if ($type eq 'community');
 5491:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5492:                            '<input type="text" name="'.$type.'_credits"'.
 5493:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 5494:         }
 5495:         $additional .= '</tr></table></div>'."\n";
 5496:         %defaultchecked = ('coursecredits' => 'off');
 5497:         @toggles = ('coursecredits');
 5498:         my $current = {
 5499:                         'coursecredits' => $currusecredits,
 5500:                       };
 5501:         (my $table,$itemcount) =
 5502:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5503:                                \%choices,$itemcount,$onclick,$additional,'left');
 5504:         $datatable .= $table;
 5505:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 5506:         my $display = 'none';
 5507:         if ($postsubmitclient) {
 5508:             $display = 'block';
 5509:         }
 5510:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 5511:                       &mt('Number of seconds submit is disabled').'<br />'.
 5512:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 5513:                       '<table><tr>';
 5514:         foreach my $type (@types) {
 5515:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5516:                            '<input type="text" name="'.$type.'_timeout" value="'.
 5517:                            $deftimeout{$type}.'" size="5" /></td>';
 5518:         }
 5519:         $additional .= '</tr></table></div>'."\n";
 5520:         %defaultchecked = ('postsubmit' => 'on');
 5521:         @toggles = ('postsubmit');
 5522:         $current = {
 5523:                        'postsubmit' => $postsubmitclient,
 5524:                    };
 5525:         ($table,$itemcount) =
 5526:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5527:                                \%choices,$itemcount,$onclick,$additional,'left');
 5528:         $datatable .= $table;
 5529:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5530:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5531:                       $choices{'mysqltables'}.
 5532:                       '</span></td>'.
 5533:                       '<td style="text-align: right" class="LC_right_item">'.
 5534:                       '<table><tr>';
 5535:         foreach my $type (@types) {
 5536:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5537:                            '<input type="text" name="mysqltables_'.$type.'"'.
 5538:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 5539:         }
 5540:         $datatable .= '</tr></table></td></tr>'."\n";
 5541:         $itemcount ++;
 5542: 
 5543:     }
 5544:     $$rowtotal += $itemcount;
 5545:     return $datatable;
 5546: }
 5547: 
 5548: sub print_selfenrollment {
 5549:     my ($position,$dom,$settings,$rowtotal) = @_;
 5550:     my ($css_class,$datatable);
 5551:     my $itemcount = 1;
 5552:     my @types = ('official','unofficial','community','textbook','placement');
 5553:     if (($position eq 'top') || ($position eq 'middle')) {
 5554:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 5555:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 5556:         my @rows;
 5557:         my $key;
 5558:         if ($position eq 'top') {
 5559:             $key = 'admin'; 
 5560:             if (ref($rowsref) eq 'ARRAY') {
 5561:                 @rows = @{$rowsref};
 5562:             }
 5563:         } elsif ($position eq 'middle') {
 5564:             $key = 'default';
 5565:             @rows = ('types','registered','approval','limit');
 5566:         }
 5567:         foreach my $row (@rows) {
 5568:             if (defined($titlesref->{$row})) {
 5569:                 $itemcount ++;
 5570:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5571:                 $datatable .= '<tr'.$css_class.'>'.
 5572:                               '<td>'.$titlesref->{$row}.'</td>'.
 5573:                               '<td class="LC_left_item">'.
 5574:                               '<table><tr>';
 5575:                 my (%current,%currentcap);
 5576:                 if (ref($settings) eq 'HASH') {
 5577:                     if (ref($settings->{$key}) eq 'HASH') {
 5578:                         foreach my $type (@types) {
 5579:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5580:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 5581:                             }
 5582:                             if (($row eq 'limit') && ($key eq 'default')) {
 5583:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5584:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 5585:                                 }
 5586:                             }
 5587:                         }
 5588:                     }
 5589:                 }
 5590:                 my %roles = (
 5591:                              '0' => &Apache::lonnet::plaintext('dc'),
 5592:                             ); 
 5593:             
 5594:                 foreach my $type (@types) {
 5595:                     unless (($row eq 'registered') && ($key eq 'default')) {
 5596:                         $datatable .= '<th>'.&mt($type).'</th>';
 5597:                     }
 5598:                 }
 5599:                 unless (($row eq 'registered') && ($key eq 'default')) {
 5600:                     $datatable .= '</tr><tr>';
 5601:                 }
 5602:                 foreach my $type (@types) {
 5603:                     if ($type eq 'community') {
 5604:                         $roles{'1'} = &mt('Community personnel');
 5605:                     } else {
 5606:                         $roles{'1'} = &mt('Course personnel');
 5607:                     }
 5608:                     $datatable .= '<td style="vertical-align: top">';
 5609:                     if ($position eq 'top') {
 5610:                         my %checked;
 5611:                         if ($current{$type} eq '0') {
 5612:                             $checked{'0'} = ' checked="checked"';
 5613:                         } else {
 5614:                             $checked{'1'} = ' checked="checked"';
 5615:                         }
 5616:                         foreach my $role ('1','0') {
 5617:                             $datatable .= '<span class="LC_nobreak"><label>'.
 5618:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 5619:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 5620:                                           $roles{$role}.'</label></span> ';
 5621:                         }
 5622:                     } else {
 5623:                         if ($row eq 'types') {
 5624:                             my %checked;
 5625:                             if ($current{$type} =~ /^(all|dom)$/) {
 5626:                                 $checked{$1} = ' checked="checked"';
 5627:                             } else {
 5628:                                 $checked{''} = ' checked="checked"';
 5629:                             }
 5630:                             foreach my $val ('','dom','all') {
 5631:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5632:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5633:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5634:                             }
 5635:                         } elsif ($row eq 'registered') {
 5636:                             my %checked;
 5637:                             if ($current{$type} eq '1') {
 5638:                                 $checked{'1'} = ' checked="checked"';
 5639:                             } else {
 5640:                                 $checked{'0'} = ' checked="checked"';
 5641:                             }
 5642:                             foreach my $val ('0','1') {
 5643:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5644:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5645:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5646:                             }
 5647:                         } elsif ($row eq 'approval') {
 5648:                             my %checked;
 5649:                             if ($current{$type} =~ /^([12])$/) {
 5650:                                 $checked{$1} = ' checked="checked"';
 5651:                             } else {
 5652:                                 $checked{'0'} = ' checked="checked"';
 5653:                             }
 5654:                             for my $val (0..2) {
 5655:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5656:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5657:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5658:                             }
 5659:                         } elsif ($row eq 'limit') {
 5660:                             my %checked;
 5661:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 5662:                                 $checked{$1} = ' checked="checked"';
 5663:                             } else {
 5664:                                 $checked{'none'} = ' checked="checked"';
 5665:                             }
 5666:                             my $cap;
 5667:                             if ($currentcap{$type} =~ /^\d+$/) {
 5668:                                 $cap = $currentcap{$type};
 5669:                             }
 5670:                             foreach my $val ('none','allstudents','selfenrolled') {
 5671:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5672:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5673:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5674:                             }
 5675:                             $datatable .= '<br />'.
 5676:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 5677:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 5678:                                           '</span>'; 
 5679:                         }
 5680:                     }
 5681:                     $datatable .= '</td>';
 5682:                 }
 5683:                 $datatable .= '</tr>';
 5684:             }
 5685:             $datatable .= '</table></td></tr>';
 5686:         }
 5687:     } elsif ($position eq 'bottom') {
 5688:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 5689:     }
 5690:     $$rowtotal += $itemcount;
 5691:     return $datatable;
 5692: }
 5693: 
 5694: sub print_validation_rows {
 5695:     my ($caller,$dom,$settings,$rowtotal) = @_;
 5696:     my ($itemsref,$namesref,$fieldsref);
 5697:     if ($caller eq 'selfenroll') { 
 5698:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 5699:     } elsif ($caller eq 'requestcourses') {
 5700:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 5701:     }
 5702:     my %currvalidation;
 5703:     if (ref($settings) eq 'HASH') {
 5704:         if (ref($settings->{'validation'}) eq 'HASH') {
 5705:             %currvalidation = %{$settings->{'validation'}};
 5706:         }
 5707:     }
 5708:     my $datatable;
 5709:     my $itemcount = 0;
 5710:     foreach my $item (@{$itemsref}) {
 5711:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5712:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5713:                       $namesref->{$item}.
 5714:                       '</span></td>'.
 5715:                       '<td class="LC_left_item">';
 5716:         if (($item eq 'url') || ($item eq 'button')) {
 5717:             $datatable .= '<span class="LC_nobreak">'.
 5718:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 5719:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 5720:         } elsif ($item eq 'fields') {
 5721:             my @currfields;
 5722:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 5723:                 @currfields = @{$currvalidation{$item}};
 5724:             }
 5725:             foreach my $field (@{$fieldsref}) {
 5726:                 my $check = '';
 5727:                 if (grep(/^\Q$field\E$/,@currfields)) {
 5728:                     $check = ' checked="checked"';
 5729:                 }
 5730:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5731:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 5732:                               ' value="'.$field.'"'.$check.' />'.$field.
 5733:                               '</label></span> ';
 5734:             }
 5735:         } elsif ($item eq 'markup') {
 5736:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 5737:                            $currvalidation{$item}.
 5738:                               '</textarea>';
 5739:         }
 5740:         $datatable .= '</td></tr>'."\n";
 5741:         if (ref($rowtotal)) {
 5742:             $itemcount ++;
 5743:         }
 5744:     }
 5745:     if ($caller eq 'requestcourses') {
 5746:         my %currhash;
 5747:         if (ref($settings) eq 'HASH') {
 5748:             if (ref($settings->{'validation'}) eq 'HASH') {
 5749:                 if ($settings->{'validation'}{'dc'} ne '') {
 5750:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 5751:                 }
 5752:             }
 5753:         }
 5754:         my $numinrow = 2;
 5755:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 5756:                                                        'validationdc',%currhash);
 5757:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5758:         $datatable .= '<tr'.$css_class.'><td>';
 5759:         if ($numdc > 1) {
 5760:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 5761:         } else {
 5762:             $datatable .=  &mt('Course creation processed as: ');
 5763:         }
 5764:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 5765:         $itemcount ++;
 5766:     }
 5767:     if (ref($rowtotal)) {
 5768:         $$rowtotal += $itemcount;
 5769:     }
 5770:     return $datatable;
 5771: }
 5772: 
 5773: sub print_usersessions {
 5774:     my ($position,$dom,$settings,$rowtotal) = @_;
 5775:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 5776:     my (%by_ip,%by_location,@intdoms,@instdoms);
 5777:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5778: 
 5779:     my @alldoms = &Apache::lonnet::all_domains();
 5780:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5781:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5782:     my %altids = &id_for_thisdom(%servers);
 5783:     if ($position eq 'top') {
 5784:         if (keys(%serverhomes) > 1) {
 5785:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 5786:             my $curroffloadnow;
 5787:             if (ref($settings) eq 'HASH') {
 5788:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 5789:                     $curroffloadnow = $settings->{'offloadnow'};
 5790:                 }
 5791:             }
 5792:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 5793:         } else {
 5794:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5795:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 5796:                           '</td></tr>';
 5797:         }
 5798:     } else {
 5799:         my %titles = &usersession_titles();
 5800:         my ($prefix,@types);
 5801:         if ($position eq 'bottom') {
 5802:             $prefix = 'remote';
 5803:             @types = ('version','excludedomain','includedomain');
 5804:         } else {
 5805:             $prefix = 'hosted';
 5806:             @types = ('excludedomain','includedomain');
 5807:         }
 5808:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 5809:     }
 5810:     $$rowtotal += $itemcount;
 5811:     return $datatable;
 5812: }
 5813: 
 5814: sub rules_by_location {
 5815:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 5816:     my ($datatable,$itemcount,$css_class);
 5817:     if (keys(%{$by_location}) == 0) {
 5818:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5819:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 5820:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 5821:                      '</td></tr>';
 5822:         $itemcount = 1;
 5823:     } else {
 5824:         $itemcount = 0;
 5825:         my $numinrow = 5;
 5826:         my (%current,%checkedon,%checkedoff);
 5827:         my @locations = sort(keys(%{$by_location}));
 5828:         foreach my $type (@{$types}) {
 5829:             $checkedon{$type} = '';
 5830:             $checkedoff{$type} = ' checked="checked"';
 5831:         }
 5832:         if (ref($settings) eq 'HASH') {
 5833:             if (ref($settings->{$prefix}) eq 'HASH') {
 5834:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 5835:                     $current{$key} = $settings->{$prefix}{$key};
 5836:                     if ($key eq 'version') {
 5837:                         if ($current{$key} ne '') {
 5838:                             $checkedon{$key} = ' checked="checked"';
 5839:                             $checkedoff{$key} = '';
 5840:                         }
 5841:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 5842:                         $checkedon{$key} = ' checked="checked"';
 5843:                         $checkedoff{$key} = '';
 5844:                     }
 5845:                 }
 5846:             }
 5847:         }
 5848:         foreach my $type (@{$types}) {
 5849:             next if ($type ne 'version' && !@locations);
 5850:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5851:             $datatable .= '<tr'.$css_class.'>
 5852:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 5853:                            <span class="LC_nobreak">&nbsp;
 5854:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 5855:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 5856:             if ($type eq 'version') {
 5857:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 5858:                 my $selector = '<select name="'.$prefix.'_version">';
 5859:                 foreach my $version (@lcversions) {
 5860:                     my $selected = '';
 5861:                     if ($current{'version'} eq $version) {
 5862:                         $selected = ' selected="selected"';
 5863:                     }
 5864:                     $selector .= ' <option value="'.$version.'"'.
 5865:                                  $selected.'>'.$version.'</option>';
 5866:                 }
 5867:                 $selector .= '</select> ';
 5868:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 5869:             } else {
 5870:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 5871:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 5872:                              ' />'.('&nbsp;'x2).
 5873:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 5874:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 5875:                              "\n".
 5876:                              '</div><div><table>';
 5877:                 my $rem;
 5878:                 for (my $i=0; $i<@locations; $i++) {
 5879:                     my ($showloc,$value,$checkedtype);
 5880:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 5881:                         my $ip = $by_location->{$locations[$i]}->[0];
 5882:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5883:                             $value = join(':',@{$by_ip->{$ip}});
 5884:                             $showloc = join(', ',@{$by_ip->{$ip}});
 5885:                             if (ref($current{$type}) eq 'ARRAY') {
 5886:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 5887:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 5888:                                         $checkedtype = ' checked="checked"';
 5889:                                         last;
 5890:                                     }
 5891:                                 }
 5892:                             }
 5893:                         }
 5894:                     }
 5895:                     $rem = $i%($numinrow);
 5896:                     if ($rem == 0) {
 5897:                         if ($i > 0) {
 5898:                             $datatable .= '</tr>';
 5899:                         }
 5900:                         $datatable .= '<tr>';
 5901:                     }
 5902:                     $datatable .= '<td class="LC_left_item">'.
 5903:                                   '<span class="LC_nobreak"><label>'.
 5904:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 5905:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 5906:                                   '</label></span></td>';
 5907:                 }
 5908:                 $rem = @locations%($numinrow);
 5909:                 my $colsleft = $numinrow - $rem;
 5910:                 if ($colsleft > 1 ) {
 5911:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5912:                                   '&nbsp;</td>';
 5913:                 } elsif ($colsleft == 1) {
 5914:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5915:                 }
 5916:                 $datatable .= '</tr></table>';
 5917:             }
 5918:             $datatable .= '</td></tr>';
 5919:             $itemcount ++;
 5920:         }
 5921:     }
 5922:     return ($datatable,$itemcount);
 5923: }
 5924: 
 5925: sub print_ssl {
 5926:     my ($position,$dom,$settings,$rowtotal) = @_;
 5927:     my ($css_class,$datatable);
 5928:     my $itemcount = 1;
 5929:     if ($position eq 'top') {
 5930:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5931:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5932:         my $same_institution;
 5933:         if ($intdom ne '') {
 5934:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 5935:             if (ref($internet_names) eq 'ARRAY') {
 5936:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 5937:                     $same_institution = 1;
 5938:                 }
 5939:             }
 5940:         }
 5941:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5942:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 5943:         if ($same_institution) {
 5944:             my %domservers = &Apache::lonnet::get_servers($dom);
 5945:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 5946:         } else {
 5947:             $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.");
 5948:         }
 5949:         $datatable .= '</td></tr>';
 5950:         $itemcount ++;
 5951:     } else {
 5952:         my %titles = &ssl_titles();
 5953:         my (%by_ip,%by_location,@intdoms,@instdoms);
 5954:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5955:         my @alldoms = &Apache::lonnet::all_domains();
 5956:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5957:         my @domservers = &Apache::lonnet::get_servers($dom);
 5958:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5959:         my %altids = &id_for_thisdom(%servers);
 5960:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 5961:             my $legacy;
 5962:             unless (ref($settings) eq 'HASH') {
 5963:                 my $name;
 5964:                 if ($position eq 'connto') {
 5965:                     $name = 'loncAllowInsecure';
 5966:                 } else {
 5967:                     $name = 'londAllowInsecure';
 5968:                 }
 5969:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 5970:                 my @ids=&Apache::lonnet::current_machine_ids();
 5971:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 5972:                     my %what = (
 5973:                                    $name => 1,
 5974:                                );
 5975:                     my ($result,$returnhash) =
 5976:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 5977:                     if ($result eq 'ok') {
 5978:                         if (ref($returnhash) eq 'HASH') {
 5979:                             $legacy = $returnhash->{$name};
 5980:                         }
 5981:                     }
 5982:                 } else {
 5983:                     $legacy = $Apache::lonnet::perlvar{$name};
 5984:                 }
 5985:             }
 5986:             foreach my $type ('dom','intdom','other') {
 5987:                 my %checked;
 5988:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5989:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 5990:                               '<td class="LC_right_item">';
 5991:                 my $skip; 
 5992:                 if ($type eq 'dom') {
 5993:                     unless (keys(%servers) > 1) {
 5994:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 5995:                         $skip = 1;
 5996:                     }
 5997:                 }
 5998:                 if ($type eq 'intdom') {
 5999:                     unless (@instdoms > 1) {
 6000:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 6001:                         $skip = 1;
 6002:                     } 
 6003:                 } elsif ($type eq 'other') {
 6004:                     if (keys(%by_location) == 0) {
 6005:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 6006:                         $skip = 1;
 6007:                     }
 6008:                 }
 6009:                 unless ($skip) {
 6010:                     $checked{'yes'} = ' checked="checked"'; 
 6011:                     if (ref($settings) eq 'HASH') {
 6012:                         if (ref($settings->{$position}) eq 'HASH') {
 6013:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 6014:                                 $checked{$1} = $checked{'yes'};
 6015:                                 delete($checked{'yes'}); 
 6016:                             }
 6017:                         }
 6018:                     } else {
 6019:                         if ($legacy == 0) {
 6020:                             $checked{'req'} = $checked{'yes'};
 6021:                             delete($checked{'yes'});    
 6022:                         }
 6023:                     }
 6024:                     foreach my $option ('no','yes','req') {
 6025:                         $datatable .= '<span class="LC_nobreak"><label>'.
 6026:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 6027:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 6028:                                       '</label></span>'.('&nbsp;'x2);
 6029:                     }
 6030:                 }
 6031:                 $datatable .= '</td></tr>';
 6032:                 $itemcount ++; 
 6033:             }
 6034:         } else {
 6035:             my $prefix = 'replication';
 6036:             my @types = ('certreq','nocertreq');
 6037:             if (keys(%by_location) == 0) {
 6038:                 $datatable .= '<tr'.$css_class.'><td>'.
 6039:                               &mt('Nothing to set here, as there are no other institutions').
 6040:                               '</td></tr>';
 6041:                 $itemcount ++;
 6042:             } else {
 6043:                 ($datatable,$itemcount) = 
 6044:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6045:             }
 6046:         }
 6047:     }
 6048:     $$rowtotal += $itemcount;
 6049:     return $datatable;
 6050: }
 6051: 
 6052: sub ssl_titles {
 6053:     return &Apache::lonlocal::texthash (
 6054:                dom           => 'LON-CAPA servers/VMs from same domain',
 6055:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 6056:                other         => 'External LON-CAPA servers/VMs',
 6057:                connto        => 'Connections to other servers',
 6058:                connfrom      => 'Connections from other servers',
 6059:                replication   => 'Replicating content to other institutions',
 6060:                certreq       => 'Client certificate required, but specific domains exempt',
 6061:                nocertreq     => 'No client certificate required, except for specific domains',
 6062:                no            => 'SSL not used',
 6063:                yes           => 'SSL Optional (used if available)',
 6064:                req           => 'SSL Required',
 6065:     );
 6066: }
 6067: 
 6068: sub print_trust {
 6069:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 6070:     my ($css_class,$datatable,%checked,%choices);
 6071:     my (%by_ip,%by_location,@intdoms,@instdoms);
 6072:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6073:     my $itemcount = 1;
 6074:     my %titles = &trust_titles();
 6075:     my @types = ('exc','inc');
 6076:     if ($prefix eq 'top') {
 6077:         $prefix = 'content';
 6078:     } elsif ($prefix eq 'bottom') {
 6079:         $prefix = 'msg';
 6080:     }
 6081:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6082:     $$rowtotal += $itemcount;
 6083:     return $datatable;
 6084: }
 6085: 
 6086: sub trust_titles {
 6087:     return &Apache::lonlocal::texthash(
 6088:                content  => "Access to this domain's content by others",
 6089:                shared   => "Access to other domain's content by this domain",
 6090:                enroll   => "Enrollment in this domain's courses by others", 
 6091:                othcoau  => "Co-author roles in this domain for others",
 6092:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 6093:                domroles => "Domain roles in this domain assignable to others",
 6094:                catalog  => "Course Catalog for this domain displayed elsewhere",
 6095:                reqcrs   => "Requests for creation of courses in this domain by others",
 6096:                msg      => "Users in other domains can send messages to this domain",
 6097:                exc      => "Allow all, but exclude specific domains",
 6098:                inc      => "Deny all, but include specific domains",
 6099:            );
 6100: } 
 6101: 
 6102: sub build_location_hashes {
 6103:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 6104:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 6105:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 6106:     my %iphost = &Apache::lonnet::get_iphost();
 6107:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 6108:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 6109:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 6110:         foreach my $id (@{$iphost{$primary_ip}}) {
 6111:             my $intdom = &Apache::lonnet::internet_dom($id);
 6112:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 6113:                 push(@{$intdoms},$intdom);
 6114:             }
 6115:         }
 6116:     }
 6117:     foreach my $ip (keys(%iphost)) {
 6118:         if (ref($iphost{$ip}) eq 'ARRAY') {
 6119:             foreach my $id (@{$iphost{$ip}}) {
 6120:                 my $location = &Apache::lonnet::internet_dom($id);
 6121:                 if ($location) {
 6122:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 6123:                         my $dom = &Apache::lonnet::host_domain($id);
 6124:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 6125:                             push(@{$instdoms},$dom);
 6126:                         }
 6127:                         next;
 6128:                     }
 6129:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6130:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 6131:                             push(@{$by_ip->{$ip}},$location);
 6132:                         }
 6133:                     } else {
 6134:                         $by_ip->{$ip} = [$location];
 6135:                     }
 6136:                 }
 6137:             }
 6138:         }
 6139:     }
 6140:     foreach my $ip (sort(keys(%{$by_ip}))) {
 6141:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6142:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 6143:             my $first = $by_ip->{$ip}->[0];
 6144:             if (ref($by_location->{$first}) eq 'ARRAY') {
 6145:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 6146:                     push(@{$by_location->{$first}},$ip);
 6147:                 }
 6148:             } else {
 6149:                 $by_location->{$first} = [$ip];
 6150:             }
 6151:         }
 6152:     }
 6153:     return;
 6154: }
 6155: 
 6156: sub current_offloads_to {
 6157:     my ($dom,$settings,$servers) = @_;
 6158:     my (%spareid,%otherdomconfigs);
 6159:     if (ref($servers) eq 'HASH') {
 6160:         foreach my $lonhost (sort(keys(%{$servers}))) {
 6161:             my $gotspares;
 6162:             if (ref($settings) eq 'HASH') {
 6163:                 if (ref($settings->{'spares'}) eq 'HASH') {
 6164:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 6165:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 6166:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 6167:                         $gotspares = 1;
 6168:                     }
 6169:                 }
 6170:             }
 6171:             unless ($gotspares) {
 6172:                 my $gotspares;
 6173:                 my $serverhomeID =
 6174:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 6175:                 my $serverhomedom =
 6176:                     &Apache::lonnet::host_domain($serverhomeID);
 6177:                 if ($serverhomedom ne $dom) {
 6178:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 6179:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6180:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6181:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6182:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6183:                                 $gotspares = 1;
 6184:                             }
 6185:                         }
 6186:                     } else {
 6187:                         $otherdomconfigs{$serverhomedom} =
 6188:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 6189:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 6190:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6191:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6192:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 6193:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6194:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6195:                                         $gotspares = 1;
 6196:                                     }
 6197:                                 }
 6198:                             }
 6199:                         }
 6200:                     }
 6201:                 }
 6202:             }
 6203:             unless ($gotspares) {
 6204:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6205:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6206:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6207:                } else {
 6208:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 6209:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 6210:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6211:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6212:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6213:                     } else {
 6214:                         my %what = (
 6215:                              spareid => 1,
 6216:                         );
 6217:                         my ($result,$returnhash) = 
 6218:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 6219:                         if ($result eq 'ok') { 
 6220:                             if (ref($returnhash) eq 'HASH') {
 6221:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 6222:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 6223:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 6224:                                 }
 6225:                             }
 6226:                         }
 6227:                     }
 6228:                 }
 6229:             }
 6230:         }
 6231:     }
 6232:     return %spareid;
 6233: }
 6234: 
 6235: sub spares_row {
 6236:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 6237:     my $css_class;
 6238:     my $numinrow = 4;
 6239:     my $itemcount = 1;
 6240:     my $datatable;
 6241:     my %typetitles = &sparestype_titles();
 6242:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6243:         foreach my $server (sort(keys(%{$servers}))) {
 6244:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 6245:             my ($othercontrol,$serverdom);
 6246:             if ($serverhome ne $server) {
 6247:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 6248:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6249:             } else {
 6250:                 $serverdom = &Apache::lonnet::host_domain($server);
 6251:                 if ($serverdom ne $dom) {
 6252:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6253:                 }
 6254:             }
 6255:             next unless (ref($spareid->{$server}) eq 'HASH');
 6256:             my $checkednow;
 6257:             if (ref($curroffloadnow) eq 'HASH') {
 6258:                 if ($curroffloadnow->{$server}) {
 6259:                     $checkednow = ' checked="checked"';
 6260:                 }
 6261:             }
 6262:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6263:             $datatable .= '<tr'.$css_class.'>
 6264:                            <td rowspan="2">
 6265:                             <span class="LC_nobreak">'.
 6266:                           &mt('[_1] when busy, offloads to:'
 6267:                               ,'<b>'.$server.'</b>').'</span><br />'.
 6268:                           '<span class="LC_nobreak">'."\n".
 6269:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 6270:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 6271:                           "\n";
 6272:             my (%current,%canselect);
 6273:             my @choices = 
 6274:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 6275:             foreach my $type ('primary','default') {
 6276:                 if (ref($spareid->{$server}) eq 'HASH') {
 6277:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 6278:                         my @spares = @{$spareid->{$server}{$type}};
 6279:                         if (@spares > 0) {
 6280:                             if ($othercontrol) {
 6281:                                 $current{$type} = join(', ',@spares);
 6282:                             } else {
 6283:                                 $current{$type} .= '<table>';
 6284:                                 my $numspares = scalar(@spares);
 6285:                                 for (my $i=0;  $i<@spares; $i++) {
 6286:                                     my $rem = $i%($numinrow);
 6287:                                     if ($rem == 0) {
 6288:                                         if ($i > 0) {
 6289:                                             $current{$type} .= '</tr>';
 6290:                                         }
 6291:                                         $current{$type} .= '<tr>';
 6292:                                     }
 6293:                                     $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;'.
 6294:                                                        $spareid->{$server}{$type}[$i].
 6295:                                                        '</label></td>'."\n";
 6296:                                 }
 6297:                                 my $rem = @spares%($numinrow);
 6298:                                 my $colsleft = $numinrow - $rem;
 6299:                                 if ($colsleft > 1 ) {
 6300:                                     $current{$type} .= '<td colspan="'.$colsleft.
 6301:                                                        '" class="LC_left_item">'.
 6302:                                                        '&nbsp;</td>';
 6303:                                 } elsif ($colsleft == 1) {
 6304:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 6305:                                 }
 6306:                                 $current{$type} .= '</tr></table>';
 6307:                             }
 6308:                         }
 6309:                     }
 6310:                     if ($current{$type} eq '') {
 6311:                         $current{$type} = &mt('None specified');
 6312:                     }
 6313:                     if ($othercontrol) {
 6314:                         if ($type eq 'primary') {
 6315:                             $canselect{$type} = $othercontrol;
 6316:                         }
 6317:                     } else {
 6318:                         $canselect{$type} = 
 6319:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 6320:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 6321:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 6322:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 6323:                         if (@choices > 0) {
 6324:                             foreach my $lonhost (@choices) {
 6325:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 6326:                             }
 6327:                         }
 6328:                         $canselect{$type} .= '</select>'."\n";
 6329:                     }
 6330:                 } else {
 6331:                     $current{$type} = &mt('Could not be determined');
 6332:                     if ($type eq 'primary') {
 6333:                         $canselect{$type} =  $othercontrol;
 6334:                     }
 6335:                 }
 6336:                 if ($type eq 'default') {
 6337:                     $datatable .= '<tr'.$css_class.'>';
 6338:                 }
 6339:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 6340:                               '<td>'.$current{$type}.'</td>'."\n".
 6341:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 6342:             }
 6343:             $itemcount ++;
 6344:         }
 6345:     }
 6346:     $$rowtotal += $itemcount;
 6347:     return $datatable;
 6348: }
 6349: 
 6350: sub possible_newspares {
 6351:     my ($server,$currspares,$serverhomes,$altids) = @_;
 6352:     my $serverhostname = &Apache::lonnet::hostname($server);
 6353:     my %excluded;
 6354:     if ($serverhostname ne '') {
 6355:         %excluded = (
 6356:                        $serverhostname => 1,
 6357:                     );
 6358:     }
 6359:     if (ref($currspares) eq 'HASH') {
 6360:         foreach my $type (keys(%{$currspares})) {
 6361:             if (ref($currspares->{$type}) eq 'ARRAY') {
 6362:                 if (@{$currspares->{$type}} > 0) {
 6363:                     foreach my $curr (@{$currspares->{$type}}) {
 6364:                         my $hostname = &Apache::lonnet::hostname($curr);
 6365:                         $excluded{$hostname} = 1;
 6366:                     }
 6367:                 }
 6368:             }
 6369:         }
 6370:     }
 6371:     my @choices;
 6372:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6373:         if (keys(%{$serverhomes}) > 1) {
 6374:             foreach my $name (sort(keys(%{$serverhomes}))) {
 6375:                 unless ($excluded{$name}) {
 6376:                     if (exists($altids->{$serverhomes->{$name}})) {
 6377:                         push(@choices,$altids->{$serverhomes->{$name}});
 6378:                     } else {
 6379:                         push(@choices,$serverhomes->{$name});
 6380:                     }
 6381:                 }
 6382:             }
 6383:         }
 6384:     }
 6385:     return sort(@choices);
 6386: }
 6387: 
 6388: sub print_loadbalancing {
 6389:     my ($dom,$settings,$rowtotal) = @_;
 6390:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6391:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6392:     my $numinrow = 1;
 6393:     my $datatable;
 6394:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6395:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 6396:     if (ref($settings) eq 'HASH') {
 6397:         %existing = %{$settings};
 6398:     }
 6399:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 6400:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 6401:                                   \%currtargets,\%currrules,\%currcookies);
 6402:     } else {
 6403:         return;
 6404:     }
 6405:     my ($othertitle,$usertypes,$types) =
 6406:         &Apache::loncommon::sorted_inst_types($dom);
 6407:     my $rownum = 8;
 6408:     if (ref($types) eq 'ARRAY') {
 6409:         $rownum += scalar(@{$types});
 6410:     }
 6411:     my @css_class = ('LC_odd_row','LC_even_row');
 6412:     my $balnum = 0;
 6413:     my $islast;
 6414:     my (@toshow,$disabledtext);
 6415:     if (keys(%currbalancer) > 0) {
 6416:         @toshow = sort(keys(%currbalancer));
 6417:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 6418:             push(@toshow,'');
 6419:         }
 6420:     } else {
 6421:         @toshow = ('');
 6422:         $disabledtext = &mt('No existing load balancer');
 6423:     }
 6424:     foreach my $lonhost (@toshow) {
 6425:         if ($balnum == scalar(@toshow)-1) {
 6426:             $islast = 1;
 6427:         } else {
 6428:             $islast = 0;
 6429:         }
 6430:         my $cssidx = $balnum%2;
 6431:         my $targets_div_style = 'display: none';
 6432:         my $disabled_div_style = 'display: block';
 6433:         my $homedom_div_style = 'display: none';
 6434:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 6435:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6436:                       '<p>';
 6437:         if ($lonhost eq '') {
 6438:             $datatable .= '<span class="LC_nobreak">';
 6439:             if (keys(%currbalancer) > 0) {
 6440:                 $datatable .= &mt('Add balancer:');
 6441:             } else {
 6442:                 $datatable .= &mt('Enable balancer:');
 6443:             }
 6444:             $datatable .= '&nbsp;'.
 6445:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 6446:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 6447:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 6448:                           '<option value="" selected="selected">'.&mt('None').
 6449:                           '</option>'."\n";
 6450:             foreach my $server (sort(keys(%servers))) {
 6451:                 next if ($currbalancer{$server});
 6452:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 6453:             }
 6454:             $datatable .=
 6455:                 '</select>'."\n".
 6456:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 6457:         } else {
 6458:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 6459:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 6460:                            &mt('Stop balancing').'</label>'.
 6461:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 6462:             $targets_div_style = 'display: block';
 6463:             $disabled_div_style = 'display: none';
 6464:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 6465:                 $homedom_div_style = 'display: block';
 6466:             }
 6467:         }
 6468:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6469:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 6470:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 6471:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 6472:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 6473:         my @sparestypes = ('primary','default');
 6474:         my %typetitles = &sparestype_titles();
 6475:         my %hostherechecked = (
 6476:                                   no => ' checked="checked"',
 6477:                               );
 6478:         my %balcookiechecked = (
 6479:                                   no => ' checked="checked"', 
 6480:                                );
 6481:         foreach my $sparetype (@sparestypes) {
 6482:             my $targettable;
 6483:             for (my $i=0; $i<$numspares; $i++) {
 6484:                 my $checked;
 6485:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 6486:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6487:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6488:                             $checked = ' checked="checked"';
 6489:                         }
 6490:                     }
 6491:                 }
 6492:                 my ($chkboxval,$disabled);
 6493:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 6494:                     $chkboxval = $spares[$i];
 6495:                 }
 6496:                 if (exists($currbalancer{$spares[$i]})) {
 6497:                     $disabled = ' disabled="disabled"';
 6498:                 }
 6499:                 $targettable .=
 6500:                     '<td><span class="LC_nobreak"><label>'.
 6501:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 6502:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 6503:                     '</span></label></span></td>';
 6504:                 my $rem = $i%($numinrow);
 6505:                 if ($rem == 0) {
 6506:                     if (($i > 0) && ($i < $numspares-1)) {
 6507:                         $targettable .= '</tr>';
 6508:                     }
 6509:                     if ($i < $numspares-1) {
 6510:                         $targettable .= '<tr>';
 6511:                     }
 6512:                 }
 6513:             }
 6514:             if ($targettable ne '') {
 6515:                 my $rem = $numspares%($numinrow);
 6516:                 my $colsleft = $numinrow - $rem;
 6517:                 if ($colsleft > 1 ) {
 6518:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6519:                                     '&nbsp;</td>';
 6520:                 } elsif ($colsleft == 1) {
 6521:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 6522:                 }
 6523:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 6524:                                '<table><tr>'.$targettable.'</tr></table><br />';
 6525:             }
 6526:             $hostherechecked{$sparetype} = '';
 6527:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 6528:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6529:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6530:                         $hostherechecked{$sparetype} = ' checked="checked"';
 6531:                         $hostherechecked{'no'} = '';
 6532:                     }
 6533:                 }
 6534:             }
 6535:         }
 6536:         if ($currcookies{$lonhost}) {
 6537:             %balcookiechecked = (
 6538:                                     yes => ' checked="checked"',
 6539:                                 );
 6540:         }
 6541:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 6542:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 6543:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 6544:         foreach my $sparetype (@sparestypes) {
 6545:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 6546:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 6547:                           '</i></label><br />';
 6548:         }
 6549:         $datatable .= &mt('Use balancer cookie').'<br />'.
 6550:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 6551:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 6552:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 6553:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 6554:                       '</div></td></tr>'.
 6555:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 6556:                                            $othertitle,$usertypes,$types,\%servers,
 6557:                                            \%currbalancer,$lonhost,
 6558:                                            $targets_div_style,$homedom_div_style,
 6559:                                            $css_class[$cssidx],$balnum,$islast);
 6560:         $$rowtotal += $rownum;
 6561:         $balnum ++;
 6562:     }
 6563:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 6564:     return $datatable;
 6565: }
 6566: 
 6567: sub get_loadbalancers_config {
 6568:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 6569:     return unless ((ref($servers) eq 'HASH') &&
 6570:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 6571:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 6572:                    (ref($currcookies) eq 'HASH'));
 6573:     if (keys(%{$existing}) > 0) {
 6574:         my $oldlonhost;
 6575:         foreach my $key (sort(keys(%{$existing}))) {
 6576:             if ($key eq 'lonhost') {
 6577:                 $oldlonhost = $existing->{'lonhost'};
 6578:                 $currbalancer->{$oldlonhost} = 1;
 6579:             } elsif ($key eq 'targets') {
 6580:                 if ($oldlonhost) {
 6581:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 6582:                 }
 6583:             } elsif ($key eq 'rules') {
 6584:                 if ($oldlonhost) {
 6585:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 6586:                 }
 6587:             } elsif (ref($existing->{$key}) eq 'HASH') {
 6588:                 $currbalancer->{$key} = 1;
 6589:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 6590:                 $currrules->{$key} = $existing->{$key}{'rules'};
 6591:                 if ($existing->{$key}{'cookie'}) {
 6592:                     $currcookies->{$key} = 1;
 6593:                 }
 6594:             }
 6595:         }
 6596:     } else {
 6597:         my ($balancerref,$targetsref) =
 6598:                 &Apache::lonnet::get_lonbalancer_config($servers);
 6599:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 6600:             foreach my $server (sort(keys(%{$balancerref}))) {
 6601:                 $currbalancer->{$server} = 1;
 6602:                 $currtargets->{$server} = $targetsref->{$server};
 6603:             }
 6604:         }
 6605:     }
 6606:     return;
 6607: }
 6608: 
 6609: sub loadbalancing_rules {
 6610:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 6611:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 6612:         $css_class,$balnum,$islast) = @_;
 6613:     my $output;
 6614:     my $num = 0;
 6615:     my ($alltypes,$othertypes,$titles) =
 6616:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 6617:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 6618:         foreach my $type (@{$alltypes}) {
 6619:             $num ++;
 6620:             my $current;
 6621:             if (ref($currrules) eq 'HASH') {
 6622:                 $current = $currrules->{$type};
 6623:             }
 6624:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6625:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 6626:                     $current = '';
 6627:                 }
 6628:             }
 6629:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 6630:                                              $servers,$currbalancer,$lonhost,$dom,
 6631:                                              $targets_div_style,$homedom_div_style,
 6632:                                              $css_class,$balnum,$num,$islast);
 6633:         }
 6634:     }
 6635:     return $output;
 6636: }
 6637: 
 6638: sub loadbalancing_titles {
 6639:     my ($dom,$intdom,$usertypes,$types) = @_;
 6640:     my %othertypes = (
 6641:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 6642:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 6643:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 6644:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 6645:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 6646:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 6647:                      );
 6648:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 6649:     my @available;
 6650:     if (ref($types) eq 'ARRAY') {
 6651:         @available = @{$types};
 6652:     }
 6653:     unless (grep(/^default$/,@available)) {
 6654:         push(@available,'default');
 6655:     }
 6656:     unshift(@alltypes,@available);
 6657:     my %titles;
 6658:     foreach my $type (@alltypes) {
 6659:         if ($type =~ /^_LC_/) {
 6660:             $titles{$type} = $othertypes{$type};
 6661:         } elsif ($type eq 'default') {
 6662:             $titles{$type} = &mt('All users from [_1]',$dom);
 6663:             if (ref($types) eq 'ARRAY') {
 6664:                 if (@{$types} > 0) {
 6665:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 6666:                 }
 6667:             }
 6668:         } elsif (ref($usertypes) eq 'HASH') {
 6669:             $titles{$type} = $usertypes->{$type};
 6670:         }
 6671:     }
 6672:     return (\@alltypes,\%othertypes,\%titles);
 6673: }
 6674: 
 6675: sub loadbalance_rule_row {
 6676:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 6677:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 6678:     my @rulenames;
 6679:     my %ruletitles = &offloadtype_text();
 6680:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 6681:         @rulenames = ('balancer','offloadedto','specific');
 6682:     } else {
 6683:         @rulenames = ('default','homeserver');
 6684:         if ($type eq '_LC_external') {
 6685:             push(@rulenames,'externalbalancer');
 6686:         } else {
 6687:             push(@rulenames,'specific');
 6688:         }
 6689:         push(@rulenames,'none');
 6690:     }
 6691:     my $style = $targets_div_style;
 6692:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6693:         $style = $homedom_div_style;
 6694:     }
 6695:     my $space;
 6696:     if ($islast && $num == 1) {
 6697:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 6698:     }
 6699:     my $output =
 6700:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 6701:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 6702:         '<td valaign="top">'.$space.
 6703:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 6704:     for (my $i=0; $i<@rulenames; $i++) {
 6705:         my $rule = $rulenames[$i];
 6706:         my ($checked,$extra);
 6707:         if ($rulenames[$i] eq 'default') {
 6708:             $rule = '';
 6709:         }
 6710:         if ($rulenames[$i] eq 'specific') {
 6711:             if (ref($servers) eq 'HASH') {
 6712:                 my $default;
 6713:                 if (($current ne '') && (exists($servers->{$current}))) {
 6714:                     $checked = ' checked="checked"';
 6715:                 }
 6716:                 unless ($checked) {
 6717:                     $default = ' selected="selected"';
 6718:                 }
 6719:                 $extra =
 6720:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6721:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6722:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 6723:                     '<option value=""'.$default.'></option>'."\n";
 6724:                 foreach my $server (sort(keys(%{$servers}))) {
 6725:                     if (ref($currbalancer) eq 'HASH') {
 6726:                         next if (exists($currbalancer->{$server}));
 6727:                     }
 6728:                     my $selected;
 6729:                     if ($server eq $current) {
 6730:                         $selected = ' selected="selected"';
 6731:                     }
 6732:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 6733:                 }
 6734:                 $extra .= '</select>';
 6735:             }
 6736:         } elsif ($rule eq $current) {
 6737:             $checked = ' checked="checked"';
 6738:         }
 6739:         $output .= '<span class="LC_nobreak"><label>'.
 6740:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 6741:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 6742:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 6743:                    ')"'.$checked.' />&nbsp;';
 6744:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 6745:             $output .= $ruletitles{'particular'};
 6746:         } else {
 6747:             $output .= $ruletitles{$rulenames[$i]};
 6748:         }
 6749:         $output .= '</label>'.$extra.'</span><br />'."\n";
 6750:     }
 6751:     $output .= '</div></td></tr>'."\n";
 6752:     return $output;
 6753: }
 6754: 
 6755: sub offloadtype_text {
 6756:     my %ruletitles = &Apache::lonlocal::texthash (
 6757:            'default'          => 'Offloads to default destinations',
 6758:            'homeserver'       => "Offloads to user's home server",
 6759:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 6760:            'specific'         => 'Offloads to specific server',
 6761:            'none'             => 'No offload',
 6762:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 6763:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 6764:            'particular'       => 'Session hosted (after re-auth) on server:',
 6765:     );
 6766:     return %ruletitles;
 6767: }
 6768: 
 6769: sub sparestype_titles {
 6770:     my %typestitles = &Apache::lonlocal::texthash (
 6771:                           'primary' => 'primary',
 6772:                           'default' => 'default',
 6773:                       );
 6774:     return %typestitles;
 6775: }
 6776: 
 6777: sub contact_titles {
 6778:     my %titles = &Apache::lonlocal::texthash (
 6779:                    'supportemail'    => 'Support E-mail address',
 6780:                    'adminemail'      => 'Default Server Admin E-mail address',
 6781:                    'errormail'       => 'Error reports to be e-mailed to',
 6782:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 6783:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 6784:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 6785:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 6786:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 6787:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 6788:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 6789:                    'errorthreshold'  => 'Error/warning threshold for status e-mail',
 6790:                    'errorsysmail'    => 'Error threshold for e-mail to core group',
 6791:                    'errorweights'    => 'Weights used to compute error count',
 6792:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 6793:                  );
 6794:     my %short_titles = &Apache::lonlocal::texthash (
 6795:                            adminemail   => 'Admin E-mail address',
 6796:                            supportemail => 'Support E-mail',
 6797:                        );   
 6798:     return (\%titles,\%short_titles);
 6799: }
 6800: 
 6801: sub helpform_fields {
 6802:     my %titles =  &Apache::lonlocal::texthash (
 6803:                        'username'   => 'Name',
 6804:                        'user'       => 'Username/domain',
 6805:                        'phone'      => 'Phone',
 6806:                        'cc'         => 'Cc e-mail',
 6807:                        'course'     => 'Course Details',
 6808:                        'section'    => 'Sections',
 6809:                        'screenshot' => 'File upload',
 6810:     );
 6811:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 6812:     my %possoptions = (
 6813:                         username     => ['yes','no','req'],
 6814:                         phone        => ['yes','no','req'],
 6815:                         user         => ['yes','no'],
 6816:                         cc           => ['yes','no'],
 6817:                         course       => ['yes','no'],
 6818:                         section      => ['yes','no'],
 6819:                         screenshot   => ['yes','no'],
 6820:                       );
 6821:     my %fieldoptions = &Apache::lonlocal::texthash (
 6822:                          'yes'  => 'Optional',
 6823:                          'req'  => 'Required',
 6824:                          'no'   => "Not shown",
 6825:     );
 6826:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 6827: }
 6828: 
 6829: sub tool_titles {
 6830:     my %titles = &Apache::lonlocal::texthash (
 6831:                      aboutme    => 'Personal web page',
 6832:                      blog       => 'Blog',
 6833:                      webdav     => 'WebDAV',
 6834:                      portfolio  => 'Portfolio',
 6835:                      official   => 'Official courses (with institutional codes)',
 6836:                      unofficial => 'Unofficial courses',
 6837:                      community  => 'Communities',
 6838:                      textbook   => 'Textbook courses',
 6839:                      placement  => 'Placement tests',
 6840:                  );
 6841:     return %titles;
 6842: }
 6843: 
 6844: sub courserequest_titles {
 6845:     my %titles = &Apache::lonlocal::texthash (
 6846:                                    official   => 'Official',
 6847:                                    unofficial => 'Unofficial',
 6848:                                    community  => 'Communities',
 6849:                                    textbook   => 'Textbook',
 6850:                                    placement  => 'Placement tests',
 6851:                                    lti        => 'LTI Provider',
 6852:                                    norequest  => 'Not allowed',
 6853:                                    approval   => 'Approval by DC',
 6854:                                    validate   => 'With validation',
 6855:                                    autolimit  => 'Numerical limit',
 6856:                                    unlimited  => '(blank for unlimited)',
 6857:                  );
 6858:     return %titles;
 6859: }
 6860: 
 6861: sub authorrequest_titles {
 6862:     my %titles = &Apache::lonlocal::texthash (
 6863:                                    norequest  => 'Not allowed',
 6864:                                    approval   => 'Approval by Dom. Coord.',
 6865:                                    automatic  => 'Automatic approval',
 6866:                  );
 6867:     return %titles;
 6868: }
 6869: 
 6870: sub courserequest_conditions {
 6871:     my %conditions = &Apache::lonlocal::texthash (
 6872:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6873:        validate   => '(Processing of request subject to institutional validation).',
 6874:                  );
 6875:     return %conditions;
 6876: }
 6877: 
 6878: 
 6879: sub print_usercreation {
 6880:     my ($position,$dom,$settings,$rowtotal) = @_;
 6881:     my $numinrow = 4;
 6882:     my $datatable;
 6883:     if ($position eq 'top') {
 6884:         $$rowtotal ++;
 6885:         my $rowcount = 0;
 6886:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6887:         if (ref($rules) eq 'HASH') {
 6888:             if (keys(%{$rules}) > 0) {
 6889:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6890:                                                 $ruleorder,$numinrow,$rowcount);
 6891:                 $$rowtotal ++;
 6892:                 $rowcount ++;
 6893:             }
 6894:         }
 6895:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6896:         if (ref($idrules) eq 'HASH') {
 6897:             if (keys(%{$idrules}) > 0) {
 6898:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6899:                                                 $idruleorder,$numinrow,$rowcount);
 6900:                 $$rowtotal ++;
 6901:                 $rowcount ++;
 6902:             }
 6903:         }
 6904:         if ($rowcount == 0) {
 6905:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6906:             $$rowtotal ++;
 6907:             $rowcount ++;
 6908:         }
 6909:     } elsif ($position eq 'middle') {
 6910:         my @creators = ('author','course','requestcrs');
 6911:         my ($rules,$ruleorder) =
 6912:             &Apache::lonnet::inst_userrules($dom,'username');
 6913:         my %lt = &usercreation_types();
 6914:         my %checked;
 6915:         if (ref($settings) eq 'HASH') {
 6916:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6917:                 foreach my $item (@creators) {
 6918:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6919:                 }
 6920:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6921:                 foreach my $item (@creators) {
 6922:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6923:                         $checked{$item} = 'none';
 6924:                     }
 6925:                 }
 6926:             }
 6927:         }
 6928:         my $rownum = 0;
 6929:         foreach my $item (@creators) {
 6930:             $rownum ++;
 6931:             if ($checked{$item} eq '') {
 6932:                 $checked{$item} = 'any';
 6933:             }
 6934:             my $css_class;
 6935:             if ($rownum%2) {
 6936:                 $css_class = '';
 6937:             } else {
 6938:                 $css_class = ' class="LC_odd_row" ';
 6939:             }
 6940:             $datatable .= '<tr'.$css_class.'>'.
 6941:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6942:                          '</span></td><td style="text-align: right">';
 6943:             my @options = ('any');
 6944:             if (ref($rules) eq 'HASH') {
 6945:                 if (keys(%{$rules}) > 0) {
 6946:                     push(@options,('official','unofficial'));
 6947:                 }
 6948:             }
 6949:             push(@options,'none');
 6950:             foreach my $option (@options) {
 6951:                 my $type = 'radio';
 6952:                 my $check = ' ';
 6953:                 if ($checked{$item} eq $option) {
 6954:                     $check = ' checked="checked" ';
 6955:                 } 
 6956:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6957:                               '<input type="'.$type.'" name="can_createuser_'.
 6958:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6959:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6960:             }
 6961:             $datatable .= '</td></tr>';
 6962:         }
 6963:     } else {
 6964:         my @contexts = ('author','course','domain');
 6965:         my @authtypes = ('int','krb4','krb5','loc','lti');
 6966:         my %checked;
 6967:         if (ref($settings) eq 'HASH') {
 6968:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6969:                 foreach my $item (@contexts) {
 6970:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6971:                         foreach my $auth (@authtypes) {
 6972:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6973:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6974:                             }
 6975:                         }
 6976:                     }
 6977:                 }
 6978:             }
 6979:         } else {
 6980:             foreach my $item (@contexts) {
 6981:                 foreach my $auth (@authtypes) {
 6982:                     $checked{$item}{$auth} = ' checked="checked" ';
 6983:                 }
 6984:             }
 6985:         }
 6986:         my %title = &context_names();
 6987:         my %authname = &authtype_names();
 6988:         my $rownum = 0;
 6989:         my $css_class; 
 6990:         foreach my $item (@contexts) {
 6991:             if ($rownum%2) {
 6992:                 $css_class = '';
 6993:             } else {
 6994:                 $css_class = ' class="LC_odd_row" ';
 6995:             }
 6996:             $datatable .=   '<tr'.$css_class.'>'.
 6997:                             '<td>'.$title{$item}.
 6998:                             '</td><td class="LC_left_item">'.
 6999:                             '<span class="LC_nobreak">';
 7000:             foreach my $auth (@authtypes) {
 7001:                 $datatable .= '<label>'. 
 7002:                               '<input type="checkbox" name="'.$item.'_auth" '.
 7003:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 7004:                               $authname{$auth}.'</label>&nbsp;';
 7005:             }
 7006:             $datatable .= '</span></td></tr>';
 7007:             $rownum ++;
 7008:         }
 7009:         $$rowtotal += $rownum;
 7010:     }
 7011:     return $datatable;
 7012: }
 7013: 
 7014: sub print_selfcreation {
 7015:     my ($position,$dom,$settings,$rowtotal) = @_;
 7016:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 7017:         $emaildomain,$datatable);
 7018:     if (ref($settings) eq 'HASH') {
 7019:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 7020:             $createsettings = $settings->{'cancreate'};
 7021:             if (ref($createsettings) eq 'HASH') {
 7022:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 7023:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 7024:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 7025:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 7026:                         @selfcreate = ('email','login','sso');
 7027:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 7028:                         @selfcreate = ($createsettings->{'selfcreate'});
 7029:                     }
 7030:                 }
 7031:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 7032:                     $processing = $createsettings->{'selfcreateprocessing'};
 7033:                 }
 7034:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 7035:                     $emailoptions = $createsettings->{'emailoptions'};
 7036:                 }
 7037:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 7038:                     $emailverified = $createsettings->{'emailverified'};
 7039:                 }
 7040:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 7041:                     $emaildomain = $createsettings->{'emaildomain'};
 7042:                 }
 7043:             }
 7044:         }
 7045:     }
 7046:     my %radiohash;
 7047:     my $numinrow = 4;
 7048:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 7049:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7050:     if ($position eq 'top') {
 7051:         my %choices = &Apache::lonlocal::texthash (
 7052:                                                       cancreate_login      => 'Institutional Login',
 7053:                                                       cancreate_sso        => 'Institutional Single Sign On',
 7054:                                                   );
 7055:         my @toggles = sort(keys(%choices));
 7056:         my %defaultchecked = (
 7057:                                'cancreate_login' => 'off',
 7058:                                'cancreate_sso'   => 'off',
 7059:                              );
 7060:         my ($onclick,$itemcount);
 7061:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7062:                                                      \%choices,$itemcount,$onclick);
 7063:         $$rowtotal += $itemcount;
 7064:         
 7065:         if (ref($usertypes) eq 'HASH') {
 7066:             if (keys(%{$usertypes}) > 0) {
 7067:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 7068:                                              $dom,$numinrow,$othertitle,
 7069:                                              'statustocreate',$rowtotal);
 7070:                 $$rowtotal ++;
 7071:             }
 7072:         }
 7073:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 7074:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7075:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 7076:         my $rem;
 7077:         my $numperrow = 2;
 7078:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 7079:         $datatable .= '<tr'.$css_class.'>'.
 7080:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 7081:                      '<td class="LC_left_item">'."\n".
 7082:                      '<table>'."\n";
 7083:         for (my $i=0; $i<@fields; $i++) {
 7084:             $rem = $i%($numperrow);
 7085:             if ($rem == 0) {
 7086:                 if ($i > 0) {
 7087:                     $datatable .= '</tr>';
 7088:                 }
 7089:                 $datatable .= '<tr>';
 7090:             }
 7091:             my $currval;
 7092:             if (ref($createsettings) eq 'HASH') {
 7093:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 7094:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 7095:                 }
 7096:             }
 7097:             $datatable .= '<td class="LC_left_item">'.
 7098:                           '<span class="LC_nobreak">'.
 7099:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 7100:                           'value="'.$currval.'" size="10" />&nbsp;'.
 7101:                           $fieldtitles{$fields[$i]}.'</span></td>';
 7102:         }
 7103:         my $colsleft = $numperrow - $rem;
 7104:         if ($colsleft > 1 ) {
 7105:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7106:                          '&nbsp;</td>';
 7107:         } elsif ($colsleft == 1) {
 7108:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7109:         }
 7110:         $datatable .= '</tr></table></td></tr>';
 7111:         $$rowtotal ++;
 7112:     } elsif ($position eq 'middle') {
 7113:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 7114:         my @posstypes;
 7115:         if (ref($types) eq 'ARRAY') {
 7116:             @posstypes = @{$types};
 7117:         }
 7118:         unless (grep(/^default$/,@posstypes)) {
 7119:             push(@posstypes,'default');
 7120:         }
 7121:         my %usertypeshash;
 7122:         if (ref($usertypes) eq 'HASH') {
 7123:             %usertypeshash = %{$usertypes};
 7124:         }
 7125:         $usertypeshash{'default'} = $othertitle;
 7126:         foreach my $status (@posstypes) {
 7127:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 7128:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 7129:             $$rowtotal ++;
 7130:         }
 7131:     } else {
 7132:         my %choices = &Apache::lonlocal::texthash (
 7133:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 7134:                                                   );
 7135:         my @toggles = sort(keys(%choices));
 7136:         my %defaultchecked = (
 7137:                                'cancreate_email' => 'off',
 7138:                              );
 7139:         my $customclass = 'LC_selfcreate_email';
 7140:         my $classprefix = 'LC_canmodify_emailusername_';
 7141:         my $optionsprefix = 'LC_options_emailusername_';
 7142:         my $display = 'none';
 7143:         my $rowstyle = 'display:none';
 7144:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 7145:             $display = 'block';
 7146:             $rowstyle = 'display:table-row';
 7147:         }
 7148:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 7149:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7150:                                                      \%choices,$$rowtotal,$onclick);
 7151:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 7152:                                          $rowstyle);
 7153:         $$rowtotal ++;
 7154:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 7155:                                       $rowstyle);
 7156:         $$rowtotal ++;
 7157:         my (@ordered,@posstypes,%usertypeshash);
 7158:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7159:         my ($emailrules,$emailruleorder) =
 7160:             &Apache::lonnet::inst_userrules($dom,'email');
 7161:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7162:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7163:         if (ref($types) eq 'ARRAY') {
 7164:             @posstypes = @{$types};
 7165:         }
 7166:         if (@posstypes) {
 7167:             unless (grep(/^default$/,@posstypes)) {
 7168:                 push(@posstypes,'default');
 7169:             }
 7170:             if (ref($usertypes) eq 'HASH') {
 7171:                 %usertypeshash = %{$usertypes};
 7172:             }
 7173:             my $currassign;
 7174:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 7175:                 $currassign = {
 7176:                                   selfassign => $domdefaults{'inststatusguest'},
 7177:                               };
 7178:                 @ordered = @{$domdefaults{'inststatusguest'}};
 7179:             } else {
 7180:                 $currassign = { selfassign => [] };
 7181:             }
 7182:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 7183:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 7184:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 7185:                                          $numinrow,$othertitle,'selfassign',
 7186:                                          $rowtotal,$onclicktypes,$customclass,
 7187:                                          $rowstyle);
 7188:             $$rowtotal ++;
 7189:             $usertypeshash{'default'} = $othertitle;
 7190:             foreach my $status (@posstypes) {
 7191:                 my $css_class;
 7192:                 if ($$rowtotal%2) {
 7193:                     $css_class = 'LC_odd_row ';
 7194:                 }
 7195:                 $css_class .= $customclass;
 7196:                 my $rowid = $optionsprefix.$status;
 7197:                 my $hidden = 1;
 7198:                 my $currstyle = 'display:none';
 7199:                 if (grep(/^\Q$status\E$/,@ordered)) {
 7200:                     $currstyle = $rowstyle;
 7201:                     $hidden = 0; 
 7202:                 }
 7203:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7204:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 7205:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 7206:                 unless ($hidden) {
 7207:                     $$rowtotal ++;
 7208:                 }
 7209:             }
 7210:         } else {
 7211:             my $css_class;
 7212:             if ($$rowtotal%2) {
 7213:                 $css_class = 'LC_odd_row ';
 7214:             }
 7215:             $css_class .= $customclass;
 7216:             $usertypeshash{'default'} = $othertitle;
 7217:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7218:                                          $emailrules,$emailruleorder,$settings,'default','',
 7219:                                          $othertitle,$css_class,$rowstyle,$intdom);
 7220:             $$rowtotal ++;
 7221:         }
 7222:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7223:         $numinrow = 1;
 7224:         if (@posstypes) {
 7225:             foreach my $status (@posstypes) {
 7226:                 my $rowid = $classprefix.$status;
 7227:                 my $datarowstyle = 'display:none';
 7228:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 7229:                     $datarowstyle = $rowstyle; 
 7230:                 }
 7231:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 7232:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7233:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 7234:                 unless ($datarowstyle eq 'display:none') {
 7235:                     $$rowtotal ++;
 7236:                 }
 7237:             }
 7238:         } else {
 7239:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 7240:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7241:                                                    $infotitles,'',$customclass,$rowstyle);
 7242:         }
 7243:     }
 7244:     return $datatable;
 7245: }
 7246: 
 7247: sub selfcreate_javascript {
 7248:     return <<"ENDSCRIPT";
 7249: 
 7250: <script type="text/javascript">
 7251: // <![CDATA[
 7252: 
 7253: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 7254:     var x = document.getElementsByClassName(target);
 7255:     var insttypes = 0;
 7256:     var insttypeRegExp = new RegExp(prefix);
 7257:     if ((x.length != undefined) && (x.length > 0)) {
 7258:         if (form.elements[radio].length != undefined) {
 7259:             for (var i=0; i<form.elements[radio].length; i++) {
 7260:                 if (form.elements[radio][i].checked) {
 7261:                     if (form.elements[radio][i].value == 1) {
 7262:                         for (var j=0; j<x.length; j++) {
 7263:                             if (x[j].id == 'undefined') {
 7264:                                 x[j].style.display = 'table-row';
 7265:                             } else if (insttypeRegExp.test(x[j].id)) {
 7266:                                 insttypes ++;
 7267:                             } else {
 7268:                                 x[j].style.display = 'table-row';
 7269:                             }
 7270:                         }
 7271:                     } else {
 7272:                         for (var j=0; j<x.length; j++) {
 7273:                             x[j].style.display = 'none';
 7274:                         }
 7275:                     }
 7276:                     break;
 7277:                 }
 7278:             }
 7279:             if (insttypes > 0) {
 7280:                 toggleDataRow(form,checkbox,target,altprefix);
 7281:                 toggleDataRow(form,checkbox,target,prefix,1);
 7282:             }
 7283:         }
 7284:     }
 7285:     return;
 7286: }
 7287: 
 7288: function toggleDataRow(form,checkbox,target,prefix,docount) {
 7289:     if (form.elements[checkbox].length != undefined) {
 7290:         var count = 0;
 7291:         if (docount) {
 7292:             for (var i=0; i<form.elements[checkbox].length; i++) {
 7293:                 if (form.elements[checkbox][i].checked) {
 7294:                     count ++;
 7295:                 }
 7296:             }
 7297:         }
 7298:         for (var i=0; i<form.elements[checkbox].length; i++) {
 7299:             var type = form.elements[checkbox][i].value;
 7300:             if (document.getElementById(prefix+type)) {
 7301:                 if (form.elements[checkbox][i].checked) {
 7302:                     document.getElementById(prefix+type).style.display = 'table-row';
 7303:                     if (count % 2 == 1) {
 7304:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 7305:                     } else {
 7306:                         document.getElementById(prefix+type).className = target;
 7307:                     }
 7308:                     count ++;
 7309:                 } else {
 7310:                     document.getElementById(prefix+type).style.display = 'none';
 7311:                 }
 7312:             }
 7313:         }
 7314:     }
 7315:     return;
 7316: }
 7317: 
 7318: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 7319:     var caller = radio+'_'+status;
 7320:     if (form.elements[caller].length != undefined) {
 7321:         for (var i=0; i<form.elements[caller].length; i++) {
 7322:             if (form.elements[caller][i].checked) {
 7323:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 7324:                     var curr = form.elements[caller][i].value;
 7325:                     if (prefix) {
 7326:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 7327:                     }
 7328:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 7329:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 7330:                     if (curr == 'custom') {
 7331:                         if (prefix) { 
 7332:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 7333:                         }
 7334:                     } else if (curr == 'inst') {
 7335:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 7336:                     } else if (curr == 'noninst') {
 7337:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 7338:                     }
 7339:                     break;
 7340:                 }
 7341:             }
 7342:         }
 7343:     }
 7344: }
 7345: 
 7346: // ]]>
 7347: </script>
 7348: 
 7349: ENDSCRIPT
 7350: }
 7351: 
 7352: sub noninst_users {
 7353:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 7354:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 7355:     my $class = 'LC_left_item';
 7356:     if ($css_class) {
 7357:         $css_class = ' class="'.$css_class.'"'; 
 7358:     }
 7359:     if ($rowid) {
 7360:         $rowid = ' id="'.$rowid.'"';
 7361:     }
 7362:     if ($rowstyle) {
 7363:         $rowstyle = ' style="'.$rowstyle.'"';
 7364:     }
 7365:     my ($output,$description);
 7366:     if ($type eq 'default') {
 7367:         $description = &mt('Requests for: [_1]',$typetitle);
 7368:     } else {
 7369:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 7370:     }
 7371:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 7372:               "<td>$description</td>\n".           
 7373:               '<td class="'.$class.'" colspan="2">'.
 7374:               '<table><tr>';
 7375:     my %headers = &Apache::lonlocal::texthash( 
 7376:               approve  => 'Processing',
 7377:               email    => 'E-mail',
 7378:               username => 'Username',
 7379:     );
 7380:     foreach my $item ('approve','email','username') {
 7381:         $output .= '<th>'.$headers{$item}.'</th>';
 7382:     }
 7383:     $output .= '</tr><tr>';
 7384:     foreach my $item ('approve','email','username') {
 7385:         $output .= '<td style="vertical-align: top">';
 7386:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 7387:         if ($item eq 'approve') {
 7388:             %choices = &Apache::lonlocal::texthash (
 7389:                                                      automatic => 'Automatically approved',
 7390:                                                      approval  => 'Queued for approval',
 7391:                                                    );
 7392:             @options = ('automatic','approval');
 7393:             $hashref = $processing;
 7394:             $defoption = 'automatic';
 7395:             $name = 'cancreate_emailprocess_'.$type;
 7396:         } elsif ($item eq 'email') {
 7397:             %choices = &Apache::lonlocal::texthash (
 7398:                                                      any     => 'Any e-mail',
 7399:                                                      inst    => 'Institutional only',
 7400:                                                      noninst => 'Non-institutional only',
 7401:                                                      custom  => 'Custom restrictions',
 7402:                                                    );
 7403:             @options = ('any','inst','noninst');
 7404:             my $showcustom;
 7405:             if (ref($emailrules) eq 'HASH') {
 7406:                 if (keys(%{$emailrules}) > 0) {
 7407:                     push(@options,'custom');
 7408:                     $showcustom = 'cancreate_emailrule';
 7409:                     if (ref($settings) eq 'HASH') {
 7410:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 7411:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 7412:                                 if (exists($emailrules->{$rule})) {
 7413:                                     $hascustom ++;
 7414:                                 }
 7415:                             }
 7416:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 7417:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 7418:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 7419:                                     if (exists($emailrules->{$rule})) {
 7420:                                         $hascustom ++;
 7421:                                     }
 7422:                                 }
 7423:                             }
 7424:                         }
 7425:                     }
 7426:                 }
 7427:             }
 7428:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 7429:                                                      "'cancreate_emaildomain','$type'".');"';
 7430:             $hashref = $emailoptions;
 7431:             $defoption = 'any';
 7432:             $name = 'cancreate_emailoptions_'.$type;
 7433:         } elsif ($item eq 'username') {
 7434:             %choices = &Apache::lonlocal::texthash (
 7435:                                                      all    => 'Same as e-mail',
 7436:                                                      first  => 'Omit @domain',
 7437:                                                      free   => 'Free to choose',
 7438:                                                    );
 7439:             @options = ('all','first','free');
 7440:             $hashref = $emailverified;
 7441:             $defoption = 'all';
 7442:             $name = 'cancreate_usernameoptions_'.$type;
 7443:         }
 7444:         foreach my $option (@options) {
 7445:             my $checked;
 7446:             if (ref($hashref) eq 'HASH') {
 7447:                 if ($type eq '') {
 7448:                     if (!exists($hashref->{'default'})) {
 7449:                         if ($option eq $defoption) {
 7450:                             $checked = ' checked="checked"';
 7451:                         }
 7452:                     } else {
 7453:                         if ($hashref->{'default'} eq $option) {
 7454:                             $checked = ' checked="checked"';
 7455:                         }
 7456:                     }
 7457:                 } else {
 7458:                     if (!exists($hashref->{$type})) {
 7459:                         if ($option eq $defoption) {
 7460:                             $checked = ' checked="checked"';
 7461:                         }
 7462:                     } else {
 7463:                         if ($hashref->{$type} eq $option) {
 7464:                             $checked = ' checked="checked"';
 7465:                         }
 7466:                     }
 7467:                 }
 7468:             } elsif (($item eq 'email') && ($hascustom)) {
 7469:                 if ($option eq 'custom') {
 7470:                     $checked = ' checked="checked"';
 7471:                 }
 7472:             } elsif ($option eq $defoption) {
 7473:                 $checked = ' checked="checked"';
 7474:             }
 7475:             $output .= '<span class="LC_nobreak"><label>'.
 7476:                        '<input type="radio" name="'.$name.'"'.
 7477:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 7478:                        $choices{$option}.'</label></span><br />';
 7479:             if ($item eq 'email') {
 7480:                 if ($option eq 'custom') {
 7481:                     my $id = 'cancreate_emailrule_'.$type;
 7482:                     my $display = 'none';
 7483:                     if ($checked) {
 7484:                         $display = 'inline';
 7485:                     }
 7486:                     my $numinrow = 2;
 7487:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 7488:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 7489:                                &user_formats_row('email',$settings,$emailrules,
 7490:                                                  $emailruleorder,$numinrow,'',$type);
 7491:                               '</table></fieldset>';
 7492:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 7493:                     my %text = &Apache::lonlocal::texthash (
 7494:                                                              inst    => 'must end:',
 7495:                                                              noninst => 'cannot end:',
 7496:                                                            );
 7497:                     my $value;
 7498:                     if (ref($emaildomain) eq 'HASH') {
 7499:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 7500:                             $value = $emaildomain->{$type}->{$option}; 
 7501:                         }
 7502:                     }
 7503:                     if ($value eq '') {
 7504:                         $value = '@'.$intdom;
 7505:                     }
 7506:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 7507:                     my $display = 'none';
 7508:                     if ($checked) {
 7509:                         $display = 'inline';
 7510:                     }
 7511:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 7512:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 7513:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 7514:                                '</div>';
 7515:                 }
 7516:             }
 7517:         }
 7518:         $output .= '</td>'."\n";
 7519:     }
 7520:     $output .= "</tr></table></td></tr>\n";
 7521:     return $output;
 7522: }
 7523: 
 7524: sub captcha_choice {
 7525:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 7526:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 7527:         $vertext,$currver);
 7528:     my %lt = &captcha_phrases();
 7529:     $keyentry = 'hidden';
 7530:     if ($context eq 'cancreate') {
 7531:         $rowname = &mt('CAPTCHA validation');
 7532:     } elsif ($context eq 'login') {
 7533:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 7534:     }
 7535:     if (ref($settings) eq 'HASH') {
 7536:         if ($settings->{'captcha'}) {
 7537:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 7538:         } else {
 7539:             $checked{'original'} = ' checked="checked"';
 7540:         }
 7541:         if ($settings->{'captcha'} eq 'recaptcha') {
 7542:             $pubtext = $lt{'pub'};
 7543:             $privtext = $lt{'priv'};
 7544:             $keyentry = 'text';
 7545:             $vertext = $lt{'ver'};
 7546:             $currver = $settings->{'recaptchaversion'};
 7547:             if ($currver ne '2') {
 7548:                 $currver = 1;
 7549:             }
 7550:         }
 7551:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 7552:             $currpub = $settings->{'recaptchakeys'}{'public'};
 7553:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 7554:         }
 7555:     } else {
 7556:         $checked{'original'} = ' checked="checked"';
 7557:     }
 7558:     my $css_class;
 7559:     if ($itemcount%2) {
 7560:         $css_class = 'LC_odd_row';
 7561:     }
 7562:     if ($customcss) {
 7563:         $css_class .= " $customcss";
 7564:     }
 7565:     $css_class =~ s/^\s+//;
 7566:     if ($css_class) {
 7567:         $css_class = ' class="'.$css_class.'"';
 7568:     }
 7569:     if ($rowstyle) {
 7570:         $css_class .= ' style="'.$rowstyle.'"';
 7571:     }
 7572:     my $output = '<tr'.$css_class.'>'.
 7573:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 7574:                  '<table><tr><td>'."\n";
 7575:     foreach my $option ('original','recaptcha','notused') {
 7576:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 7577:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 7578:                    $lt{$option}.'</label></span>';
 7579:         unless ($option eq 'notused') {
 7580:             $output .= ('&nbsp;'x2)."\n";
 7581:         }
 7582:     }
 7583: #
 7584: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 7585: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 7586: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 7587: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 7588: #
 7589:     $output .= '</td></tr>'."\n".
 7590:                '<tr><td class="LC_zero_height">'."\n".
 7591:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 7592:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 7593:                $currpub.'" size="40" /></span><br />'."\n".
 7594:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 7595:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 7596:                $currpriv.'" size="40" /></span><br />'.
 7597:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 7598:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 7599:                $currver.'" size="3" /></span><br />'.
 7600:                '</td></tr></table>'."\n".
 7601:                '</td></tr>';
 7602:     return $output;
 7603: }
 7604: 
 7605: sub user_formats_row {
 7606:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 7607:     my $output;
 7608:     my %text = (
 7609:                    'username' => 'new usernames',
 7610:                    'id'       => 'IDs',
 7611:                );
 7612:     unless ($type eq 'email') {
 7613:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7614:         $output = '<tr '.$css_class.'>'.
 7615:                   '<td><span class="LC_nobreak">'.
 7616:                   &mt("Format rules to check for $text{$type}: ").
 7617:                   '</td><td class="LC_left_item" colspan="2"><table>';
 7618:     }
 7619:     my $rem;
 7620:     if (ref($ruleorder) eq 'ARRAY') {
 7621:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 7622:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 7623:                 my $rem = $i%($numinrow);
 7624:                 if ($rem == 0) {
 7625:                     if ($i > 0) {
 7626:                         $output .= '</tr>';
 7627:                     }
 7628:                     $output .= '<tr>';
 7629:                 }
 7630:                 my $check = ' ';
 7631:                 if (ref($settings) eq 'HASH') {
 7632:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 7633:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 7634:                             $check = ' checked="checked" ';
 7635:                         }
 7636:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 7637:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 7638:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 7639:                                 $check = ' checked="checked" ';
 7640:                             }
 7641:                         }
 7642:                     }
 7643:                 }
 7644:                 my $name = $type.'_rule';
 7645:                 if ($type eq 'email') {
 7646:                     $name .= '_'.$status;
 7647:                 }
 7648:                 $output .= '<td class="LC_left_item">'.
 7649:                            '<span class="LC_nobreak"><label>'.
 7650:                            '<input type="checkbox" name="'.$name.'" '.
 7651:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 7652:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 7653:             }
 7654:         }
 7655:         $rem = @{$ruleorder}%($numinrow);
 7656:     }
 7657:     my $colsleft;
 7658:     if ($rem) {
 7659:         $colsleft = $numinrow - $rem;
 7660:     }
 7661:     if ($colsleft > 1 ) {
 7662:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7663:                    '&nbsp;</td>';
 7664:     } elsif ($colsleft == 1) {
 7665:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7666:     }
 7667:     $output .= '</tr></table>';
 7668:     unless ($type eq 'email') {
 7669:         $output .= '</td></tr>';
 7670:     }
 7671:     return $output;
 7672: }
 7673: 
 7674: sub usercreation_types {
 7675:     my %lt = &Apache::lonlocal::texthash (
 7676:                     author     => 'When adding a co-author',
 7677:                     course     => 'When adding a user to a course',
 7678:                     requestcrs => 'When requesting a course',
 7679:                     any        => 'Any',
 7680:                     official   => 'Institutional only ',
 7681:                     unofficial => 'Non-institutional only',
 7682:                     none       => 'None',
 7683:     );
 7684:     return %lt;
 7685: }
 7686: 
 7687: sub selfcreation_types {
 7688:     my %lt = &Apache::lonlocal::texthash (
 7689:                     selfcreate => 'User creates own account',
 7690:                     any        => 'Any',
 7691:                     official   => 'Institutional only ',
 7692:                     unofficial => 'Non-institutional only',
 7693:                     email      => 'E-mail address',
 7694:                     login      => 'Institutional Login',
 7695:                     sso        => 'SSO',
 7696:              );
 7697: }
 7698: 
 7699: sub authtype_names {
 7700:     my %lt = &Apache::lonlocal::texthash(
 7701:                       int    => 'Internal',
 7702:                       krb4   => 'Kerberos 4',
 7703:                       krb5   => 'Kerberos 5',
 7704:                       loc    => 'Local',
 7705:                       lti    => 'LTI',
 7706:                   );
 7707:     return %lt;
 7708: }
 7709: 
 7710: sub context_names {
 7711:     my %context_title = &Apache::lonlocal::texthash(
 7712:        author => 'Creating users when an Author',
 7713:        course => 'Creating users when in a course',
 7714:        domain => 'Creating users when a Domain Coordinator',
 7715:     );
 7716:     return %context_title;
 7717: }
 7718: 
 7719: sub print_usermodification {
 7720:     my ($position,$dom,$settings,$rowtotal) = @_;
 7721:     my $numinrow = 4;
 7722:     my ($context,$datatable,$rowcount);
 7723:     if ($position eq 'top') {
 7724:         $rowcount = 0;
 7725:         $context = 'author'; 
 7726:         foreach my $role ('ca','aa') {
 7727:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7728:                                                    $numinrow,$rowcount);
 7729:             $$rowtotal ++;
 7730:             $rowcount ++;
 7731:         }
 7732:     } elsif ($position eq 'bottom') {
 7733:         $context = 'course';
 7734:         $rowcount = 0;
 7735:         foreach my $role ('st','ep','ta','in','cr') {
 7736:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7737:                                                    $numinrow,$rowcount);
 7738:             $$rowtotal ++;
 7739:             $rowcount ++;
 7740:         }
 7741:     }
 7742:     return $datatable;
 7743: }
 7744: 
 7745: sub print_defaults {
 7746:     my ($position,$dom,$settings,$rowtotal) = @_;
 7747:     my $rownum = 0;
 7748:     my ($datatable,$css_class,$titles);
 7749:     unless ($position eq 'bottom') {
 7750:         $titles = &defaults_titles($dom);
 7751:     }
 7752:     if ($position eq 'top') {
 7753:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 7754:                      'datelocale_def','portal_def');
 7755:         my %defaults;
 7756:         if (ref($settings) eq 'HASH') {
 7757:             %defaults = %{$settings};
 7758:         } else {
 7759:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7760:             foreach my $item (@items) {
 7761:                 $defaults{$item} = $domdefaults{$item};
 7762:             }
 7763:         }
 7764:         foreach my $item (@items) {
 7765:             if ($rownum%2) {
 7766:                 $css_class = '';
 7767:             } else {
 7768:                 $css_class = ' class="LC_odd_row" ';
 7769:             }
 7770:             $datatable .= '<tr'.$css_class.'>'.
 7771:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7772:                           '</span></td><td class="LC_right_item" colspan="3">';
 7773:             if ($item eq 'auth_def') {
 7774:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 7775:                 my %shortauth = (
 7776:                                  internal => 'int',
 7777:                                  krb4 => 'krb4',
 7778:                                  krb5 => 'krb5',
 7779:                                  localauth  => 'loc',
 7780:                                  lti => 'lti',
 7781:                                 );
 7782:                 my %authnames = &authtype_names();
 7783:                 foreach my $auth (@authtypes) {
 7784:                     my $checked = ' ';
 7785:                     if ($defaults{$item} eq $auth) {
 7786:                         $checked = ' checked="checked" ';
 7787:                     }
 7788:                     $datatable .= '<label><input type="radio" name="'.$item.
 7789:                                   '" value="'.$auth.'"'.$checked.'/>'.
 7790:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 7791:                 }
 7792:             } elsif ($item eq 'timezone_def') {
 7793:                 my $includeempty = 1;
 7794:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 7795:             } elsif ($item eq 'datelocale_def') {
 7796:                 my $includeempty = 1;
 7797:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 7798:             } elsif ($item eq 'lang_def') {
 7799:                 my $includeempty = 1;
 7800:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 7801:             } else {
 7802:                 my $size;
 7803:                 if ($item eq 'portal_def') {
 7804:                     $size = ' size="25"';
 7805:                 }
 7806:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7807:                               $defaults{$item}.'"'.$size.' />';
 7808:             }
 7809:             $datatable .= '</td></tr>';
 7810:             $rownum ++;
 7811:         }
 7812:     } elsif ($position eq 'middle') {
 7813:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7814:         my %defaults;
 7815:         if (ref($settings) eq 'HASH') {
 7816:             %defaults = %{$settings};
 7817:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7818:                 $defaults{'intauth_cost'} = 10;
 7819:             }
 7820:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7821:                 $defaults{'intauth_check'} = 0;
 7822:             }
 7823:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7824:                 $defaults{'intauth_switch'} = 0;
 7825:             }
 7826:         } else {
 7827:             %defaults = (
 7828:                           'intauth_cost'   => 10,
 7829:                           'intauth_check'  => 0,
 7830:                           'intauth_switch' => 0,
 7831:                         );
 7832:         }
 7833:         foreach my $item (@items) {
 7834:             if ($rownum%2) {
 7835:                 $css_class = '';
 7836:             } else {
 7837:                 $css_class = ' class="LC_odd_row" ';
 7838:             }
 7839:             $datatable .= '<tr'.$css_class.'>'.
 7840:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7841:                           '</span></td><td class="LC_left_item" colspan="3">';
 7842:             if ($item eq 'intauth_switch') {
 7843:                 my @options = (0,1,2);
 7844:                 my %optiondesc = &Apache::lonlocal::texthash (
 7845:                                    0 => 'No',
 7846:                                    1 => 'Yes',
 7847:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7848:                                  );
 7849:                 $datatable .= '<table width="100%">';
 7850:                 foreach my $option (@options) {
 7851:                     my $checked = ' ';
 7852:                     if ($defaults{$item} eq $option) {
 7853:                         $checked = ' checked="checked"';
 7854:                     }
 7855:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7856:                                   '<label><input type="radio" name="'.$item.
 7857:                                   '" value="'.$option.'"'.$checked.' />'.
 7858:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7859:                 }
 7860:                 $datatable .= '</table>';
 7861:             } elsif ($item eq 'intauth_check') {
 7862:                 my @options = (0,1,2);
 7863:                 my %optiondesc = &Apache::lonlocal::texthash (
 7864:                                    0 => 'No',
 7865:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7866:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7867:                                  );
 7868:                 $datatable .= '<table width="100%">';
 7869:                 foreach my $option (@options) {
 7870:                     my $checked = ' ';
 7871:                     my $onclick;
 7872:                     if ($defaults{$item} eq $option) {
 7873:                         $checked = ' checked="checked"';
 7874:                     }
 7875:                     if ($option == 2) {
 7876:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7877:                     }
 7878:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7879:                                   '<label><input type="radio" name="'.$item.
 7880:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7881:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7882:                 }
 7883:                 $datatable .= '</table>';
 7884:             } else {
 7885:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7886:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 7887:             }
 7888:             $datatable .= '</td></tr>';
 7889:             $rownum ++;
 7890:         }
 7891:     } else {
 7892:         my %defaults;
 7893:         if (ref($settings) eq 'HASH') {
 7894:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7895:                 my $maxnum = @{$settings->{'inststatusorder'}};
 7896:                 for (my $i=0; $i<$maxnum; $i++) {
 7897:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 7898:                     my $item = $settings->{'inststatusorder'}->[$i];
 7899:                     my $title = $settings->{'inststatustypes'}->{$item};
 7900:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 7901:                     $datatable .= '<tr'.$css_class.'>'.
 7902:                                   '<td><span class="LC_nobreak">'.
 7903:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 7904:                     for (my $k=0; $k<=$maxnum; $k++) {
 7905:                         my $vpos = $k+1;
 7906:                         my $selstr;
 7907:                         if ($k == $i) {
 7908:                             $selstr = ' selected="selected" ';
 7909:                         }
 7910:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7911:                     }
 7912:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 7913:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 7914:                                   &mt('delete').'</span></td>'.
 7915:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 7916:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 7917:                                   '</span></td></tr>';
 7918:                 }
 7919:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 7920:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 7921:                 $datatable .= '<tr '.$css_class.'>'.
 7922:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 7923:                 for (my $k=0; $k<=$maxnum; $k++) {
 7924:                     my $vpos = $k+1;
 7925:                     my $selstr;
 7926:                     if ($k == $maxnum) {
 7927:                         $selstr = ' selected="selected" ';
 7928:                     }
 7929:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7930:                 }
 7931:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 7932:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 7933:                               '&nbsp;'.&mt('(new)').
 7934:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 7935:                               &mt('Name displayed:').
 7936:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 7937:                               '</tr>'."\n";
 7938:                 $rownum ++;
 7939:             }
 7940:         }
 7941:     }
 7942:     $$rowtotal += $rownum;
 7943:     return $datatable;
 7944: }
 7945: 
 7946: sub get_languages_hash {
 7947:     my %langchoices;
 7948:     foreach my $id (&Apache::loncommon::languageids()) {
 7949:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7950:         if ($code ne '') {
 7951:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7952:         }
 7953:     }
 7954:     return %langchoices;
 7955: }
 7956: 
 7957: sub defaults_titles {
 7958:     my ($dom) = @_;
 7959:     my %titles = &Apache::lonlocal::texthash (
 7960:                    'auth_def'      => 'Default authentication type',
 7961:                    'auth_arg_def'  => 'Default authentication argument',
 7962:                    'lang_def'      => 'Default language',
 7963:                    'timezone_def'  => 'Default timezone',
 7964:                    'datelocale_def' => 'Default locale for dates',
 7965:                    'portal_def'     => 'Portal/Default URL',
 7966:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7967:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7968:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7969:                  );
 7970:     if ($dom) {
 7971:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7972:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7973:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7974:         $protocol = 'http' if ($protocol ne 'https');
 7975:         if ($uint_dom) {
 7976:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7977:                                          $uint_dom);
 7978:         }
 7979:     }
 7980:     return (\%titles);
 7981: }
 7982: 
 7983: sub print_scantron {
 7984:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 7985:     if ($position eq 'top') {
 7986:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 7987:     } else {
 7988:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 7989:     }
 7990: }
 7991: 
 7992: sub scantron_javascript {
 7993:     return <<"ENDSCRIPT";
 7994: 
 7995: <script type="text/javascript">
 7996: // <![CDATA[
 7997: 
 7998: function toggleScantron(form) {
 7999:     if (document.getElementById('scantroncsv_cols')) {
 8000:         var csvfieldset = document.getElementById('scantroncsv_cols');
 8001:         if (document.getElementById('scantronconfcsv')) {
 8002:             var scantroncsv = document.getElementById('scantronconfcsv'); 
 8003:             if (scantroncsv.checked) {
 8004:                 csvfieldset.style.display = 'inline-block';
 8005:             } else {
 8006:                 csvfieldset.style.display = 'none';
 8007:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 8008:                 if (csvselects.length) {
 8009:                     for (var j=0; j<csvselects.length; j++) {
 8010:                         csvselects[j].selectedIndex = 0;
 8011:                     }
 8012:                 }
 8013:             }
 8014:         }
 8015:     }
 8016:     return;
 8017: }
 8018: // ]]>
 8019: </script>
 8020: 
 8021: ENDSCRIPT
 8022: 
 8023: }
 8024: 
 8025: sub print_scantronformat {
 8026:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 8027:     my $itemcount = 1;
 8028:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 8029:         %confhash);
 8030:     my $switchserver = &check_switchserver($dom,$confname);
 8031:     my %lt = &Apache::lonlocal::texthash (
 8032:                 default => 'Default bubblesheet format file error',
 8033:                 custom  => 'Custom bubblesheet format file error',
 8034:              );
 8035:     my %scantronfiles = (
 8036:         default => 'default.tab',
 8037:         custom => 'custom.tab',
 8038:     );
 8039:     foreach my $key (keys(%scantronfiles)) {
 8040:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 8041:                               .$scantronfiles{$key};
 8042:     }
 8043:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 8044:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 8045:         if (!$switchserver) {
 8046:             my $servadm = $r->dir_config('lonAdmEMail');
 8047:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 8048:             if ($configuserok eq 'ok') {
 8049:                 if ($author_ok eq 'ok') {
 8050:                     my %legacyfile = (
 8051:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 8052:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 8053:                     );
 8054:                     my %md5chk;
 8055:                     foreach my $type (keys(%legacyfile)) {
 8056:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 8057:                         chomp($md5chk{$type});
 8058:                     }
 8059:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 8060:                         foreach my $type (keys(%legacyfile)) {
 8061:                             ($scantronurls{$type},my $error) =
 8062:                                 &legacy_scantronformat($r,$dom,$confname,
 8063:                                                  $type,$legacyfile{$type},
 8064:                                                  $scantronurls{$type},
 8065:                                                  $scantronfiles{$type});
 8066:                             if ($error ne '') {
 8067:                                 $error{$type} = $error;
 8068:                             }
 8069:                         }
 8070:                         if (keys(%error) == 0) {
 8071:                             $is_custom = 1;
 8072:                             $confhash{'scantron'}{'scantronformat'} =
 8073:                                 $scantronurls{'custom'};
 8074:                             my $putresult =
 8075:                                 &Apache::lonnet::put_dom('configuration',
 8076:                                                          \%confhash,$dom);
 8077:                             if ($putresult ne 'ok') {
 8078:                                 $error{'custom'} =
 8079:                                     '<span class="LC_error">'.
 8080:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8081:                             }
 8082:                         }
 8083:                     } else {
 8084:                         ($scantronurls{'default'},my $error) =
 8085:                             &legacy_scantronformat($r,$dom,$confname,
 8086:                                           'default',$legacyfile{'default'},
 8087:                                           $scantronurls{'default'},
 8088:                                           $scantronfiles{'default'});
 8089:                         if ($error eq '') {
 8090:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 8091:                             my $putresult =
 8092:                                 &Apache::lonnet::put_dom('configuration',
 8093:                                                          \%confhash,$dom);
 8094:                             if ($putresult ne 'ok') {
 8095:                                 $error{'default'} =
 8096:                                     '<span class="LC_error">'.
 8097:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8098:                             }
 8099:                         } else {
 8100:                             $error{'default'} = $error;
 8101:                         }
 8102:                     }
 8103:                 }
 8104:             }
 8105:         } else {
 8106:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 8107:         }
 8108:     }
 8109:     if (ref($settings) eq 'HASH') {
 8110:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 8111:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 8112:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 8113:                 $scantronurl = '';
 8114:             } else {
 8115:                 $scantronurl = $settings->{'scantronformat'};
 8116:             }
 8117:             $is_custom = 1;
 8118:         } else {
 8119:             $scantronurl = $scantronurls{'default'};
 8120:         }
 8121:     } else {
 8122:         if ($is_custom) {
 8123:             $scantronurl = $scantronurls{'custom'};
 8124:         } else {
 8125:             $scantronurl = $scantronurls{'default'};
 8126:         }
 8127:     }
 8128:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8129:     $datatable .= '<tr'.$css_class.'>';
 8130:     if (!$is_custom) {
 8131:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 8132:                       '<span class="LC_nobreak">';
 8133:         if ($scantronurl) {
 8134:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 8135:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 8136:         } else {
 8137:             $datatable = &mt('File unavailable for display');
 8138:         }
 8139:         $datatable .= '</span></td>';
 8140:         if (keys(%error) == 0) { 
 8141:             $datatable .= '<td style="vertical-align: bottom">';
 8142:             if (!$switchserver) {
 8143:                 $datatable .= &mt('Upload:').'<br />';
 8144:             }
 8145:         } else {
 8146:             my $errorstr;
 8147:             foreach my $key (sort(keys(%error))) {
 8148:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8149:             }
 8150:             $datatable .= '<td>'.$errorstr;
 8151:         }
 8152:     } else {
 8153:         if (keys(%error) > 0) {
 8154:             my $errorstr;
 8155:             foreach my $key (sort(keys(%error))) {
 8156:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8157:             } 
 8158:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 8159:         } elsif ($scantronurl) {
 8160:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 8161:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8162:             $datatable .= '<td><span class="LC_nobreak">'.
 8163:                           $link.
 8164:                           '<label><input type="checkbox" name="scantronformat_del"'.
 8165:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 8166:                           '<td><span class="LC_nobreak">&nbsp;'.
 8167:                           &mt('Replace:').'</span><br />';
 8168:         }
 8169:     }
 8170:     if (keys(%error) == 0) {
 8171:         if ($switchserver) {
 8172:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 8173:         } else {
 8174:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8175:                          '<input type="file" name="scantronformat" /></span>';
 8176:         }
 8177:     }
 8178:     $datatable .= '</td></tr>';
 8179:     $$rowtotal ++;
 8180:     return $datatable;
 8181: }
 8182: 
 8183: sub legacy_scantronformat {
 8184:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 8185:     my ($url,$error);
 8186:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 8187:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 8188:         (my $result,$url) =
 8189:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 8190:                          '','',$newfile);
 8191:         if ($result ne 'ok') {
 8192:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 8193:         }
 8194:     }
 8195:     return ($url,$error);
 8196: }
 8197: 
 8198: sub print_scantronconfig {
 8199:     my ($dom,$settings,$rowtotal) = @_;
 8200:     my $itemcount = 2;
 8201:     my $is_checked = ' checked="checked"';
 8202:     my $currcsvsty = 'none';
 8203:     my ($datatable,%csvfields,%checked,%onclick);
 8204:     my @fields = &scantroncsv_fields();
 8205:     my %titles = &scantronconfig_titles();
 8206:     if (ref($settings) eq 'HASH') {
 8207:         if (ref($settings->{config}) eq 'HASH') {
 8208:             if ($settings->{config}->{dat}) {
 8209:                 $checked{'dat'} = $is_checked;
 8210:             }
 8211:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 8212:                 %csvfields = %{$settings->{config}->{csv}};
 8213:                 if (keys(%csvfields) > 0) {
 8214:                     $checked{'csv'} = $is_checked;
 8215:                     $currcsvsty = 'inline-block';
 8216:                 }
 8217:             }
 8218:         } else {
 8219:             $checked{'dat'} = $is_checked;
 8220:         }
 8221:     } else {
 8222:         $checked{'dat'} = $is_checked;
 8223:     }
 8224:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 8225:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8226:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 8227:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 8228:     foreach my $item ('dat','csv') {
 8229:         my $id;
 8230:         if ($item eq 'csv') {
 8231:             $id = 'id="scantronconfcsv" ';
 8232:         } 
 8233:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 8234:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 8235:         if ($item eq 'csv') {
 8236:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 8237:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 8238:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 8239:             foreach my $col (@fields) {
 8240:                 my $selnone;
 8241:                 if ($csvfields{$col} eq '') {
 8242:                     $selnone = ' selected="selected"';
 8243:                 }
 8244:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 8245:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 8246:                               '<option value=""'.$selnone.'></option>';
 8247:                 for (my $i=0; $i<20; $i++) {
 8248:                     my $shown = $i+1;
 8249:                     my $sel;
 8250:                     unless ($selnone) {
 8251:                         if (exists($csvfields{$col})) {
 8252:                             if ($csvfields{$col} == $i) {
 8253:                                 $sel = ' selected="selected"';
 8254:                             }
 8255:                         }
 8256:                     }
 8257:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 8258:                 }
 8259:                 $datatable .= '</select></td></tr>';
 8260:            }
 8261:            $datatable .= '</table></fieldset>';
 8262:            $itemcount ++;
 8263:         }
 8264:     }
 8265:     $datatable .= '</td></tr>';
 8266:     $$rowtotal ++;
 8267:     return $datatable;
 8268: }
 8269: 
 8270: sub scantronconfig_titles {
 8271:     return &Apache::lonlocal::texthash(
 8272:                                           dat => 'Standard format (.dat)',
 8273:                                           csv => 'Comma separated values (.csv)',
 8274:                                           CODE => 'CODE',
 8275:                                           ID   => 'Student ID',
 8276:                                           PaperID => 'Paper ID',
 8277:                                           FirstName => 'First Name',
 8278:                                           LastName => 'Last Name',
 8279:                                           FirstQuestion => 'First Question Response',
 8280:                                           Section => 'Section',
 8281:     );
 8282: }
 8283: 
 8284: sub scantroncsv_fields {
 8285:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 8286: }
 8287: 
 8288: sub print_coursecategories {
 8289:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 8290:     my $datatable;
 8291:     if ($position eq 'top') {
 8292:         my (%checked);
 8293:         my @catitems = ('unauth','auth');
 8294:         my @cattypes = ('std','domonly','codesrch','none');
 8295:         $checked{'unauth'} = 'std';
 8296:         $checked{'auth'} = 'std';
 8297:         if (ref($settings) eq 'HASH') {
 8298:             foreach my $type (@cattypes) {
 8299:                 if ($type eq $settings->{'unauth'}) {
 8300:                     $checked{'unauth'} = $type;
 8301:                 }
 8302:                 if ($type eq $settings->{'auth'}) {
 8303:                     $checked{'auth'} = $type;
 8304:                 }
 8305:             }
 8306:         }
 8307:         my %lt = &Apache::lonlocal::texthash (
 8308:                                                unauth   => 'Catalog type for unauthenticated users',
 8309:                                                auth     => 'Catalog type for authenticated users',
 8310:                                                none     => 'No catalog',
 8311:                                                std      => 'Standard catalog',
 8312:                                                domonly  => 'Domain-only catalog',
 8313:                                                codesrch => "Code search form",
 8314:                                              );
 8315:        my $itemcount = 0;
 8316:        foreach my $item (@catitems) {
 8317:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8318:            $datatable .= '<tr '.$css_class.'>'.
 8319:                          '<td>'.$lt{$item}.'</td>'.
 8320:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 8321:            foreach my $type (@cattypes) {
 8322:                my $ischecked;
 8323:                if ($checked{$item} eq $type) {
 8324:                    $ischecked=' checked="checked"';
 8325:                }
 8326:                $datatable .= '<label>'.
 8327:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 8328:                              ' />'.$lt{$type}.'</label>&nbsp;';
 8329:            }
 8330:            $datatable .= '</span></td></tr>';
 8331:            $itemcount ++;
 8332:         }
 8333:         $$rowtotal += $itemcount;
 8334:     } elsif ($position eq 'middle') {
 8335:         my $toggle_cats_crs = ' ';
 8336:         my $toggle_cats_dom = ' checked="checked" ';
 8337:         my $can_cat_crs = ' ';
 8338:         my $can_cat_dom = ' checked="checked" ';
 8339:         my $toggle_catscomm_comm = ' ';
 8340:         my $toggle_catscomm_dom = ' checked="checked" ';
 8341:         my $can_catcomm_comm = ' ';
 8342:         my $can_catcomm_dom = ' checked="checked" ';
 8343:         my $toggle_catsplace_place = ' ';
 8344:         my $toggle_catsplace_dom = ' checked="checked" ';
 8345:         my $can_catplace_place = ' ';
 8346:         my $can_catplace_dom = ' checked="checked" ';
 8347: 
 8348:         if (ref($settings) eq 'HASH') {
 8349:             if ($settings->{'togglecats'} eq 'crs') {
 8350:                 $toggle_cats_crs = $toggle_cats_dom;
 8351:                 $toggle_cats_dom = ' ';
 8352:             }
 8353:             if ($settings->{'categorize'} eq 'crs') {
 8354:                 $can_cat_crs = $can_cat_dom;
 8355:                 $can_cat_dom = ' ';
 8356:             }
 8357:             if ($settings->{'togglecatscomm'} eq 'comm') {
 8358:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 8359:                 $toggle_catscomm_dom = ' ';
 8360:             }
 8361:             if ($settings->{'categorizecomm'} eq 'comm') {
 8362:                 $can_catcomm_comm = $can_catcomm_dom;
 8363:                 $can_catcomm_dom = ' ';
 8364:             }
 8365:             if ($settings->{'togglecatsplace'} eq 'place') {
 8366:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 8367:                 $toggle_catsplace_dom = ' ';
 8368:             }
 8369:             if ($settings->{'categorizeplace'} eq 'place') {
 8370:                 $can_catplace_place = $can_catplace_dom;
 8371:                 $can_catplace_dom = ' ';
 8372:             }
 8373:         }
 8374:         my %title = &Apache::lonlocal::texthash (
 8375:                      togglecats      => 'Show/Hide a course in catalog',
 8376:                      togglecatscomm  => 'Show/Hide a community in catalog',
 8377:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 8378:                      categorize      => 'Assign a category to a course',
 8379:                      categorizecomm  => 'Assign a category to a community',
 8380:                      categorizeplace => 'Assign a category to a placement test',
 8381:                     );
 8382:         my %level = &Apache::lonlocal::texthash (
 8383:                      dom   => 'Set in Domain',
 8384:                      crs   => 'Set in Course',
 8385:                      comm  => 'Set in Community',
 8386:                      place => 'Set in Placement Test',
 8387:                     );
 8388:         $datatable = '<tr class="LC_odd_row">'.
 8389:                   '<td>'.$title{'togglecats'}.'</td>'.
 8390:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8391:                   '<input type="radio" name="togglecats"'.
 8392:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8393:                   '<label><input type="radio" name="togglecats"'.
 8394:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8395:                   '</tr><tr>'.
 8396:                   '<td>'.$title{'categorize'}.'</td>'.
 8397:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8398:                   '<label><input type="radio" name="categorize"'.
 8399:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8400:                   '<label><input type="radio" name="categorize"'.
 8401:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8402:                   '</tr><tr class="LC_odd_row">'.
 8403:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 8404:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8405:                   '<input type="radio" name="togglecatscomm"'.
 8406:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8407:                   '<label><input type="radio" name="togglecatscomm"'.
 8408:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8409:                   '</tr><tr>'.
 8410:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 8411:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8412:                   '<label><input type="radio" name="categorizecomm"'.
 8413:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8414:                   '<label><input type="radio" name="categorizecomm"'.
 8415:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8416:                   '</tr><tr class="LC_odd_row">'.
 8417:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 8418:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8419:                   '<input type="radio" name="togglecatsplace"'.
 8420:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8421:                   '<label><input type="radio" name="togglecatscomm"'.
 8422:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 8423:                   '</tr><tr>'.
 8424:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 8425:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8426:                   '<label><input type="radio" name="categorizeplace"'.
 8427:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8428:                   '<label><input type="radio" name="categorizeplace"'.
 8429:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 8430:                   '</tr>';
 8431:         $$rowtotal += 6;
 8432:     } else {
 8433:         my $css_class;
 8434:         my $itemcount = 1;
 8435:         my $cathash; 
 8436:         if (ref($settings) eq 'HASH') {
 8437:             $cathash = $settings->{'cats'};
 8438:         }
 8439:         if (ref($cathash) eq 'HASH') {
 8440:             my (@cats,@trails,%allitems,%idx,@jsarray);
 8441:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 8442:                                                    \%allitems,\%idx,\@jsarray);
 8443:             my $maxdepth = scalar(@cats);
 8444:             my $colattrib = '';
 8445:             if ($maxdepth > 2) {
 8446:                 $colattrib = ' colspan="2" ';
 8447:             }
 8448:             my @path;
 8449:             if (@cats > 0) {
 8450:                 if (ref($cats[0]) eq 'ARRAY') {
 8451:                     my $numtop = @{$cats[0]};
 8452:                     my $maxnum = $numtop;
 8453:                     my %default_names = (
 8454:                           instcode    => &mt('Official courses'),
 8455:                           communities => &mt('Communities'),
 8456:                           placement   => &mt('Placement Tests'),
 8457:                     );
 8458: 
 8459:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 8460:                         ($cathash->{'instcode::0'} eq '') ||
 8461:                         (!grep(/^communities$/,@{$cats[0]})) || 
 8462:                         ($cathash->{'communities::0'} eq '') ||
 8463:                         (!grep(/^placement$/,@{$cats[0]})) ||
 8464:                         ($cathash->{'placement::0'} eq '')) {
 8465:                         $maxnum ++;
 8466:                     }
 8467:                     my $lastidx;
 8468:                     for (my $i=0; $i<$numtop; $i++) {
 8469:                         my $parent = $cats[0][$i];
 8470:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8471:                         my $item = &escape($parent).'::0';
 8472:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 8473:                         $lastidx = $idx{$item};
 8474:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8475:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 8476:                         for (my $k=0; $k<=$maxnum; $k++) {
 8477:                             my $vpos = $k+1;
 8478:                             my $selstr;
 8479:                             if ($k == $i) {
 8480:                                 $selstr = ' selected="selected" ';
 8481:                             }
 8482:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8483:                         }
 8484:                         $datatable .= '</select></span></td><td>';
 8485:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 8486:                             $datatable .=  '<span class="LC_nobreak">'
 8487:                                            .$default_names{$parent}.'</span>';
 8488:                             if ($parent eq 'instcode') {
 8489:                                 $datatable .= '<br /><span class="LC_nobreak">('
 8490:                                               .&mt('with institutional codes')
 8491:                                               .')</span></td><td'.$colattrib.'>';
 8492:                             } else {
 8493:                                 $datatable .= '<table><tr><td>';
 8494:                             }
 8495:                             $datatable .= '<span class="LC_nobreak">'
 8496:                                           .'<label><input type="radio" name="'
 8497:                                           .$parent.'" value="1" checked="checked" />'
 8498:                                           .&mt('Display').'</label>';
 8499:                             if ($parent eq 'instcode') {
 8500:                                 $datatable .= '&nbsp;';
 8501:                             } else {
 8502:                                 $datatable .= '</span></td></tr><tr><td>'
 8503:                                               .'<span class="LC_nobreak">';
 8504:                             }
 8505:                             $datatable .= '<label><input type="radio" name="'
 8506:                                           .$parent.'" value="0" />'
 8507:                                           .&mt('Do not display').'</label></span>';
 8508:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 8509:                                 $datatable .= '</td></tr></table>';
 8510:                             }
 8511:                             $datatable .= '</td>';
 8512:                         } else {
 8513:                             $datatable .= $parent
 8514:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 8515:                                           .'<input type="checkbox" name="deletecategory" '
 8516:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 8517:                         }
 8518:                         my $depth = 1;
 8519:                         push(@path,$parent);
 8520:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 8521:                         pop(@path);
 8522:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 8523:                         $itemcount ++;
 8524:                     }
 8525:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8526:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 8527:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 8528:                     for (my $k=0; $k<=$maxnum; $k++) {
 8529:                         my $vpos = $k+1;
 8530:                         my $selstr;
 8531:                         if ($k == $numtop) {
 8532:                             $selstr = ' selected="selected" ';
 8533:                         }
 8534:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8535:                     }
 8536:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 8537:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 8538:                                   .'</tr>'."\n";
 8539:                     $itemcount ++;
 8540:                     foreach my $default ('instcode','communities','placement') {
 8541:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 8542:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8543:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 8544:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 8545:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 8546:                             for (my $k=0; $k<=$maxnum; $k++) {
 8547:                                 my $vpos = $k+1;
 8548:                                 my $selstr;
 8549:                                 if ($k == $maxnum) {
 8550:                                     $selstr = ' selected="selected" ';
 8551:                                 }
 8552:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8553:                             }
 8554:                             $datatable .= '</select></span></td>'.
 8555:                                           '<td><span class="LC_nobreak">'.
 8556:                                           $default_names{$default}.'</span>';
 8557:                             if ($default eq 'instcode') {
 8558:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 8559:                                               .&mt('with institutional codes').')</span>';
 8560:                             }
 8561:                             $datatable .= '</td>'
 8562:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 8563:                                           .&mt('Display').'</label>&nbsp;'
 8564:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 8565:                                           .&mt('Do not display').'</label></span></td></tr>';
 8566:                         }
 8567:                     }
 8568:                 }
 8569:             } else {
 8570:                 $datatable .= &initialize_categories($itemcount);
 8571:             }
 8572:         } else {
 8573:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 8574:                           .&initialize_categories($itemcount);
 8575:         }
 8576:         $$rowtotal += $itemcount;
 8577:     }
 8578:     return $datatable;
 8579: }
 8580: 
 8581: sub print_serverstatuses {
 8582:     my ($dom,$settings,$rowtotal) = @_;
 8583:     my $datatable;
 8584:     my @pages = &serverstatus_pages();
 8585:     my (%namedaccess,%machineaccess);
 8586:     foreach my $type (@pages) {
 8587:         $namedaccess{$type} = '';
 8588:         $machineaccess{$type}= '';
 8589:     }
 8590:     if (ref($settings) eq 'HASH') {
 8591:         foreach my $type (@pages) {
 8592:             if (exists($settings->{$type})) {
 8593:                 if (ref($settings->{$type}) eq 'HASH') {
 8594:                     foreach my $key (keys(%{$settings->{$type}})) {
 8595:                         if ($key eq 'namedusers') {
 8596:                             $namedaccess{$type} = $settings->{$type}->{$key};
 8597:                         } elsif ($key eq 'machines') {
 8598:                             $machineaccess{$type} = $settings->{$type}->{$key};
 8599:                         }
 8600:                     }
 8601:                 }
 8602:             }
 8603:         }
 8604:     }
 8605:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8606:     my $rownum = 0;
 8607:     my $css_class;
 8608:     foreach my $type (@pages) {
 8609:         $rownum ++;
 8610:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 8611:         $datatable .= '<tr'.$css_class.'>'.
 8612:                       '<td><span class="LC_nobreak">'.
 8613:                       $titles->{$type}.'</span></td>'.
 8614:                       '<td class="LC_left_item">'.
 8615:                       '<input type="text" name="'.$type.'_namedusers" '.
 8616:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 8617:                       '<td class="LC_right_item">'.
 8618:                       '<span class="LC_nobreak">'.
 8619:                       '<input type="text" name="'.$type.'_machines" '.
 8620:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 8621:                       '</span></td></tr>'."\n";
 8622:     }
 8623:     $$rowtotal += $rownum;
 8624:     return $datatable;
 8625: }
 8626: 
 8627: sub serverstatus_pages {
 8628:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 8629:             'checksums','clusterstatus','certstatus','metadata_keywords',
 8630:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 8631:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 8632: }
 8633: 
 8634: sub defaults_javascript {
 8635:     my ($settings) = @_;
 8636:     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.');
 8637:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 8638:     &js_escape(\$intauthcheck);
 8639:     &js_escape(\$intauthcost);
 8640:     my $intauthjs = <<"ENDSCRIPT";
 8641: 
 8642: function warnIntAuth(field) {
 8643:     if (field.name == 'intauth_check') {
 8644:         if (field.value == '2') {
 8645:             alert('$intauthcheck');
 8646:         }
 8647:     }
 8648:     if (field.name == 'intauth_cost') {
 8649:         field.value.replace(/\s/g,'');
 8650:         if (field.value != '') {
 8651:             var regexdigit=/^\\d+\$/;
 8652:             if (!regexdigit.test(field.value)) {
 8653:                 alert('$intauthcost');
 8654:             }
 8655:         }
 8656:     }
 8657:     return;
 8658: }
 8659: 
 8660: ENDSCRIPT
 8661: 
 8662:     if (ref($settings) ne 'HASH') {
 8663:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8664:     }
 8665:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 8666:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 8667:         if ($maxnum eq '') {
 8668:             $maxnum = 0;
 8669:         }
 8670:         $maxnum ++;
 8671:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 8672:         return <<"ENDSCRIPT";
 8673: <script type="text/javascript">
 8674: // <![CDATA[
 8675: function reorderTypes(form,caller) {
 8676:     var changedVal;
 8677: $jstext 
 8678:     var newpos = 'addinststatus_pos';
 8679:     var current = new Array;
 8680:     var maxh = $maxnum;
 8681:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8682:     var oldVal;
 8683:     if (caller == newpos) {
 8684:         changedVal = newitemVal;
 8685:     } else {
 8686:         var curritem = 'inststatus_pos_'+caller;
 8687:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 8688:         current[newitemVal] = newpos;
 8689:     }
 8690:     for (var i=0; i<inststatuses.length; i++) {
 8691:         if (inststatuses[i] != caller) {
 8692:             var elementName = 'inststatus_pos_'+inststatuses[i];
 8693:             if (form.elements[elementName]) {
 8694:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8695:                 current[currVal] = elementName;
 8696:             }
 8697:         }
 8698:     }
 8699:     for (var j=0; j<maxh; j++) {
 8700:         if (current[j] == undefined) {
 8701:             oldVal = j;
 8702:         }
 8703:     }
 8704:     if (oldVal < changedVal) {
 8705:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8706:            var elementName = current[k];
 8707:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8708:         }
 8709:     } else {
 8710:         for (var k=changedVal; k<oldVal; k++) {
 8711:             var elementName = current[k];
 8712:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8713:         }
 8714:     }
 8715:     return;
 8716: }
 8717: 
 8718: $intauthjs
 8719: 
 8720: // ]]>
 8721: </script>
 8722: 
 8723: ENDSCRIPT
 8724:     } else {
 8725:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8726:     }
 8727: }
 8728: 
 8729: sub coursecategories_javascript {
 8730:     my ($settings) = @_;
 8731:     my ($output,$jstext,$cathash);
 8732:     if (ref($settings) eq 'HASH') {
 8733:         $cathash = $settings->{'cats'};
 8734:     }
 8735:     if (ref($cathash) eq 'HASH') {
 8736:         my (@cats,@jsarray,%idx);
 8737:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 8738:         if (@jsarray > 0) {
 8739:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 8740:             for (my $i=0; $i<@jsarray; $i++) {
 8741:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 8742:                     my $catstr = join('","',@{$jsarray[$i]});
 8743:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 8744:                 }
 8745:             }
 8746:         }
 8747:     } else {
 8748:         $jstext  = '    var categories = Array(1);'."\n".
 8749:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 8750:     }
 8751:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 8752:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 8753:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 8754:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 8755:     &js_escape(\$instcode_reserved);
 8756:     &js_escape(\$communities_reserved);
 8757:     &js_escape(\$placement_reserved);
 8758:     &js_escape(\$choose_again);
 8759:     $output = <<"ENDSCRIPT";
 8760: <script type="text/javascript">
 8761: // <![CDATA[
 8762: function reorderCats(form,parent,item,idx) {
 8763:     var changedVal;
 8764: $jstext
 8765:     var newpos = 'addcategory_pos';
 8766:     if (parent == '') {
 8767:         var has_instcode = 0;
 8768:         var maxtop = categories[idx].length;
 8769:         for (var j=0; j<maxtop; j++) {
 8770:             if (categories[idx][j] == 'instcode::0') {
 8771:                 has_instcode == 1;
 8772:             }
 8773:         }
 8774:         if (has_instcode == 0) {
 8775:             categories[idx][maxtop] = 'instcode_pos';
 8776:         }
 8777:     } else {
 8778:         newpos += '_'+parent;
 8779:     }
 8780:     var maxh = 1 + categories[idx].length;
 8781:     var current = new Array;
 8782:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8783:     if (item == newpos) {
 8784:         changedVal = newitemVal;
 8785:     } else {
 8786:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 8787:         current[newitemVal] = newpos;
 8788:     }
 8789:     for (var i=0; i<categories[idx].length; i++) {
 8790:         var elementName = categories[idx][i];
 8791:         if (elementName != item) {
 8792:             if (form.elements[elementName]) {
 8793:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8794:                 current[currVal] = elementName;
 8795:             }
 8796:         }
 8797:     }
 8798:     var oldVal;
 8799:     for (var j=0; j<maxh; j++) {
 8800:         if (current[j] == undefined) {
 8801:             oldVal = j;
 8802:         }
 8803:     }
 8804:     if (oldVal < changedVal) {
 8805:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8806:            var elementName = current[k];
 8807:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8808:         }
 8809:     } else {
 8810:         for (var k=changedVal; k<oldVal; k++) {
 8811:             var elementName = current[k];
 8812:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8813:         }
 8814:     }
 8815:     return;
 8816: }
 8817: 
 8818: function categoryCheck(form) {
 8819:     if (form.elements['addcategory_name'].value == 'instcode') {
 8820:         alert('$instcode_reserved\\n$choose_again');
 8821:         return false;
 8822:     }
 8823:     if (form.elements['addcategory_name'].value == 'communities') {
 8824:         alert('$communities_reserved\\n$choose_again');
 8825:         return false;
 8826:     }
 8827:     if (form.elements['addcategory_name'].value == 'placement') {
 8828:         alert('$placement_reserved\\n$choose_again');
 8829:         return false;
 8830:     }
 8831:     return true;
 8832: }
 8833: 
 8834: // ]]>
 8835: </script>
 8836: 
 8837: ENDSCRIPT
 8838:     return $output;
 8839: }
 8840: 
 8841: sub initialize_categories {
 8842:     my ($itemcount) = @_;
 8843:     my ($datatable,$css_class,$chgstr);
 8844:     my %default_names = (
 8845:                       instcode    => 'Official courses (with institutional codes)',
 8846:                       communities => 'Communities',
 8847:                       placement   => 'Placement Tests',
 8848:                         );
 8849:     my %selnum = (
 8850:                    instcode    => '0',
 8851:                    communities => '1',
 8852:                    placement   => '2',
 8853:                  );
 8854:     my %selected;
 8855:     foreach my $default ('instcode','communities','placement') {
 8856:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8857:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 8858:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
 8859:         $selected{$selnum{$default}} = ' selected="selected"';
 8860:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8861:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
 8862:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
 8863:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
 8864:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
 8865:                      .'<option value="3">4</option></select>&nbsp;'
 8866:                      .$default_names{$default}
 8867:                      .'</span></td><td><span class="LC_nobreak">'
 8868:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 8869:                      .&mt('Display').'</label>&nbsp;<label>'
 8870:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 8871:                  .'</label></span></td></tr>';
 8872:         $itemcount ++;
 8873:     }
 8874:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8875:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 8876:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8877:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 8878:                   .'<option value="0">1</option>'
 8879:                   .'<option value="1">2</option>'
 8880:                   .'<option value="2">3</option>'
 8881:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
 8882:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 8883:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 8884:                   .'</td></tr>';
 8885:     return $datatable;
 8886: }
 8887: 
 8888: sub build_category_rows {
 8889:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 8890:     my ($text,$name,$item,$chgstr);
 8891:     if (ref($cats) eq 'ARRAY') {
 8892:         my $maxdepth = scalar(@{$cats});
 8893:         if (ref($cats->[$depth]) eq 'HASH') {
 8894:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8895:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8896:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8897:                 $text .= '<td><table class="LC_data_table">';
 8898:                 my ($idxnum,$parent_name,$parent_item);
 8899:                 my $higher = $depth - 1;
 8900:                 if ($higher == 0) {
 8901:                     $parent_name = &escape($parent).'::'.$higher;
 8902:                 } else {
 8903:                     if (ref($path) eq 'ARRAY') {
 8904:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8905:                     }
 8906:                 }
 8907:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8908:                 for (my $j=0; $j<=$numchildren; $j++) {
 8909:                     if ($j < $numchildren) {
 8910:                         $name = $cats->[$depth]{$parent}[$j];
 8911:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8912:                         $idxnum = $idx->{$item};
 8913:                     } else {
 8914:                         $name = $parent_name;
 8915:                         $item = $parent_item;
 8916:                     }
 8917:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8918:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8919:                     for (my $i=0; $i<=$numchildren; $i++) {
 8920:                         my $vpos = $i+1;
 8921:                         my $selstr;
 8922:                         if ($j == $i) {
 8923:                             $selstr = ' selected="selected" ';
 8924:                         }
 8925:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8926:                     }
 8927:                     $text .= '</select>&nbsp;';
 8928:                     if ($j < $numchildren) {
 8929:                         my $deeper = $depth+1;
 8930:                         $text .= $name.'&nbsp;'
 8931:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8932:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8933:                         if(ref($path) eq 'ARRAY') {
 8934:                             push(@{$path},$name);
 8935:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8936:                             pop(@{$path});
 8937:                         }
 8938:                     } else {
 8939:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 8940:                         if ($j == $numchildren) {
 8941:                             $text .= $name;
 8942:                         } else {
 8943:                             $text .= $item;
 8944:                         }
 8945:                         $text .= '" value="" />';
 8946:                     }
 8947:                     $text .= '</td></tr>';
 8948:                 }
 8949:                 $text .= '</table></td>';
 8950:             } else {
 8951:                 my $higher = $depth-1;
 8952:                 if ($higher == 0) {
 8953:                     $name = &escape($parent).'::'.$higher;
 8954:                 } else {
 8955:                     if (ref($path) eq 'ARRAY') {
 8956:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8957:                     }
 8958:                 }
 8959:                 my $colspan;
 8960:                 if ($parent ne 'instcode') {
 8961:                     $colspan = $maxdepth - $depth - 1;
 8962:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 8963:                 }
 8964:             }
 8965:         }
 8966:     }
 8967:     return $text;
 8968: }
 8969: 
 8970: sub modifiable_userdata_row {
 8971:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 8972:         $rowid,$customcss,$rowstyle) = @_;
 8973:     my ($role,$rolename,$statustype);
 8974:     $role = $item;
 8975:     if ($context eq 'cancreate') {
 8976:         if ($item =~ /^(emailusername)_(.+)$/) {
 8977:             $role = $1;
 8978:             $statustype = $2;
 8979:             if (ref($usertypes) eq 'HASH') {
 8980:                 if ($usertypes->{$statustype}) {
 8981:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 8982:                 } else {
 8983:                     $rolename = &mt('Data provided by user');
 8984:                 }
 8985:             }
 8986:         }
 8987:     } elsif ($context eq 'selfcreate') {
 8988:         if (ref($usertypes) eq 'HASH') {
 8989:             $rolename = $usertypes->{$role};
 8990:         } else {
 8991:             $rolename = $role;
 8992:         }
 8993:     } elsif ($context eq 'lti') {
 8994:         $rolename = &mt('Institutional data used (if available)');
 8995:     } else {
 8996:         if ($role eq 'cr') {
 8997:             $rolename = &mt('Custom role');
 8998:         } else {
 8999:             $rolename = &Apache::lonnet::plaintext($role);
 9000:         }
 9001:     }
 9002:     my (@fields,%fieldtitles);
 9003:     if (ref($fieldsref) eq 'ARRAY') {
 9004:         @fields = @{$fieldsref};
 9005:     } else {
 9006:         @fields = ('lastname','firstname','middlename','generation',
 9007:                    'permanentemail','id');
 9008:     }
 9009:     if ((ref($titlesref) eq 'HASH')) {
 9010:         %fieldtitles = %{$titlesref};
 9011:     } else {
 9012:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9013:     }
 9014:     my $output;
 9015:     my $css_class;
 9016:     if ($rowcount%2) {
 9017:         $css_class = 'LC_odd_row';
 9018:     }
 9019:     if ($customcss) {
 9020:         $css_class .= " $customcss";
 9021:     }
 9022:     $css_class =~ s/^\s+//;
 9023:     if ($css_class) {
 9024:         $css_class = ' class="'.$css_class.'"';
 9025:     }
 9026:     if ($rowstyle) {
 9027:         $css_class .= ' style="'.$rowstyle.'"';
 9028:     }
 9029:     if ($rowid) {
 9030:         $rowid = ' id="'.$rowid.'"';
 9031:     }
 9032:     $output = '<tr '.$css_class.$rowid.'>'.
 9033:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 9034:               '<td class="LC_left_item" colspan="2"><table>';
 9035:     my $rem;
 9036:     my %checks;
 9037:     my %current;
 9038:     if (ref($settings) eq 'HASH') {
 9039:         my $hashref;
 9040:         if ($context eq 'lti') {
 9041:             if (ref($settings) eq 'HASH') {
 9042:                 $hashref = $settings->{'instdata'};
 9043:             }
 9044:         } elsif (ref($settings->{$context}) eq 'HASH') {
 9045:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 9046:                 $hashref = $settings->{'lti_instdata'};
 9047:             }
 9048:             if ($role eq 'emailusername') {
 9049:                 if ($statustype) {
 9050:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 9051:                         $hashref = $settings->{$context}->{$role}->{$statustype};
 9052:                     }
 9053:                 }
 9054:             }
 9055:         }
 9056:         if (ref($hashref) eq 'HASH') { 
 9057:             foreach my $field (@fields) {
 9058:                 if ($hashref->{$field}) {
 9059:                     if ($role eq 'emailusername') {
 9060:                         $checks{$field} = $hashref->{$field};
 9061:                     } else {
 9062:                         $checks{$field} = ' checked="checked" ';
 9063:                     }
 9064:                 }
 9065:             }
 9066:         }
 9067:     }
 9068:  
 9069:     my $total = scalar(@fields);
 9070:     for (my $i=0; $i<$total; $i++) {
 9071:         $rem = $i%($numinrow);
 9072:         if ($rem == 0) {
 9073:             if ($i > 0) {
 9074:                 $output .= '</tr>';
 9075:             }
 9076:             $output .= '<tr>';
 9077:         }
 9078:         my $check = ' ';
 9079:         unless ($role eq 'emailusername') {
 9080:             if (exists($checks{$fields[$i]})) {
 9081:                 $check = $checks{$fields[$i]}
 9082:             } elsif ($context ne 'lti') {
 9083:                 if ($role eq 'st') {
 9084:                     if (ref($settings) ne 'HASH') {
 9085:                         $check = ' checked="checked" '; 
 9086:                     }
 9087:                 }
 9088:             }
 9089:         }
 9090:         $output .= '<td class="LC_left_item">'.
 9091:                    '<span class="LC_nobreak">';
 9092:         my $prefix = 'canmodify';
 9093:         if ($role eq 'emailusername') {
 9094:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 9095:                 $checks{$fields[$i]} = 'omit';
 9096:             }
 9097:             foreach my $option ('required','optional','omit') {
 9098:                 my $checked='';
 9099:                 if ($checks{$fields[$i]} eq $option) {
 9100:                     $checked='checked="checked" ';
 9101:                 }
 9102:                 $output .= '<label>'.
 9103:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 9104:                            &mt($option).'</label>'.('&nbsp;' x2);
 9105:             }
 9106:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 9107:         } else {
 9108:             if ($context eq 'lti') {
 9109:                 $prefix = 'lti';
 9110:             }
 9111:             $output .= '<label>'.
 9112:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
 9113:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 9114:                        '</label>';
 9115:         }
 9116:         $output .= '</span></td>';
 9117:     }
 9118:     $rem = $total%$numinrow;
 9119:     my $colsleft;
 9120:     if ($rem) {
 9121:         $colsleft = $numinrow - $rem;
 9122:     }
 9123:     if ($colsleft > 1) {
 9124:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9125:                    '&nbsp;</td>';
 9126:     } elsif ($colsleft == 1) {
 9127:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9128:     }
 9129:     $output .= '</tr></table></td></tr>';
 9130:     return $output;
 9131: }
 9132: 
 9133: sub insttypes_row {
 9134:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 9135:         $customcss,$rowstyle) = @_;
 9136:     my %lt = &Apache::lonlocal::texthash (
 9137:                       cansearch => 'Users allowed to search',
 9138:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 9139:                       lockablenames => 'User preference to lock name',
 9140:                       selfassign    => 'Self-reportable affiliations',
 9141:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 9142:              );
 9143:     my $showdom;
 9144:     if ($context eq 'cansearch') {
 9145:         $showdom = ' ('.$dom.')';
 9146:     }
 9147:     my $class = 'LC_left_item';
 9148:     if ($context eq 'statustocreate') {
 9149:         $class = 'LC_right_item';
 9150:     }
 9151:     my $css_class;
 9152:     if ($$rowtotal%2) {
 9153:         $css_class = 'LC_odd_row';
 9154:     }
 9155:     if ($customcss) {
 9156:         $css_class .= ' '.$customcss;
 9157:     }
 9158:     $css_class =~ s/^\s+//;
 9159:     if ($css_class) {
 9160:         $css_class = ' class="'.$css_class.'"';
 9161:     }
 9162:     if ($rowstyle) {
 9163:         $css_class .= ' style="'.$rowstyle.'"';
 9164:     }
 9165:     if ($onclick) {
 9166:         $onclick = 'onclick="'.$onclick.'" ';
 9167:     }
 9168:     my $output = '<tr'.$css_class.'>'.
 9169:                  '<td>'.$lt{$context}.$showdom.
 9170:                  '</td><td class="'.$class.'" colspan="2"><table>';
 9171:     my $rem;
 9172:     if (ref($types) eq 'ARRAY') {
 9173:         for (my $i=0; $i<@{$types}; $i++) {
 9174:             if (defined($usertypes->{$types->[$i]})) {
 9175:                 my $rem = $i%($numinrow);
 9176:                 if ($rem == 0) {
 9177:                     if ($i > 0) {
 9178:                         $output .= '</tr>';
 9179:                     }
 9180:                     $output .= '<tr>';
 9181:                 }
 9182:                 my $check = ' ';
 9183:                 if (ref($settings) eq 'HASH') {
 9184:                     if (ref($settings->{$context}) eq 'ARRAY') {
 9185:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 9186:                             $check = ' checked="checked" ';
 9187:                         }
 9188:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 9189:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 9190:                             $check = ' checked="checked" ';
 9191:                         }
 9192:                     } elsif ($context eq 'statustocreate') {
 9193:                         $check = ' checked="checked" ';
 9194:                     }
 9195:                 }
 9196:                 $output .= '<td class="LC_left_item">'.
 9197:                            '<span class="LC_nobreak"><label>'.
 9198:                            '<input type="checkbox" name="'.$context.'" '.
 9199:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 9200:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 9201:             }
 9202:         }
 9203:         $rem = @{$types}%($numinrow);
 9204:     }
 9205:     my $colsleft = $numinrow - $rem;
 9206:     if ($context eq 'overrides') {
 9207:         if ($colsleft > 1) {
 9208:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9209:         } else {
 9210:             $output .= '<td class="LC_left_item">';
 9211:         }
 9212:         $output .= '&nbsp;';  
 9213:     } else {
 9214:         if ($rem == 0) {
 9215:             $output .= '<tr>';
 9216:         }
 9217:         if ($colsleft > 1) {
 9218:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9219:         } else {
 9220:             $output .= '<td class="LC_left_item">';
 9221:         }
 9222:         my $defcheck = ' ';
 9223:         if (ref($settings) eq 'HASH') {  
 9224:             if (ref($settings->{$context}) eq 'ARRAY') {
 9225:                 if (grep(/^default$/,@{$settings->{$context}})) {
 9226:                     $defcheck = ' checked="checked" ';
 9227:                 }
 9228:             } elsif ($context eq 'statustocreate') {
 9229:                 $defcheck = ' checked="checked" ';
 9230:             }
 9231:         }
 9232:         $output .= '<span class="LC_nobreak"><label>'.
 9233:                    '<input type="checkbox" name="'.$context.'" '.
 9234:                    'value="default"'.$defcheck.$onclick.' />'.
 9235:                    $othertitle.'</label></span>';
 9236:     }
 9237:     $output .= '</td></tr></table></td></tr>';
 9238:     return $output;
 9239: }
 9240: 
 9241: sub sorted_searchtitles {
 9242:     my %searchtitles = &Apache::lonlocal::texthash(
 9243:                          'uname' => 'username',
 9244:                          'lastname' => 'last name',
 9245:                          'lastfirst' => 'last name, first name',
 9246:                      );
 9247:     my @titleorder = ('uname','lastname','lastfirst');
 9248:     return (\%searchtitles,\@titleorder);
 9249: }
 9250: 
 9251: sub sorted_searchtypes {
 9252:     my %srchtypes_desc = (
 9253:                            exact    => 'is exact match',
 9254:                            contains => 'contains ..',
 9255:                            begins   => 'begins with ..',
 9256:                          );
 9257:     my @srchtypeorder = ('exact','begins','contains');
 9258:     return (\%srchtypes_desc,\@srchtypeorder);
 9259: }
 9260: 
 9261: sub usertype_update_row {
 9262:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 9263:     my $datatable;
 9264:     my $numinrow = 4;
 9265:     foreach my $type (@{$types}) {
 9266:         if (defined($usertypes->{$type})) {
 9267:             $$rownums ++;
 9268:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 9269:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 9270:                           '</td><td class="LC_left_item"><table>';
 9271:             for (my $i=0; $i<@{$fields}; $i++) {
 9272:                 my $rem = $i%($numinrow);
 9273:                 if ($rem == 0) {
 9274:                     if ($i > 0) {
 9275:                         $datatable .= '</tr>';
 9276:                     }
 9277:                     $datatable .= '<tr>';
 9278:                 }
 9279:                 my $check = ' ';
 9280:                 if (ref($settings) eq 'HASH') {
 9281:                     if (ref($settings->{'fields'}) eq 'HASH') {
 9282:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 9283:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 9284:                                 $check = ' checked="checked" ';
 9285:                             }
 9286:                         }
 9287:                     }
 9288:                 }
 9289: 
 9290:                 if ($i == @{$fields}-1) {
 9291:                     my $colsleft = $numinrow - $rem;
 9292:                     if ($colsleft > 1) {
 9293:                         $datatable .= '<td colspan="'.$colsleft.'">';
 9294:                     } else {
 9295:                         $datatable .= '<td>';
 9296:                     }
 9297:                 } else {
 9298:                     $datatable .= '<td>';
 9299:                 }
 9300:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9301:                               '<input type="checkbox" name="updateable_'.$type.
 9302:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 9303:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 9304:             }
 9305:             $datatable .= '</tr></table></td></tr>';
 9306:         }
 9307:     }
 9308:     return $datatable;
 9309: }
 9310: 
 9311: sub modify_login {
 9312:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9313:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 9314:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 9315:     %title = ( coursecatalog => 'Display course catalog',
 9316:                adminmail => 'Display administrator E-mail address',
 9317:                helpdesk  => 'Display "Contact Helpdesk" link',
 9318:                newuser => 'Link for visitors to create a user account',
 9319:                loginheader => 'Log-in box header');
 9320:     @offon = ('off','on');
 9321:     if (ref($domconfig{login}) eq 'HASH') {
 9322:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 9323:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 9324:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 9325:             }
 9326:         }
 9327:     }
 9328:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 9329:                                            \%domconfig,\%loginhash);
 9330:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9331:     foreach my $item (@toggles) {
 9332:         $loginhash{login}{$item} = $env{'form.'.$item};
 9333:     }
 9334:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 9335:     if (ref($colchanges{'login'}) eq 'HASH') {  
 9336:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 9337:                                          \%loginhash);
 9338:     }
 9339: 
 9340:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9341:     my %domservers = &Apache::lonnet::get_servers($dom);
 9342:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 9343:     if (keys(%servers) > 1) {
 9344:         foreach my $lonhost (keys(%servers)) {
 9345:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 9346:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 9347:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 9348:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 9349:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 9350:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9351:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9352:                         $changes{'loginvia'}{$lonhost} = 1;
 9353:                     } else {
 9354:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 9355:                         $changes{'loginvia'}{$lonhost} = 1;
 9356:                     }
 9357:                 } else {
 9358:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9359:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9360:                         $changes{'loginvia'}{$lonhost} = 1;
 9361:                     }
 9362:                 }
 9363:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 9364:                     foreach my $item (@loginvia_attribs) {
 9365:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 9366:                     }
 9367:                 } else {
 9368:                     foreach my $item (@loginvia_attribs) {
 9369:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9370:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9371:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 9372:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9373:                                 $new = '/';
 9374:                             }
 9375:                         }
 9376:                         if (($item eq 'custompath') && 
 9377:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9378:                             $new = '';
 9379:                         }
 9380:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 9381:                             $changes{'loginvia'}{$lonhost} = 1;
 9382:                         }
 9383:                         if ($item eq 'exempt') {
 9384:                             $new = &check_exempt_addresses($new);
 9385:                         }
 9386:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9387:                     }
 9388:                 }
 9389:             } else {
 9390:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9391:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9392:                     $changes{'loginvia'}{$lonhost} = 1;
 9393:                     foreach my $item (@loginvia_attribs) {
 9394:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9395:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9396:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9397:                                 $new = '/';
 9398:                             }
 9399:                         }
 9400:                         if (($item eq 'custompath') && 
 9401:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9402:                             $new = '';
 9403:                         }
 9404:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9405:                     }
 9406:                 }
 9407:             }
 9408:         }
 9409:     }
 9410: 
 9411:     my $servadm = $r->dir_config('lonAdmEMail');
 9412:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 9413:     if (ref($domconfig{'login'}) eq 'HASH') {
 9414:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 9415:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 9416:                 if ($lang eq 'nolang') {
 9417:                     push(@currlangs,$lang);
 9418:                 } elsif (defined($langchoices{$lang})) {
 9419:                     push(@currlangs,$lang);
 9420:                 } else {
 9421:                     next;
 9422:                 }
 9423:             }
 9424:         }
 9425:     }
 9426:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 9427:     if (@currlangs > 0) {
 9428:         foreach my $lang (@currlangs) {
 9429:             if (grep(/^\Q$lang\E$/,@delurls)) {
 9430:                 $changes{'helpurl'}{$lang} = 1;
 9431:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 9432:                 $changes{'helpurl'}{$lang} = 1;
 9433:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 9434:                 push(@newlangs,$lang);
 9435:             } else {
 9436:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9437:             }
 9438:         }
 9439:     }
 9440:     unless (grep(/^nolang$/,@currlangs)) {
 9441:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 9442:             $changes{'helpurl'}{'nolang'} = 1;
 9443:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 9444:             push(@newlangs,'nolang');
 9445:         }
 9446:     }
 9447:     if ($env{'form.loginhelpurl_add_lang'}) {
 9448:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 9449:             ($env{'form.loginhelpurl_add_file.filename'})) {
 9450:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 9451:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 9452:         }
 9453:     }
 9454:     if ((@newlangs > 0) || ($addedfile)) {
 9455:         my $error;
 9456:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9457:         if ($configuserok eq 'ok') {
 9458:             if ($switchserver) {
 9459:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 9460:             } elsif ($author_ok eq 'ok') {
 9461:                 my @allnew = @newlangs;
 9462:                 if ($addedfile ne '') {
 9463:                     push(@allnew,$addedfile);
 9464:                 }
 9465:                 foreach my $lang (@allnew) {
 9466:                     my $formelem = 'loginhelpurl_'.$lang;
 9467:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 9468:                         $formelem = 'loginhelpurl_add_file';
 9469:                     }
 9470:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9471:                                                                "help/$lang",'','',$newfile{$lang});
 9472:                     if ($result eq 'ok') {
 9473:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 9474:                         $changes{'helpurl'}{$lang} = 1;
 9475:                     } else {
 9476:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 9477:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9478:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 9479:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 9480:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9481:                         }
 9482:                     }
 9483:                 }
 9484:             } else {
 9485:                 $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);
 9486:             }
 9487:         } else {
 9488:             $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);
 9489:         }
 9490:         if ($error) {
 9491:             &Apache::lonnet::logthis($error);
 9492:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9493:         }
 9494:     }
 9495: 
 9496:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 9497:     if (ref($domconfig{'login'}) eq 'HASH') {
 9498:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 9499:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 9500:                 if ($domservers{$lonhost}) {
 9501:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9502:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 9503:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 9504:                     }
 9505:                 }
 9506:             }
 9507:         }
 9508:     }
 9509:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 9510:     foreach my $lonhost (sort(keys(%domservers))) {
 9511:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9512:             $changes{'headtag'}{$lonhost} = 1;
 9513:         } else {
 9514:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 9515:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 9516:             }
 9517:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 9518:                 push(@newhosts,$lonhost);
 9519:             } elsif ($currheadtagurls{$lonhost}) {
 9520:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 9521:                 if ($currexempt{$lonhost}) {
 9522:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 9523:                         $changes{'headtag'}{$lonhost} = 1;
 9524:                     }
 9525:                 } elsif ($possexempt{$lonhost}) {
 9526:                     $changes{'headtag'}{$lonhost} = 1;
 9527:                 }
 9528:                 if ($possexempt{$lonhost}) {
 9529:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9530:                 }
 9531:             }
 9532:         }
 9533:     }
 9534:     if (@newhosts) {
 9535:         my $error;
 9536:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9537:         if ($configuserok eq 'ok') {
 9538:             if ($switchserver) {
 9539:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 9540:             } elsif ($author_ok eq 'ok') {
 9541:                 foreach my $lonhost (@newhosts) {
 9542:                     my $formelem = 'loginheadtag_'.$lonhost;
 9543:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9544:                                                                           "login/headtag/$lonhost",'','',
 9545:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 9546:                     if ($result eq 'ok') {
 9547:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 9548:                         $changes{'headtag'}{$lonhost} = 1;
 9549:                         if ($possexempt{$lonhost}) {
 9550:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9551:                         }
 9552:                     } else {
 9553:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 9554:                                            $newheadtagurls{$lonhost},$result);
 9555:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9556:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 9557:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 9558:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 9559:                         }
 9560:                     }
 9561:                 }
 9562:             } else {
 9563:                 $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);
 9564:             }
 9565:         } else {
 9566:             $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);
 9567:         }
 9568:         if ($error) {
 9569:             &Apache::lonnet::logthis($error);
 9570:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9571:         }
 9572:     }
 9573:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 9574: 
 9575:     my $defaulthelpfile = '/adm/loginproblems.html';
 9576:     my $defaulttext = &mt('Default in use');
 9577: 
 9578:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 9579:                                              $dom);
 9580:     if ($putresult eq 'ok') {
 9581:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9582:         my %defaultchecked = (
 9583:                     'coursecatalog' => 'on',
 9584:                     'helpdesk'      => 'on',
 9585:                     'adminmail'     => 'off',
 9586:                     'newuser'       => 'off',
 9587:         );
 9588:         if (ref($domconfig{'login'}) eq 'HASH') {
 9589:             foreach my $item (@toggles) {
 9590:                 if ($defaultchecked{$item} eq 'on') { 
 9591:                     if (($domconfig{'login'}{$item} eq '0') &&
 9592:                         ($env{'form.'.$item} eq '1')) {
 9593:                         $changes{$item} = 1;
 9594:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9595:                               $domconfig{'login'}{$item} eq '1') &&
 9596:                              ($env{'form.'.$item} eq '0')) {
 9597:                         $changes{$item} = 1;
 9598:                     }
 9599:                 } elsif ($defaultchecked{$item} eq 'off') {
 9600:                     if (($domconfig{'login'}{$item} eq '1') &&
 9601:                         ($env{'form.'.$item} eq '0')) {
 9602:                         $changes{$item} = 1;
 9603:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9604:                               $domconfig{'login'}{$item} eq '0') &&
 9605:                              ($env{'form.'.$item} eq '1')) {
 9606:                         $changes{$item} = 1;
 9607:                     }
 9608:                 }
 9609:             }
 9610:         }
 9611:         if (keys(%changes) > 0 || $colchgtext) {
 9612:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9613:             if (ref($lastactref) eq 'HASH') {
 9614:                 $lastactref->{'domainconfig'} = 1;
 9615:             }
 9616:             $resulttext = &mt('Changes made:').'<ul>';
 9617:             foreach my $item (sort(keys(%changes))) {
 9618:                 if ($item eq 'loginvia') {
 9619:                     if (ref($changes{$item}) eq 'HASH') {
 9620:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 9621:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9622:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 9623:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 9624:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 9625:                                     $protocol = 'http' if ($protocol ne 'https');
 9626:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 9627: 
 9628:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 9629:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 9630:                                     } else {
 9631:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 9632:                                     }
 9633:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 9634:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 9635:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 9636:                                     }
 9637:                                     $resulttext .= '</li>';
 9638:                                 } else {
 9639:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 9640:                                 }
 9641:                             } else {
 9642:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 9643:                             }
 9644:                         }
 9645:                         $resulttext .= '</ul></li>';
 9646:                     }
 9647:                 } elsif ($item eq 'helpurl') {
 9648:                     if (ref($changes{$item}) eq 'HASH') {
 9649:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 9650:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 9651:                                 my ($chg,$link);
 9652:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 9653:                                 if ($lang eq 'nolang') {
 9654:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 9655:                                 } else {
 9656:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 9657:                                 }
 9658:                                 $resulttext .= '<li>'.$chg.'</li>';
 9659:                             } else {
 9660:                                 my $chg;
 9661:                                 if ($lang eq 'nolang') {
 9662:                                     $chg = &mt('custom log-in help file for no preferred language');
 9663:                                 } else {
 9664:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 9665:                                 }
 9666:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 9667:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 9668:                                                       '?inhibitmenu=yes',$chg,600,500).
 9669:                                                '</li>';
 9670:                             }
 9671:                         }
 9672:                     }
 9673:                 } elsif ($item eq 'headtag') {
 9674:                     if (ref($changes{$item}) eq 'HASH') {
 9675:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9676:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9677:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 9678:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9679:                                 $resulttext .= '<li><a href="'.
 9680:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 9681:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 9682:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 9683:                                 if ($possexempt{$lonhost}) {
 9684:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 9685:                                 } else {
 9686:                                     $resulttext .= &mt('included for any client IP');
 9687:                                 }
 9688:                                 $resulttext .= '</li>';
 9689:                             }
 9690:                         }
 9691:                     }
 9692:                 } elsif ($item eq 'captcha') {
 9693:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9694:                         my $chgtxt;
 9695:                         if ($loginhash{'login'}{$item} eq 'notused') {
 9696:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 9697:                         } else {
 9698:                             my %captchas = &captcha_phrases();
 9699:                             if ($captchas{$loginhash{'login'}{$item}}) {
 9700:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 9701:                             } else {
 9702:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 9703:                             }
 9704:                         }
 9705:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9706:                     }
 9707:                 } elsif ($item eq 'recaptchakeys') {
 9708:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9709:                         my ($privkey,$pubkey);
 9710:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 9711:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 9712:                             $privkey = $loginhash{'login'}{$item}{'private'};
 9713:                         }
 9714:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 9715:                         if (!$pubkey) {
 9716:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 9717:                         } else {
 9718:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9719:                         }
 9720:                         if (!$privkey) {
 9721:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 9722:                         } else {
 9723:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 9724:                         }
 9725:                         $chgtxt .= '</ul>';
 9726:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9727:                     }
 9728:                 } elsif ($item eq 'recaptchaversion') {
 9729:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9730:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 9731:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 9732:                                            '</li>';
 9733:                         }
 9734:                     }
 9735:                 } else {
 9736:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 9737:                 }
 9738:             }
 9739:             $resulttext .= $colchgtext.'</ul>';
 9740:         } else {
 9741:             $resulttext = &mt('No changes made to log-in page settings');
 9742:         }
 9743:     } else {
 9744:         $resulttext = '<span class="LC_error">'.
 9745: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9746:     }
 9747:     if ($errors) {
 9748:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9749:                        $errors.'</ul>';
 9750:     }
 9751:     return $resulttext;
 9752: }
 9753: 
 9754: sub check_exempt_addresses {
 9755:     my ($iplist) = @_;
 9756:     $iplist =~ s/^\s+//;
 9757:     $iplist =~ s/\s+$//;
 9758:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 9759:     my (@okips,$new);
 9760:     foreach my $ip (@poss_ips) {
 9761:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 9762:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 9763:                 push(@okips,$ip);
 9764:             }
 9765:         }
 9766:     }
 9767:     if (@okips > 0) {
 9768:         $new = join(',',@okips);
 9769:     } else {
 9770:         $new = '';
 9771:     }
 9772:     return $new;
 9773: }
 9774: 
 9775: sub color_font_choices {
 9776:     my %choices =
 9777:         &Apache::lonlocal::texthash (
 9778:             img => "Header",
 9779:             bgs => "Background colors",
 9780:             links => "Link colors",
 9781:             images => "Images",
 9782:             font => "Font color",
 9783:             fontmenu => "Font menu",
 9784:             pgbg => "Page",
 9785:             tabbg => "Header",
 9786:             sidebg => "Border",
 9787:             link => "Link",
 9788:             alink => "Active link",
 9789:             vlink => "Visited link",
 9790:         );
 9791:     return %choices;
 9792: }
 9793: 
 9794: sub modify_rolecolors {
 9795:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 9796:     my ($resulttext,%rolehash);
 9797:     $rolehash{'rolecolors'} = {};
 9798:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 9799:         if ($domconfig{'rolecolors'} eq '') {
 9800:             $domconfig{'rolecolors'} = {};
 9801:         }
 9802:     }
 9803:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 9804:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 9805:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 9806:                                              $dom);
 9807:     if ($putresult eq 'ok') {
 9808:         if (keys(%changes) > 0) {
 9809:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9810:             if (ref($lastactref) eq 'HASH') {
 9811:                 $lastactref->{'domainconfig'} = 1;
 9812:             }
 9813:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 9814:                                              $rolehash{'rolecolors'});
 9815:         } else {
 9816:             $resulttext = &mt('No changes made to default color schemes');
 9817:         }
 9818:     } else {
 9819:         $resulttext = '<span class="LC_error">'.
 9820: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9821:     }
 9822:     if ($errors) {
 9823:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9824:                        $errors.'</ul>';
 9825:     }
 9826:     return $resulttext;
 9827: }
 9828: 
 9829: sub modify_colors {
 9830:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 9831:     my (%changes,%choices);
 9832:     my @bgs;
 9833:     my @links = ('link','alink','vlink');
 9834:     my @logintext;
 9835:     my @images;
 9836:     my $servadm = $r->dir_config('lonAdmEMail');
 9837:     my $errors;
 9838:     my %defaults;
 9839:     foreach my $role (@{$roles}) {
 9840:         if ($role eq 'login') {
 9841:             %choices = &login_choices();
 9842:             @logintext = ('textcol','bgcol');
 9843:         } else {
 9844:             %choices = &color_font_choices();
 9845:         }
 9846:         if ($role eq 'login') {
 9847:             @images = ('img','logo','domlogo','login');
 9848:             @bgs = ('pgbg','mainbg','sidebg');
 9849:         } else {
 9850:             @images = ('img');
 9851:             @bgs = ('pgbg','tabbg','sidebg');
 9852:         }
 9853:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 9854:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 9855:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 9856:         }
 9857:         if ($role eq 'login') {
 9858:             foreach my $item (@logintext) {
 9859:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9860:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9861:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9862:                 }
 9863:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 9864:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9865:                 }
 9866:             }
 9867:         } else {
 9868:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 9869:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 9870:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 9871:             }
 9872:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 9873:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 9874:             }
 9875:         }
 9876:         foreach my $item (@bgs) {
 9877:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9878:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9879:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9880:             }
 9881:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 9882:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9883:             }
 9884:         }
 9885:         foreach my $item (@links) {
 9886:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9887:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9888:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9889:             }
 9890:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 9891:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9892:             }
 9893:         }
 9894:         my ($configuserok,$author_ok,$switchserver) = 
 9895:             &config_check($dom,$confname,$servadm);
 9896:         my ($width,$height) = &thumb_dimensions();
 9897:         if (ref($domconfig->{$role}) ne 'HASH') {
 9898:             $domconfig->{$role} = {};
 9899:         }
 9900:         foreach my $img (@images) {
 9901:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 9902:                 if (defined($env{'form.login_showlogo_'.$img})) {
 9903:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 9904:                 } else { 
 9905:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 9906:                 }
 9907:             } 
 9908: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 9909: 		 && !defined($domconfig->{$role}{$img})
 9910: 		 && !$env{'form.'.$role.'_del_'.$img}
 9911: 		 && $env{'form.'.$role.'_import_'.$img}) {
 9912: 		# import the old configured image from the .tab setting
 9913: 		# if they haven't provided a new one 
 9914: 		$domconfig->{$role}{$img} = 
 9915: 		    $env{'form.'.$role.'_import_'.$img};
 9916: 	    }
 9917:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 9918:                 my $error;
 9919:                 if ($configuserok eq 'ok') {
 9920:                     if ($switchserver) {
 9921:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 9922:                     } else {
 9923:                         if ($author_ok eq 'ok') {
 9924:                             my ($result,$logourl) = 
 9925:                                 &publishlogo($r,'upload',$role.'_'.$img,
 9926:                                            $dom,$confname,$img,$width,$height);
 9927:                             if ($result eq 'ok') {
 9928:                                 $confhash->{$role}{$img} = $logourl;
 9929:                                 $changes{$role}{'images'}{$img} = 1;
 9930:                             } else {
 9931:                                 $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);
 9932:                             }
 9933:                         } else {
 9934:                             $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);
 9935:                         }
 9936:                     }
 9937:                 } else {
 9938:                     $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);
 9939:                 }
 9940:                 if ($error) {
 9941:                     &Apache::lonnet::logthis($error);
 9942:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9943:                 }
 9944:             } elsif ($domconfig->{$role}{$img} ne '') {
 9945:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 9946:                     my $error;
 9947:                     if ($configuserok eq 'ok') {
 9948: # is confname an author?
 9949:                         if ($switchserver eq '') {
 9950:                             if ($author_ok eq 'ok') {
 9951:                                 my ($result,$logourl) = 
 9952:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 9953:                                             $dom,$confname,$img,$width,$height);
 9954:                                 if ($result eq 'ok') {
 9955:                                     $confhash->{$role}{$img} = $logourl;
 9956: 				    $changes{$role}{'images'}{$img} = 1;
 9957:                                 }
 9958:                             }
 9959:                         }
 9960:                     }
 9961:                 }
 9962:             }
 9963:         }
 9964:         if (ref($domconfig) eq 'HASH') {
 9965:             if (ref($domconfig->{$role}) eq 'HASH') {
 9966:                 foreach my $img (@images) {
 9967:                     if ($domconfig->{$role}{$img} ne '') {
 9968:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9969:                             $confhash->{$role}{$img} = '';
 9970:                             $changes{$role}{'images'}{$img} = 1;
 9971:                         } else {
 9972:                             if ($confhash->{$role}{$img} eq '') {
 9973:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 9974:                             }
 9975:                         }
 9976:                     } else {
 9977:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9978:                             $confhash->{$role}{$img} = '';
 9979:                             $changes{$role}{'images'}{$img} = 1;
 9980:                         } 
 9981:                     }
 9982:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 9983:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 9984:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 9985:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 9986:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 9987:                             }
 9988:                         } else {
 9989:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9990:                                 $changes{$role}{'showlogo'}{$img} = 1;
 9991:                             }
 9992:                         }
 9993:                     }
 9994:                 }
 9995:                 if ($domconfig->{$role}{'font'} ne '') {
 9996:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 9997:                         $changes{$role}{'font'} = 1;
 9998:                     }
 9999:                 } else {
10000:                     if ($confhash->{$role}{'font'}) {
10001:                         $changes{$role}{'font'} = 1;
10002:                     }
10003:                 }
10004:                 if ($role ne 'login') {
10005:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
10006:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
10007:                             $changes{$role}{'fontmenu'} = 1;
10008:                         }
10009:                     } else {
10010:                         if ($confhash->{$role}{'fontmenu'}) {
10011:                             $changes{$role}{'fontmenu'} = 1;
10012:                         }
10013:                     }
10014:                 }
10015:                 foreach my $item (@bgs) {
10016:                     if ($domconfig->{$role}{$item} ne '') {
10017:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10018:                             $changes{$role}{'bgs'}{$item} = 1;
10019:                         } 
10020:                     } else {
10021:                         if ($confhash->{$role}{$item}) {
10022:                             $changes{$role}{'bgs'}{$item} = 1;
10023:                         }
10024:                     }
10025:                 }
10026:                 foreach my $item (@links) {
10027:                     if ($domconfig->{$role}{$item} ne '') {
10028:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10029:                             $changes{$role}{'links'}{$item} = 1;
10030:                         }
10031:                     } else {
10032:                         if ($confhash->{$role}{$item}) {
10033:                             $changes{$role}{'links'}{$item} = 1;
10034:                         }
10035:                     }
10036:                 }
10037:                 foreach my $item (@logintext) {
10038:                     if ($domconfig->{$role}{$item} ne '') {
10039:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10040:                             $changes{$role}{'logintext'}{$item} = 1;
10041:                         }
10042:                     } else {
10043:                         if ($confhash->{$role}{$item}) {
10044:                             $changes{$role}{'logintext'}{$item} = 1;
10045:                         }
10046:                     }
10047:                 }
10048:             } else {
10049:                 &default_change_checker($role,\@images,\@links,\@bgs,
10050:                                         \@logintext,$confhash,\%changes); 
10051:             }
10052:         } else {
10053:             &default_change_checker($role,\@images,\@links,\@bgs,
10054:                                     \@logintext,$confhash,\%changes); 
10055:         }
10056:     }
10057:     return ($errors,%changes);
10058: }
10059: 
10060: sub config_check {
10061:     my ($dom,$confname,$servadm) = @_;
10062:     my ($configuserok,$author_ok,$switchserver,%currroles);
10063:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
10064:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
10065:                                                    $confname,$servadm);
10066:     if ($configuserok eq 'ok') {
10067:         $switchserver = &check_switchserver($dom,$confname);
10068:         if ($switchserver eq '') {
10069:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
10070:         }
10071:     }
10072:     return ($configuserok,$author_ok,$switchserver);
10073: }
10074: 
10075: sub default_change_checker {
10076:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
10077:     foreach my $item (@{$links}) {
10078:         if ($confhash->{$role}{$item}) {
10079:             $changes->{$role}{'links'}{$item} = 1;
10080:         }
10081:     }
10082:     foreach my $item (@{$bgs}) {
10083:         if ($confhash->{$role}{$item}) {
10084:             $changes->{$role}{'bgs'}{$item} = 1;
10085:         }
10086:     }
10087:     foreach my $item (@{$logintext}) {
10088:         if ($confhash->{$role}{$item}) {
10089:             $changes->{$role}{'logintext'}{$item} = 1;
10090:         }
10091:     }
10092:     foreach my $img (@{$images}) {
10093:         if ($env{'form.'.$role.'_del_'.$img}) {
10094:             $confhash->{$role}{$img} = '';
10095:             $changes->{$role}{'images'}{$img} = 1;
10096:         }
10097:         if ($role eq 'login') {
10098:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10099:                 $changes->{$role}{'showlogo'}{$img} = 1;
10100:             }
10101:         }
10102:     }
10103:     if ($confhash->{$role}{'font'}) {
10104:         $changes->{$role}{'font'} = 1;
10105:     }
10106: }
10107: 
10108: sub display_colorchgs {
10109:     my ($dom,$changes,$roles,$confhash) = @_;
10110:     my (%choices,$resulttext);
10111:     if (!grep(/^login$/,@{$roles})) {
10112:         $resulttext = &mt('Changes made:').'<br />';
10113:     }
10114:     foreach my $role (@{$roles}) {
10115:         if ($role eq 'login') {
10116:             %choices = &login_choices();
10117:         } else {
10118:             %choices = &color_font_choices();
10119:         }
10120:         if (ref($changes->{$role}) eq 'HASH') {
10121:             if ($role ne 'login') {
10122:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
10123:             }
10124:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
10125:                 if ($role ne 'login') {
10126:                     $resulttext .= '<ul>';
10127:                 }
10128:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
10129:                     if ($role ne 'login') {
10130:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
10131:                     }
10132:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
10133:                         if (($role eq 'login') && ($key eq 'showlogo')) {
10134:                             if ($confhash->{$role}{$key}{$item}) {
10135:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
10136:                             } else {
10137:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
10138:                             }
10139:                         } elsif ($confhash->{$role}{$item} eq '') {
10140:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
10141:                         } else {
10142:                             my $newitem = $confhash->{$role}{$item};
10143:                             if ($key eq 'images') {
10144:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
10145:                             }
10146:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
10147:                         }
10148:                     }
10149:                     if ($role ne 'login') {
10150:                         $resulttext .= '</ul></li>';
10151:                     }
10152:                 } else {
10153:                     if ($confhash->{$role}{$key} eq '') {
10154:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
10155:                     } else {
10156:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
10157:                     }
10158:                 }
10159:                 if ($role ne 'login') {
10160:                     $resulttext .= '</ul>';
10161:                 }
10162:             }
10163:         }
10164:     }
10165:     return $resulttext;
10166: }
10167: 
10168: sub thumb_dimensions {
10169:     return ('200','50');
10170: }
10171: 
10172: sub check_dimensions {
10173:     my ($inputfile) = @_;
10174:     my ($fullwidth,$fullheight);
10175:     if ($inputfile =~ m|^[/\w.\-]+$|) {
10176:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
10177:             my $imageinfo = <PIPE>;
10178:             if (!close(PIPE)) {
10179:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
10180:             }
10181:             chomp($imageinfo);
10182:             my ($fullsize) = 
10183:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
10184:             if ($fullsize) {
10185:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
10186:             }
10187:         }
10188:     }
10189:     return ($fullwidth,$fullheight);
10190: }
10191: 
10192: sub check_configuser {
10193:     my ($uhome,$dom,$confname,$servadm) = @_;
10194:     my ($configuserok,%currroles);
10195:     if ($uhome eq 'no_host') {
10196:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
10197:         my $configpass = &LONCAPA::Enrollment::create_password();
10198:         $configuserok = 
10199:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
10200:                              $configpass,'','','','','',undef,$servadm);
10201:     } else {
10202:         $configuserok = 'ok';
10203:         %currroles = 
10204:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
10205:     }
10206:     return ($configuserok,%currroles);
10207: }
10208: 
10209: sub check_authorstatus {
10210:     my ($dom,$confname,%currroles) = @_;
10211:     my $author_ok;
10212:     if (!$currroles{':'.$dom.':au'}) {
10213:         my $start = time;
10214:         my $end = 0;
10215:         $author_ok = 
10216:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
10217:                                         'au',$end,$start,'','','domconfig');
10218:     } else {
10219:         $author_ok = 'ok';
10220:     }
10221:     return $author_ok;
10222: }
10223: 
10224: sub publishlogo {
10225:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
10226:     my ($output,$fname,$logourl,$madethumb);
10227:     if ($action eq 'upload') {
10228:         $fname=$env{'form.'.$formname.'.filename'};
10229:         chop($env{'form.'.$formname});
10230:     } else {
10231:         ($fname) = ($formname =~ /([^\/]+)$/);
10232:     }
10233:     if ($savefileas ne '') {
10234:         $fname = $savefileas;
10235:     }
10236:     $fname=&Apache::lonnet::clean_filename($fname);
10237: # See if there is anything left
10238:     unless ($fname) { return ('error: no uploaded file'); }
10239:     $fname="$subdir/$fname";
10240:     my $docroot=$r->dir_config('lonDocRoot');
10241:     my $filepath="$docroot/priv";
10242:     my $relpath = "$dom/$confname";
10243:     my ($fnamepath,$file,$fetchthumb);
10244:     $file=$fname;
10245:     if ($fname=~m|/|) {
10246:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
10247:     }
10248:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
10249:     my $count;
10250:     for ($count=5;$count<=$#parts;$count++) {
10251:         $filepath.="/$parts[$count]";
10252:         if ((-e $filepath)!=1) {
10253:             mkdir($filepath,02770);
10254:         }
10255:     }
10256:     # Check for bad extension and disallow upload
10257:     if ($file=~/\.(\w+)$/ &&
10258:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
10259:         $output = 
10260:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
10261:     } elsif ($file=~/\.(\w+)$/ &&
10262:         !defined(&Apache::loncommon::fileembstyle($1))) {
10263:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
10264:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
10265:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
10266:     } elsif (-d "$filepath/$file") {
10267:         $output = &mt('Filename is a directory name - rename the file and re-upload');
10268:     } else {
10269:         my $source = $filepath.'/'.$file;
10270:         my $logfile;
10271:         if (!open($logfile,">>",$source.'.log')) {
10272:             return (&mt('No write permission to Authoring Space'));
10273:         }
10274:         print $logfile
10275: "\n================= Publish ".localtime()." ================\n".
10276: $env{'user.name'}.':'.$env{'user.domain'}."\n";
10277: # Save the file
10278:         if (!open(FH,">",$source)) {
10279:             &Apache::lonnet::logthis('Failed to create '.$source);
10280:             return (&mt('Failed to create file'));
10281:         }
10282:         if ($action eq 'upload') {
10283:             if (!print FH ($env{'form.'.$formname})) {
10284:                 &Apache::lonnet::logthis('Failed to write to '.$source);
10285:                 return (&mt('Failed to write file'));
10286:             }
10287:         } else {
10288:             my $original = &Apache::lonnet::filelocation('',$formname);
10289:             if(!copy($original,$source)) {
10290:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
10291:                 return (&mt('Failed to write file'));
10292:             }
10293:         }
10294:         close(FH);
10295:         chmod(0660, $source); # Permissions to rw-rw---.
10296: 
10297:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
10298:         my $copyfile=$targetdir.'/'.$file;
10299: 
10300:         my @parts=split(/\//,$targetdir);
10301:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
10302:         for (my $count=5;$count<=$#parts;$count++) {
10303:             $path.="/$parts[$count]";
10304:             if (!-e $path) {
10305:                 print $logfile "\nCreating directory ".$path;
10306:                 mkdir($path,02770);
10307:             }
10308:         }
10309:         my $versionresult;
10310:         if (-e $copyfile) {
10311:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
10312:         } else {
10313:             $versionresult = 'ok';
10314:         }
10315:         if ($versionresult eq 'ok') {
10316:             if (copy($source,$copyfile)) {
10317:                 print $logfile "\nCopied original source to ".$copyfile."\n";
10318:                 $output = 'ok';
10319:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
10320:                 push(@{$modified_urls},[$copyfile,$source]);
10321:                 my $metaoutput = 
10322:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
10323:                 unless ($registered_cleanup) {
10324:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10325:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10326:                     $registered_cleanup=1;
10327:                 }
10328:             } else {
10329:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
10330:                 $output = &mt('Failed to copy file to RES space').", $!";
10331:             }
10332:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
10333:                 my $inputfile = $filepath.'/'.$file;
10334:                 my $outfile = $filepath.'/'.'tn-'.$file;
10335:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
10336:                 if ($fullwidth ne '' && $fullheight ne '') { 
10337:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
10338:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
10339:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
10340:                         system({$args[0]} @args);
10341:                         chmod(0660, $filepath.'/tn-'.$file);
10342:                         if (-e $outfile) {
10343:                             my $copyfile=$targetdir.'/tn-'.$file;
10344:                             if (copy($outfile,$copyfile)) {
10345:                                 print $logfile "\nCopied source to ".$copyfile."\n";
10346:                                 my $thumb_metaoutput = 
10347:                                     &write_metadata($dom,$confname,$formname,
10348:                                                     $targetdir,'tn-'.$file,$logfile);
10349:                                 push(@{$modified_urls},[$copyfile,$outfile]);
10350:                                 unless ($registered_cleanup) {
10351:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10352:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10353:                                     $registered_cleanup=1;
10354:                                 }
10355:                                 $madethumb = 1;
10356:                             } else {
10357:                                 print $logfile "\nUnable to write ".$copyfile.
10358:                                                ':'.$!."\n";
10359:                             }
10360:                         }
10361:                     }
10362:                 }
10363:             }
10364:         } else {
10365:             $output = $versionresult;
10366:         }
10367:     }
10368:     return ($output,$logourl,$madethumb);
10369: }
10370: 
10371: sub logo_versioning {
10372:     my ($targetdir,$file,$logfile) = @_;
10373:     my $target = $targetdir.'/'.$file;
10374:     my ($maxversion,$fn,$extn,$output);
10375:     $maxversion = 0;
10376:     if ($file =~ /^(.+)\.(\w+)$/) {
10377:         $fn=$1;
10378:         $extn=$2;
10379:     }
10380:     opendir(DIR,$targetdir);
10381:     while (my $filename=readdir(DIR)) {
10382:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
10383:             $maxversion=($1>$maxversion)?$1:$maxversion;
10384:         }
10385:     }
10386:     $maxversion++;
10387:     print $logfile "\nCreating old version ".$maxversion."\n";
10388:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
10389:     if (copy($target,$copyfile)) {
10390:         print $logfile "Copied old target to ".$copyfile."\n";
10391:         $copyfile=$copyfile.'.meta';
10392:         if (copy($target.'.meta',$copyfile)) {
10393:             print $logfile "Copied old target metadata to ".$copyfile."\n";
10394:             $output = 'ok';
10395:         } else {
10396:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
10397:             $output = &mt('Failed to copy old meta').", $!, ";
10398:         }
10399:     } else {
10400:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
10401:         $output = &mt('Failed to copy old target').", $!, ";
10402:     }
10403:     return $output;
10404: }
10405: 
10406: sub write_metadata {
10407:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
10408:     my (%metadatafields,%metadatakeys,$output);
10409:     $metadatafields{'title'}=$formname;
10410:     $metadatafields{'creationdate'}=time;
10411:     $metadatafields{'lastrevisiondate'}=time;
10412:     $metadatafields{'copyright'}='public';
10413:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
10414:                                          $env{'user.domain'};
10415:     $metadatafields{'authorspace'}=$confname.':'.$dom;
10416:     $metadatafields{'domain'}=$dom;
10417:     {
10418:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
10419:         my $mfh;
10420:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
10421:             foreach (sort(keys(%metadatafields))) {
10422:                 unless ($_=~/\./) {
10423:                     my $unikey=$_;
10424:                     $unikey=~/^([A-Za-z]+)/;
10425:                     my $tag=$1;
10426:                     $tag=~tr/A-Z/a-z/;
10427:                     print $mfh "\n\<$tag";
10428:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
10429:                         my $value=$metadatafields{$unikey.'.'.$_};
10430:                         $value=~s/\"/\'\'/g;
10431:                         print $mfh ' '.$_.'="'.$value.'"';
10432:                     }
10433:                     print $mfh '>'.
10434:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
10435:                             .'</'.$tag.'>';
10436:                 }
10437:             }
10438:             $output = 'ok';
10439:             print $logfile "\nWrote metadata";
10440:             close($mfh);
10441:         } else {
10442:             print $logfile "\nFailed to open metadata file";
10443:             $output = &mt('Could not write metadata');
10444:         }
10445:     }
10446:     return $output;
10447: }
10448: 
10449: sub notifysubscribed {
10450:     foreach my $targetsource (@{$modified_urls}){
10451:         next unless (ref($targetsource) eq 'ARRAY');
10452:         my ($target,$source)=@{$targetsource};
10453:         if ($source ne '') {
10454:             if (open(my $logfh,">>",$source.'.log')) {
10455:                 print $logfh "\nCleanup phase: Notifications\n";
10456:                 my @subscribed=&subscribed_hosts($target);
10457:                 foreach my $subhost (@subscribed) {
10458:                     print $logfh "\nNotifying host ".$subhost.':';
10459:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
10460:                     print $logfh $reply;
10461:                 }
10462:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
10463:                 foreach my $subhost (@subscribedmeta) {
10464:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
10465:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
10466:                                                         $subhost);
10467:                     print $logfh $reply;
10468:                 }
10469:                 print $logfh "\n============ Done ============\n";
10470:                 close($logfh);
10471:             }
10472:         }
10473:     }
10474:     return OK;
10475: }
10476: 
10477: sub subscribed_hosts {
10478:     my ($target) = @_;
10479:     my @subscribed;
10480:     if (open(my $fh,"<","$target.subscription")) {
10481:         while (my $subline=<$fh>) {
10482:             if ($subline =~ /^($match_lonid):/) {
10483:                 my $host = $1;
10484:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
10485:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
10486:                         push(@subscribed,$host);
10487:                     }
10488:                 }
10489:             }
10490:         }
10491:     }
10492:     return @subscribed;
10493: }
10494: 
10495: sub check_switchserver {
10496:     my ($dom,$confname) = @_;
10497:     my ($allowed,$switchserver);
10498:     my $home = &Apache::lonnet::homeserver($confname,$dom);
10499:     if ($home eq 'no_host') {
10500:         $home = &Apache::lonnet::domain($dom,'primary');
10501:     }
10502:     my @ids=&Apache::lonnet::current_machine_ids();
10503:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
10504:     if (!$allowed) {
10505: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
10506:     }
10507:     return $switchserver;
10508: }
10509: 
10510: sub modify_quotas {
10511:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10512:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
10513:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
10514:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
10515:         $validationfieldsref);
10516:     if ($action eq 'quotas') {
10517:         $context = 'tools'; 
10518:     } else {
10519:         $context = $action;
10520:     }
10521:     if ($context eq 'requestcourses') {
10522:         @usertools = ('official','unofficial','community','textbook','placement','lti');
10523:         @options =('norequest','approval','validate','autolimit');
10524:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
10525:         %titles = &courserequest_titles();
10526:         $toolregexp = join('|',@usertools);
10527:         %conditions = &courserequest_conditions();
10528:         $confname = $dom.'-domainconfig';
10529:         my $servadm = $r->dir_config('lonAdmEMail');
10530:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10531:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
10532:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
10533:     } elsif ($context eq 'requestauthor') {
10534:         @usertools = ('author');
10535:         %titles = &authorrequest_titles();
10536:     } else {
10537:         @usertools = ('aboutme','blog','webdav','portfolio');
10538:         %titles = &tool_titles();
10539:     }
10540:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10541:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10542:     foreach my $key (keys(%env)) {
10543:         if ($context eq 'requestcourses') {
10544:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
10545:                 my $item = $1;
10546:                 my $type = $2;
10547:                 if ($type =~ /^limit_(.+)/) {
10548:                     $limithash{$item}{$1} = $env{$key};
10549:                 } else {
10550:                     $confhash{$item}{$type} = $env{$key};
10551:                 }
10552:             }
10553:         } elsif ($context eq 'requestauthor') {
10554:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
10555:                 $confhash{$1} = $env{$key};
10556:             }
10557:         } else {
10558:             if ($key =~ /^form\.quota_(.+)$/) {
10559:                 $confhash{'defaultquota'}{$1} = $env{$key};
10560:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
10561:                 $confhash{'authorquota'}{$1} = $env{$key};
10562:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
10563:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
10564:             }
10565:         }
10566:     }
10567:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10568:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
10569:         @approvalnotify = sort(@approvalnotify);
10570:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
10571:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
10572:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
10573:         foreach my $type (@hasuniquecode) {
10574:             if (grep(/^\Q$type\E$/,@crstypes)) {
10575:                 $confhash{'uniquecode'}{$type} = 1;
10576:             }
10577:         }
10578:         my (%newbook,%allpos);
10579:         if ($context eq 'requestcourses') {
10580:             foreach my $type ('textbooks','templates') {
10581:                 @{$allpos{$type}} = (); 
10582:                 my $invalid;
10583:                 if ($type eq 'textbooks') {
10584:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
10585:                 } else {
10586:                     $invalid = &mt('Invalid LON-CAPA course for template');
10587:                 }
10588:                 if ($env{'form.'.$type.'_addbook'}) {
10589:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
10590:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
10591:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
10592:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
10593:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10594:                         } else {
10595:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
10596:                             my $position = $env{'form.'.$type.'_addbook_pos'};
10597:                             $position =~ s/\D+//g;
10598:                             if ($position ne '') {
10599:                                 $allpos{$type}[$position] = $newbook{$type};
10600:                             }
10601:                         }
10602:                     } else {
10603:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10604:                     }
10605:                 }
10606:             } 
10607:         }
10608:         if (ref($domconfig{$action}) eq 'HASH') {
10609:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
10610:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
10611:                     $changes{'notify'}{'approval'} = 1;
10612:                 }
10613:             } else {
10614:                 if ($confhash{'notify'}{'approval'}) {
10615:                     $changes{'notify'}{'approval'} = 1;
10616:                 }
10617:             }
10618:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
10619:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
10620:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
10621:                         unless ($confhash{'uniquecode'}{$crstype}) {
10622:                             $changes{'uniquecode'} = 1;
10623:                         }
10624:                     }
10625:                     unless ($changes{'uniquecode'}) {
10626:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
10627:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
10628:                                 $changes{'uniquecode'} = 1;
10629:                             }
10630:                         }
10631:                     }
10632:                } else {
10633:                    $changes{'uniquecode'} = 1;
10634:                }
10635:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
10636:                 $changes{'uniquecode'} = 1;
10637:             }
10638:             if ($context eq 'requestcourses') {
10639:                 foreach my $type ('textbooks','templates') {
10640:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
10641:                         my %deletions;
10642:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
10643:                         if (@todelete) {
10644:                             map { $deletions{$_} = 1; } @todelete;
10645:                         }
10646:                         my %imgdeletions;
10647:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
10648:                         if (@todeleteimages) {
10649:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
10650:                         }
10651:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
10652:                         for (my $i=0; $i<=$maxnum; $i++) {
10653:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
10654:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
10655:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
10656:                                 if ($deletions{$key}) {
10657:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
10658:                                         #FIXME need to obsolete item in RES space
10659:                                     }
10660:                                     next;
10661:                                 } else {
10662:                                     my $newpos = $env{'form.'.$itemid};
10663:                                     $newpos =~ s/\D+//g;
10664:                                     foreach my $item ('subject','title','publisher','author') {
10665:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10666:                                                  ($type eq 'templates'));
10667:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
10668:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
10669:                                             $changes{$type}{$key} = 1;
10670:                                         }
10671:                                     }
10672:                                     $allpos{$type}[$newpos] = $key;
10673:                                 }
10674:                                 if ($imgdeletions{$key}) {
10675:                                     $changes{$type}{$key} = 1;
10676:                                     #FIXME need to obsolete item in RES space
10677:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
10678:                                     my ($cdom,$cnum) = split(/_/,$key);
10679:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10680:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10681:                                     } else {
10682:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
10683:                                                                                       $cdom,$cnum,$type,$configuserok,
10684:                                                                                       $switchserver,$author_ok);
10685:                                         if ($imgurl) {
10686:                                             $confhash{$type}{$key}{'image'} = $imgurl;
10687:                                             $changes{$type}{$key} = 1; 
10688:                                         }
10689:                                         if ($error) {
10690:                                             &Apache::lonnet::logthis($error);
10691:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10692:                                         }
10693:                                     }
10694:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
10695:                                     $confhash{$type}{$key}{'image'} = 
10696:                                         $domconfig{$action}{$type}{$key}{'image'};
10697:                                 }
10698:                             }
10699:                         }
10700:                     }
10701:                 }
10702:             }
10703:         } else {
10704:             if ($confhash{'notify'}{'approval'}) {
10705:                 $changes{'notify'}{'approval'} = 1;
10706:             }
10707:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
10708:                 $changes{'uniquecode'} = 1;
10709:             }
10710:         }
10711:         if ($context eq 'requestcourses') {
10712:             foreach my $type ('textbooks','templates') {
10713:                 if ($newbook{$type}) {
10714:                     $changes{$type}{$newbook{$type}} = 1;
10715:                     foreach my $item ('subject','title','publisher','author') {
10716:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10717:                                  ($type eq 'template'));
10718:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
10719:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
10720:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
10721:                         }
10722:                     }
10723:                     if ($type eq 'textbooks') {
10724:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
10725:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
10726:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10727:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10728:                             } else {
10729:                                 my ($imageurl,$error) =
10730:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
10731:                                                             $configuserok,$switchserver,$author_ok);
10732:                                 if ($imageurl) {
10733:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
10734:                                 }
10735:                                 if ($error) {
10736:                                     &Apache::lonnet::logthis($error);
10737:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10738:                                 }
10739:                             }
10740:                         }
10741:                     }
10742:                 }
10743:                 if (@{$allpos{$type}} > 0) {
10744:                     my $idx = 0;
10745:                     foreach my $item (@{$allpos{$type}}) {
10746:                         if ($item ne '') {
10747:                             $confhash{$type}{$item}{'order'} = $idx;
10748:                             if (ref($domconfig{$action}) eq 'HASH') {
10749:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
10750:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
10751:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
10752:                                             $changes{$type}{$item} = 1;
10753:                                         }
10754:                                     }
10755:                                 }
10756:                             }
10757:                             $idx ++;
10758:                         }
10759:                     }
10760:                 }
10761:             }
10762:             if (ref($validationitemsref) eq 'ARRAY') {
10763:                 foreach my $item (@{$validationitemsref}) {
10764:                     if ($item eq 'fields') {
10765:                         my @changed;
10766:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
10767:                         if (@{$confhash{'validation'}{$item}} > 0) {
10768:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
10769:                         }
10770:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10771:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10772:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
10773:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
10774:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
10775:                                 } else {
10776:                                     @changed = @{$confhash{'validation'}{$item}};
10777:                                 }
10778:                             } else {
10779:                                 @changed = @{$confhash{'validation'}{$item}};
10780:                             }
10781:                         } else {
10782:                             @changed = @{$confhash{'validation'}{$item}};
10783:                         }
10784:                         if (@changed) {
10785:                             if ($confhash{'validation'}{$item}) {
10786:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
10787:                             } else {
10788:                                 $changes{'validation'}{$item} = &mt('None');
10789:                             }
10790:                         }
10791:                     } else {
10792:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
10793:                         if ($item eq 'markup') {
10794:                             if ($env{'form.requestcourses_validation_'.$item}) {
10795:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10796:                             }
10797:                         }
10798:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10799:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10800:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
10801:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10802:                                 }
10803:                             } else {
10804:                                 if ($confhash{'validation'}{$item} ne '') {
10805:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10806:                                 }
10807:                             }
10808:                         } else {
10809:                             if ($confhash{'validation'}{$item} ne '') {
10810:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10811:                             }
10812:                         }
10813:                     }
10814:                 }
10815:             }
10816:             if ($env{'form.validationdc'}) {
10817:                 my $newval = $env{'form.validationdc'};
10818:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10819:                 if (exists($domcoords{$newval})) {
10820:                     $confhash{'validation'}{'dc'} = $newval;
10821:                 }
10822:             }
10823:             if (ref($confhash{'validation'}) eq 'HASH') {
10824:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10825:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10826:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10827:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
10828:                                 if ($confhash{'validation'}{'dc'} eq '') {
10829:                                     $changes{'validation'}{'dc'} = &mt('None');
10830:                                 } else {
10831:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10832:                                 }
10833:                             }
10834:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
10835:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10836:                         }
10837:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
10838:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10839:                     }
10840:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
10841:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10842:                 }
10843:             } else {
10844:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10845:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10846:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10847:                             $changes{'validation'}{'dc'} = &mt('None');
10848:                         }
10849:                     }
10850:                 }
10851:             }
10852:         }
10853:     } else {
10854:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
10855:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
10856:     }
10857:     foreach my $item (@usertools) {
10858:         foreach my $type (@{$types},'default','_LC_adv') {
10859:             my $unset; 
10860:             if ($context eq 'requestcourses') {
10861:                 $unset = '0';
10862:                 if ($type eq '_LC_adv') {
10863:                     $unset = '';
10864:                 }
10865:                 if ($confhash{$item}{$type} eq 'autolimit') {
10866:                     $confhash{$item}{$type} .= '=';
10867:                     unless ($limithash{$item}{$type} =~ /\D/) {
10868:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
10869:                     }
10870:                 }
10871:             } elsif ($context eq 'requestauthor') {
10872:                 $unset = '0';
10873:                 if ($type eq '_LC_adv') {
10874:                     $unset = '';
10875:                 }
10876:             } else {
10877:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
10878:                     $confhash{$item}{$type} = 1;
10879:                 } else {
10880:                     $confhash{$item}{$type} = 0;
10881:                 }
10882:             }
10883:             if (ref($domconfig{$action}) eq 'HASH') {
10884:                 if ($action eq 'requestauthor') {
10885:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
10886:                         $changes{$type} = 1;
10887:                     }
10888:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
10889:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
10890:                         $changes{$item}{$type} = 1;
10891:                     }
10892:                 } else {
10893:                     if ($context eq 'requestcourses') {
10894:                         if ($confhash{$item}{$type} ne $unset) {
10895:                             $changes{$item}{$type} = 1;
10896:                         }
10897:                     } else {
10898:                         if (!$confhash{$item}{$type}) {
10899:                             $changes{$item}{$type} = 1;
10900:                         }
10901:                     }
10902:                 }
10903:             } else {
10904:                 if ($context eq 'requestcourses') {
10905:                     if ($confhash{$item}{$type} ne $unset) {
10906:                         $changes{$item}{$type} = 1;
10907:                     }
10908:                 } elsif ($context eq 'requestauthor') {
10909:                     if ($confhash{$type} ne $unset) {
10910:                         $changes{$type} = 1;
10911:                     }
10912:                 } else {
10913:                     if (!$confhash{$item}{$type}) {
10914:                         $changes{$item}{$type} = 1;
10915:                     }
10916:                 }
10917:             }
10918:         }
10919:     }
10920:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10921:         if (ref($domconfig{'quotas'}) eq 'HASH') {
10922:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10923:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
10924:                     if (exists($confhash{'defaultquota'}{$key})) {
10925:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
10926:                             $changes{'defaultquota'}{$key} = 1;
10927:                         }
10928:                     } else {
10929:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
10930:                     }
10931:                 }
10932:             } else {
10933:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
10934:                     if (exists($confhash{'defaultquota'}{$key})) {
10935:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
10936:                             $changes{'defaultquota'}{$key} = 1;
10937:                         }
10938:                     } else {
10939:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
10940:                     }
10941:                 }
10942:             }
10943:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10944:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
10945:                     if (exists($confhash{'authorquota'}{$key})) {
10946:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
10947:                             $changes{'authorquota'}{$key} = 1;
10948:                         }
10949:                     } else {
10950:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
10951:                     }
10952:                 }
10953:             }
10954:         }
10955:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
10956:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
10957:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10958:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10959:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
10960:                             $changes{'defaultquota'}{$key} = 1;
10961:                         }
10962:                     } else {
10963:                         if (!exists($domconfig{'quotas'}{$key})) {
10964:                             $changes{'defaultquota'}{$key} = 1;
10965:                         }
10966:                     }
10967:                 } else {
10968:                     $changes{'defaultquota'}{$key} = 1;
10969:                 }
10970:             }
10971:         }
10972:         if (ref($confhash{'authorquota'}) eq 'HASH') {
10973:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
10974:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10975:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10976:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
10977:                             $changes{'authorquota'}{$key} = 1;
10978:                         }
10979:                     } else {
10980:                         $changes{'authorquota'}{$key} = 1;
10981:                     }
10982:                 } else {
10983:                     $changes{'authorquota'}{$key} = 1;
10984:                 }
10985:             }
10986:         }
10987:     }
10988: 
10989:     if ($context eq 'requestauthor') {
10990:         $domdefaults{'requestauthor'} = \%confhash;
10991:     } else {
10992:         foreach my $key (keys(%confhash)) {
10993:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
10994:                 $domdefaults{$key} = $confhash{$key};
10995:             }
10996:         }
10997:     }
10998: 
10999:     my %quotahash = (
11000:                       $action => { %confhash }
11001:                     );
11002:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
11003:                                              $dom);
11004:     if ($putresult eq 'ok') {
11005:         if (keys(%changes) > 0) {
11006:             my $cachetime = 24*60*60;
11007:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11008:             if (ref($lastactref) eq 'HASH') {
11009:                 $lastactref->{'domdefaults'} = 1;
11010:             }
11011:             $resulttext = &mt('Changes made:').'<ul>';
11012:             unless (($context eq 'requestcourses') ||
11013:                     ($context eq 'requestauthor')) {
11014:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
11015:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
11016:                     foreach my $type (@{$types},'default') {
11017:                         if (defined($changes{'defaultquota'}{$type})) {
11018:                             my $typetitle = $usertypes->{$type};
11019:                             if ($type eq 'default') {
11020:                                 $typetitle = $othertitle;
11021:                             }
11022:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
11023:                         }
11024:                     }
11025:                     $resulttext .= '</ul></li>';
11026:                 }
11027:                 if (ref($changes{'authorquota'}) eq 'HASH') {
11028:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
11029:                     foreach my $type (@{$types},'default') {
11030:                         if (defined($changes{'authorquota'}{$type})) {
11031:                             my $typetitle = $usertypes->{$type};
11032:                             if ($type eq 'default') {
11033:                                 $typetitle = $othertitle;
11034:                             }
11035:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
11036:                         }
11037:                     }
11038:                     $resulttext .= '</ul></li>';
11039:                 }
11040:             }
11041:             my %newenv;
11042:             foreach my $item (@usertools) {
11043:                 my (%haschgs,%inconf);
11044:                 if ($context eq 'requestauthor') {
11045:                     %haschgs = %changes;
11046:                     %inconf = %confhash;
11047:                 } else {
11048:                     if (ref($changes{$item}) eq 'HASH') {
11049:                         %haschgs = %{$changes{$item}};
11050:                     }
11051:                     if (ref($confhash{$item}) eq 'HASH') {
11052:                         %inconf = %{$confhash{$item}};
11053:                     }
11054:                 }
11055:                 if (keys(%haschgs) > 0) {
11056:                     my $newacc = 
11057:                         &Apache::lonnet::usertools_access($env{'user.name'},
11058:                                                           $env{'user.domain'},
11059:                                                           $item,'reload',$context);
11060:                     if (($context eq 'requestcourses') ||
11061:                         ($context eq 'requestauthor')) {
11062:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
11063:                             $newenv{'environment.canrequest.'.$item} = $newacc;
11064:                         }
11065:                     } else {
11066:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
11067:                             $newenv{'environment.availabletools.'.$item} = $newacc;
11068:                         }
11069:                     }
11070:                     unless ($context eq 'requestauthor') {
11071:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
11072:                     }
11073:                     foreach my $type (@{$types},'default','_LC_adv') {
11074:                         if ($haschgs{$type}) {
11075:                             my $typetitle = $usertypes->{$type};
11076:                             if ($type eq 'default') {
11077:                                 $typetitle = $othertitle;
11078:                             } elsif ($type eq '_LC_adv') {
11079:                                 $typetitle = 'LON-CAPA Advanced Users'; 
11080:                             }
11081:                             if ($inconf{$type}) {
11082:                                 if ($context eq 'requestcourses') {
11083:                                     my $cond;
11084:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
11085:                                         if ($1 eq '') {
11086:                                             $cond = &mt('(Automatic processing of any request).');
11087:                                         } else {
11088:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
11089:                                         }
11090:                                     } else { 
11091:                                         $cond = $conditions{$inconf{$type}};
11092:                                     }
11093:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
11094:                                 } elsif ($context eq 'requestauthor') {
11095:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
11096:                                                              $titles{$inconf{$type}},$typetitle);
11097: 
11098:                                 } else {
11099:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
11100:                                 }
11101:                             } else {
11102:                                 if ($type eq '_LC_adv') {
11103:                                     if ($inconf{$type} eq '0') {
11104:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11105:                                     } else { 
11106:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
11107:                                     }
11108:                                 } else {
11109:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11110:                                 }
11111:                             }
11112:                         }
11113:                     }
11114:                     unless ($context eq 'requestauthor') {
11115:                         $resulttext .= '</ul></li>';
11116:                     }
11117:                 }
11118:             }
11119:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
11120:                 if (ref($changes{'notify'}) eq 'HASH') {
11121:                     if ($changes{'notify'}{'approval'}) {
11122:                         if (ref($confhash{'notify'}) eq 'HASH') {
11123:                             if ($confhash{'notify'}{'approval'}) {
11124:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
11125:                             } else {
11126:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
11127:                             }
11128:                         }
11129:                     }
11130:                 }
11131:             }
11132:             if ($action eq 'requestcourses') {
11133:                 my @offon = ('off','on');
11134:                 if ($changes{'uniquecode'}) {
11135:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
11136:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
11137:                         $resulttext .= '<li>'.
11138:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
11139:                                        '</li>';
11140:                     } else {
11141:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
11142:                                        '</li>';
11143:                     }
11144:                 }
11145:                 foreach my $type ('textbooks','templates') {
11146:                     if (ref($changes{$type}) eq 'HASH') {
11147:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
11148:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
11149:                             my %coursehash = &Apache::lonnet::coursedescription($key);
11150:                             my $coursetitle = $coursehash{'description'};
11151:                             my $position = $confhash{$type}{$key}{'order'} + 1;
11152:                             $resulttext .= '<li>';
11153:                             foreach my $item ('subject','title','publisher','author') {
11154:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
11155:                                          ($type eq 'templates'));
11156:                                 my $name = $item.':';
11157:                                 $name =~ s/^(\w)/\U$1/;
11158:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
11159:                             }
11160:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
11161:                             if ($type eq 'textbooks') {
11162:                                 if ($confhash{$type}{$key}{'image'}) {
11163:                                     $resulttext .= ' '.&mt('Image: [_1]',
11164:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
11165:                                                    ' alt="Textbook cover" />').'<br />';
11166:                                 }
11167:                             }
11168:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
11169:                         }
11170:                         $resulttext .= '</ul></li>';
11171:                     }
11172:                 }
11173:                 if (ref($changes{'validation'}) eq 'HASH') {
11174:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
11175:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
11176:                         foreach my $item (@{$validationitemsref}) {
11177:                             if (exists($changes{'validation'}{$item})) {
11178:                                 if ($item eq 'markup') {
11179:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11180:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
11181:                                 } else {
11182:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11183:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
11184:                                 }
11185:                             }
11186:                         }
11187:                         if (exists($changes{'validation'}{'dc'})) {
11188:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
11189:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
11190:                         }
11191:                     }
11192:                 }
11193:             }
11194:             $resulttext .= '</ul>';
11195:             if (keys(%newenv)) {
11196:                 &Apache::lonnet::appenv(\%newenv);
11197:             }
11198:         } else {
11199:             if ($context eq 'requestcourses') {
11200:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
11201:             } elsif ($context eq 'requestauthor') {
11202:                 $resulttext = &mt('No changes made to rights to request author space.');
11203:             } else {
11204:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
11205:             }
11206:         }
11207:     } else {
11208:         $resulttext = '<span class="LC_error">'.
11209: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11210:     }
11211:     if ($errors) {
11212:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
11213:                        '<ul>'.$errors.'</ul></p>';
11214:     }
11215:     return $resulttext;
11216: }
11217: 
11218: sub process_textbook_image {
11219:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
11220:     my $filename = $env{'form.'.$caller.'.filename'};
11221:     my ($error,$url);
11222:     my ($width,$height) = (50,50);
11223:     if ($configuserok eq 'ok') {
11224:         if ($switchserver) {
11225:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
11226:                          $switchserver);
11227:         } elsif ($author_ok eq 'ok') {
11228:             my ($result,$imageurl) =
11229:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11230:                              "$type/$cdom/$cnum/cover",$width,$height);
11231:             if ($result eq 'ok') {
11232:                 $url = $imageurl;
11233:             } else {
11234:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11235:             }
11236:         } else {
11237:             $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);
11238:         }
11239:     } else {
11240:         $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);
11241:     }
11242:     return ($url,$error);
11243: }
11244: 
11245: sub modify_ltitools {
11246:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11247:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11248:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11249:     my $confname = $dom.'-domainconfig';
11250:     my $servadm = $r->dir_config('lonAdmEMail');
11251:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11252:     my (%posslti,%possfield);
11253:     my @courseroles = ('cc','in','ta','ep','st');
11254:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
11255:     map { $posslti{$_} = 1; } @ltiroles;
11256:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
11257:     map { $possfield{$_} = 1; } @allfields;
11258:     my %lt = &ltitools_names(); 
11259:     if ($env{'form.ltitools_add'}) {
11260:         my $title = $env{'form.ltitools_add_title'};
11261:         $title =~ s/(`)/'/g;
11262:         ($newid,my $error) = &get_ltitools_id($dom,$title);
11263:         if ($newid) {
11264:             my $position = $env{'form.ltitools_add_pos'};
11265:             $position =~ s/\D+//g;
11266:             if ($position ne '') {
11267:                 $allpos[$position] = $newid;
11268:             }
11269:             $changes{$newid} = 1;
11270:             foreach my $item ('title','url','key','secret','lifetime') {
11271:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
11272:                 if ($item eq 'lifetime') {
11273:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
11274:                 }
11275:                 if ($env{'form.ltitools_add_'.$item}) {
11276:                     if (($item eq 'key') || ($item eq 'secret')) {
11277:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11278:                     } else {
11279:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11280:                     }
11281:                 }
11282:             }
11283:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
11284:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
11285:             }
11286:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
11287:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
11288:             }
11289:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
11290:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
11291:             } else {
11292:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
11293:             }
11294:             foreach my $item ('width','height','linktext','explanation') {
11295:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
11296:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
11297:                 if (($item eq 'width') || ($item eq 'height')) {
11298:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
11299:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
11300:                     }
11301:                 } else {
11302:                     if ($env{'form.ltitools_add_'.$item} ne '') {
11303:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
11304:                     }
11305:                 }
11306:             }
11307:             if ($env{'form.ltitools_add_target'} eq 'window') {
11308:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11309:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
11310:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11311:             } else {
11312:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
11313:             }
11314:             foreach my $item ('passback','roster') {
11315:                 if ($env{'form.ltitools_'.$item.'_add'}) {
11316:                     $confhash{$newid}{$item} = 1;
11317:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
11318:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
11319:                         $lifetime =~ s/^\s+|\s+$//g;
11320:                         if ($lifetime =~ /^\d+\.?\d*$/) {
11321:                             $confhash{$newid}{$item.'valid'} = $lifetime;
11322:                         }
11323:                     }
11324:                 }
11325:             }
11326:             if ($env{'form.ltitools_add_image.filename'} ne '') {
11327:                 my ($imageurl,$error) =
11328:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
11329:                                             $configuserok,$switchserver,$author_ok);
11330:                 if ($imageurl) {
11331:                     $confhash{$newid}{'image'} = $imageurl;
11332:                 }
11333:                 if ($error) {
11334:                     &Apache::lonnet::logthis($error);
11335:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11336:                 }
11337:             }
11338:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
11339:             foreach my $field (@fields) {
11340:                 if ($possfield{$field}) {
11341:                     if ($field eq 'roles') {
11342:                         foreach my $role (@courseroles) {
11343:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
11344:                             if (($choice ne '') && ($posslti{$choice})) {
11345:                                 $confhash{$newid}{'roles'}{$role} = $choice;
11346:                                 if ($role eq 'cc') {
11347:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
11348:                                 }
11349:                             }
11350:                         }
11351:                     } else {
11352:                         $confhash{$newid}{'fields'}{$field} = 1;
11353:                     }
11354:                 }
11355:             }
11356:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
11357:                 if ($confhash{$newid}{'fields'}{'user'}) {
11358:                     if ($env{'form.ltitools_userincdom_add'}) {
11359:                         $confhash{$newid}{'incdom'} = 1;
11360:                     }
11361:                 }
11362:             }
11363:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
11364:             foreach my $item (@courseconfig) {
11365:                 $confhash{$newid}{'crsconf'}{$item} = 1;
11366:             }
11367:             if ($env{'form.ltitools_add_custom'}) {
11368:                 my $name = $env{'form.ltitools_add_custom_name'};
11369:                 my $value = $env{'form.ltitools_add_custom_value'};
11370:                 $value =~ s/(`)/'/g;
11371:                 $name =~ s/(`)/'/g;
11372:                 $confhash{$newid}{'custom'}{$name} = $value;
11373:             }
11374:         } else {
11375:             my $error = &mt('Failed to acquire unique ID for new external tool');   
11376:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11377:         }
11378:     }
11379:     if (ref($domconfig{$action}) eq 'HASH') {
11380:         my %deletions;
11381:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
11382:         if (@todelete) {
11383:             map { $deletions{$_} = 1; } @todelete;
11384:         }
11385:         my %customadds;
11386:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
11387:         if (@newcustom) {
11388:             map { $customadds{$_} = 1; } @newcustom;
11389:         } 
11390:         my %imgdeletions;
11391:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
11392:         if (@todeleteimages) {
11393:             map { $imgdeletions{$_} = 1; } @todeleteimages;
11394:         }
11395:         my $maxnum = $env{'form.ltitools_maxnum'};
11396:         for (my $i=0; $i<=$maxnum; $i++) {
11397:             my $itemid = $env{'form.ltitools_id_'.$i};
11398:             $itemid =~ s/\D+//g;
11399:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11400:                 if ($deletions{$itemid}) {
11401:                     if ($domconfig{$action}{$itemid}{'image'}) {
11402:                         #FIXME need to obsolete item in RES space
11403:                     }
11404:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
11405:                     next;
11406:                 } else {
11407:                     my $newpos = $env{'form.ltitools_'.$itemid};
11408:                     $newpos =~ s/\D+//g;
11409:                     foreach my $item ('title','url','lifetime') {
11410:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11411:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
11412:                             $changes{$itemid} = 1;
11413:                         }
11414:                     }
11415:                     foreach my $item ('key','secret') {
11416:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11417:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
11418:                             $changes{$itemid} = 1;
11419:                         }
11420:                     }
11421:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
11422:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
11423:                     }
11424:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
11425:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
11426:                     }
11427:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
11428:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
11429:                     } else {
11430:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
11431:                     }
11432:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
11433:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
11434:                             $changes{$itemid} = 1;
11435:                         }
11436:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
11437:                         $changes{$itemid} = 1;
11438:                     }
11439:                     foreach my $size ('width','height') {
11440:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
11441:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
11442:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
11443:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
11444:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11445:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
11446:                                     $changes{$itemid} = 1;
11447:                                 }
11448:                             } else {
11449:                                 $changes{$itemid} = 1;
11450:                             }
11451:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11452:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
11453:                                 $changes{$itemid} = 1;
11454:                             }
11455:                         }
11456:                     }
11457:                     foreach my $item ('linktext','explanation') {
11458:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
11459:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
11460:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
11461:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11462:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11463:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
11464:                                     $changes{$itemid} = 1;
11465:                                 }
11466:                             } else {
11467:                                 $changes{$itemid} = 1;
11468:                             }
11469:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11470:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
11471:                                 $changes{$itemid} = 1;
11472:                             }
11473:                         }
11474:                     }
11475:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
11476:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11477:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
11478:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11479:                     } else {
11480:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
11481:                     }
11482:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11483:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
11484:                             $changes{$itemid} = 1;
11485:                         }
11486:                     } else {
11487:                         $changes{$itemid} = 1;
11488:                     }
11489:                     foreach my $extra ('passback','roster') {
11490:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
11491:                             $confhash{$itemid}{$extra} = 1;
11492:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
11493:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
11494:                                 $lifetime =~ s/^\s+|\s+$//g;
11495:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
11496:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
11497:                                 }
11498:                             }
11499:                         }
11500:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
11501:                             $changes{$itemid} = 1;
11502:                         }
11503:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
11504:                             $changes{$itemid} = 1;
11505:                         }
11506:                     }
11507:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
11508:                     foreach my $item ('label','title','target','linktext','explanation','append') {
11509:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
11510:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
11511:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
11512:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
11513:                                     $changes{$itemid} = 1;
11514:                                 }
11515:                             } else {
11516:                                 $changes{$itemid} = 1;
11517:                             }
11518:                         }
11519:                     }
11520:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
11521:                     foreach my $field (@fields) {
11522:                         if ($possfield{$field}) {
11523:                             if ($field eq 'roles') {
11524:                                 foreach my $role (@courseroles) {
11525:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
11526:                                     if (($choice ne '') && ($posslti{$choice})) {
11527:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
11528:                                         if ($role eq 'cc') {
11529:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
11530:                                         }
11531:                                     }
11532:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
11533:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
11534:                                             $changes{$itemid} = 1;
11535:                                         }
11536:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
11537:                                         $changes{$itemid} = 1;
11538:                                     }
11539:                                 }
11540:                             } else {
11541:                                 $confhash{$itemid}{'fields'}{$field} = 1;
11542:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
11543:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
11544:                                         $changes{$itemid} = 1;
11545:                                     }
11546:                                 } else {
11547:                                     $changes{$itemid} = 1;
11548:                                 }
11549:                             }
11550:                         }
11551:                     }
11552:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11553:                         if ($confhash{$itemid}{'fields'}{'user'}) {
11554:                             if ($env{'form.ltitools_userincdom_'.$i}) {
11555:                                 $confhash{$itemid}{'incdom'} = 1;
11556:                             }
11557:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
11558:                                 $changes{$itemid} = 1;
11559:                             }
11560:                         }
11561:                     }
11562:                     $allpos[$newpos] = $itemid;
11563:                 }
11564:                 if ($imgdeletions{$itemid}) {
11565:                     $changes{$itemid} = 1;
11566:                     #FIXME need to obsolete item in RES space
11567:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
11568:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
11569:                                                                  $itemid,$configuserok,$switchserver,
11570:                                                                  $author_ok);
11571:                     if ($imgurl) {
11572:                         $confhash{$itemid}{'image'} = $imgurl;
11573:                         $changes{$itemid} = 1;
11574:                     }
11575:                     if ($error) {
11576:                         &Apache::lonnet::logthis($error);
11577:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11578:                     }
11579:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
11580:                     $confhash{$itemid}{'image'} =
11581:                        $domconfig{$action}{$itemid}{'image'};
11582:                 }
11583:                 if ($customadds{$i}) {
11584:                     my $name = $env{'form.ltitools_custom_name_'.$i};
11585:                     $name =~ s/(`)/'/g;
11586:                     $name =~ s/^\s+//;
11587:                     $name =~ s/\s+$//;
11588:                     my $value = $env{'form.ltitools_custom_value_'.$i};
11589:                     $value =~ s/(`)/'/g;
11590:                     $value =~ s/^\s+//;
11591:                     $value =~ s/\s+$//;
11592:                     if ($name ne '') {
11593:                         $confhash{$itemid}{'custom'}{$name} = $value;
11594:                         $changes{$itemid} = 1;
11595:                     }
11596:                 }
11597:                 my %customdels;
11598:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
11599:                 if (@customdeletions) {
11600:                     $changes{$itemid} = 1;
11601:                 }
11602:                 map { $customdels{$_} = 1; } @customdeletions;
11603:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
11604:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
11605:                         unless ($customdels{$key}) {
11606:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
11607:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
11608:                             }
11609:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
11610:                                 $changes{$itemid} = 1;
11611:                             }
11612:                         }
11613:                     }
11614:                 }
11615:                 unless ($changes{$itemid}) {
11616:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
11617:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
11618:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
11619:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
11620:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
11621:                                         $changes{$itemid} = 1;
11622:                                         last;
11623:                                     }
11624:                                 }
11625:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
11626:                                 $changes{$itemid} = 1;
11627:                             }
11628:                         }
11629:                         last if ($changes{$itemid});
11630:                     }
11631:                 }
11632:             }
11633:         }
11634:     }
11635:     if (@allpos > 0) {
11636:         my $idx = 0;
11637:         foreach my $itemid (@allpos) {
11638:             if ($itemid ne '') {
11639:                 $confhash{$itemid}{'order'} = $idx;
11640:                 if (ref($domconfig{$action}) eq 'HASH') {
11641:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11642:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
11643:                             $changes{$itemid} = 1;
11644:                         }
11645:                     }
11646:                 }
11647:                 $idx ++;
11648:             }
11649:         }
11650:     }
11651:     my %ltitoolshash = (
11652:                           $action => { %confhash }
11653:                        );
11654:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
11655:                                              $dom);
11656:     if ($putresult eq 'ok') {
11657:         my %ltienchash = (
11658:                              $action => { %encconfig }
11659:                          );
11660:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
11661:         if (keys(%changes) > 0) {
11662:             my $cachetime = 24*60*60;
11663:             my %ltiall = %confhash;
11664:             foreach my $id (keys(%ltiall)) {
11665:                 if (ref($encconfig{$id}) eq 'HASH') {
11666:                     foreach my $item ('key','secret') {
11667:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
11668:                     }
11669:                 }
11670:             }
11671:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
11672:             if (ref($lastactref) eq 'HASH') {
11673:                 $lastactref->{'ltitools'} = 1;
11674:             }
11675:             $resulttext = &mt('Changes made:').'<ul>';
11676:             my %bynum;
11677:             foreach my $itemid (sort(keys(%changes))) {
11678:                 my $position = $confhash{$itemid}{'order'};
11679:                 $bynum{$position} = $itemid;
11680:             }
11681:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
11682:                 my $itemid = $bynum{$pos}; 
11683:                 if (ref($confhash{$itemid}) ne 'HASH') {
11684:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
11685:                 } else {
11686:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
11687:                     if ($confhash{$itemid}{'image'}) {
11688:                         $resulttext .= '&nbsp;'.
11689:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
11690:                                        ' alt="'.&mt('Tool Provider icon').'" />';
11691:                     }
11692:                     $resulttext .= '</li><ul>';
11693:                     my $position = $pos + 1;
11694:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
11695:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
11696:                         if ($confhash{$itemid}{$item} ne '') {
11697:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
11698:                         }
11699:                     }
11700:                     if ($encconfig{$itemid}{'key'} ne '') {
11701:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
11702:                     }
11703:                     if ($encconfig{$itemid}{'secret'} ne '') {
11704:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
11705:                         my $num = length($encconfig{$itemid}{'secret'});
11706:                         $resulttext .= ('*'x$num).'</li>';
11707:                     }
11708:                     $resulttext .= '<li>'.&mt('Configurable in course:');
11709:                     my @possconfig = ('label','title','target','linktext','explanation','append');
11710:                     my $numconfig = 0; 
11711:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
11712:                         foreach my $item (@possconfig) {
11713:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
11714:                                 $numconfig ++;
11715:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
11716:                             }
11717:                         }
11718:                     }
11719:                     if (!$numconfig) {
11720:                         $resulttext .= &mt('None');
11721:                     }
11722:                     $resulttext .= '</li>';
11723:                     foreach my $item ('passback','roster') {
11724:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
11725:                         if ($confhash{$itemid}{$item}) {
11726:                             $resulttext .= &mt('Yes');
11727:                             if ($confhash{$itemid}{$item.'valid'}) {
11728:                                 if ($item eq 'passback') {
11729:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
11730:                                                            $confhash{$itemid}{$item.'valid'});
11731:                                 } else {
11732:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
11733:                                                            $confhash{$itemid}{$item.'valid'});
11734:                                 }
11735:                             }
11736:                         } else {
11737:                             $resulttext .= &mt('No');
11738:                         }
11739:                         $resulttext .= '</li>';
11740:                     }
11741:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
11742:                         my $displaylist;
11743:                         if ($confhash{$itemid}{'display'}{'target'}) {
11744:                             $displaylist = &mt('Display target').':&nbsp;'.
11745:                                            $confhash{$itemid}{'display'}{'target'}.',';
11746:                         }
11747:                         foreach my $size ('width','height') { 
11748:                             if ($confhash{$itemid}{'display'}{$size}) {
11749:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
11750:                                                 $confhash{$itemid}{'display'}{$size}.',';
11751:                             }
11752:                         }
11753:                         if ($displaylist) {
11754:                             $displaylist =~ s/,$//;
11755:                             $resulttext .= '<li>'.$displaylist.'</li>';
11756:                         }
11757:                         foreach my $item ('linktext','explanation') {
11758:                             if ($confhash{$itemid}{'display'}{$item}) {
11759:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
11760:                             }
11761:                         }
11762:                     }
11763:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11764:                         my $fieldlist;
11765:                         foreach my $field (@allfields) {
11766:                             if ($confhash{$itemid}{'fields'}{$field}) {
11767:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
11768:                             }
11769:                         }
11770:                         if ($fieldlist) {
11771:                             $fieldlist =~ s/,$//;
11772:                             if ($confhash{$itemid}{'fields'}{'user'}) {
11773:                                 if ($confhash{$itemid}{'incdom'}) {
11774:                                     $fieldlist .= ' ('.&mt('username:domain').')';
11775:                                 } else {
11776:                                     $fieldlist .= ' ('.&mt('username').')';
11777:                                 }
11778:                             }
11779:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
11780:                         }
11781:                     }
11782:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
11783:                         my $rolemaps;
11784:                         foreach my $role (@courseroles) {
11785:                             if ($confhash{$itemid}{'roles'}{$role}) {
11786:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
11787:                                              $confhash{$itemid}{'roles'}{$role}.',';
11788:                             }
11789:                         }
11790:                         if ($rolemaps) {
11791:                             $rolemaps =~ s/,$//; 
11792:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
11793:                         }
11794:                     }
11795:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
11796:                         my $customlist;
11797:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
11798:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
11799:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
11800:                             } 
11801:                         }
11802:                         if ($customlist) {
11803:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
11804:                         }
11805:                     } 
11806:                     $resulttext .= '</ul></li>';
11807:                 }
11808:             }
11809:             $resulttext .= '</ul>';
11810:         } else {
11811:             $resulttext = &mt('No changes made.');
11812:         }
11813:     } else {
11814:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11815:     }
11816:     if ($errors) {
11817:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11818:                        $errors.'</ul>';
11819:     }
11820:     return $resulttext;
11821: }
11822: 
11823: sub process_ltitools_image {
11824:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
11825:     my $filename = $env{'form.'.$caller.'.filename'};
11826:     my ($error,$url);
11827:     my ($width,$height) = (21,21);
11828:     if ($configuserok eq 'ok') {
11829:         if ($switchserver) {
11830:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
11831:                          $switchserver);
11832:         } elsif ($author_ok eq 'ok') {
11833:             my ($result,$imageurl,$madethumb) =
11834:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11835:                              "ltitools/$itemid/icon",$width,$height);
11836:             if ($result eq 'ok') {
11837:                 if ($madethumb) {
11838:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
11839:                     my $imagethumb = "$path/tn-".$imagefile;
11840:                     $url = $imagethumb;
11841:                 } else {
11842:                     $url = $imageurl;
11843:                 }
11844:             } else {
11845:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11846:             }
11847:         } else {
11848:             $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);
11849:         }
11850:     } else {
11851:         $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);
11852:     }
11853:     return ($url,$error);
11854: }
11855: 
11856: sub get_ltitools_id {
11857:     my ($cdom,$title) = @_;
11858:     # get lock on ltitools db
11859:     my $lockhash = {
11860:                       lock => $env{'user.name'}.
11861:                               ':'.$env{'user.domain'},
11862:                    };
11863:     my $tries = 0;
11864:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11865:     my ($id,$error);
11866:  
11867:     while (($gotlock ne 'ok') && ($tries<10)) {
11868:         $tries ++;
11869:         sleep (0.1);
11870:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11871:     }
11872:     if ($gotlock eq 'ok') {
11873:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
11874:         if ($currids{'lock'}) {
11875:             delete($currids{'lock'});
11876:             if (keys(%currids)) {
11877:                 my @curr = sort { $a <=> $b } keys(%currids);
11878:                 if ($curr[-1] =~ /^\d+$/) {
11879:                     $id = 1 + $curr[-1];
11880:                 }
11881:             } else {
11882:                 $id = 1;
11883:             }
11884:             if ($id) {
11885:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
11886:                     $error = 'nostore';
11887:                 }
11888:             } else {
11889:                 $error = 'nonumber';
11890:             }
11891:         }
11892:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
11893:     } else {
11894:         $error = 'nolock';
11895:     }
11896:     return ($id,$error);
11897: }
11898: 
11899: sub modify_lti {
11900:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11901:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11902:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11903:     my (%posslti,%posslticrs,%posscrstype);
11904:     my @courseroles = ('cc','in','ta','ep','st');
11905:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
11906:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
11907:     my @coursetypes = ('official','unofficial','community','textbook','placement');
11908:     my %coursetypetitles = &Apache::lonlocal::texthash (
11909:                                official   => 'Official',
11910:                                unofficial => 'Unofficial',
11911:                                community  => 'Community',
11912:                                textbook   => 'Textbook',
11913:                                placement  => 'Placement Test',
11914:     );
11915:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11916:     my %lt = &lti_names();
11917:     map { $posslti{$_} = 1; } @ltiroles;
11918:     map { $posslticrs{$_} = 1; } @lticourseroles;
11919:     map { $posscrstype{$_} = 1; } @coursetypes;
11920: 
11921:     my %menutitles = &ltimenu_titles();
11922: 
11923:     my (@items,%deletions,%itemids);
11924:     if ($env{'form.lti_add'}) {
11925:         my $consumer = $env{'form.lti_consumer_add'};
11926:         $consumer =~ s/(`)/'/g;
11927:         ($newid,my $error) = &get_lti_id($dom,$consumer);
11928:         if ($newid) {
11929:             $itemids{'add'} = $newid;
11930:             push(@items,'add');
11931:             $changes{$newid} = 1;
11932:         } else {
11933:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
11934:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11935:         }
11936:     }
11937:     if (ref($domconfig{$action}) eq 'HASH') {
11938:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
11939:         if (@todelete) {
11940:             map { $deletions{$_} = 1; } @todelete;
11941:         }
11942:         my $maxnum = $env{'form.lti_maxnum'};
11943:         for (my $i=0; $i<=$maxnum; $i++) {
11944:             my $itemid = $env{'form.lti_id_'.$i};
11945:             $itemid =~ s/\D+//g;
11946:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11947:                 if ($deletions{$itemid}) {
11948:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
11949:                 } else {
11950:                    push(@items,$i);
11951:                    $itemids{$i} = $itemid;
11952:                 }
11953:             }
11954:         }
11955:     }
11956:     foreach my $idx (@items) {
11957:         my $itemid = $itemids{$idx};
11958:         next unless ($itemid);
11959:         my $position = $env{'form.lti_pos_'.$idx};
11960:         $position =~ s/\D+//g;
11961:         if ($position ne '') {
11962:             $allpos[$position] = $itemid;
11963:         }
11964:         foreach my $item ('consumer','key','secret','lifetime','requser') {
11965:             my $formitem = 'form.lti_'.$item.'_'.$idx;
11966:             $env{$formitem} =~ s/(`)/'/g;
11967:             if ($item eq 'lifetime') {
11968:                 $env{$formitem} =~ s/[^\d.]//g;
11969:             }
11970:             if ($env{$formitem} ne '') {
11971:                 if (($item eq 'key') || ($item eq 'secret')) {
11972:                     $encconfig{$itemid}{$item} = $env{$formitem};
11973:                 } else {
11974:                     $confhash{$itemid}{$item} = $env{$formitem};
11975:                     unless (($idx eq 'add') || ($changes{$itemid})) {
11976:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
11977:                             $changes{$itemid} = 1;
11978:                         }
11979:                     }
11980:                 }
11981:             }
11982:         }
11983:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
11984:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
11985:         }
11986:         if ($confhash{$itemid}{'requser'}) {
11987:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
11988:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
11989:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
11990:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
11991:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
11992:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
11993:                 $mapuser =~ s/(`)/'/g;
11994:                 $mapuser =~ s/^\s+|\s+$//g; 
11995:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
11996:             }
11997:             foreach my $ltirole (@lticourseroles) {
11998:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
11999:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
12000:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
12001:                 }
12002:             }
12003:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
12004:             my @makeuser;
12005:             foreach my $ltirole (sort(@possmakeuser)) {
12006:                 if ($posslti{$ltirole}) {
12007:                     push(@makeuser,$ltirole);
12008:                 }
12009:             }
12010:             $confhash{$itemid}{'makeuser'} = \@makeuser;
12011:             if (@makeuser) {
12012:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
12013:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
12014:                     $confhash{$itemid}{'lcauth'} = $lcauth;
12015:                     if ($lcauth ne 'internal') {
12016:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
12017:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
12018:                         $lcauthparm =~ s/`//g;
12019:                         if ($lcauthparm ne '') {
12020:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
12021:                         }
12022:                     }
12023:                 } else {
12024:                     $confhash{$itemid}{'lcauth'} = 'lti';
12025:                 }
12026:             }
12027:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
12028:             if (@possinstdata) {
12029:                 foreach my $field (@possinstdata) {
12030:                     if (exists($fieldtitles{$field})) {
12031:                         push(@{$confhash{$itemid}{'instdata'}});
12032:                     }
12033:                 }
12034:             }
12035:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
12036:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
12037:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
12038:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
12039:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
12040:                 $mapcrs =~ s/(`)/'/g;
12041:                 $mapcrs =~ s/^\s+|\s+$//g;
12042:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
12043:             }
12044:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
12045:             my @crstypes;
12046:             foreach my $type (sort(@posstypes)) {
12047:                 if ($posscrstype{$type}) {
12048:                     push(@crstypes,$type);
12049:                 }
12050:             }
12051:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
12052:             if ($env{'form.lti_makecrs_'.$idx}) {
12053:                 $confhash{$itemid}{'makecrs'} = 1;
12054:             }
12055:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
12056:             my @selfenroll;
12057:             foreach my $type (sort(@possenroll)) {
12058:                 if ($posslticrs{$type}) {
12059:                     push(@selfenroll,$type);
12060:                 }
12061:             }
12062:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
12063:             if ($env{'form.lti_crssec_'.$idx}) {
12064:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
12065:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
12066:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
12067:                     my $section = $env{'form.lti_customsection_'.$idx};
12068:                     $section =~ s/(`)/'/g;
12069:                     $section =~ s/^\s+|\s+$//g;
12070:                     if ($section ne '') {
12071:                         $confhash{$itemid}{'section'} = $section;
12072:                     }
12073:                 }
12074:             }
12075:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
12076:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
12077:                     $confhash{$itemid}{$field} = 1;
12078:                 }
12079:             }
12080:             if ($env{'form.lti_passback_'.$idx}) {
12081:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
12082:                     $confhash{$itemid}{'passbackformat'} = '1.0';
12083:                 } else {
12084:                     $confhash{$itemid}{'passbackformat'} = '1.1';
12085:                 }
12086:             }
12087:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
12088:                 $confhash{$itemid}{lcmenu} = [];
12089:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
12090:                 foreach my $field (@possmenu) {
12091:                     if (exists($menutitles{$field})) {
12092:                         if ($field eq 'grades') {
12093:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
12094:                         }
12095:                         push(@{$confhash{$itemid}{lcmenu}},$field);
12096:                     }
12097:                 }
12098:             }
12099:             unless (($idx eq 'add') || ($changes{$itemid})) {
12100:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu') {
12101:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
12102:                         $changes{$itemid} = 1;
12103:                     }
12104:                 }
12105:                 unless ($changes{$itemid}) {
12106:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
12107:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
12108:                             $changes{$itemid} = 1;
12109:                         }
12110:                     }
12111:                 }
12112:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
12113:                     unless ($changes{$itemid}) {
12114:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
12115:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12116:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
12117:                                                                                $confhash{$itemid}{$field});
12118:                                 if (@diffs) {
12119:                                     $changes{$itemid} = 1;
12120:                                 }
12121:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
12122:                                 $changes{$itemid} = 1;
12123:                             }
12124:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12125:                             if (@{$confhash{$itemid}{$field}} > 0) {
12126:                                 $changes{$itemid} = 1;
12127:                             }
12128:                         }
12129:                     }
12130:                 }
12131:                 unless ($changes{$itemid}) {
12132:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
12133:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12134:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
12135:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
12136:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
12137:                                     $changes{$itemid} = 1;
12138:                                     last;
12139:                                 }
12140:                             }
12141:                             unless ($changes{$itemid}) {
12142:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
12143:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
12144:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
12145:                                         $changes{$itemid} = 1;
12146:                                         last;
12147:                                     }
12148:                                 }
12149:                             }
12150:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
12151:                             $changes{$itemid} = 1;
12152:                         }
12153:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12154:                         unless ($changes{$itemid}) {
12155:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
12156:                                 $changes{$itemid} = 1;
12157:                             }
12158:                         }
12159:                     }
12160:                 }
12161:             }
12162:         }
12163:     }
12164:     if (@allpos > 0) {
12165:         my $idx = 0;
12166:         foreach my $itemid (@allpos) {
12167:             if ($itemid ne '') {
12168:                 $confhash{$itemid}{'order'} = $idx;
12169:                 if (ref($domconfig{$action}) eq 'HASH') {
12170:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12171:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
12172:                             $changes{$itemid} = 1;
12173:                         }
12174:                     }
12175:                 }
12176:                 $idx ++;
12177:             }
12178:         }
12179:     }
12180:     my %ltihash = (
12181:                           $action => { %confhash }
12182:                        );
12183:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
12184:                                              $dom);
12185:     if ($putresult eq 'ok') {
12186:         my %ltienchash = (
12187:                              $action => { %encconfig }
12188:                          );
12189:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
12190:         if (keys(%changes) > 0) {
12191:             my $cachetime = 24*60*60;
12192:             my %ltiall = %confhash;
12193:             foreach my $id (keys(%ltiall)) {
12194:                 if (ref($encconfig{$id}) eq 'HASH') {
12195:                     foreach my $item ('key','secret') {
12196:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
12197:                     }
12198:                 }
12199:             }
12200:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
12201:             if (ref($lastactref) eq 'HASH') {
12202:                 $lastactref->{'lti'} = 1;
12203:             }
12204:             $resulttext = &mt('Changes made:').'<ul>';
12205:             my %bynum;
12206:             foreach my $itemid (sort(keys(%changes))) {
12207:                 my $position = $confhash{$itemid}{'order'};
12208:                 $bynum{$position} = $itemid;
12209:             }
12210:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12211:                 my $itemid = $bynum{$pos};
12212:                 if (ref($confhash{$itemid}) ne 'HASH') {
12213:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12214:                 } else {
12215:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
12216:                     my $position = $pos + 1;
12217:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12218:                     foreach my $item ('version','lifetime') {
12219:                         if ($confhash{$itemid}{$item} ne '') {
12220:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
12221:                         }
12222:                     }
12223:                     if ($encconfig{$itemid}{'key'} ne '') {
12224:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
12225:                     }
12226:                     if ($encconfig{$itemid}{'secret'} ne '') {
12227:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
12228:                         my $num = length($encconfig{$itemid}{'secret'});
12229:                         $resulttext .= ('*'x$num).'</li>';
12230:                     }
12231:                     if ($confhash{$itemid}{'requser'}) {
12232:                         if ($confhash{$itemid}{'mapuser'}) {
12233:                             my $shownmapuser;
12234:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
12235:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
12236:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
12237:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
12238:                             } else {
12239:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
12240:                             }
12241:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
12242:                         }
12243:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12244:                             my $rolemaps;
12245:                             foreach my $role (@ltiroles) {
12246:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
12247:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
12248:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
12249:                                                                             'Course').',';
12250:                                 }
12251:                             }
12252:                             if ($rolemaps) {
12253:                                 $rolemaps =~ s/,$//;
12254:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
12255:                             }
12256:                         }
12257:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
12258:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
12259:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
12260:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
12261:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
12262:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
12263:                                 } else {
12264:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
12265:                                                        $confhash{$itemid}{'lcauth'});
12266:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
12267:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
12268:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
12269:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
12270:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
12271:                                         }
12272:                                     } else {
12273:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
12274:                                     }
12275:                                 }
12276:                                 $resulttext .= '</li>';
12277:                             } else {
12278:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
12279:                             }
12280:                         }
12281:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
12282:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
12283:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
12284:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
12285:                             } else {
12286:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
12287:                             }
12288:                         }
12289:                         if ($confhash{$itemid}{'mapcrs'}) {
12290:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
12291:                         }
12292:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
12293:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
12294:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
12295:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
12296:                                                '</li>';
12297:                             } else {
12298:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
12299:                             }
12300:                         }
12301:                         if ($confhash{$itemid}{'makecrs'}) {
12302:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
12303:                         } else {
12304:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
12305:                         }
12306:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
12307:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
12308:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
12309:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
12310:                                                '</li>';
12311:                             } else {
12312:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
12313:                             }
12314:                         }
12315:                         if ($confhash{$itemid}{'section'}) {
12316:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
12317:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
12318:                                                      ' (course_section_sourcedid)'.'</li>';  
12319:                             } else {
12320:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
12321:                                                       $confhash{$itemid}{'section'}.'</li>';
12322:                             }
12323:                         } else {
12324:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
12325:                         }
12326:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
12327:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
12328:                             if ($confhash{$itemid}{$item}) {
12329:                                 $resulttext .= &mt('Yes');
12330:                                 if ($item eq 'passback') {
12331:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
12332:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
12333:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
12334:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
12335:                                     }
12336:                                 }
12337:                             } else {
12338:                                 $resulttext .= &mt('No');
12339:                             }
12340:                             $resulttext .= '</li>';
12341:                         }
12342:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
12343:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
12344:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
12345:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
12346:                             } else {
12347:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
12348:                             }
12349:                         }
12350:                     }
12351:                     $resulttext .= '</ul></li>';
12352:                 }
12353:             }
12354:             $resulttext .= '</ul>';
12355:         } else {
12356:             $resulttext = &mt('No changes made.');
12357:         }
12358:     } else {
12359:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12360:     }
12361:     if ($errors) {
12362:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12363:                        $errors.'</ul>';
12364:     }
12365:     return $resulttext;
12366: }
12367: 
12368: sub get_lti_id {
12369:     my ($domain,$consumer) = @_;
12370:     # get lock on lti db
12371:     my $lockhash = {
12372:                       lock => $env{'user.name'}.
12373:                               ':'.$env{'user.domain'},
12374:                    };
12375:     my $tries = 0;
12376:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12377:     my ($id,$error);
12378: 
12379:     while (($gotlock ne 'ok') && ($tries<10)) {
12380:         $tries ++;
12381:         sleep (0.1);
12382:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12383:     }
12384:     if ($gotlock eq 'ok') {
12385:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
12386:         if ($currids{'lock'}) {
12387:             delete($currids{'lock'});
12388:             if (keys(%currids)) {
12389:                 my @curr = sort { $a <=> $b } keys(%currids);
12390:                 if ($curr[-1] =~ /^\d+$/) {
12391:                     $id = 1 + $curr[-1];
12392:                 }
12393:             } else {
12394:                 $id = 1;
12395:             }
12396:             if ($id) {
12397:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
12398:                     $error = 'nostore';
12399:                 }
12400:             } else {
12401:                 $error = 'nonumber';
12402:             }
12403:         }
12404:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
12405:     } else {
12406:         $error = 'nolock';
12407:     }
12408:     return ($id,$error);
12409: }
12410: 
12411: sub modify_autoenroll {
12412:     my ($dom,$lastactref,%domconfig) = @_;
12413:     my ($resulttext,%changes);
12414:     my %currautoenroll;
12415:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
12416:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
12417:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
12418:         }
12419:     }
12420:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
12421:     my %title = ( run => 'Auto-enrollment active',
12422:                   sender => 'Sender for notification messages',
12423:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
12424:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
12425:     my @offon = ('off','on');
12426:     my $sender_uname = $env{'form.sender_uname'};
12427:     my $sender_domain = $env{'form.sender_domain'};
12428:     if ($sender_domain eq '') {
12429:         $sender_uname = '';
12430:     } elsif ($sender_uname eq '') {
12431:         $sender_domain = '';
12432:     }
12433:     my $coowners = $env{'form.autoassign_coowners'};
12434:     my $failsafe = $env{'form.autoenroll_failsafe'};
12435:     $failsafe =~ s{^\s+|\s+$}{}g;
12436:     if ($failsafe =~ /\D/) {
12437:         undef($failsafe);
12438:     }
12439:     my %autoenrollhash =  (
12440:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
12441:                                        'sender_uname' => $sender_uname,
12442:                                        'sender_domain' => $sender_domain,
12443:                                        'co-owners' => $coowners,
12444:                                        'autofailsafe' => $failsafe,
12445:                                 }
12446:                      );
12447:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
12448:                                              $dom);
12449:     if ($putresult eq 'ok') {
12450:         if (exists($currautoenroll{'run'})) {
12451:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
12452:                  $changes{'run'} = 1;
12453:              }
12454:         } elsif ($autorun) {
12455:             if ($env{'form.autoenroll_run'} ne '1') {
12456:                  $changes{'run'} = 1;
12457:             }
12458:         }
12459:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
12460:             $changes{'sender'} = 1;
12461:         }
12462:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
12463:             $changes{'sender'} = 1;
12464:         }
12465:         if ($currautoenroll{'co-owners'} ne '') {
12466:             if ($currautoenroll{'co-owners'} ne $coowners) {
12467:                 $changes{'coowners'} = 1;
12468:             }
12469:         } elsif ($coowners) {
12470:             $changes{'coowners'} = 1;
12471:         }
12472:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
12473:             $changes{'autofailsafe'} = 1;
12474:         }
12475:         if (keys(%changes) > 0) {
12476:             $resulttext = &mt('Changes made:').'<ul>';
12477:             if ($changes{'run'}) {
12478:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
12479:             }
12480:             if ($changes{'sender'}) {
12481:                 if ($sender_uname eq '' || $sender_domain eq '') {
12482:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
12483:                 } else {
12484:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
12485:                 }
12486:             }
12487:             if ($changes{'coowners'}) {
12488:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
12489:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12490:                 if (ref($lastactref) eq 'HASH') {
12491:                     $lastactref->{'domainconfig'} = 1;
12492:                 }
12493:             }
12494:             if ($changes{'autofailsafe'}) {
12495:                 if ($failsafe ne '') {
12496:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
12497:                 } else {
12498:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
12499:                 }
12500:                 &Apache::lonnet::get_domain_defaults($dom,1);
12501:                 if (ref($lastactref) eq 'HASH') {
12502:                     $lastactref->{'domdefaults'} = 1;
12503:                 }
12504:             }
12505:             $resulttext .= '</ul>';
12506:         } else {
12507:             $resulttext = &mt('No changes made to auto-enrollment settings');
12508:         }
12509:     } else {
12510:         $resulttext = '<span class="LC_error">'.
12511: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12512:     }
12513:     return $resulttext;
12514: }
12515: 
12516: sub modify_autoupdate {
12517:     my ($dom,%domconfig) = @_;
12518:     my ($resulttext,%currautoupdate,%fields,%changes);
12519:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
12520:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
12521:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
12522:         }
12523:     }
12524:     my @offon = ('off','on');
12525:     my %title = &Apache::lonlocal::texthash (
12526:                    run => 'Auto-update:',
12527:                    classlists => 'Updates to user information in classlists?'
12528:                 );
12529:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12530:     my %fieldtitles = &Apache::lonlocal::texthash (
12531:                         id => 'Student/Employee ID',
12532:                         permanentemail => 'E-mail address',
12533:                         lastname => 'Last Name',
12534:                         firstname => 'First Name',
12535:                         middlename => 'Middle Name',
12536:                         generation => 'Generation',
12537:                       );
12538:     $othertitle = &mt('All users');
12539:     if (keys(%{$usertypes}) >  0) {
12540:         $othertitle = &mt('Other users');
12541:     }
12542:     foreach my $key (keys(%env)) {
12543:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
12544:             my ($usertype,$item) = ($1,$2);
12545:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
12546:                 if ($usertype eq 'default') {   
12547:                     push(@{$fields{$1}},$2);
12548:                 } elsif (ref($types) eq 'ARRAY') {
12549:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
12550:                         push(@{$fields{$1}},$2);
12551:                     }
12552:                 }
12553:             }
12554:         }
12555:     }
12556:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
12557:     @lockablenames = sort(@lockablenames);
12558:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
12559:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12560:         if (@changed) {
12561:             $changes{'lockablenames'} = 1;
12562:         }
12563:     } else {
12564:         if (@lockablenames) {
12565:             $changes{'lockablenames'} = 1;
12566:         }
12567:     }
12568:     my %updatehash = (
12569:                       autoupdate => { run => $env{'form.autoupdate_run'},
12570:                                       classlists => $env{'form.classlists'},
12571:                                       fields => {%fields},
12572:                                       lockablenames => \@lockablenames,
12573:                                     }
12574:                      );
12575:     foreach my $key (keys(%currautoupdate)) {
12576:         if (($key eq 'run') || ($key eq 'classlists')) {
12577:             if (exists($updatehash{autoupdate}{$key})) {
12578:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
12579:                     $changes{$key} = 1;
12580:                 }
12581:             }
12582:         } elsif ($key eq 'fields') {
12583:             if (ref($currautoupdate{$key}) eq 'HASH') {
12584:                 foreach my $item (@{$types},'default') {
12585:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
12586:                         my $change = 0;
12587:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
12588:                             if (!exists($fields{$item})) {
12589:                                 $change = 1;
12590:                                 last;
12591:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
12592:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
12593:                                     $change = 1;
12594:                                     last;
12595:                                 }
12596:                             }
12597:                         }
12598:                         if ($change) {
12599:                             push(@{$changes{$key}},$item);
12600:                         }
12601:                     } 
12602:                 }
12603:             }
12604:         } elsif ($key eq 'lockablenames') {
12605:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
12606:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12607:                 if (@changed) {
12608:                     $changes{'lockablenames'} = 1;
12609:                 }
12610:             } else {
12611:                 if (@lockablenames) {
12612:                     $changes{'lockablenames'} = 1;
12613:                 }
12614:             }
12615:         }
12616:     }
12617:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
12618:         if (@lockablenames) {
12619:             $changes{'lockablenames'} = 1;
12620:         }
12621:     }
12622:     foreach my $item (@{$types},'default') {
12623:         if (defined($fields{$item})) {
12624:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
12625:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
12626:                     my $change = 0;
12627:                     if (ref($fields{$item}) eq 'ARRAY') {
12628:                         foreach my $type (@{$fields{$item}}) {
12629:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
12630:                                 $change = 1;
12631:                                 last;
12632:                             }
12633:                         }
12634:                     }
12635:                     if ($change) {
12636:                         push(@{$changes{'fields'}},$item);
12637:                     }
12638:                 } else {
12639:                     push(@{$changes{'fields'}},$item);
12640:                 }
12641:             } else {
12642:                 push(@{$changes{'fields'}},$item);
12643:             }
12644:         }
12645:     }
12646:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
12647:                                              $dom);
12648:     if ($putresult eq 'ok') {
12649:         if (keys(%changes) > 0) {
12650:             $resulttext = &mt('Changes made:').'<ul>';
12651:             foreach my $key (sort(keys(%changes))) {
12652:                 if ($key eq 'lockablenames') {
12653:                     $resulttext .= '<li>';
12654:                     if (@lockablenames) {
12655:                         $usertypes->{'default'} = $othertitle;
12656:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
12657:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
12658:                     } else {
12659:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
12660:                     }
12661:                     $resulttext .= '</li>';
12662:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
12663:                     foreach my $item (@{$changes{$key}}) {
12664:                         my @newvalues;
12665:                         foreach my $type (@{$fields{$item}}) {
12666:                             push(@newvalues,$fieldtitles{$type});
12667:                         }
12668:                         my $newvaluestr;
12669:                         if (@newvalues > 0) {
12670:                             $newvaluestr = join(', ',@newvalues);
12671:                         } else {
12672:                             $newvaluestr = &mt('none');
12673:                         }
12674:                         if ($item eq 'default') {
12675:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
12676:                         } else {
12677:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
12678:                         }
12679:                     }
12680:                 } else {
12681:                     my $newvalue;
12682:                     if ($key eq 'run') {
12683:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
12684:                     } else {
12685:                         $newvalue = $offon[$env{'form.'.$key}];
12686:                     }
12687:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
12688:                 }
12689:             }
12690:             $resulttext .= '</ul>';
12691:         } else {
12692:             $resulttext = &mt('No changes made to autoupdates');
12693:         }
12694:     } else {
12695:         $resulttext = '<span class="LC_error">'.
12696: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12697:     }
12698:     return $resulttext;
12699: }
12700: 
12701: sub modify_autocreate {
12702:     my ($dom,%domconfig) = @_;
12703:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
12704:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
12705:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
12706:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
12707:         }
12708:     }
12709:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
12710:                  req => 'Auto-creation of validated requests for official courses',
12711:                  xmldc => 'Identity of course creator of courses from XML files',
12712:                );
12713:     my @types = ('xml','req');
12714:     foreach my $item (@types) {
12715:         $newvals{$item} = $env{'form.autocreate_'.$item};
12716:         $newvals{$item} =~ s/\D//g;
12717:         $newvals{$item} = 0 if ($newvals{$item} eq '');
12718:     }
12719:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
12720:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12721:     unless (exists($domcoords{$newvals{'xmldc'}})) {
12722:         $newvals{'xmldc'} = '';
12723:     } 
12724:     %autocreatehash =  (
12725:                         autocreate => { xml => $newvals{'xml'},
12726:                                         req => $newvals{'req'},
12727:                                       }
12728:                        );
12729:     if ($newvals{'xmldc'} ne '') {
12730:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
12731:     }
12732:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
12733:                                              $dom);
12734:     if ($putresult eq 'ok') {
12735:         my @items = @types;
12736:         if ($newvals{'xml'}) {
12737:             push(@items,'xmldc');
12738:         }
12739:         foreach my $item (@items) {
12740:             if (exists($currautocreate{$item})) {
12741:                 if ($currautocreate{$item} ne $newvals{$item}) {
12742:                     $changes{$item} = 1;
12743:                 }
12744:             } elsif ($newvals{$item}) {
12745:                 $changes{$item} = 1;
12746:             }
12747:         }
12748:         if (keys(%changes) > 0) {
12749:             my @offon = ('off','on'); 
12750:             $resulttext = &mt('Changes made:').'<ul>';
12751:             foreach my $item (@types) {
12752:                 if ($changes{$item}) {
12753:                     my $newtxt = $offon[$newvals{$item}];
12754:                     $resulttext .= '<li>'.
12755:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
12756:                                        '<b>','</b>').
12757:                                    '</li>';
12758:                 }
12759:             }
12760:             if ($changes{'xmldc'}) {
12761:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
12762:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
12763:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
12764:             }
12765:             $resulttext .= '</ul>';
12766:         } else {
12767:             $resulttext = &mt('No changes made to auto-creation settings');
12768:         }
12769:     } else {
12770:         $resulttext = '<span class="LC_error">'.
12771:             &mt('An error occurred: [_1]',$putresult).'</span>';
12772:     }
12773:     return $resulttext;
12774: }
12775: 
12776: sub modify_directorysrch {
12777:     my ($dom,$lastactref,%domconfig) = @_;
12778:     my ($resulttext,%changes);
12779:     my %currdirsrch;
12780:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
12781:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
12782:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
12783:         }
12784:     }
12785:     my %title = ( available => 'Institutional directory search available',
12786:                   localonly => 'Other domains can search institution',
12787:                   lcavailable => 'LON-CAPA directory search available',
12788:                   lclocalonly => 'Other domains can search LON-CAPA domain',
12789:                   searchby => 'Search types',
12790:                   searchtypes => 'Search latitude');
12791:     my @offon = ('off','on');
12792:     my @otherdoms = ('Yes','No');
12793: 
12794:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
12795:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
12796:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
12797: 
12798:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12799:     if (keys(%{$usertypes}) == 0) {
12800:         @cansearch = ('default');
12801:     } else {
12802:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
12803:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
12804:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
12805:                     push(@{$changes{'cansearch'}},$type);
12806:                 }
12807:             }
12808:             foreach my $type (@cansearch) {
12809:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
12810:                     push(@{$changes{'cansearch'}},$type);
12811:                 }
12812:             }
12813:         } else {
12814:             push(@{$changes{'cansearch'}},@cansearch);
12815:         }
12816:     }
12817: 
12818:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
12819:         foreach my $by (@{$currdirsrch{'searchby'}}) {
12820:             if (!grep(/^\Q$by\E$/,@searchby)) {
12821:                 push(@{$changes{'searchby'}},$by);
12822:             }
12823:         }
12824:         foreach my $by (@searchby) {
12825:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
12826:                 push(@{$changes{'searchby'}},$by);
12827:             }
12828:         }
12829:     } else {
12830:         push(@{$changes{'searchby'}},@searchby);
12831:     }
12832: 
12833:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
12834:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
12835:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
12836:                 push(@{$changes{'searchtypes'}},$type);
12837:             }
12838:         }
12839:         foreach my $type (@searchtypes) {
12840:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
12841:                 push(@{$changes{'searchtypes'}},$type);
12842:             }
12843:         }
12844:     } else {
12845:         if (exists($currdirsrch{'searchtypes'})) {
12846:             foreach my $type (@searchtypes) {  
12847:                 if ($type ne $currdirsrch{'searchtypes'}) { 
12848:                     push(@{$changes{'searchtypes'}},$type);
12849:                 }
12850:             }
12851:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
12852:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
12853:             }   
12854:         } else {
12855:             push(@{$changes{'searchtypes'}},@searchtypes); 
12856:         }
12857:     }
12858: 
12859:     my %dirsrch_hash =  (
12860:             directorysrch => { available => $env{'form.dirsrch_available'},
12861:                                cansearch => \@cansearch,
12862:                                localonly => $env{'form.dirsrch_instlocalonly'},
12863:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
12864:                                lcavailable => $env{'form.dirsrch_domavailable'},
12865:                                searchby => \@searchby,
12866:                                searchtypes => \@searchtypes,
12867:                              }
12868:             );
12869:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
12870:                                              $dom);
12871:     if ($putresult eq 'ok') {
12872:         if (exists($currdirsrch{'available'})) {
12873:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
12874:                  $changes{'available'} = 1;
12875:              }
12876:         } else {
12877:             if ($env{'form.dirsrch_available'} eq '1') {
12878:                 $changes{'available'} = 1;
12879:             }
12880:         }
12881:         if (exists($currdirsrch{'lcavailable'})) {
12882:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
12883:                 $changes{'lcavailable'} = 1;
12884:             }
12885:         } else {
12886:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
12887:                 $changes{'lcavailable'} = 1;
12888:             }
12889:         }
12890:         if (exists($currdirsrch{'localonly'})) {
12891:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
12892:                 $changes{'localonly'} = 1;
12893:             }
12894:         } else {
12895:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
12896:                 $changes{'localonly'} = 1;
12897:             }
12898:         }
12899:         if (exists($currdirsrch{'lclocalonly'})) {
12900:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
12901:                 $changes{'lclocalonly'} = 1;
12902:             }
12903:         } else {
12904:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
12905:                 $changes{'lclocalonly'} = 1;
12906:             }
12907:         }
12908:         if (keys(%changes) > 0) {
12909:             $resulttext = &mt('Changes made:').'<ul>';
12910:             if ($changes{'available'}) {
12911:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
12912:             }
12913:             if ($changes{'lcavailable'}) {
12914:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
12915:             }
12916:             if ($changes{'localonly'}) {
12917:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
12918:             }
12919:             if ($changes{'lclocalonly'}) {
12920:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
12921:             }
12922:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
12923:                 my $chgtext;
12924:                 if (ref($usertypes) eq 'HASH') {
12925:                     if (keys(%{$usertypes}) > 0) {
12926:                         foreach my $type (@{$types}) {
12927:                             if (grep(/^\Q$type\E$/,@cansearch)) {
12928:                                 $chgtext .= $usertypes->{$type}.'; ';
12929:                             }
12930:                         }
12931:                         if (grep(/^default$/,@cansearch)) {
12932:                             $chgtext .= $othertitle;
12933:                         } else {
12934:                             $chgtext =~ s/\; $//;
12935:                         }
12936:                         $resulttext .=
12937:                             '<li>'.
12938:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
12939:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
12940:                             '</li>';
12941:                     }
12942:                 }
12943:             }
12944:             if (ref($changes{'searchby'}) eq 'ARRAY') {
12945:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
12946:                 my $chgtext;
12947:                 foreach my $type (@{$titleorder}) {
12948:                     if (grep(/^\Q$type\E$/,@searchby)) {
12949:                         if (defined($searchtitles->{$type})) {
12950:                             $chgtext .= $searchtitles->{$type}.'; ';
12951:                         }
12952:                     }
12953:                 }
12954:                 $chgtext =~ s/\; $//;
12955:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
12956:             }
12957:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
12958:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
12959:                 my $chgtext;
12960:                 foreach my $type (@{$srchtypeorder}) {
12961:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
12962:                         if (defined($srchtypes_desc->{$type})) {
12963:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
12964:                         }
12965:                     }
12966:                 }
12967:                 $chgtext =~ s/\; $//;
12968:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
12969:             }
12970:             $resulttext .= '</ul>';
12971:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
12972:             if (ref($lastactref) eq 'HASH') {
12973:                 $lastactref->{'directorysrch'} = 1;
12974:             }
12975:         } else {
12976:             $resulttext = &mt('No changes made to directory search settings');
12977:         }
12978:     } else {
12979:         $resulttext = '<span class="LC_error">'.
12980:                       &mt('An error occurred: [_1]',$putresult).'</span>';
12981:     }
12982:     return $resulttext;
12983: }
12984: 
12985: sub modify_contacts {
12986:     my ($dom,$lastactref,%domconfig) = @_;
12987:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
12988:     if (ref($domconfig{'contacts'}) eq 'HASH') {
12989:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
12990:             $currsetting{$key} = $domconfig{'contacts'}{$key};
12991:         }
12992:     }
12993:     my (%others,%to,%bcc,%includestr,%includeloc);
12994:     my @contacts = ('supportemail','adminemail');
12995:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
12996:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
12997:     my @toggles = ('reporterrors','reportupdates','reportstatus');
12998:     my @lonstatus = ('threshold','sysmail','weights','excluded');
12999:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
13000:     foreach my $type (@mailings) {
13001:         @{$newsetting{$type}} = 
13002:             &Apache::loncommon::get_env_multiple('form.'.$type);
13003:         foreach my $item (@contacts) {
13004:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
13005:                 $contacts_hash{contacts}{$type}{$item} = 1;
13006:             } else {
13007:                 $contacts_hash{contacts}{$type}{$item} = 0;
13008:             }
13009:         }
13010:         $others{$type} = $env{'form.'.$type.'_others'};
13011:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
13012:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13013:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
13014:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
13015:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
13016:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
13017:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
13018:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13019:             }
13020:         }
13021:     }
13022:     foreach my $item (@contacts) {
13023:         $to{$item} = $env{'form.'.$item};
13024:         $contacts_hash{'contacts'}{$item} = $to{$item};
13025:     }
13026:     foreach my $item (@toggles) {
13027:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13028:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
13029:         }
13030:     }
13031:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
13032:     foreach my $item (@lonstatus) {
13033:         if ($item eq 'excluded') {
13034:             my (%serverhomes,@excluded);
13035:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
13036:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
13037:             if (@possexcluded) {
13038:                 foreach my $id (sort(@possexcluded)) {
13039:                     if ($serverhomes{$id}) {
13040:                         push(@excluded,$id);
13041:                     }
13042:                 }
13043:             }
13044:             if (@excluded) {
13045:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
13046:             }
13047:         } elsif ($item eq 'weights') {
13048:             foreach my $type ('E','W','N') {
13049:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
13050:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
13051:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
13052:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
13053:                             $env{'form.error'.$item.'_'.$type};
13054:                     }
13055:                 }
13056:             }
13057:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
13058:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
13059:             if ($env{'form.error'.$item} =~ /^\d+$/) {
13060:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
13061:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
13062:                 }
13063:             }
13064:         }
13065:     }
13066:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
13067:         foreach my $field (@{$fields}) {
13068:             if (ref($possoptions->{$field}) eq 'ARRAY') {
13069:                 my $value = $env{'form.helpform_'.$field};
13070:                 $value =~ s/^\s+|\s+$//g;
13071:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
13072:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
13073:                     if ($field eq 'screenshot') {
13074:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
13075:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
13076:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
13077:                         }
13078:                     }
13079:                 }
13080:             }
13081:         }
13082:     }
13083:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13084:     my (@statuses,%usertypeshash,@overrides);
13085:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
13086:         @statuses = @{$types};
13087:         if (ref($usertypes) eq 'HASH') {
13088:             %usertypeshash = %{$usertypes};
13089:         }
13090:     }
13091:     if (@statuses) {
13092:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
13093:         foreach my $type (@possoverrides) {
13094:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
13095:                 push(@overrides,$type);
13096:             }
13097:         }
13098:         if (@overrides) {
13099:             foreach my $type (@overrides) {
13100:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
13101:                 foreach my $item (@contacts) {
13102:                     if (grep(/^\Q$item\E$/,@standard)) {
13103:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
13104:                         $newsetting{'override_'.$type}{$item} = 1;
13105:                     } else {
13106:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
13107:                         $newsetting{'override_'.$type}{$item} = 0;
13108:                     }
13109:                 }
13110:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
13111:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13112:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
13113:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13114:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
13115:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
13116:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
13117:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13118:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
13119:                 }
13120:             }     
13121:         }
13122:     }
13123:     if (keys(%currsetting) > 0) {
13124:         foreach my $item (@contacts) {
13125:             if ($to{$item} ne $currsetting{$item}) {
13126:                 $changes{$item} = 1;
13127:             }
13128:         }
13129:         foreach my $type (@mailings) {
13130:             foreach my $item (@contacts) {
13131:                 if (ref($currsetting{$type}) eq 'HASH') {
13132:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
13133:                         push(@{$changes{$type}},$item);
13134:                     }
13135:                 } else {
13136:                     push(@{$changes{$type}},@{$newsetting{$type}});
13137:                 }
13138:             }
13139:             if ($others{$type} ne $currsetting{$type}{'others'}) {
13140:                 push(@{$changes{$type}},'others');
13141:             }
13142:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13143:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
13144:                     push(@{$changes{$type}},'bcc'); 
13145:                 }
13146:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
13147:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
13148:                     push(@{$changes{$type}},'include');
13149:                 }
13150:             }
13151:         }
13152:         if (ref($fields) eq 'ARRAY') {
13153:             if (ref($currsetting{'helpform'}) eq 'HASH') {
13154:                 foreach my $field (@{$fields}) {
13155:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
13156:                         push(@{$changes{'helpform'}},$field);
13157:                     }
13158:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13159:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
13160:                             push(@{$changes{'helpform'}},'maxsize');
13161:                         }
13162:                     }
13163:                 }
13164:             } else {
13165:                 foreach my $field (@{$fields}) {
13166:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13167:                         push(@{$changes{'helpform'}},$field);
13168:                     }
13169:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13170:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13171:                             push(@{$changes{'helpform'}},'maxsize');
13172:                         }
13173:                     }
13174:                 }
13175:             }
13176:         }
13177:         if (@statuses) {
13178:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
13179:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
13180:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
13181:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
13182:                             foreach my $item (@contacts,'bcc','others','include') {
13183:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
13184:                                     push(@{$changes{'overrides'}},$key);
13185:                                     last;
13186:                                 }
13187:                             }
13188:                         } else {
13189:                             push(@{$changes{'overrides'}},$key);
13190:                         }
13191:                     }
13192:                 }
13193:                 foreach my $key (@overrides) {
13194:                     unless (exists($currsetting{'overrides'}{$key})) {
13195:                         push(@{$changes{'overrides'}},$key);
13196:                     }
13197:                 }
13198:             } else {
13199:                 foreach my $key (@overrides) {
13200:                     push(@{$changes{'overrides'}},$key); 
13201:                 }
13202:             }
13203:         }
13204:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
13205:             foreach my $key ('excluded','weights','threshold','sysmail') {
13206:                 if ($key eq 'excluded') {
13207:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13208:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
13209:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13210:                             (@{$currsetting{'lonstatus'}{$key}})) {
13211:                             my @diffs =
13212:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
13213:                                                                    $currsetting{'lonstatus'}{$key});
13214:                             if (@diffs) {
13215:                                 push(@{$changes{'lonstatus'}},$key);
13216:                             }
13217:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
13218:                             push(@{$changes{'lonstatus'}},$key);
13219:                         }
13220:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13221:                              (@{$currsetting{'lonstatus'}{$key}})) {
13222:                         push(@{$changes{'lonstatus'}},$key);
13223:                     }
13224:                 } elsif ($key eq 'weights') {
13225:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13226:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
13227:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13228:                             foreach my $type ('E','W','N','U') {
13229:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
13230:                                         $currsetting{'lonstatus'}{$key}{$type}) {
13231:                                     push(@{$changes{'lonstatus'}},$key);
13232:                                     last;
13233:                                 }
13234:                             }
13235:                         } else {
13236:                             foreach my $type ('E','W','N','U') {
13237:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
13238:                                     push(@{$changes{'lonstatus'}},$key);
13239:                                     last;
13240:                                 }
13241:                             }
13242:                         }
13243:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13244:                         foreach my $type ('E','W','N','U') {
13245:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
13246:                                 push(@{$changes{'lonstatus'}},$key);
13247:                                 last;
13248:                             }
13249:                         }
13250:                     }
13251:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
13252:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13253:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13254:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
13255:                                 push(@{$changes{'lonstatus'}},$key);
13256:                             }
13257:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
13258:                             push(@{$changes{'lonstatus'}},$key);
13259:                         }
13260:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13261:                         push(@{$changes{'lonstatus'}},$key);
13262:                     }
13263:                 }
13264:             }
13265:         } else {
13266:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13267:                 foreach my $key ('excluded','weights','threshold','sysmail') {
13268:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13269:                         push(@{$changes{'lonstatus'}},$key);
13270:                     }
13271:                 }
13272:             }
13273:         }
13274:     } else {
13275:         my %default;
13276:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
13277:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
13278:         $default{'errormail'} = 'adminemail';
13279:         $default{'packagesmail'} = 'adminemail';
13280:         $default{'helpdeskmail'} = 'supportemail';
13281:         $default{'otherdomsmail'} = 'supportemail';
13282:         $default{'lonstatusmail'} = 'adminemail';
13283:         $default{'requestsmail'} = 'adminemail';
13284:         $default{'updatesmail'} = 'adminemail';
13285:         foreach my $item (@contacts) {
13286:            if ($to{$item} ne $default{$item}) {
13287:                $changes{$item} = 1;
13288:            }
13289:         }
13290:         foreach my $type (@mailings) {
13291:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
13292:                 push(@{$changes{$type}},@{$newsetting{$type}});
13293:             }
13294:             if ($others{$type} ne '') {
13295:                 push(@{$changes{$type}},'others');
13296:             }
13297:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13298:                 if ($bcc{$type} ne '') {
13299:                     push(@{$changes{$type}},'bcc');
13300:                 }
13301:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
13302:                     push(@{$changes{$type}},'include');
13303:                 }
13304:             }
13305:         }
13306:         if (ref($fields) eq 'ARRAY') {
13307:             foreach my $field (@{$fields}) {
13308:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13309:                     push(@{$changes{'helpform'}},$field);
13310:                 }
13311:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13312:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13313:                         push(@{$changes{'helpform'}},'maxsize');
13314:                     }
13315:                 }
13316:             }
13317:         }
13318:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13319:             foreach my $key ('excluded','weights','threshold','sysmail') {
13320:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13321:                     push(@{$changes{'lonstatus'}},$key);
13322:                 }
13323:             }
13324:         }
13325:     }
13326:     foreach my $item (@toggles) {
13327:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
13328:             $changes{$item} = 1;
13329:         } elsif ((!$env{'form.'.$item}) &&
13330:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
13331:             $changes{$item} = 1;
13332:         }
13333:     }
13334:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
13335:                                              $dom);
13336:     if ($putresult eq 'ok') {
13337:         if (keys(%changes) > 0) {
13338:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13339:             if (ref($lastactref) eq 'HASH') {
13340:                 $lastactref->{'domainconfig'} = 1;
13341:             }
13342:             my ($titles,$short_titles)  = &contact_titles();
13343:             $resulttext = &mt('Changes made:').'<ul>';
13344:             foreach my $item (@contacts) {
13345:                 if ($changes{$item}) {
13346:                     $resulttext .= '<li>'.$titles->{$item}.
13347:                                     &mt(' set to: ').
13348:                                     '<span class="LC_cusr_emph">'.
13349:                                     $to{$item}.'</span></li>';
13350:                 }
13351:             }
13352:             foreach my $type (@mailings) {
13353:                 if (ref($changes{$type}) eq 'ARRAY') {
13354:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13355:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
13356:                     } else {
13357:                         $resulttext .= '<li>'.$titles->{$type}.': ';
13358:                     }
13359:                     my @text;
13360:                     foreach my $item (@{$newsetting{$type}}) {
13361:                         push(@text,$short_titles->{$item});
13362:                     }
13363:                     if ($others{$type} ne '') {
13364:                         push(@text,$others{$type});
13365:                     }
13366:                     if (@text) {
13367:                         $resulttext .= '<span class="LC_cusr_emph">'.
13368:                                        join(', ',@text).'</span>';
13369:                     }
13370:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13371:                         if ($bcc{$type} ne '') {
13372:                             my $bcctext;
13373:                             if (@text) {
13374:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
13375:                             } else {
13376:                                 $bcctext = '(Bcc)';
13377:                             }
13378:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
13379:                         } elsif (!@text) {
13380:                             $resulttext .= &mt('No one');
13381:                         }   
13382:                         if ($includestr{$type} ne '') {
13383:                             if ($includeloc{$type} eq 'b') {
13384:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
13385:                             } elsif ($includeloc{$type} eq 's') {
13386:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
13387:                             }
13388:                         }
13389:                     } elsif (!@text) {
13390:                         $resulttext .= &mt('No recipients');
13391:                     }
13392:                     $resulttext .= '</li>';
13393:                 }
13394:             }
13395:             if (ref($changes{'overrides'}) eq 'ARRAY') {
13396:                 my @deletions;
13397:                 foreach my $type (@{$changes{'overrides'}}) {
13398:                     if ($usertypeshash{$type}) {
13399:                         if (grep(/^\Q$type\E/,@overrides)) {
13400:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
13401:                                                       $usertypeshash{$type}).'<ul><li>';
13402:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
13403:                                 my @text;
13404:                                 foreach my $item (@contacts) {
13405:                                     if ($newsetting{'override_'.$type}{$item}) { 
13406:                                         push(@text,$short_titles->{$item});
13407:                                     }
13408:                                 }
13409:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
13410:                                     push(@text,$newsetting{'override_'.$type}{'others'});
13411:                                 }
13412:   
13413:                                 if (@text) {
13414:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
13415:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
13416:                                 }
13417:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
13418:                                     my $bcctext;
13419:                                     if (@text) {
13420:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
13421:                                     } else {
13422:                                         $bcctext = '(Bcc)';
13423:                                     }
13424:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
13425:                                 } elsif (!@text) {
13426:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
13427:                                 }
13428:                                 $resulttext .= '</li>';
13429:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
13430:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
13431:                                     if ($loc eq 'b') {
13432:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
13433:                                     } elsif ($loc eq 's') {
13434:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
13435:                                     }
13436:                                 }
13437:                             }
13438:                             $resulttext .= '</li></ul></li>';
13439:                         } else {
13440:                             push(@deletions,$usertypeshash{$type});
13441:                         }
13442:                     }
13443:                 }
13444:                 if (@deletions) {
13445:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
13446:                                               join(', ',@deletions)).'</li>';
13447:                 }
13448:             }
13449:             my @offon = ('off','on');
13450:             my $corelink = &core_link_msu();
13451:             if ($changes{'reporterrors'}) {
13452:                 $resulttext .= '<li>'.
13453:                                &mt('E-mail error reports to [_1] set to "'.
13454:                                    $offon[$env{'form.reporterrors'}].'".',
13455:                                    $corelink).
13456:                                '</li>';
13457:             }
13458:             if ($changes{'reportupdates'}) {
13459:                 $resulttext .= '<li>'.
13460:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
13461:                                     $offon[$env{'form.reportupdates'}].'".',
13462:                                     $corelink).
13463:                                 '</li>';
13464:             }
13465:             if ($changes{'reportstatus'}) {
13466:                 $resulttext .= '<li>'.
13467:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
13468:                                     $offon[$env{'form.reportstatus'}].'".',
13469:                                     $corelink).
13470:                                 '</li>';
13471:             }
13472:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
13473:                 $resulttext .= '<li>'.
13474:                                &mt('Nightly status check e-mail settings').':<ul>';
13475:                 my (%defval,%use_def,%shown);
13476:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
13477:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
13478:                 $defval{'weights'} =
13479:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
13480:                 $defval{'excluded'} = &mt('None');
13481:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
13482:                     foreach my $item ('threshold','sysmail','weights','excluded') {
13483:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
13484:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
13485:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
13486:                             } elsif ($item eq 'weights') {
13487:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
13488:                                     foreach my $type ('E','W','N','U') {
13489:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
13490:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
13491:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
13492:                                         } else {
13493:                                             $shown{$item} .= $lonstatus_defs->{$type};
13494:                                         }
13495:                                         $shown{$item} .= ', ';
13496:                                     }
13497:                                     $shown{$item} =~ s/, $//;
13498:                                 } else {
13499:                                     $shown{$item} = $defval{$item};
13500:                                 }
13501:                             } elsif ($item eq 'excluded') {
13502:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
13503:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
13504:                                 } else {
13505:                                     $shown{$item} = $defval{$item};
13506:                                 }
13507:                             }
13508:                         } else {
13509:                             $shown{$item} = $defval{$item};
13510:                         }
13511:                     }
13512:                 } else {
13513:                     foreach my $item ('threshold','weights','excluded','sysmail') {
13514:                         $shown{$item} = $defval{$item};
13515:                     }
13516:                 }
13517:                 foreach my $item ('threshold','weights','excluded','sysmail') {
13518:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
13519:                                           $shown{$item}).'</li>';
13520:                 }
13521:                 $resulttext .= '</ul></li>';
13522:             }
13523:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
13524:                 my (@optional,@required,@unused,$maxsizechg);
13525:                 foreach my $field (@{$changes{'helpform'}}) {
13526:                     if ($field eq 'maxsize') {
13527:                         $maxsizechg = 1;
13528:                         next;
13529:                     }
13530:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
13531:                         push(@optional,$field);
13532:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
13533:                         push(@unused,$field);
13534:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
13535:                         push(@required,$field);
13536:                     }
13537:                 }
13538:                 if (@optional) {
13539:                     $resulttext .= '<li>'.
13540:                                    &mt('Help form fields changed to "Optional": [_1].',
13541:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
13542:                                    '</li>';
13543:                 }
13544:                 if (@required) {
13545:                     $resulttext .= '<li>'.
13546:                                    &mt('Help form fields changed to "Required": [_1].',
13547:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
13548:                                    '</li>';
13549:                 }
13550:                 if (@unused) {
13551:                     $resulttext .= '<li>'.
13552:                                    &mt('Help form fields changed to "Not shown": [_1].',
13553:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
13554:                                    '</li>';
13555:                 }
13556:                 if ($maxsizechg) {
13557:                     $resulttext .= '<li>'.
13558:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
13559:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
13560:                                    '</li>';
13561:                 }
13562:             }
13563:             $resulttext .= '</ul>';
13564:         } else {
13565:             $resulttext = &mt('No changes made to contacts and form settings');
13566:         }
13567:     } else {
13568:         $resulttext = '<span class="LC_error">'.
13569:             &mt('An error occurred: [_1].',$putresult).'</span>';
13570:     }
13571:     return $resulttext;
13572: }
13573: 
13574: sub modify_usercreation {
13575:     my ($dom,%domconfig) = @_;
13576:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
13577:     my $warningmsg;
13578:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13579:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13580:             if ($key eq 'cancreate') {
13581:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13582:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13583:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
13584:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13585:                         } else {
13586:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13587:                         }
13588:                     }
13589:                 }
13590:             } elsif ($key eq 'email_rule') {
13591:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13592:             } else {
13593:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13594:             }
13595:         }
13596:     }
13597:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
13598:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
13599:     my @contexts = ('author','course','requestcrs');
13600:     foreach my $item(@contexts) {
13601:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
13602:     }
13603:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13604:         foreach my $item (@contexts) {
13605:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
13606:                 push(@{$changes{'cancreate'}},$item);
13607:             }
13608:         }
13609:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
13610:         foreach my $item (@contexts) {
13611:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
13612:                 if ($cancreate{$item} ne 'any') {
13613:                     push(@{$changes{'cancreate'}},$item);
13614:                 }
13615:             } else {
13616:                 if ($cancreate{$item} ne 'none') {
13617:                     push(@{$changes{'cancreate'}},$item);
13618:                 }
13619:             }
13620:         }
13621:     } else {
13622:         foreach my $item (@contexts)  {
13623:             push(@{$changes{'cancreate'}},$item);
13624:         }
13625:     }
13626: 
13627:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
13628:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
13629:             if (!grep(/^\Q$type\E$/,@username_rule)) {
13630:                 push(@{$changes{'username_rule'}},$type);
13631:             }
13632:         }
13633:         foreach my $type (@username_rule) {
13634:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
13635:                 push(@{$changes{'username_rule'}},$type);
13636:             }
13637:         }
13638:     } else {
13639:         push(@{$changes{'username_rule'}},@username_rule);
13640:     }
13641: 
13642:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
13643:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
13644:             if (!grep(/^\Q$type\E$/,@id_rule)) {
13645:                 push(@{$changes{'id_rule'}},$type);
13646:             }
13647:         }
13648:         foreach my $type (@id_rule) {
13649:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
13650:                 push(@{$changes{'id_rule'}},$type);
13651:             }
13652:         }
13653:     } else {
13654:         push(@{$changes{'id_rule'}},@id_rule);
13655:     }
13656: 
13657:     my @authen_contexts = ('author','course','domain');
13658:     my @authtypes = ('int','krb4','krb5','loc','lti');
13659:     my %authhash;
13660:     foreach my $item (@authen_contexts) {
13661:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
13662:         foreach my $auth (@authtypes) {
13663:             if (grep(/^\Q$auth\E$/,@authallowed)) {
13664:                 $authhash{$item}{$auth} = 1;
13665:             } else {
13666:                 $authhash{$item}{$auth} = 0;
13667:             }
13668:         }
13669:     }
13670:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
13671:         foreach my $item (@authen_contexts) {
13672:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
13673:                 foreach my $auth (@authtypes) {
13674:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
13675:                         push(@{$changes{'authtypes'}},$item);
13676:                         last;
13677:                     }
13678:                 }
13679:             }
13680:         }
13681:     } else {
13682:         foreach my $item (@authen_contexts) {
13683:             push(@{$changes{'authtypes'}},$item);
13684:         }
13685:     }
13686: 
13687:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
13688:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
13689:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
13690:     $save_usercreate{'id_rule'} = \@id_rule;
13691:     $save_usercreate{'username_rule'} = \@username_rule,
13692:     $save_usercreate{'authtypes'} = \%authhash;
13693: 
13694:     my %usercreation_hash =  (
13695:         usercreation     => \%save_usercreate,
13696:     );
13697: 
13698:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
13699:                                              $dom);
13700: 
13701:     if ($putresult eq 'ok') {
13702:         if (keys(%changes) > 0) {
13703:             $resulttext = &mt('Changes made:').'<ul>';
13704:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13705:                 my %lt = &usercreation_types();
13706:                 foreach my $type (@{$changes{'cancreate'}}) {
13707:                     my $chgtext = $lt{$type}.', ';
13708:                     if ($cancreate{$type} eq 'none') {
13709:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
13710:                     } elsif ($cancreate{$type} eq 'any') {
13711:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
13712:                     } elsif ($cancreate{$type} eq 'official') {
13713:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
13714:                     } elsif ($cancreate{$type} eq 'unofficial') {
13715:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
13716:                     }
13717:                     $resulttext .= '<li>'.$chgtext.'</li>';
13718:                 }
13719:             }
13720:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
13721:                 my ($rules,$ruleorder) = 
13722:                     &Apache::lonnet::inst_userrules($dom,'username');
13723:                 my $chgtext = '<ul>';
13724:                 foreach my $type (@username_rule) {
13725:                     if (ref($rules->{$type}) eq 'HASH') {
13726:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
13727:                     }
13728:                 }
13729:                 $chgtext .= '</ul>';
13730:                 if (@username_rule > 0) {
13731:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
13732:                 } else {
13733:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
13734:                 }
13735:             }
13736:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
13737:                 my ($idrules,$idruleorder) = 
13738:                     &Apache::lonnet::inst_userrules($dom,'id');
13739:                 my $chgtext = '<ul>';
13740:                 foreach my $type (@id_rule) {
13741:                     if (ref($idrules->{$type}) eq 'HASH') {
13742:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
13743:                     }
13744:                 }
13745:                 $chgtext .= '</ul>';
13746:                 if (@id_rule > 0) {
13747:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
13748:                 } else {
13749:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
13750:                 }
13751:             }
13752:             my %authname = &authtype_names();
13753:             my %context_title = &context_names();
13754:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
13755:                 my $chgtext = '<ul>';
13756:                 foreach my $type (@{$changes{'authtypes'}}) {
13757:                     my @allowed;
13758:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
13759:                     foreach my $auth (@authtypes) {
13760:                         if ($authhash{$type}{$auth}) {
13761:                             push(@allowed,$authname{$auth});
13762:                         }
13763:                     }
13764:                     if (@allowed > 0) {
13765:                         $chgtext .= join(', ',@allowed).'</li>';
13766:                     } else {
13767:                         $chgtext .= &mt('none').'</li>';
13768:                     }
13769:                 }
13770:                 $chgtext .= '</ul>';
13771:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
13772:                 $resulttext .= '</li>';
13773:             }
13774:             $resulttext .= '</ul>';
13775:         } else {
13776:             $resulttext = &mt('No changes made to user creation settings');
13777:         }
13778:     } else {
13779:         $resulttext = '<span class="LC_error">'.
13780:             &mt('An error occurred: [_1]',$putresult).'</span>';
13781:     }
13782:     if ($warningmsg ne '') {
13783:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13784:     }
13785:     return $resulttext;
13786: }
13787: 
13788: sub modify_selfcreation {
13789:     my ($dom,$lastactref,%domconfig) = @_;
13790:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
13791:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
13792:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13793:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
13794:     if (ref($typesref) eq 'ARRAY') {
13795:         @types = @{$typesref};
13796:     }
13797:     if (ref($usertypesref) eq 'HASH') {
13798:         %usertypes = %{$usertypesref};
13799:     }
13800:     $usertypes{'default'} = $othertitle;
13801: #
13802: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
13803: #
13804:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13805:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13806:             if ($key eq 'cancreate') {
13807:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13808:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13809:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
13810:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
13811:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
13812:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
13813:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
13814:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
13815:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13816:                         } else {
13817:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13818:                         }
13819:                     }
13820:                 }
13821:             } elsif ($key eq 'email_rule') {
13822:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13823:             } else {
13824:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13825:             }
13826:         }
13827:     }
13828: #
13829: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
13830: #
13831:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13832:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13833:             if ($key eq 'selfcreate') {
13834:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
13835:             } else {
13836:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
13837:             }
13838:         }
13839:     }
13840: #
13841: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
13842: #
13843:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13844:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
13845:             if ($key eq 'inststatusguest') {
13846:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
13847:             } else {
13848:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
13849:             }
13850:         }
13851:     }
13852: 
13853:     my @contexts = ('selfcreate');
13854:     @{$cancreate{'selfcreate'}} = ();
13855:     %{$cancreate{'emailusername'}} = ();
13856:     if (@types) {
13857:         @{$cancreate{'statustocreate'}} = ();
13858:     }
13859:     %{$cancreate{'selfcreateprocessing'}} = ();
13860:     %{$cancreate{'shibenv'}} = ();
13861:     %{$cancreate{'emailverified'}} = ();
13862:     %{$cancreate{'emailoptions'}} = ();
13863:     %{$cancreate{'emaildomain'}} = ();
13864:     my %selfcreatetypes = (
13865:                              sso   => 'users authenticated by institutional single sign on',
13866:                              login => 'users authenticated by institutional log-in',
13867:                              email => 'users verified by e-mail',
13868:                           );
13869: #
13870: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
13871: # is permitted.
13872: #
13873:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
13874: 
13875:     my (@statuses,%email_rule);
13876:     foreach my $item ('login','sso','email') {
13877:         if ($item eq 'email') {
13878:             if ($env{'form.cancreate_email'}) {
13879:                 if (@types) {
13880:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
13881:                     foreach my $status (@poss_statuses) {
13882:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
13883:                             push(@statuses,$status);
13884:                         }
13885:                     }
13886:                     $save_inststatus{'inststatusguest'} = \@statuses;
13887:                 } else {
13888:                     push(@statuses,'default');
13889:                 }
13890:                 if (@statuses) {
13891:                     my %curr_rule;
13892:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
13893:                         foreach my $type (@statuses) {
13894:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
13895:                         }
13896:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
13897:                         foreach my $type (@statuses) {
13898:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
13899:                         }
13900:                     }
13901:                     push(@{$cancreate{'selfcreate'}},'email');
13902:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
13903:                     my %curremaildom;
13904:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
13905:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
13906:                     }
13907:                     foreach my $type (@statuses) {
13908:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
13909:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
13910:                         }
13911:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
13912:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
13913:                         }
13914:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
13915: #
13916: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
13917: #
13918:                             my $chosen = $1;
13919:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
13920:                                 my $emaildom;
13921:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
13922:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
13923:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
13924:                                     if (ref($curremaildom{$type}) eq 'HASH') {
13925:                                         if (exists($curremaildom{$type}{$chosen})) {
13926:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
13927:                                                 push(@{$changes{'cancreate'}},'emaildomain');
13928:                                             }
13929:                                         } elsif ($emaildom ne '') {
13930:                                             push(@{$changes{'cancreate'}},'emaildomain');
13931:                                         }
13932:                                     } elsif ($emaildom ne '') {
13933:                                         push(@{$changes{'cancreate'}},'emaildomain');
13934:                                     } 
13935:                                 }
13936:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13937:                             } elsif ($chosen eq 'custom') {
13938:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
13939:                                 $email_rule{$type} = [];
13940:                                 if (ref($emailrules) eq 'HASH') {
13941:                                     foreach my $rule (@possemail_rules) {
13942:                                         if (exists($emailrules->{$rule})) {
13943:                                             push(@{$email_rule{$type}},$rule);
13944:                                         }
13945:                                     }
13946:                                 }
13947:                                 if (@{$email_rule{$type}}) {
13948:                                     $cancreate{'emailoptions'}{$type} = 'custom';
13949:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
13950:                                         if (@{$curr_rule{$type}} > 0) {
13951:                                             foreach my $rule (@{$curr_rule{$type}}) {
13952:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
13953:                                                     push(@{$changes{'email_rule'}},$type);
13954:                                                 }
13955:                                             }
13956:                                         }
13957:                                         foreach my $type (@{$email_rule{$type}}) {
13958:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
13959:                                                 push(@{$changes{'email_rule'}},$type);
13960:                                             }
13961:                                         }
13962:                                     } else {
13963:                                         push(@{$changes{'email_rule'}},$type);
13964:                                     }
13965:                                 }
13966:                             } else {
13967:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13968:                             }
13969:                         }
13970:                     }
13971:                     if (@types) {
13972:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13973:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
13974:                             if (@changed) {
13975:                                 push(@{$changes{'inststatus'}},'inststatusguest');
13976:                             }
13977:                         } else {
13978:                             push(@{$changes{'inststatus'}},'inststatusguest');
13979:                         }
13980:                     }
13981:                 } else {
13982:                     delete($env{'form.cancreate_email'});
13983:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13984:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13985:                             push(@{$changes{'inststatus'}},'inststatusguest');
13986:                         }
13987:                     }
13988:                 }
13989:             } else {
13990:                 $save_inststatus{'inststatusguest'} = [];
13991:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13992:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13993:                         push(@{$changes{'inststatus'}},'inststatusguest');
13994:                     }
13995:                 }
13996:             }
13997:         } else {
13998:             if ($env{'form.cancreate_'.$item}) {
13999:                 push(@{$cancreate{'selfcreate'}},$item);
14000:             }
14001:         }
14002:     }
14003:     my (%userinfo,%savecaptcha);
14004:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
14005: #
14006: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
14007: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
14008: #
14009: 
14010:     if ($env{'form.cancreate_email'}) {
14011:         push(@contexts,'emailusername');
14012:         if (@statuses) {
14013:             foreach my $type (@statuses) {
14014:                 if (ref($infofields) eq 'ARRAY') {
14015:                     foreach my $field (@{$infofields}) {
14016:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
14017:                             $cancreate{'emailusername'}{$type}{$field} = $1;
14018:                         }
14019:                     }
14020:                 }
14021:             }
14022:         }
14023: #
14024: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
14025: # queued requests for self-creation of account verified by e-mail.
14026: #
14027: 
14028:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
14029:         @approvalnotify = sort(@approvalnotify);
14030:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
14031:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
14032:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
14033:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
14034:                     push(@{$changes{'cancreate'}},'notify');
14035:                 }
14036:             } else {
14037:                 if ($cancreate{'notify'}{'approval'}) {
14038:                     push(@{$changes{'cancreate'}},'notify');
14039:                 }
14040:             }
14041:         } elsif ($cancreate{'notify'}{'approval'}) {
14042:             push(@{$changes{'cancreate'}},'notify');
14043:         }
14044: 
14045:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
14046:     }
14047: #  
14048: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
14049: # institutional log-in.
14050: #
14051:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
14052:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
14053:                ($domdefaults{'auth_def'} eq 'localauth'))) {
14054:             $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.').' '.
14055:                           &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.');
14056:         }
14057:     }
14058:     my @fields = ('lastname','firstname','middlename','generation',
14059:                   'permanentemail','id');
14060:     my @shibfields = (@fields,'inststatus');
14061:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14062: #
14063: # Where usernames may created for institutional log-in and/or institutional single sign on:
14064: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
14065: # may self-create accounts 
14066: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
14067: # which the user may supply, if institutional data is unavailable.
14068: #
14069:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
14070:         if (@types) {
14071:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
14072:             push(@contexts,'statustocreate');
14073:             foreach my $type (@types) {
14074:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
14075:                 foreach my $field (@fields) {
14076:                     if (grep(/^\Q$field\E$/,@modifiable)) {
14077:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
14078:                     } else {
14079:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
14080:                     }
14081:                 }
14082:             }
14083:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
14084:                 foreach my $type (@types) {
14085:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
14086:                         foreach my $field (@fields) {
14087:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
14088:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
14089:                                 push(@{$changes{'selfcreate'}},$type);
14090:                                 last;
14091:                             }
14092:                         }
14093:                     }
14094:                 }
14095:             } else {
14096:                 foreach my $type (@types) {
14097:                     push(@{$changes{'selfcreate'}},$type);
14098:                 }
14099:             }
14100:         }
14101:         foreach my $field (@shibfields) {
14102:             if ($env{'form.shibenv_'.$field} ne '') {
14103:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
14104:             }
14105:         }
14106:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
14107:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
14108:                 foreach my $field (@shibfields) {
14109:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
14110:                         push(@{$changes{'cancreate'}},'shibenv');
14111:                     }
14112:                 }
14113:             } else {
14114:                 foreach my $field (@shibfields) {
14115:                     if ($env{'form.shibenv_'.$field}) {
14116:                         push(@{$changes{'cancreate'}},'shibenv');
14117:                         last;
14118:                     }
14119:                 }
14120:             }
14121:         }
14122:     }
14123:     foreach my $item (@contexts) {
14124:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
14125:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
14126:                 if (ref($cancreate{$item}) eq 'ARRAY') {
14127:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
14128:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14129:                             push(@{$changes{'cancreate'}},$item);
14130:                         }
14131:                     }
14132:                 }
14133:             }
14134:             if (ref($cancreate{$item}) eq 'ARRAY') {
14135:                 foreach my $type (@{$cancreate{$item}}) {
14136:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
14137:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14138:                             push(@{$changes{'cancreate'}},$item);
14139:                         }
14140:                     }
14141:                 }
14142:             }
14143:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
14144:             if (ref($cancreate{$item}) eq 'HASH') {
14145:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
14146:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
14147:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
14148:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
14149:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14150:                                     push(@{$changes{'cancreate'}},$item);
14151:                                 }
14152:                             }
14153:                         }
14154:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14155:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
14156:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14157:                                 push(@{$changes{'cancreate'}},$item);
14158:                             }
14159:                         }
14160:                     }
14161:                 }
14162:                 foreach my $type (keys(%{$cancreate{$item}})) {
14163:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
14164:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
14165:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
14166:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
14167:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14168:                                         push(@{$changes{'cancreate'}},$item);
14169:                                     }
14170:                                 }
14171:                             } else {
14172:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14173:                                     push(@{$changes{'cancreate'}},$item);
14174:                                 }
14175:                             }
14176:                         }
14177:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14178:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
14179:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14180:                                 push(@{$changes{'cancreate'}},$item);
14181:                             }
14182:                         }
14183:                     }
14184:                 }
14185:             }
14186:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
14187:             if (ref($cancreate{$item}) eq 'ARRAY') {
14188:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
14189:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14190:                         push(@{$changes{'cancreate'}},$item);
14191:                     }
14192:                 }
14193:             }
14194:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14195:             if (ref($cancreate{$item}) eq 'HASH') {
14196:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14197:                     push(@{$changes{'cancreate'}},$item);
14198:                 }
14199:             }
14200:         } elsif ($item eq 'emailusername') {
14201:             if (ref($cancreate{$item}) eq 'HASH') {
14202:                 foreach my $type (keys(%{$cancreate{$item}})) {
14203:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
14204:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
14205:                             if ($cancreate{$item}{$type}{$field}) {
14206:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14207:                                     push(@{$changes{'cancreate'}},$item);
14208:                                 }
14209:                                 last;
14210:                             }
14211:                         }
14212:                     }
14213:                 }
14214:             }
14215:         }
14216:     }
14217: #
14218: # Populate %save_usercreate hash with updates to self-creation configuration.
14219: #
14220:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
14221:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
14222:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
14223:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
14224:     if (ref($cancreate{'notify'}) eq 'HASH') {
14225:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
14226:     }
14227:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
14228:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
14229:     }
14230:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
14231:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
14232:     }
14233:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
14234:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
14235:     }
14236:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
14237:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
14238:     }
14239:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
14240:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
14241:     }
14242:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
14243:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
14244:     }
14245:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
14246:     $save_usercreate{'email_rule'} = \%email_rule;
14247: 
14248:     my %userconfig_hash = (
14249:             usercreation     => \%save_usercreate,
14250:             usermodification => \%save_usermodify,
14251:             inststatus       => \%save_inststatus,
14252:     );
14253: 
14254:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
14255:                                              $dom);
14256: #
14257: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
14258: #
14259:     if ($putresult eq 'ok') {
14260:         if (keys(%changes) > 0) {
14261:             $resulttext = &mt('Changes made:').'<ul>';
14262:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
14263:                 my %lt = &selfcreation_types();
14264:                 foreach my $type (@{$changes{'cancreate'}}) {
14265:                     my $chgtext = '';
14266:                     if ($type eq 'selfcreate') {
14267:                         if (@{$cancreate{$type}} == 0) {
14268:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
14269:                         } else {
14270:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
14271:                                         '<ul>';
14272:                             foreach my $case (@{$cancreate{$type}}) {
14273:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
14274:                             }
14275:                             $chgtext .= '</ul>';
14276:                             if (ref($cancreate{$type}) eq 'ARRAY') {
14277:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
14278:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
14279:                                         if (@{$cancreate{'statustocreate'}} == 0) {
14280:                                             $chgtext .= '<span class="LC_warning">'.
14281:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
14282:                                                         '</span><br />';
14283:                                         }
14284:                                     }
14285:                                 }
14286:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
14287:                                     if (!@statuses) {
14288:                                         $chgtext .= '<span class="LC_warning">'.
14289:                                                     &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.").
14290:                                                     '</span><br />';
14291: 
14292:                                     }
14293:                                 }
14294:                             }
14295:                         }
14296:                     } elsif ($type eq 'shibenv') {
14297:                         if (keys(%{$cancreate{$type}}) == 0) {
14298:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
14299:                         } else {
14300:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
14301:                                         '<ul>';
14302:                             foreach my $field (@shibfields) {
14303:                                 next if ($cancreate{$type}{$field} eq '');
14304:                                 if ($field eq 'inststatus') {
14305:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
14306:                                 } else {
14307:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
14308:                                 }
14309:                             }
14310:                             $chgtext .= '</ul>';
14311:                         }
14312:                     } elsif ($type eq 'statustocreate') {
14313:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
14314:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
14315:                             if (@{$cancreate{'selfcreate'}} > 0) {
14316:                                 if (@{$cancreate{'statustocreate'}} == 0) {
14317:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
14318:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
14319:                                         $chgtext .= '<br />'.
14320:                                                     '<span class="LC_warning">'.
14321:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
14322:                                                     '</span>';
14323:                                     }
14324:                                 } elsif (keys(%usertypes) > 0) {
14325:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
14326:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
14327:                                     } else {
14328:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
14329:                                     }
14330:                                     $chgtext .= '<ul>';
14331:                                     foreach my $case (@{$cancreate{$type}}) {
14332:                                         if ($case eq 'default') {
14333:                                             $chgtext .= '<li>'.$othertitle.'</li>';
14334:                                         } else {
14335:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
14336:                                         }
14337:                                     }
14338:                                     $chgtext .= '</ul>';
14339:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
14340:                                         $chgtext .= '<span class="LC_warning">'.
14341:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
14342:                                                     '</span>';
14343:                                     }
14344:                                 }
14345:                             } else {
14346:                                 if (@{$cancreate{$type}} == 0) {
14347:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
14348:                                 } else {
14349:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
14350:                                 }
14351:                             }
14352:                             $chgtext .= '<br />';
14353:                         }
14354:                     } elsif ($type eq 'selfcreateprocessing') {
14355:                         my %choices = &Apache::lonlocal::texthash (
14356:                                                                     automatic => 'Automatic approval',
14357:                                                                     approval  => 'Queued for approval',
14358:                                                                   );
14359:                         if (@types) {
14360:                             if (@statuses) {
14361:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
14362:                                             '<ul>';
14363:                                 foreach my $status (@statuses) {
14364:                                     if ($status eq 'default') {
14365:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
14366:                                     } else {
14367:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
14368:                                     }
14369:                                 }
14370:                                 $chgtext .= '</ul>';
14371:                             }
14372:                         } else {
14373:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
14374:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
14375:                         }
14376:                     } elsif ($type eq 'emailverified') {
14377:                         my %options = &Apache::lonlocal::texthash (
14378:                                                                     all   => 'Same as e-mail',
14379:                                                                     first => 'Omit @domain',
14380:                                                                     free  => 'Free to choose',
14381:                                                                   );
14382:                         if (@types) {
14383:                             if (@statuses) {
14384:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
14385:                                             '<ul>';
14386:                                 foreach my $status (@statuses) {
14387:                                     if ($type eq 'default') {
14388:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14389:                                     } else {
14390:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14391:                                     }
14392:                                 }
14393:                                 $chgtext .= '</ul>';
14394:                             }
14395:                         } else {
14396:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
14397:                                             $options{$cancreate{'emailverified'}{'default'}});
14398:                         }
14399:                     } elsif ($type eq 'emailoptions') {
14400:                         my %options = &Apache::lonlocal::texthash (
14401:                                                                     any     => 'Any e-mail',
14402:                                                                     inst    => 'Institutional only',
14403:                                                                     noninst => 'Non-institutional only',
14404:                                                                     custom  => 'Custom restrictions',
14405:                                                                   );
14406:                         if (@types) {
14407:                             if (@statuses) {
14408:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
14409:                                             '<ul>';
14410:                                 foreach my $status (@statuses) {
14411:                                     if ($type eq 'default') {
14412:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14413:                                     } else {
14414:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14415:                                     }
14416:                                 }
14417:                                 $chgtext .= '</ul>';
14418:                             }
14419:                         } else {
14420:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
14421:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
14422:                             } else {
14423:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
14424:                                                 $options{$cancreate{'emailoptions'}{'default'}});
14425:                             }
14426:                         }
14427:                     } elsif ($type eq 'emaildomain') {
14428:                         my $output;
14429:                         if (@statuses) {
14430:                             foreach my $type (@statuses) {
14431:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
14432:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
14433:                                         if ($type eq 'default') {
14434:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14435:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14436:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14437:                                             } else {
14438:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
14439:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
14440:                                             }
14441:                                         } else {
14442:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14443:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14444:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14445:                                             } else {
14446:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
14447:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
14448:                                             }
14449:                                         }
14450:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
14451:                                         if ($type eq 'default') {
14452:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14453:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14454:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14455:                                             } else {
14456:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
14457:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
14458:                                             }
14459:                                         } else {
14460:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14461:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14462:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14463:                                             } else {
14464:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
14465:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
14466:                                             }
14467:                                         }
14468:                                     }
14469:                                 }
14470:                             }
14471:                         }
14472:                         if ($output ne '') {
14473:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
14474:                                         '<ul>'.$output.'</ul>';
14475:                         }
14476:                     } elsif ($type eq 'captcha') {
14477:                         if ($savecaptcha{$type} eq 'notused') {
14478:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
14479:                         } else {
14480:                             my %captchas = &captcha_phrases();
14481:                             if ($captchas{$savecaptcha{$type}}) {
14482:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
14483:                             } else {
14484:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
14485:                             }
14486:                         }
14487:                     } elsif ($type eq 'recaptchakeys') {
14488:                         my ($privkey,$pubkey);
14489:                         if (ref($savecaptcha{$type}) eq 'HASH') {
14490:                             $pubkey = $savecaptcha{$type}{'public'};
14491:                             $privkey = $savecaptcha{$type}{'private'};
14492:                         }
14493:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
14494:                         if (!$pubkey) {
14495:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
14496:                         } else {
14497:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
14498:                         }
14499:                         if (!$privkey) {
14500:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
14501:                         } else {
14502:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
14503:                         }
14504:                         $chgtext .= '</ul>';
14505:                     } elsif ($type eq 'recaptchaversion') {
14506:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
14507:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
14508:                         }
14509:                     } elsif ($type eq 'emailusername') {
14510:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
14511:                             if (@statuses) {
14512:                                 foreach my $type (@statuses) {
14513:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
14514:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
14515:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
14516:                                                     '<ul>';
14517:                                             foreach my $field (@{$infofields}) {
14518:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
14519:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
14520:                                                 }
14521:                                             }
14522:                                             $chgtext .= '</ul>';
14523:                                         } else {
14524:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14525:                                         }
14526:                                     } else {
14527:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14528:                                     }
14529:                                 }
14530:                             }
14531:                         }
14532:                     } elsif ($type eq 'notify') {
14533:                         my $numapprove = 0;
14534:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
14535:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
14536:                                 if ($cancreate{'notify'}{'approval'}) {
14537:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
14538:                                     $numapprove ++;
14539:                                 }
14540:                             }
14541:                         }
14542:                         unless ($numapprove) {
14543:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
14544:                         }
14545:                     }
14546:                     if ($chgtext) {
14547:                         $resulttext .= '<li>'.$chgtext.'</li>';
14548:                     }
14549:                 }
14550:             }
14551:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
14552:                 my ($emailrules,$emailruleorder) =
14553:                     &Apache::lonnet::inst_userrules($dom,'email');
14554:                 foreach my $type (@{$changes{'email_rule'}}) {
14555:                     if (ref($email_rule{$type}) eq 'ARRAY') {
14556:                         my $chgtext = '<ul>';
14557:                         foreach my $rule (@{$email_rule{$type}}) {
14558:                             if (ref($emailrules->{$rule}) eq 'HASH') {
14559:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
14560:                             }
14561:                         }
14562:                         $chgtext .= '</ul>';
14563:                         my $typename;
14564:                         if (@types) {
14565:                             if ($type eq 'default') {
14566:                                 $typename = $othertitle;
14567:                             } else {
14568:                                 $typename = $usertypes{$type};
14569:                             } 
14570:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
14571:                         }
14572:                         if (@{$email_rule{$type}} > 0) {
14573:                             $resulttext .= '<li>'.
14574:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
14575:                                                $usertypes{$type}).
14576:                                            $chgtext.
14577:                                            '</li>';
14578:                         } else {
14579:                             $resulttext .= '<li>'.
14580:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
14581:                                            '</li>'.
14582:                                            &mt('(Affiliation: [_1])',$typename);
14583:                         }
14584:                     }
14585:                 }
14586:             }
14587:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
14588:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
14589:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
14590:                         my $chgtext = '<ul>';
14591:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
14592:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
14593:                         }
14594:                         $chgtext .= '</ul>';
14595:                         $resulttext .= '<li>'.
14596:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
14597:                                           $chgtext.
14598:                                        '</li>';
14599:                     } else {
14600:                         $resulttext .= '<li>'.
14601:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
14602:                                        '</li>';
14603:                     }
14604:                 }
14605:             }
14606:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
14607:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
14608:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14609:                 foreach my $type (@{$changes{'selfcreate'}}) {
14610:                     my $typename = $type;
14611:                     if (keys(%usertypes) > 0) {
14612:                         if ($usertypes{$type} ne '') {
14613:                             $typename = $usertypes{$type};
14614:                         }
14615:                     }
14616:                     my @modifiable;
14617:                     $resulttext .= '<li>'.
14618:                                     &mt('Self-creation of account by users with status: [_1]',
14619:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
14620:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
14621:                     foreach my $field (@fields) {
14622:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
14623:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
14624:                         }
14625:                     }
14626:                     if (@modifiable > 0) {
14627:                         $resulttext .= join(', ',@modifiable);
14628:                     } else {
14629:                         $resulttext .= &mt('none');
14630:                     }
14631:                     $resulttext .= '</li>';
14632:                 }
14633:                 $resulttext .= '</ul></li>';
14634:             }
14635:             $resulttext .= '</ul>';
14636:             my $cachetime = 24*60*60;
14637:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
14638:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14639:             if (ref($lastactref) eq 'HASH') {
14640:                 $lastactref->{'domdefaults'} = 1;
14641:             }
14642:         } else {
14643:             $resulttext = &mt('No changes made to self-creation settings');
14644:         }
14645:     } else {
14646:         $resulttext = '<span class="LC_error">'.
14647:             &mt('An error occurred: [_1]',$putresult).'</span>';
14648:     }
14649:     if ($warningmsg ne '') {
14650:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
14651:     }
14652:     return $resulttext;
14653: }
14654: 
14655: sub process_captcha {
14656:     my ($container,$changes,$newsettings,$current) = @_;
14657:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
14658:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
14659:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
14660:         $newsettings->{'captcha'} = 'original';
14661:     }
14662:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
14663:         if ($container eq 'cancreate') {
14664:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14665:                 push(@{$changes->{'cancreate'}},'captcha');
14666:             } elsif (!defined($changes->{'cancreate'})) {
14667:                 $changes->{'cancreate'} = ['captcha'];
14668:             }
14669:         } else {
14670:             $changes->{'captcha'} = 1;
14671:         }
14672:     }
14673:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
14674:     if ($newsettings->{'captcha'} eq 'recaptcha') {
14675:         $newpub = $env{'form.'.$container.'_recaptchapub'};
14676:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
14677:         $newpub =~ s/[^\w\-]//g;
14678:         $newpriv =~ s/[^\w\-]//g;
14679:         $newsettings->{'recaptchakeys'} = {
14680:                                              public  => $newpub,
14681:                                              private => $newpriv,
14682:                                           };
14683:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
14684:         $newversion =~ s/\D//g;
14685:         if ($newversion ne '2') {
14686:             $newversion = 1;
14687:         }
14688:         $newsettings->{'recaptchaversion'} = $newversion;
14689:     }
14690:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
14691:         $currpub = $current->{'recaptchakeys'}{'public'};
14692:         $currpriv = $current->{'recaptchakeys'}{'private'};
14693:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
14694:             $newsettings->{'recaptchakeys'} = {
14695:                                                  public  => '',
14696:                                                  private => '',
14697:                                               }
14698:         }
14699:     }
14700:     if ($current->{'captcha'} eq 'recaptcha') {
14701:         $currversion = $current->{'recaptchaversion'};
14702:         if ($currversion ne '2') {
14703:             $currversion = 1;
14704:         }
14705:     }
14706:     if ($currversion ne $newversion) {
14707:         if ($container eq 'cancreate') {
14708:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14709:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
14710:             } elsif (!defined($changes->{'cancreate'})) {
14711:                 $changes->{'cancreate'} = ['recaptchaversion'];
14712:             }
14713:         } else {
14714:             $changes->{'recaptchaversion'} = 1;
14715:         }
14716:     }
14717:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
14718:         if ($container eq 'cancreate') {
14719:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14720:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
14721:             } elsif (!defined($changes->{'cancreate'})) {
14722:                 $changes->{'cancreate'} = ['recaptchakeys'];
14723:             }
14724:         } else {
14725:             $changes->{'recaptchakeys'} = 1;
14726:         }
14727:     }
14728:     return;
14729: }
14730: 
14731: sub modify_usermodification {
14732:     my ($dom,%domconfig) = @_;
14733:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
14734:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
14735:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
14736:             if ($key eq 'selfcreate') {
14737:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
14738:             } else {  
14739:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
14740:             }
14741:         }
14742:     }
14743:     my @contexts = ('author','course');
14744:     my %context_title = (
14745:                            author => 'In author context',
14746:                            course => 'In course context',
14747:                         );
14748:     my @fields = ('lastname','firstname','middlename','generation',
14749:                   'permanentemail','id');
14750:     my %roles = (
14751:                   author => ['ca','aa'],
14752:                   course => ['st','ep','ta','in','cr'],
14753:                 );
14754:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14755:     foreach my $context (@contexts) {
14756:         foreach my $role (@{$roles{$context}}) {
14757:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
14758:             foreach my $item (@fields) {
14759:                 if (grep(/^\Q$item\E$/,@modifiable)) {
14760:                     $modifyhash{$context}{$role}{$item} = 1;
14761:                 } else {
14762:                     $modifyhash{$context}{$role}{$item} = 0;
14763:                 }
14764:             }
14765:         }
14766:         if (ref($curr_usermodification{$context}) eq 'HASH') {
14767:             foreach my $role (@{$roles{$context}}) {
14768:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
14769:                     foreach my $field (@fields) {
14770:                         if ($modifyhash{$context}{$role}{$field} ne 
14771:                                 $curr_usermodification{$context}{$role}{$field}) {
14772:                             push(@{$changes{$context}},$role);
14773:                             last;
14774:                         }
14775:                     }
14776:                 }
14777:             }
14778:         } else {
14779:             foreach my $context (@contexts) {
14780:                 foreach my $role (@{$roles{$context}}) {
14781:                     push(@{$changes{$context}},$role);
14782:                 }
14783:             }
14784:         }
14785:     }
14786:     my %usermodification_hash =  (
14787:                                    usermodification => \%modifyhash,
14788:                                  );
14789:     my $putresult = &Apache::lonnet::put_dom('configuration',
14790:                                              \%usermodification_hash,$dom);
14791:     if ($putresult eq 'ok') {
14792:         if (keys(%changes) > 0) {
14793:             $resulttext = &mt('Changes made: ').'<ul>';
14794:             foreach my $context (@contexts) {
14795:                 if (ref($changes{$context}) eq 'ARRAY') {
14796:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
14797:                     if (ref($changes{$context}) eq 'ARRAY') {
14798:                         foreach my $role (@{$changes{$context}}) {
14799:                             my $rolename;
14800:                             if ($role eq 'cr') {
14801:                                 $rolename = &mt('Custom');
14802:                             } else {
14803:                                 $rolename = &Apache::lonnet::plaintext($role);
14804:                             }
14805:                             my @modifiable;
14806:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
14807:                             foreach my $field (@fields) {
14808:                                 if ($modifyhash{$context}{$role}{$field}) {
14809:                                     push(@modifiable,$fieldtitles{$field});
14810:                                 }
14811:                             }
14812:                             if (@modifiable > 0) {
14813:                                 $resulttext .= join(', ',@modifiable);
14814:                             } else {
14815:                                 $resulttext .= &mt('none'); 
14816:                             }
14817:                             $resulttext .= '</li>';
14818:                         }
14819:                         $resulttext .= '</ul></li>';
14820:                     }
14821:                 }
14822:             }
14823:             $resulttext .= '</ul>';
14824:         } else {
14825:             $resulttext = &mt('No changes made to user modification settings');
14826:         }
14827:     } else {
14828:         $resulttext = '<span class="LC_error">'.
14829:             &mt('An error occurred: [_1]',$putresult).'</span>';
14830:     }
14831:     return $resulttext;
14832: }
14833: 
14834: sub modify_defaults {
14835:     my ($dom,$lastactref,%domconfig) = @_;
14836:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
14837:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14838:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
14839:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
14840:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
14841:     foreach my $item (@items) {
14842:         $newvalues{$item} = $env{'form.'.$item};
14843:         if ($item eq 'auth_def') {
14844:             if ($newvalues{$item} ne '') {
14845:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
14846:                     push(@errors,$item);
14847:                 }
14848:             }
14849:         } elsif ($item eq 'lang_def') {
14850:             if ($newvalues{$item} ne '') {
14851:                 if ($newvalues{$item} =~ /^(\w+)/) {
14852:                     my $langcode = $1;
14853:                     if ($langcode ne 'x_chef') {
14854:                         if (code2language($langcode) eq '') {
14855:                             push(@errors,$item);
14856:                         }
14857:                     }
14858:                 } else {
14859:                     push(@errors,$item);
14860:                 }
14861:             }
14862:         } elsif ($item eq 'timezone_def') {
14863:             if ($newvalues{$item} ne '') {
14864:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
14865:                     push(@errors,$item);   
14866:                 }
14867:             }
14868:         } elsif ($item eq 'datelocale_def') {
14869:             if ($newvalues{$item} ne '') {
14870:                 my @datelocale_ids = DateTime::Locale->ids();
14871:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
14872:                     push(@errors,$item);
14873:                 }
14874:             }
14875:         } elsif ($item eq 'portal_def') {
14876:             if ($newvalues{$item} ne '') {
14877:                 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])\/?$/) {
14878:                     push(@errors,$item);
14879:                 }
14880:             }
14881:         } elsif ($item eq 'intauth_cost') {
14882:             if ($newvalues{$item} ne '') {
14883:                 if ($newvalues{$item} =~ /\D/) {
14884:                     push(@errors,$item);
14885:                 }
14886:             }
14887:         } elsif ($item eq 'intauth_check') {
14888:             if ($newvalues{$item} ne '') {
14889:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14890:                     push(@errors,$item);
14891:                 }
14892:             }
14893:         } elsif ($item eq 'intauth_switch') {
14894:             if ($newvalues{$item} ne '') {
14895:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14896:                     push(@errors,$item);
14897:                 }
14898:             }
14899:         }
14900:         if (grep(/^\Q$item\E$/,@errors)) {
14901:             $newvalues{$item} = $domdefaults{$item};
14902:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
14903:             $changes{$item} = 1;
14904:         }
14905:         $domdefaults{$item} = $newvalues{$item};
14906:     }
14907:     my %defaults_hash = (
14908:                          defaults => \%newvalues,
14909:                         );
14910:     my $title = &defaults_titles();
14911: 
14912:     my $currinststatus;
14913:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
14914:         $currinststatus = $domconfig{'inststatus'};
14915:     } else {
14916:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14917:         $currinststatus = {
14918:                              inststatustypes => $usertypes,
14919:                              inststatusorder => $types,
14920:                              inststatusguest => [],
14921:                           };
14922:     }
14923:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
14924:     my @allpos;
14925:     my %alltypes;
14926:     my @inststatusguest;
14927:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
14928:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
14929:             unless (grep(/^\Q$type\E$/,@todelete)) {
14930:                 push(@inststatusguest,$type);
14931:             }
14932:         }
14933:     }
14934:     my ($currtitles,$currorder);
14935:     if (ref($currinststatus) eq 'HASH') {
14936:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
14937:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
14938:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
14939:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
14940:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
14941:                     }
14942:                 }
14943:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
14944:                     my $position = $env{'form.inststatus_pos_'.$type};
14945:                     $position =~ s/\D+//g;
14946:                     $allpos[$position] = $type;
14947:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
14948:                     $alltypes{$type} =~ s/`//g;
14949:                 }
14950:             }
14951:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
14952:             $currtitles =~ s/,$//;
14953:         }
14954:     }
14955:     if ($env{'form.addinststatus'}) {
14956:         my $newtype = $env{'form.addinststatus'};
14957:         $newtype =~ s/\W//g;
14958:         unless (exists($alltypes{$newtype})) {
14959:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
14960:             $alltypes{$newtype} =~ s/`//g; 
14961:             my $position = $env{'form.addinststatus_pos'};
14962:             $position =~ s/\D+//g;
14963:             if ($position ne '') {
14964:                 $allpos[$position] = $newtype;
14965:             }
14966:         }
14967:     }
14968:     my @orderedstatus;
14969:     foreach my $type (@allpos) {
14970:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
14971:             push(@orderedstatus,$type);
14972:         }
14973:     }
14974:     foreach my $type (keys(%alltypes)) {
14975:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
14976:             delete($alltypes{$type});
14977:         }
14978:     }
14979:     $defaults_hash{'inststatus'} = {
14980:                                      inststatustypes => \%alltypes,
14981:                                      inststatusorder => \@orderedstatus,
14982:                                      inststatusguest => \@inststatusguest,
14983:                                    };
14984:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
14985:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
14986:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
14987:         }
14988:     }
14989:     if ($currorder ne join(',',@orderedstatus)) {
14990:         $changes{'inststatus'}{'inststatusorder'} = 1;
14991:     }
14992:     my $newtitles;
14993:     foreach my $item (@orderedstatus) {
14994:         $newtitles .= $alltypes{$item}.',';
14995:     }
14996:     $newtitles =~ s/,$//;
14997:     if ($currtitles ne $newtitles) {
14998:         $changes{'inststatus'}{'inststatustypes'} = 1;
14999:     }
15000:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
15001:                                              $dom);
15002:     if ($putresult eq 'ok') {
15003:         if (keys(%changes) > 0) {
15004:             $resulttext = &mt('Changes made:').'<ul>';
15005:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
15006:             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";
15007:             foreach my $item (sort(keys(%changes))) {
15008:                 if ($item eq 'inststatus') {
15009:                     if (ref($changes{'inststatus'}) eq 'HASH') {
15010:                         if (@orderedstatus) {
15011:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
15012:                             foreach my $type (@orderedstatus) { 
15013:                                 $resulttext .= $alltypes{$type}.', ';
15014:                             }
15015:                             $resulttext =~ s/, $//;
15016:                             $resulttext .= '</li>';
15017:                         } else {
15018:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
15019:                         }
15020:                     }
15021:                 } else {
15022:                     my $value = $env{'form.'.$item};
15023:                     if ($value eq '') {
15024:                         $value = &mt('none');
15025:                     } elsif ($item eq 'auth_def') {
15026:                         my %authnames = &authtype_names();
15027:                         my %shortauth = (
15028:                                           internal   => 'int',
15029:                                           krb4       => 'krb4',
15030:                                           krb5       => 'krb5',
15031:                                           localauth  => 'loc',
15032:                                           lti        => 'lti',
15033:                         );
15034:                         $value = $authnames{$shortauth{$value}};
15035:                     } elsif ($item eq 'intauth_switch') {
15036:                         my %optiondesc = &Apache::lonlocal::texthash (
15037:                                             0 => 'No',
15038:                                             1 => 'Yes',
15039:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
15040:                                          );
15041:                         if ($value =~ /^(0|1|2)$/) {
15042:                             $value = $optiondesc{$value};
15043:                         } else {
15044:                             $value = &mt('none -- defaults to No');
15045:                         }
15046:                     } elsif ($item eq 'intauth_check') {
15047:                         my %optiondesc = &Apache::lonlocal::texthash (
15048:                                              0 => 'No',
15049:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
15050:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
15051:                                          );
15052:                         if ($value =~ /^(0|1|2)$/) {
15053:                             $value = $optiondesc{$value};
15054:                         } else {
15055:                             $value = &mt('none -- defaults to No');
15056:                         }
15057:                     }
15058:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
15059:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
15060:                 }
15061:             }
15062:             $resulttext .= '</ul>';
15063:             $mailmsgtext .= "\n";
15064:             my $cachetime = 24*60*60;
15065:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15066:             if (ref($lastactref) eq 'HASH') {
15067:                 $lastactref->{'domdefaults'} = 1;
15068:             }
15069:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
15070:                 my $notify = 1;
15071:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
15072:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
15073:                         $notify = 0;
15074:                     }
15075:                 }
15076:                 if ($notify) {
15077:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
15078:                                                "LON-CAPA Domain Settings Change - $dom",
15079:                                                $mailmsgtext);
15080:                 }
15081:             }
15082:         } else {
15083:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
15084:         }
15085:     } else {
15086:         $resulttext = '<span class="LC_error">'.
15087:             &mt('An error occurred: [_1]',$putresult).'</span>';
15088:     }
15089:     if (@errors > 0) {
15090:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
15091:         foreach my $item (@errors) {
15092:             $resulttext .= ' "'.$title->{$item}.'",';
15093:         }
15094:         $resulttext =~ s/,$//;
15095:     }
15096:     return $resulttext;
15097: }
15098: 
15099: sub modify_scantron {
15100:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15101:     my ($resulttext,%confhash,%changes,$errors);
15102:     my $custom = 'custom.tab';
15103:     my $default = 'default.tab';
15104:     my $servadm = $r->dir_config('lonAdmEMail');
15105:     my ($configuserok,$author_ok,$switchserver) =
15106:         &config_check($dom,$confname,$servadm);
15107:     if ($env{'form.scantronformat.filename'} ne '') {
15108:         my $error;
15109:         if ($configuserok eq 'ok') {
15110:             if ($switchserver) {
15111:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
15112:             } else {
15113:                 if ($author_ok eq 'ok') {
15114:                     my ($result,$scantronurl) =
15115:                         &publishlogo($r,'upload','scantronformat',$dom,
15116:                                      $confname,'scantron','','',$custom);
15117:                     if ($result eq 'ok') {
15118:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
15119:                         $changes{'scantronformat'} = 1;
15120:                     } else {
15121:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
15122:                     }
15123:                 } else {
15124:                     $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);
15125:                 }
15126:             }
15127:         } else {
15128:             $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);
15129:         }
15130:         if ($error) {
15131:             &Apache::lonnet::logthis($error);
15132:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15133:         }
15134:     }
15135:     if (ref($domconfig{'scantron'}) eq 'HASH') {
15136:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
15137:             if ($env{'form.scantronformat_del'}) {
15138:                 $confhash{'scantron'}{'scantronformat'} = '';
15139:                 $changes{'scantronformat'} = 1;
15140:             }
15141:         }
15142:     }
15143:     my @fields = &scantroncsv_fields();
15144:     my %titles = &scantronconfig_titles();
15145:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronformat');
15146:     my ($newdat,$currdat,%newcol,%currcol);
15147:     if (grep(/^dat$/,@formats)) {
15148:         $confhash{'scantron'}{config}{dat} = 1;
15149:         $newdat = 1;
15150:     } else {
15151:         $confhash{'scantron'}{config}{dat} = 0;
15152:         $newdat = 0;
15153:     }
15154:     if (grep(/^csv$/,@formats)) {
15155:         my %bynum;
15156:         foreach my $field (@fields) {
15157:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
15158:                 my $posscol = $1;
15159:                 if (($posscol < 20) && (!$bynum{$posscol})) {
15160:                     $confhash{'scantron'}{config}{csv}{$field} = $posscol;
15161:                     $bynum{$posscol} = $field;
15162:                     $newcol{$field} = $posscol;
15163:                 }
15164:             }
15165:         }
15166:     }
15167:     $currdat = 1;
15168:     if (ref($domconfig{'scantron'}) eq 'HASH') {
15169:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
15170:             if (!$domconfig{'scantron'}{'config'}{'dat'}) {
15171:                 $currdat = 0;
15172:             }
15173:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
15174:                 %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}};
15175:             }
15176:         }
15177:     }
15178:     if ($currdat != $newdat) {
15179:         $changes{'config'} = 1;
15180:     } else {
15181:         foreach my $field (@fields) {
15182:             if ($currcol{$field} ne '') {
15183:                 if ($currcol{$field} ne $newcol{$field}) {
15184:                     $changes{'config'} = 1;
15185:                     last;
15186:                 } 
15187:             } elsif ($newcol{$field} ne '') {
15188:                 $changes{'config'} = 1;
15189:                 last;
15190:             }
15191:         }
15192:     }
15193:     if (keys(%confhash) > 0) {
15194:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
15195:                                                  $dom);
15196:         if ($putresult eq 'ok') {
15197:             if (keys(%changes) > 0) {
15198:                 if (ref($confhash{'scantron'}) eq 'HASH') {
15199:                     $resulttext = &mt('Changes made:').'<ul>';
15200:                     if ($changes{'scantronformat'}) {
15201:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
15202:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
15203:                         } else {
15204:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
15205:                         }
15206:                     }
15207:                     if ($changes{'config'}) {    
15208:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
15209:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
15210:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
15211:                             }
15212:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
15213:                                 if (keys(%{$confhash{'scantron'}{'config'}{'csv'}})) { 
15214:                                     '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
15215:                                     foreach my $field (@fields) {
15216:                                         if ($confhash{'scantron'}{'config'}{'csv'}{$field} ne '') {
15217:                                             my $showcol = $confhash{'scantron'}{'config'}{'csv'}{$field} + 1; 
15218:                                             $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
15219:                                         }
15220:                                     }
15221:                                     $resulttext .= '</ul></li>';
15222:                                 }
15223:                             }
15224:                         } else {
15225:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
15226:                         }
15227:                     }
15228:                     $resulttext .= '</ul>';
15229:                 } else {
15230:                     $resulttext = &mt('Changes made to bubblesheet format file.');
15231:                 }
15232:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15233:                 if (ref($lastactref) eq 'HASH') {
15234:                     $lastactref->{'domainconfig'} = 1;
15235:                 }
15236:             } else {
15237:                 $resulttext = &mt('No changes made to bubblesheet format settings');
15238:             }
15239:         } else {
15240:             $resulttext = '<span class="LC_error">'.
15241:                 &mt('An error occurred: [_1]',$putresult).'</span>';
15242:         }
15243:     } else {
15244:         $resulttext = &mt('No changes made to bubblesheet format file'); 
15245:     }
15246:     if ($errors) {
15247:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15248:                        $errors.'</ul>';
15249:     }
15250:     return $resulttext;
15251: }
15252: 
15253: sub modify_coursecategories {
15254:     my ($dom,$lastactref,%domconfig) = @_;
15255:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
15256:         $cathash);
15257:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
15258:     my @catitems = ('unauth','auth');
15259:     my @cattypes = ('std','domonly','codesrch','none');
15260:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
15261:         $cathash = $domconfig{'coursecategories'}{'cats'};
15262:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
15263:             $changes{'togglecats'} = 1;
15264:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
15265:         }
15266:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
15267:             $changes{'categorize'} = 1;
15268:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
15269:         }
15270:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
15271:             $changes{'togglecatscomm'} = 1;
15272:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
15273:         }
15274:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
15275:             $changes{'categorizecomm'} = 1;
15276:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
15277: 
15278:         }
15279:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
15280:             $changes{'togglecatsplace'} = 1;
15281:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
15282:         }
15283:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
15284:             $changes{'categorizeplace'} = 1;
15285:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
15286:         }
15287:         foreach my $item (@catitems) {
15288:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
15289:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
15290:                     $changes{$item} = 1;
15291:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
15292:                 }
15293:             }
15294:         }
15295:     } else {
15296:         $changes{'togglecats'} = 1;
15297:         $changes{'categorize'} = 1;
15298:         $changes{'togglecatscomm'} = 1;
15299:         $changes{'categorizecomm'} = 1;
15300:         $changes{'togglecatsplace'} = 1;
15301:         $changes{'categorizeplace'} = 1;
15302:         $domconfig{'coursecategories'} = {
15303:                                              togglecats => $env{'form.togglecats'},
15304:                                              categorize => $env{'form.categorize'},
15305:                                              togglecatscomm => $env{'form.togglecatscomm'},
15306:                                              categorizecomm => $env{'form.categorizecomm'},
15307:                                              togglecatsplace => $env{'form.togglecatsplace'},
15308:                                              categorizeplace => $env{'form.categorizeplace'},
15309:                                          };
15310:         foreach my $item (@catitems) {
15311:             if ($env{'form.coursecat_'.$item} ne 'std') {
15312:                 $changes{$item} = 1;
15313:             }
15314:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
15315:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
15316:             }
15317:         }
15318:     }
15319:     if (ref($cathash) eq 'HASH') {
15320:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
15321:             push (@deletecategory,'instcode::0');
15322:         }
15323:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
15324:             push(@deletecategory,'communities::0');
15325:         }
15326:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
15327:             push(@deletecategory,'placement::0');
15328:         }
15329:     }
15330:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
15331:     if (ref($cathash) eq 'HASH') {
15332:         if (@deletecategory > 0) {
15333:             #FIXME Need to remove category from all courses using a deleted category 
15334:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
15335:             foreach my $item (@deletecategory) {
15336:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
15337:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
15338:                     $deletions{$item} = 1;
15339:                     &recurse_cat_deletes($item,$cathash,\%deletions);
15340:                 }
15341:             }
15342:         }
15343:         foreach my $item (keys(%{$cathash})) {
15344:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15345:             if ($cathash->{$item} ne $env{'form.'.$item}) {
15346:                 $reorderings{$item} = 1;
15347:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
15348:             }
15349:             if ($env{'form.addcategory_name_'.$item} ne '') {
15350:                 my $newcat = $env{'form.addcategory_name_'.$item};
15351:                 my $newdepth = $depth+1;
15352:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
15353:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
15354:                 $adds{$newitem} = 1; 
15355:             }
15356:             if ($env{'form.subcat_'.$item} ne '') {
15357:                 my $newcat = $env{'form.subcat_'.$item};
15358:                 my $newdepth = $depth+1;
15359:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
15360:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
15361:                 $adds{$newitem} = 1;
15362:             }
15363:         }
15364:     }
15365:     if ($env{'form.instcode'} eq '1') {
15366:         if (ref($cathash) eq 'HASH') {
15367:             my $newitem = 'instcode::0';
15368:             if ($cathash->{$newitem} eq '') {  
15369:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
15370:                 $adds{$newitem} = 1;
15371:             }
15372:         } else {
15373:             my $newitem = 'instcode::0';
15374:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
15375:             $adds{$newitem} = 1;
15376:         }
15377:     }
15378:     if ($env{'form.communities'} eq '1') {
15379:         if (ref($cathash) eq 'HASH') {
15380:             my $newitem = 'communities::0';
15381:             if ($cathash->{$newitem} eq '') {
15382:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
15383:                 $adds{$newitem} = 1;
15384:             }
15385:         } else {
15386:             my $newitem = 'communities::0';
15387:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
15388:             $adds{$newitem} = 1;
15389:         }
15390:     }
15391:     if ($env{'form.placement'} eq '1') {
15392:         if (ref($cathash) eq 'HASH') {
15393:             my $newitem = 'placement::0';
15394:             if ($cathash->{$newitem} eq '') {
15395:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
15396:                 $adds{$newitem} = 1;
15397:             }
15398:         } else {
15399:             my $newitem = 'placement::0';
15400:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
15401:             $adds{$newitem} = 1;
15402:         }
15403:     }
15404:     if ($env{'form.addcategory_name'} ne '') {
15405:         if (($env{'form.addcategory_name'} ne 'instcode') &&
15406:             ($env{'form.addcategory_name'} ne 'communities') &&
15407:             ($env{'form.addcategory_name'} ne 'placement')) {
15408:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
15409:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
15410:             $adds{$newitem} = 1;
15411:         }
15412:     }
15413:     my $putresult;
15414:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15415:         if (keys(%deletions) > 0) {
15416:             foreach my $key (keys(%deletions)) {
15417:                 if ($predelallitems{$key} ne '') {
15418:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
15419:                 }
15420:             }
15421:         }
15422:         my (@chkcats,@chktrails,%chkallitems);
15423:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
15424:         if (ref($chkcats[0]) eq 'ARRAY') {
15425:             my $depth = 0;
15426:             my $chg = 0;
15427:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
15428:                 my $name = $chkcats[0][$i];
15429:                 my $item;
15430:                 if ($name eq '') {
15431:                     $chg ++;
15432:                 } else {
15433:                     $item = &escape($name).'::0';
15434:                     if ($chg) {
15435:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
15436:                     }
15437:                     $depth ++; 
15438:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
15439:                     $depth --;
15440:                 }
15441:             }
15442:         }
15443:     }
15444:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15445:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
15446:         if ($putresult eq 'ok') {
15447:             my %title = (
15448:                          togglecats     => 'Show/Hide a course in catalog',
15449:                          categorize     => 'Assign a category to a course',
15450:                          togglecatscomm => 'Show/Hide a community in catalog',
15451:                          categorizecomm => 'Assign a category to a community',
15452:                         );
15453:             my %level = (
15454:                          dom  => 'set in Domain ("Modify Course/Community")',
15455:                          crs  => 'set in Course ("Course Configuration")',
15456:                          comm => 'set in Community ("Community Configuration")',
15457:                          none     => 'No catalog',
15458:                          std      => 'Standard catalog',
15459:                          domonly  => 'Domain-only catalog',
15460:                          codesrch => 'Code search form',
15461:                         );
15462:             $resulttext = &mt('Changes made:').'<ul>';
15463:             if ($changes{'togglecats'}) {
15464:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
15465:             }
15466:             if ($changes{'categorize'}) {
15467:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
15468:             }
15469:             if ($changes{'togglecatscomm'}) {
15470:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
15471:             }
15472:             if ($changes{'categorizecomm'}) {
15473:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
15474:             }
15475:             if ($changes{'unauth'}) {
15476:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
15477:             }
15478:             if ($changes{'auth'}) {
15479:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
15480:             }
15481:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15482:                 my $cathash;
15483:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
15484:                     $cathash = $domconfig{'coursecategories'}{'cats'};
15485:                 } else {
15486:                     $cathash = {};
15487:                 } 
15488:                 my (@cats,@trails,%allitems);
15489:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
15490:                 if (keys(%deletions) > 0) {
15491:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
15492:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
15493:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
15494:                     }
15495:                     $resulttext .= '</ul></li>';
15496:                 }
15497:                 if (keys(%reorderings) > 0) {
15498:                     my %sort_by_trail;
15499:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
15500:                     foreach my $key (keys(%reorderings)) {
15501:                         if ($allitems{$key} ne '') {
15502:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15503:                         }
15504:                     }
15505:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15506:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15507:                     }
15508:                     $resulttext .= '</ul></li>';
15509:                 }
15510:                 if (keys(%adds) > 0) {
15511:                     my %sort_by_trail;
15512:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
15513:                     foreach my $key (keys(%adds)) {
15514:                         if ($allitems{$key} ne '') {
15515:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15516:                         }
15517:                     }
15518:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15519:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15520:                     }
15521:                     $resulttext .= '</ul></li>';
15522:                 }
15523:             }
15524:             $resulttext .= '</ul>';
15525:             if ($changes{'unauth'} || $changes{'auth'}) {
15526:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15527:                 if ($changes{'auth'}) {
15528:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
15529:                 }
15530:                 if ($changes{'unauth'}) {
15531:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
15532:                 }
15533:                 my $cachetime = 24*60*60;
15534:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15535:                 if (ref($lastactref) eq 'HASH') {
15536:                     $lastactref->{'domdefaults'} = 1;
15537:                 }
15538:             }
15539:         } else {
15540:             $resulttext = '<span class="LC_error">'.
15541:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15542:         }
15543:     } else {
15544:         $resulttext = &mt('No changes made to course and community categories');
15545:     }
15546:     return $resulttext;
15547: }
15548: 
15549: sub modify_serverstatuses {
15550:     my ($dom,%domconfig) = @_;
15551:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
15552:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
15553:         %currserverstatus = %{$domconfig{'serverstatuses'}};
15554:     }
15555:     my @pages = &serverstatus_pages();
15556:     foreach my $type (@pages) {
15557:         $newserverstatus{$type}{'namedusers'} = '';
15558:         $newserverstatus{$type}{'machines'} = '';
15559:         if (defined($env{'form.'.$type.'_namedusers'})) {
15560:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
15561:             my @okusers;
15562:             foreach my $user (@users) {
15563:                 my ($uname,$udom) = split(/:/,$user);
15564:                 if (($udom =~ /^$match_domain$/) &&   
15565:                     (&Apache::lonnet::domain($udom)) &&
15566:                     ($uname =~ /^$match_username$/)) {
15567:                     if (!grep(/^\Q$user\E/,@okusers)) {
15568:                         push(@okusers,$user);
15569:                     }
15570:                 }
15571:             }
15572:             if (@okusers > 0) {
15573:                  @okusers = sort(@okusers);
15574:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
15575:             }
15576:         }
15577:         if (defined($env{'form.'.$type.'_machines'})) {
15578:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
15579:             my @okmachines;
15580:             foreach my $ip (@machines) {
15581:                 my @parts = split(/\./,$ip);
15582:                 next if (@parts < 4);
15583:                 my $badip = 0;
15584:                 for (my $i=0; $i<4; $i++) {
15585:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
15586:                         $badip = 1;
15587:                         last;
15588:                     }
15589:                 }
15590:                 if (!$badip) {
15591:                     push(@okmachines,$ip);     
15592:                 }
15593:             }
15594:             @okmachines = sort(@okmachines);
15595:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
15596:         }
15597:     }
15598:     my %serverstatushash =  (
15599:                                 serverstatuses => \%newserverstatus,
15600:                             );
15601:     foreach my $type (@pages) {
15602:         foreach my $setting ('namedusers','machines') {
15603:             my (@current,@new);
15604:             if (ref($currserverstatus{$type}) eq 'HASH') {
15605:                 if ($currserverstatus{$type}{$setting} ne '') { 
15606:                     @current = split(/,/,$currserverstatus{$type}{$setting});
15607:                 }
15608:             }
15609:             if ($newserverstatus{$type}{$setting} ne '') {
15610:                 @new = split(/,/,$newserverstatus{$type}{$setting});
15611:             }
15612:             if (@current > 0) {
15613:                 if (@new > 0) {
15614:                     foreach my $item (@current) {
15615:                         if (!grep(/^\Q$item\E$/,@new)) {
15616:                             $changes{$type}{$setting} = 1;
15617:                             last;
15618:                         }
15619:                     }
15620:                     foreach my $item (@new) {
15621:                         if (!grep(/^\Q$item\E$/,@current)) {
15622:                             $changes{$type}{$setting} = 1;
15623:                             last;
15624:                         }
15625:                     }
15626:                 } else {
15627:                     $changes{$type}{$setting} = 1;
15628:                 }
15629:             } elsif (@new > 0) {
15630:                 $changes{$type}{$setting} = 1;
15631:             }
15632:         }
15633:     }
15634:     if (keys(%changes) > 0) {
15635:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
15636:         my $putresult = &Apache::lonnet::put_dom('configuration',
15637:                                                  \%serverstatushash,$dom);
15638:         if ($putresult eq 'ok') {
15639:             $resulttext .= &mt('Changes made:').'<ul>';
15640:             foreach my $type (@pages) {
15641:                 if (ref($changes{$type}) eq 'HASH') {
15642:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
15643:                     if ($changes{$type}{'namedusers'}) {
15644:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
15645:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
15646:                         } else {
15647:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
15648:                         }
15649:                     }
15650:                     if ($changes{$type}{'machines'}) {
15651:                         if ($newserverstatus{$type}{'machines'} eq '') {
15652:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
15653:                         } else {
15654:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
15655:                         }
15656: 
15657:                     }
15658:                     $resulttext .= '</ul></li>';
15659:                 }
15660:             }
15661:             $resulttext .= '</ul>';
15662:         } else {
15663:             $resulttext = '<span class="LC_error">'.
15664:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
15665: 
15666:         }
15667:     } else {
15668:         $resulttext = &mt('No changes made to access to server status pages');
15669:     }
15670:     return $resulttext;
15671: }
15672: 
15673: sub modify_helpsettings {
15674:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15675:     my ($resulttext,$errors,%changes,%helphash);
15676:     my %defaultchecked = ('submitbugs' => 'on');
15677:     my @offon = ('off','on');
15678:     my @toggles = ('submitbugs');
15679:     my %current = ('submitbugs' => '',
15680:                    'adhoc'      => {},
15681:                   );
15682:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
15683:         %current = %{$domconfig{'helpsettings'}};
15684:     }
15685:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15686:     foreach my $item (@toggles) {
15687:         if ($defaultchecked{$item} eq 'on') { 
15688:             if ($current{$item} eq '') {
15689:                 if ($env{'form.'.$item} eq '0') {
15690:                     $changes{$item} = 1;
15691:                 }
15692:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15693:                 $changes{$item} = 1;
15694:             }
15695:         } elsif ($defaultchecked{$item} eq 'off') {
15696:             if ($current{$item} eq '') {
15697:                 if ($env{'form.'.$item} eq '1') {
15698:                     $changes{$item} = 1;
15699:                 }
15700:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15701:                 $changes{$item} = 1;
15702:             }
15703:         }
15704:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
15705:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
15706:         }
15707:     }
15708:     my $maxnum = $env{'form.helproles_maxnum'};
15709:     my $confname = $dom.'-domainconfig';
15710:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
15711:     my (@allpos,%newsettings,%changedprivs,$newrole);
15712:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15713:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
15714:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
15715:     my %lt = &Apache::lonlocal::texthash(
15716:                     s      => 'system',
15717:                     d      => 'domain',
15718:                     order  => 'Display order',
15719:                     access => 'Role usage',
15720:                     all    => 'All with domain helpdesk or helpdesk assistant role',
15721:                     dh     => 'All with domain helpdesk role',
15722:                     da     => 'All with domain helpdesk assistant role',
15723:                     none   => 'None',
15724:                     status => 'Determined based on institutional status',
15725:                     inc    => 'Include all, but exclude specific personnel',
15726:                     exc    => 'Exclude all, but include specific personnel',
15727:     );
15728:     for (my $num=0; $num<=$maxnum; $num++) {
15729:         my ($prefix,$identifier,$rolename,%curr);
15730:         if ($num == $maxnum) {
15731:             next unless ($env{'form.newcusthelp'} == $maxnum);
15732:             $identifier = 'custhelp'.$num;
15733:             $prefix = 'helproles_'.$num;
15734:             $rolename = $env{'form.custhelpname'.$num};
15735:             $rolename=~s/[^A-Za-z0-9]//gs;
15736:             next if ($rolename eq '');
15737:             next if (exists($existing{'rolesdef_'.$rolename}));
15738:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15739:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15740:                                                      $newprivs{'c'},$confname,$dom);
15741:             if ($result ne 'ok') {
15742:                 $errors .= '<li><span class="LC_error">'.
15743:                            &mt('An error occurred storing the new custom role: [_1]',
15744:                            $result).'</span></li>';
15745:                 next;
15746:             } else {
15747:                 $changedprivs{$rolename} = \%newprivs;
15748:                 $newrole = $rolename;
15749:             }
15750:         } else {
15751:             $prefix = 'helproles_'.$num;
15752:             $rolename = $env{'form.'.$prefix};
15753:             next if ($rolename eq '');
15754:             next unless (exists($existing{'rolesdef_'.$rolename}));
15755:             $identifier = 'custhelp'.$num;
15756:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15757:             my %currprivs;
15758:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
15759:                 split(/\_/,$existing{'rolesdef_'.$rolename});
15760:             foreach my $level ('c','d','s') {
15761:                 if ($newprivs{$level} ne $currprivs{$level}) {
15762:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15763:                                                              $newprivs{'c'},$confname,$dom);
15764:                     if ($result ne 'ok') {
15765:                         $errors .= '<li><span class="LC_error">'.
15766:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
15767:                                        $rolename,$result).'</span></li>';
15768:                     } else {
15769:                         $changedprivs{$rolename} = \%newprivs;
15770:                     }
15771:                     last;
15772:                 }
15773:             }
15774:             if (ref($current{'adhoc'}) eq 'HASH') {
15775:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15776:                     %curr = %{$current{'adhoc'}{$rolename}};
15777:                 }
15778:             }
15779:         }
15780:         my $newpos = $env{'form.'.$prefix.'_pos'};
15781:         $newpos =~ s/\D+//g;
15782:         $allpos[$newpos] = $rolename;
15783:         my $newdesc = $env{'form.'.$prefix.'_desc'};
15784:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
15785:         if ($curr{'desc'}) {
15786:             if ($curr{'desc'} ne $newdesc) {
15787:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
15788:                 $newsettings{$rolename}{'desc'} = $newdesc;
15789:             }
15790:         } elsif ($newdesc ne '') {
15791:             $changes{'customrole'}{$rolename}{'desc'} = 1;
15792:             $newsettings{$rolename}{'desc'} = $newdesc;
15793:         }
15794:         my $access = $env{'form.'.$prefix.'_access'};
15795:         if (grep(/^\Q$access\E$/,@accesstypes)) {
15796:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
15797:             if ($access eq 'status') {
15798:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
15799:                 if (scalar(@statuses) == 0) {
15800:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
15801:                 } else {
15802:                     my (@shownstatus,$numtypes);
15803:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15804:                     if (ref($types) eq 'ARRAY') {
15805:                         $numtypes = scalar(@{$types});
15806:                         foreach my $type (sort(@statuses)) {
15807:                             if ($type eq 'default') {
15808:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15809:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
15810:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15811:                                 push(@shownstatus,$usertypes->{$type});
15812:                             }
15813:                         }
15814:                     }
15815:                     if (grep(/^default$/,@statuses)) {
15816:                         push(@shownstatus,$othertitle);
15817:                     }
15818:                     if (scalar(@shownstatus) == 1+$numtypes) {
15819:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
15820:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
15821:                     } else {
15822:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
15823:                         if (ref($curr{'status'}) eq 'ARRAY') {
15824:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15825:                             if (@diffs) {
15826:                                 $changes{'customrole'}{$rolename}{$access} = 1;
15827:                             }
15828:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15829:                             $changes{'customrole'}{$rolename}{$access} = 1;
15830:                         }
15831:                     }
15832:                 }
15833:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
15834:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
15835:                 my @newspecstaff;
15836:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15837:                 foreach my $person (sort(@personnel)) {
15838:                     if ($domhelpdesk{$person}) {
15839:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
15840:                     }
15841:                 }
15842:                 if (ref($curr{$access}) eq 'ARRAY') {
15843:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15844:                     if (@diffs) {
15845:                         $changes{'customrole'}{$rolename}{$access} = 1;
15846:                     }
15847:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15848:                     $changes{'customrole'}{$rolename}{$access} = 1;
15849:                 }
15850:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15851:                     my ($uname,$udom) = split(/:/,$person);
15852:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
15853:                 }
15854:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
15855:             }
15856:         } else {
15857:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
15858:         }
15859:         unless ($curr{'access'} eq $access) {
15860:             $changes{'customrole'}{$rolename}{'access'} = 1;
15861:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
15862:         }
15863:     }
15864:     if (@allpos > 0) {
15865:         my $idx = 0;
15866:         foreach my $rolename (@allpos) {
15867:             if ($rolename ne '') {
15868:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
15869:                 if (ref($current{'adhoc'}) eq 'HASH') {
15870:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15871:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
15872:                             $changes{'customrole'}{$rolename}{'order'} = 1;
15873:                             $newsettings{$rolename}{'order'} = $idx+1;
15874:                         }
15875:                     }
15876:                 }
15877:                 $idx ++;
15878:             }
15879:         }
15880:     }
15881:     my $putresult;
15882:     if (keys(%changes) > 0) {
15883:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
15884:         if ($putresult eq 'ok') {
15885:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
15886:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
15887:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
15888:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
15889:                 }
15890:             }
15891:             my $cachetime = 24*60*60;
15892:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15893:             if (ref($lastactref) eq 'HASH') {
15894:                 $lastactref->{'domdefaults'} = 1;
15895:             }
15896:         } else {
15897:             $errors .= '<li><span class="LC_error">'.
15898:                        &mt('An error occurred storing the settings: [_1]',
15899:                            $putresult).'</span></li>';
15900:         }
15901:     }
15902:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
15903:         $resulttext = &mt('Changes made:').'<ul>';
15904:         my (%shownprivs,@levelorder);
15905:         @levelorder = ('c','d','s');
15906:         if ((keys(%changes)) && ($putresult eq 'ok')) {
15907:             foreach my $item (sort(keys(%changes))) {
15908:                 if ($item eq 'submitbugs') {
15909:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
15910:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
15911:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
15912:                 } elsif ($item eq 'customrole') {
15913:                     if (ref($changes{'customrole'}) eq 'HASH') {
15914:                         my @keyorder = ('order','desc','access','status','exc','inc');
15915:                         my %keytext = &Apache::lonlocal::texthash(
15916:                                                                    order  => 'Order',
15917:                                                                    desc   => 'Role description',
15918:                                                                    access => 'Role usage',
15919:                                                                    status => 'Allowed institutional types',
15920:                                                                    exc    => 'Allowed personnel',
15921:                                                                    inc    => 'Disallowed personnel',
15922:                         );
15923:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
15924:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
15925:                                 if ($role eq $newrole) {
15926:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
15927:                                                               $role).'<ul>';
15928:                                 } else {
15929:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15930:                                                               $role).'<ul>';
15931:                                 }
15932:                                 foreach my $key (@keyorder) {
15933:                                     if ($changes{'customrole'}{$role}{$key}) {
15934:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
15935:                                                                   $keytext{$key},$newsettings{$role}{$key}).
15936:                                                        '</li>';
15937:                                     }
15938:                                 }
15939:                                 if (ref($changedprivs{$role}) eq 'HASH') {
15940:                                     $shownprivs{$role} = 1;
15941:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
15942:                                     foreach my $level (@levelorder) {
15943:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15944:                                             next if ($item eq '');
15945:                                             my ($priv) = split(/\&/,$item,2);
15946:                                             if (&Apache::lonnet::plaintext($priv)) {
15947:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15948:                                                 unless ($level eq 'c') {
15949:                                                     $resulttext .= ' ('.$lt{$level}.')';
15950:                                                 }
15951:                                                 $resulttext .= '</li>';
15952:                                             }
15953:                                         }
15954:                                     }
15955:                                     $resulttext .= '</ul>';
15956:                                 }
15957:                                 $resulttext .= '</ul></li>';
15958:                             }
15959:                         }
15960:                     }
15961:                 }
15962:             }
15963:         }
15964:         if (keys(%changedprivs)) {
15965:             foreach my $role (sort(keys(%changedprivs))) {
15966:                 unless ($shownprivs{$role}) {
15967:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15968:                                               $role).'<ul>'.
15969:                                    '<li>'.&mt('Privileges set to :').'<ul>';
15970:                     foreach my $level (@levelorder) {
15971:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15972:                             next if ($item eq '');
15973:                             my ($priv) = split(/\&/,$item,2);
15974:                             if (&Apache::lonnet::plaintext($priv)) {
15975:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15976:                                 unless ($level eq 'c') {
15977:                                     $resulttext .= ' ('.$lt{$level}.')';
15978:                                 }
15979:                                 $resulttext .= '</li>';
15980:                             }
15981:                         }
15982:                     }
15983:                     $resulttext .= '</ul></li></ul></li>';
15984:                 }
15985:             }
15986:         }
15987:         $resulttext .= '</ul>';
15988:     } else {
15989:         $resulttext = &mt('No changes made to help settings');
15990:     }
15991:     if ($errors) {
15992:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
15993:                                     $errors.'</ul>';
15994:     }
15995:     return $resulttext;
15996: }
15997: 
15998: sub modify_coursedefaults {
15999:     my ($dom,$lastactref,%domconfig) = @_;
16000:     my ($resulttext,$errors,%changes,%defaultshash);
16001:     my %defaultchecked = (
16002:                            'canuse_pdfforms' => 'off',
16003:                            'uselcmath'       => 'on',
16004:                            'usejsme'         => 'on'
16005:                          );
16006:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
16007:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
16008:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
16009:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
16010:                    'mysqltables_textbook','mysqltables_placement');
16011:     my @types = ('official','unofficial','community','textbook','placement');
16012:     my %staticdefaults = (
16013:                            anonsurvey_threshold => 10,
16014:                            uploadquota          => 500,
16015:                            postsubmit           => 60,
16016:                            mysqltables          => 172800,
16017:                          );
16018:     my %texoptions = (
16019:                         MathJax  => 'MathJax',
16020:                         mimetex  => &mt('Convert to Images'),
16021:                         tth      => &mt('TeX to HTML'),
16022:                      );
16023:     $defaultshash{'coursedefaults'} = {};
16024: 
16025:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
16026:         if ($domconfig{'coursedefaults'} eq '') {
16027:             $domconfig{'coursedefaults'} = {};
16028:         }
16029:     }
16030: 
16031:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
16032:         foreach my $item (@toggles) {
16033:             if ($defaultchecked{$item} eq 'on') {
16034:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
16035:                     ($env{'form.'.$item} eq '0')) {
16036:                     $changes{$item} = 1;
16037:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
16038:                     $changes{$item} = 1;
16039:                 }
16040:             } elsif ($defaultchecked{$item} eq 'off') {
16041:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
16042:                     ($env{'form.'.$item} eq '1')) {
16043:                     $changes{$item} = 1;
16044:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
16045:                     $changes{$item} = 1;
16046:                 }
16047:             }
16048:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
16049:         }
16050:         foreach my $item (@numbers) {
16051:             my ($currdef,$newdef);
16052:             $newdef = $env{'form.'.$item};
16053:             if ($item eq 'anonsurvey_threshold') {
16054:                 $currdef = $domconfig{'coursedefaults'}{$item};
16055:                 $newdef =~ s/\D//g;
16056:                 if ($newdef eq '' || $newdef < 1) {
16057:                     $newdef = 1;
16058:                 }
16059:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
16060:             } else {
16061:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
16062:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
16063:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
16064:                 }
16065:                 $newdef =~ s/[^\w.\-]//g;
16066:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
16067:             }
16068:             if ($currdef ne $newdef) {
16069:                 my $staticdef;
16070:                 if ($item eq 'anonsurvey_threshold') {
16071:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
16072:                         $changes{$item} = 1;
16073:                     }
16074:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
16075:                     my $setting = $1;
16076:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
16077:                         $changes{$setting} = 1;
16078:                     }
16079:                 }
16080:             }
16081:         }
16082:         my $texengine;
16083:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
16084:             $texengine = $env{'form.texengine'};
16085:             if ($defaultshash{'coursedefaults'}{'texengine'} eq '') {
16086:                 unless ($texengine eq 'MathJax') {
16087:                     $changes{'texengine'} = 1;
16088:                 }
16089:             } elsif ($defaultshash{'coursedefaults'}{'texengine'} ne $texengine) {
16090:                 $changes{'texengine'} = 1;
16091:             }
16092:         }
16093:         if ($texengine ne '') {
16094:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
16095:         }
16096:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
16097:         my @currclonecode;
16098:         if (ref($currclone) eq 'HASH') {
16099:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
16100:                 @currclonecode = @{$currclone->{'instcode'}};
16101:             }
16102:         }
16103:         my $newclone;
16104:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
16105:             $newclone = $env{'form.canclone'};
16106:         }
16107:         if ($newclone eq 'instcode') {
16108:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
16109:             my (%codedefaults,@code_order,@clonecode);
16110:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
16111:                                                     \@code_order);
16112:             foreach my $item (@code_order) {
16113:                 if (grep(/^\Q$item\E$/,@newcodes)) {
16114:                     push(@clonecode,$item);
16115:                 }
16116:             }
16117:             if (@clonecode) {
16118:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
16119:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
16120:                 if (@diffs) {
16121:                     $changes{'canclone'} = 1;
16122:                 }
16123:             } else {
16124:                 $newclone eq '';
16125:             }
16126:         } elsif ($newclone ne '') {
16127:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
16128:         }
16129:         if ($newclone ne $currclone) {
16130:             $changes{'canclone'} = 1;
16131:         }
16132:         my %credits;
16133:         foreach my $type (@types) {
16134:             unless ($type eq 'community') {
16135:                 $credits{$type} = $env{'form.'.$type.'_credits'};
16136:                 $credits{$type} =~ s/[^\d.]+//g;
16137:             }
16138:         }
16139:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
16140:             ($env{'form.coursecredits'} eq '1')) {
16141:             $changes{'coursecredits'} = 1;
16142:             foreach my $type (keys(%credits)) {
16143:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
16144:             }
16145:         } else {
16146:             if ($env{'form.coursecredits'} eq '1') {
16147:                 foreach my $type (@types) {
16148:                     unless ($type eq 'community') {
16149:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
16150:                             $changes{'coursecredits'} = 1;
16151:                         }
16152:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
16153:                     }
16154:                 }
16155:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16156:                 foreach my $type (@types) {
16157:                     unless ($type eq 'community') {
16158:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
16159:                             $changes{'coursecredits'} = 1;
16160:                             last;
16161:                         }
16162:                     }
16163:                 }
16164:             }
16165:         }
16166:         if ($env{'form.postsubmit'} eq '1') {
16167:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
16168:             my %currtimeout;
16169:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16170:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
16171:                     $changes{'postsubmit'} = 1;
16172:                 }
16173:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16174:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
16175:                 }
16176:             } else {
16177:                 $changes{'postsubmit'} = 1;
16178:             }
16179:             foreach my $type (@types) {
16180:                 my $timeout = $env{'form.'.$type.'_timeout'};
16181:                 $timeout =~ s/\D//g;
16182:                 if ($timeout == $staticdefaults{'postsubmit'}) {
16183:                     $timeout = '';
16184:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
16185:                     $timeout = '0';
16186:                 }
16187:                 unless ($timeout eq '') {
16188:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
16189:                 }
16190:                 if (exists($currtimeout{$type})) {
16191:                     if ($timeout ne $currtimeout{$type}) {
16192:                         $changes{'postsubmit'} = 1;
16193:                     }
16194:                 } elsif ($timeout ne '') {
16195:                     $changes{'postsubmit'} = 1;
16196:                 }
16197:             }
16198:         } else {
16199:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
16200:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16201:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
16202:                     $changes{'postsubmit'} = 1;
16203:                 }
16204:             } else {
16205:                 $changes{'postsubmit'} = 1;
16206:             }
16207:         }
16208:     }
16209:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16210:                                              $dom);
16211:     if ($putresult eq 'ok') {
16212:         if (keys(%changes) > 0) {
16213:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16214:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
16215:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
16216:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
16217:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
16218:                     if ($changes{$item}) {
16219:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
16220:                     }
16221:                 }
16222:                 if ($changes{'coursecredits'}) {
16223:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16224:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
16225:                             $domdefaults{$type.'credits'} =
16226:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
16227:                         }
16228:                     }
16229:                 }
16230:                 if ($changes{'postsubmit'}) {
16231:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16232:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
16233:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16234:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
16235:                                 $domdefaults{$type.'postsubtimeout'} =
16236:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
16237:                             }
16238:                         }
16239:                     }
16240:                 }
16241:                 if ($changes{'uploadquota'}) {
16242:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
16243:                         foreach my $type (@types) {
16244:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
16245:                         }
16246:                     }
16247:                 }
16248:                 if ($changes{'canclone'}) {
16249:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
16250:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
16251:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
16252:                             if (@clonecodes) {
16253:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
16254:                             }
16255:                         }
16256:                     } else {
16257:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
16258:                     }
16259:                 }
16260:                 my $cachetime = 24*60*60;
16261:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16262:                 if (ref($lastactref) eq 'HASH') {
16263:                     $lastactref->{'domdefaults'} = 1;
16264:                 }
16265:             }
16266:             $resulttext = &mt('Changes made:').'<ul>';
16267:             foreach my $item (sort(keys(%changes))) {
16268:                 if ($item eq 'canuse_pdfforms') {
16269:                     if ($env{'form.'.$item} eq '1') {
16270:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
16271:                     } else {
16272:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
16273:                     }
16274:                 } elsif ($item eq 'uselcmath') {
16275:                     if ($env{'form.'.$item} eq '1') {
16276:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
16277:                     } else {
16278:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
16279:                     }
16280:                 } elsif ($item eq 'usejsme') {
16281:                     if ($env{'form.'.$item} eq '1') {
16282:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
16283:                     } else {
16284:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
16285:                     }
16286:                 } elsif ($item eq 'texengine') {
16287:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
16288:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
16289:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
16290:                     }
16291:                 } elsif ($item eq 'anonsurvey_threshold') {
16292:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
16293:                 } elsif ($item eq 'uploadquota') {
16294:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
16295:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
16296:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
16297:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
16298:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
16299:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
16300:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
16301:                                        '</ul>'.
16302:                                        '</li>';
16303:                     } else {
16304:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
16305:                     }
16306:                 } elsif ($item eq 'mysqltables') {
16307:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
16308:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
16309:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
16310:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
16311:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
16312:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
16313:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
16314:                                        '</ul>'.
16315:                                        '</li>';
16316:                     } else {
16317:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
16318:                     }
16319:                 } elsif ($item eq 'postsubmit') {
16320:                     if ($domdefaults{'postsubmit'} eq 'off') {
16321:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
16322:                     } else {
16323:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
16324:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16325:                             $resulttext .= &mt('durations:').'<ul>';
16326:                             foreach my $type (@types) {
16327:                                 $resulttext .= '<li>';
16328:                                 my $timeout;
16329:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16330:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
16331:                                 }
16332:                                 my $display;
16333:                                 if ($timeout eq '0') {
16334:                                     $display = &mt('unlimited');
16335:                                 } elsif ($timeout eq '') {
16336:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
16337:                                 } else {
16338:                                     $display = &mt('[quant,_1,second]',$timeout);
16339:                                 }
16340:                                 if ($type eq 'community') {
16341:                                     $resulttext .= &mt('Communities');
16342:                                 } elsif ($type eq 'official') {
16343:                                     $resulttext .= &mt('Official courses');
16344:                                 } elsif ($type eq 'unofficial') {
16345:                                     $resulttext .= &mt('Unofficial courses');
16346:                                 } elsif ($type eq 'textbook') {
16347:                                     $resulttext .= &mt('Textbook courses');
16348:                                 } elsif ($type eq 'placement') {
16349:                                     $resulttext .= &mt('Placement tests');
16350:                                 }
16351:                                 $resulttext .= ' -- '.$display.'</li>';
16352:                             }
16353:                             $resulttext .= '</ul>';
16354:                         }
16355:                         $resulttext .= '</li>';
16356:                     }
16357:                 } elsif ($item eq 'coursecredits') {
16358:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16359:                         if (($domdefaults{'officialcredits'} eq '') &&
16360:                             ($domdefaults{'unofficialcredits'} eq '') &&
16361:                             ($domdefaults{'textbookcredits'} eq '')) {
16362:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
16363:                         } else {
16364:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
16365:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
16366:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
16367:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
16368:                                            '</ul>'.
16369:                                            '</li>';
16370:                         }
16371:                     } else {
16372:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
16373:                     }
16374:                 } elsif ($item eq 'canclone') {
16375:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
16376:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
16377:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
16378:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
16379:                         }
16380:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
16381:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
16382:                     } else {
16383:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
16384:                     }
16385:                 }
16386:             }
16387:             $resulttext .= '</ul>';
16388:         } else {
16389:             $resulttext = &mt('No changes made to course defaults');
16390:         }
16391:     } else {
16392:         $resulttext = '<span class="LC_error">'.
16393:             &mt('An error occurred: [_1]',$putresult).'</span>';
16394:     }
16395:     return $resulttext;
16396: }
16397: 
16398: sub modify_selfenrollment {
16399:     my ($dom,$lastactref,%domconfig) = @_;
16400:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
16401:     my @types = ('official','unofficial','community','textbook','placement');
16402:     my %titles = &tool_titles();
16403:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
16404:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
16405:     $ordered{'default'} = ['types','registered','approval','limit'];
16406: 
16407:     my (%roles,%shown,%toplevel);
16408:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
16409: 
16410:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
16411:         if ($domconfig{'selfenrollment'} eq '') {
16412:             $domconfig{'selfenrollment'} = {};
16413:         }
16414:     }
16415:     %toplevel = (
16416:                   admin      => 'Configuration Rights',
16417:                   default    => 'Default settings',
16418:                   validation => 'Validation of self-enrollment requests',
16419:                 );
16420:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
16421: 
16422:     if (ref($ordered{'admin'}) eq 'ARRAY') {
16423:         foreach my $item (@{$ordered{'admin'}}) {
16424:             foreach my $type (@types) {
16425:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
16426:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
16427:                 } else {
16428:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
16429:                 }
16430:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
16431:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
16432:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
16433:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
16434:                             push(@{$changes{'admin'}{$type}},$item);
16435:                         }
16436:                     } else {
16437:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
16438:                             push(@{$changes{'admin'}{$type}},$item);
16439:                         }
16440:                     }
16441:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
16442:                     push(@{$changes{'admin'}{$type}},$item);
16443:                 }
16444:             }
16445:         }
16446:     }
16447: 
16448:     foreach my $item (@{$ordered{'default'}}) {
16449:         foreach my $type (@types) {
16450:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
16451:             if ($item eq 'types') {
16452:                 unless (($value eq 'all') || ($value eq 'dom')) {
16453:                     $value = '';
16454:                 }
16455:             } elsif ($item eq 'registered') {
16456:                 unless ($value eq '1') {
16457:                     $value = 0;
16458:                 }
16459:             } elsif ($item eq 'approval') {
16460:                 unless ($value =~ /^[012]$/) {
16461:                     $value = 0;
16462:                 }
16463:             } else {
16464:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16465:                     $value = 'none';
16466:                 }
16467:             }
16468:             $selfenrollhash{'default'}{$type}{$item} = $value;
16469:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
16470:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16471:                     if ($selfenrollhash{'default'}{$type}{$item} ne
16472:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
16473:                          push(@{$changes{'default'}{$type}},$item);
16474:                     }
16475:                 } else {
16476:                     push(@{$changes{'default'}{$type}},$item);
16477:                 }
16478:             } else {
16479:                 push(@{$changes{'default'}{$type}},$item);
16480:             }
16481:             if ($item eq 'limit') {
16482:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16483:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
16484:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
16485:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
16486:                     }
16487:                 } else {
16488:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
16489:                 }
16490:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16491:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
16492:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
16493:                          push(@{$changes{'default'}{$type}},'cap');
16494:                     }
16495:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
16496:                     push(@{$changes{'default'}{$type}},'cap');
16497:                 }
16498:             }
16499:         }
16500:     }
16501: 
16502:     foreach my $item (@{$itemsref}) {
16503:         if ($item eq 'fields') {
16504:             my @changed;
16505:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
16506:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
16507:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
16508:             }
16509:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16510:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
16511:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
16512:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
16513:                 } else {
16514:                     @changed = @{$selfenrollhash{'validation'}{$item}};
16515:                 }
16516:             } else {
16517:                 @changed = @{$selfenrollhash{'validation'}{$item}};
16518:             }
16519:             if (@changed) {
16520:                 if ($selfenrollhash{'validation'}{$item}) { 
16521:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
16522:                 } else {
16523:                     $changes{'validation'}{$item} = &mt('None');
16524:                 }
16525:             }
16526:         } else {
16527:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
16528:             if ($item eq 'markup') {
16529:                if ($env{'form.selfenroll_validation_'.$item}) {
16530:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
16531:                }
16532:             }
16533:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16534:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
16535:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
16536:                 }
16537:             }
16538:         }
16539:     }
16540: 
16541:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
16542:                                              $dom);
16543:     if ($putresult eq 'ok') {
16544:         if (keys(%changes) > 0) {
16545:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16546:             $resulttext = &mt('Changes made:').'<ul>';
16547:             foreach my $key ('admin','default','validation') {
16548:                 if (ref($changes{$key}) eq 'HASH') {
16549:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
16550:                     if ($key eq 'validation') {
16551:                         foreach my $item (@{$itemsref}) {
16552:                             if (exists($changes{$key}{$item})) {
16553:                                 if ($item eq 'markup') {
16554:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16555:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
16556:                                 } else {  
16557:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16558:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
16559:                                 }
16560:                             }
16561:                         }
16562:                     } else {
16563:                         foreach my $type (@types) {
16564:                             if ($type eq 'community') {
16565:                                 $roles{'1'} = &mt('Community personnel');
16566:                             } else {
16567:                                 $roles{'1'} = &mt('Course personnel');
16568:                             }
16569:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
16570:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
16571:                                     if ($key eq 'admin') {
16572:                                         my @mgrdc = ();
16573:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16574:                                             foreach my $item (@{$ordered{'admin'}}) {
16575:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
16576:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
16577:                                                         push(@mgrdc,$item);
16578:                                                     }
16579:                                                 }
16580:                                             }
16581:                                             if (@mgrdc) {
16582:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
16583:                                             } else {
16584:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
16585:                                             }
16586:                                         }
16587:                                     } else {
16588:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16589:                                             foreach my $item (@{$ordered{$key}}) {
16590:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16591:                                                     $domdefaults{$type.'selfenroll'.$item} =
16592:                                                         $selfenrollhash{$key}{$type}{$item};
16593:                                                 }
16594:                                             }
16595:                                         }
16596:                                     }
16597:                                 }
16598:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
16599:                                 foreach my $item (@{$ordered{$key}}) {
16600:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16601:                                         $resulttext .= '<li>';
16602:                                         if ($key eq 'admin') {
16603:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
16604:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
16605:                                         } else {
16606:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
16607:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
16608:                                         }
16609:                                         $resulttext .= '</li>';
16610:                                     }
16611:                                 }
16612:                                 $resulttext .= '</ul></li>';
16613:                             }
16614:                         }
16615:                         $resulttext .= '</ul></li>'; 
16616:                     }
16617:                 }
16618:             }
16619:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
16620:                 my $cachetime = 24*60*60;
16621:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16622:                 if (ref($lastactref) eq 'HASH') {
16623:                     $lastactref->{'domdefaults'} = 1;
16624:                 }
16625:             }
16626:             $resulttext .= '</ul>';
16627:         } else {
16628:             $resulttext = &mt('No changes made to self-enrollment settings');
16629:         }
16630:     } else {
16631:         $resulttext = '<span class="LC_error">'.
16632:             &mt('An error occurred: [_1]',$putresult).'</span>';
16633:     }
16634:     return $resulttext;
16635: }
16636: 
16637: sub modify_usersessions {
16638:     my ($dom,$lastactref,%domconfig) = @_;
16639:     my @hostingtypes = ('version','excludedomain','includedomain');
16640:     my @offloadtypes = ('primary','default');
16641:     my %types = (
16642:                   remote => \@hostingtypes,
16643:                   hosted => \@hostingtypes,
16644:                   spares => \@offloadtypes,
16645:                 );
16646:     my @prefixes = ('remote','hosted','spares');
16647:     my @lcversions = &Apache::lonnet::all_loncaparevs();
16648:     my (%by_ip,%by_location,@intdoms,@instdoms);
16649:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16650:     my @locations = sort(keys(%by_location));
16651:     my (%defaultshash,%changes);
16652:     foreach my $prefix (@prefixes) {
16653:         $defaultshash{'usersessions'}{$prefix} = {};
16654:     }
16655:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16656:     my $resulttext;
16657:     my %iphost = &Apache::lonnet::get_iphost();
16658:     foreach my $prefix (@prefixes) {
16659:         next if ($prefix eq 'spares');
16660:         foreach my $type (@{$types{$prefix}}) {
16661:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16662:             if ($type eq 'version') {
16663:                 my $value = $env{'form.'.$prefix.'_'.$type};
16664:                 my $okvalue;
16665:                 if ($value ne '') {
16666:                     if (grep(/^\Q$value\E$/,@lcversions)) {
16667:                         $okvalue = $value;
16668:                     }
16669:                 }
16670:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16671:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16672:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
16673:                             if ($inuse == 0) {
16674:                                 $changes{$prefix}{$type} = 1;
16675:                             } else {
16676:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
16677:                                     $changes{$prefix}{$type} = 1;
16678:                                 }
16679:                                 if ($okvalue ne '') {
16680:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16681:                                 } 
16682:                             }
16683:                         } else {
16684:                             if (($inuse == 1) && ($okvalue ne '')) {
16685:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16686:                                 $changes{$prefix}{$type} = 1;
16687:                             }
16688:                         }
16689:                     } else {
16690:                         if (($inuse == 1) && ($okvalue ne '')) {
16691:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16692:                             $changes{$prefix}{$type} = 1;
16693:                         }
16694:                     }
16695:                 } else {
16696:                     if (($inuse == 1) && ($okvalue ne '')) {
16697:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16698:                         $changes{$prefix}{$type} = 1;
16699:                     }
16700:                 }
16701:             } else {
16702:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16703:                 my @okvals;
16704:                 foreach my $val (@vals) {
16705:                     if ($val =~ /:/) {
16706:                         my @items = split(/:/,$val);
16707:                         foreach my $item (@items) {
16708:                             if (ref($by_location{$item}) eq 'ARRAY') {
16709:                                 push(@okvals,$item);
16710:                             }
16711:                         }
16712:                     } else {
16713:                         if (ref($by_location{$val}) eq 'ARRAY') {
16714:                             push(@okvals,$val);
16715:                         }
16716:                     }
16717:                 }
16718:                 @okvals = sort(@okvals);
16719:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16720:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16721:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16722:                             if ($inuse == 0) {
16723:                                 $changes{$prefix}{$type} = 1; 
16724:                             } else {
16725:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16726:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
16727:                                 if (@changed > 0) {
16728:                                     $changes{$prefix}{$type} = 1;
16729:                                 }
16730:                             }
16731:                         } else {
16732:                             if ($inuse == 1) {
16733:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16734:                                 $changes{$prefix}{$type} = 1;
16735:                             }
16736:                         } 
16737:                     } else {
16738:                         if ($inuse == 1) {
16739:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16740:                             $changes{$prefix}{$type} = 1;
16741:                         }
16742:                     }
16743:                 } else {
16744:                     if ($inuse == 1) {
16745:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16746:                         $changes{$prefix}{$type} = 1;
16747:                     }
16748:                 }
16749:             }
16750:         }
16751:     }
16752: 
16753:     my @alldoms = &Apache::lonnet::all_domains();
16754:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16755:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
16756:     my $savespares;
16757: 
16758:     foreach my $lonhost (sort(keys(%servers))) {
16759:         my $serverhomeID =
16760:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
16761:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
16762:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
16763:         my %spareschg;
16764:         foreach my $type (@{$types{'spares'}}) {
16765:             my @okspares;
16766:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
16767:             foreach my $server (@checked) {
16768:                 if (&Apache::lonnet::hostname($server) ne '') {
16769:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
16770:                         unless (grep(/^\Q$server\E$/,@okspares)) {
16771:                             push(@okspares,$server);
16772:                         }
16773:                     }
16774:                 }
16775:             }
16776:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
16777:             my $newspare;
16778:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
16779:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
16780:                     $newspare = $new;
16781:                 }
16782:             }
16783:             my @spares;
16784:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
16785:                 @spares = sort(@okspares,$newspare);
16786:             } else {
16787:                 @spares = sort(@okspares);
16788:             }
16789:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
16790:             if (ref($spareid{$lonhost}) eq 'HASH') {
16791:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
16792:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
16793:                     if (@diffs > 0) {
16794:                         $spareschg{$type} = 1;
16795:                     }
16796:                 }
16797:             }
16798:         }
16799:         if (keys(%spareschg) > 0) {
16800:             $changes{'spares'}{$lonhost} = \%spareschg;
16801:         }
16802:     }
16803:     $defaultshash{'usersessions'}{'offloadnow'} = {};
16804:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
16805:     my @okoffload;
16806:     if (@offloadnow) {
16807:         foreach my $server (@offloadnow) {
16808:             if (&Apache::lonnet::hostname($server) ne '') {
16809:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
16810:                     push(@okoffload,$server);
16811:                 }
16812:             }
16813:         }
16814:         if (@okoffload) {
16815:             foreach my $lonhost (@okoffload) {
16816:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
16817:             }
16818:         }
16819:     }
16820:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
16821:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
16822:             if (ref($changes{'spares'}) eq 'HASH') {
16823:                 if (keys(%{$changes{'spares'}}) > 0) {
16824:                     $savespares = 1;
16825:                 }
16826:             }
16827:         } else {
16828:             $savespares = 1;
16829:         }
16830:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
16831:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
16832:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
16833:                     $changes{'offloadnow'} = 1;
16834:                     last;
16835:                 }
16836:             }
16837:             unless ($changes{'offloadnow'}) {
16838:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
16839:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
16840:                         $changes{'offloadnow'} = 1;
16841:                         last;
16842:                     }
16843:                 }
16844:             }
16845:         } elsif (@okoffload) {
16846:             $changes{'offloadnow'} = 1;
16847:         }
16848:     } elsif (@okoffload) {
16849:         $changes{'offloadnow'} = 1;
16850:     }
16851:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
16852:     if ((keys(%changes) > 0) || ($savespares)) {
16853:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16854:                                                  $dom);
16855:         if ($putresult eq 'ok') {
16856:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16857:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
16858:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
16859:                 }
16860:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
16861:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
16862:                 }
16863:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16864:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
16865:                 }
16866:             }
16867:             my $cachetime = 24*60*60;
16868:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16869:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
16870:             if (ref($lastactref) eq 'HASH') {
16871:                 $lastactref->{'domdefaults'} = 1;
16872:                 $lastactref->{'usersessions'} = 1;
16873:             }
16874:             if (keys(%changes) > 0) {
16875:                 my %lt = &usersession_titles();
16876:                 $resulttext = &mt('Changes made:').'<ul>';
16877:                 foreach my $prefix (@prefixes) {
16878:                     if (ref($changes{$prefix}) eq 'HASH') {
16879:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
16880:                         if ($prefix eq 'spares') {
16881:                             if (ref($changes{$prefix}) eq 'HASH') {
16882:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
16883:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
16884:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
16885:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
16886:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16887:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
16888:                                         foreach my $type (@{$types{$prefix}}) {
16889:                                             if ($changes{$prefix}{$lonhost}{$type}) {
16890:                                                 my $offloadto = &mt('None');
16891:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
16892:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
16893:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
16894:                                                     }
16895:                                                 }
16896:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
16897:                                             }
16898:                                         }
16899:                                     }
16900:                                     $resulttext .= '</li>';
16901:                                 }
16902:                             }
16903:                         } else {
16904:                             foreach my $type (@{$types{$prefix}}) {
16905:                                 if (defined($changes{$prefix}{$type})) {
16906:                                     my ($newvalue,$notinuse);
16907:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16908:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
16909:                                             if ($type eq 'version') {
16910:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
16911:                                             } else {
16912:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16913:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
16914:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
16915:                                                     }
16916:                                                 } else {
16917:                                                     $notinuse = 1;
16918:                                                 }
16919:                                             }
16920:                                         }
16921:                                     }
16922:                                     if ($newvalue eq '') {
16923:                                         if ($type eq 'version') {
16924:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
16925:                                         } elsif ($notinuse) {
16926:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
16927:                                         } else {
16928:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
16929:                                         }
16930:                                     } else {
16931:                                         if ($type eq 'version') {
16932:                                             $newvalue .= ' '.&mt('(or later)');
16933:                                         }
16934:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
16935:                                     }
16936:                                 }
16937:                             }
16938:                         }
16939:                         $resulttext .= '</ul>';
16940:                     }
16941:                 }
16942:                 if ($changes{'offloadnow'}) {
16943:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16944:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
16945:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
16946:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
16947:                                 $resulttext .= '<li>'.$lonhost.'</li>';
16948:                             }
16949:                             $resulttext .= '</ul>';
16950:                         } else {
16951:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
16952:                         }
16953:                     } else {
16954:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
16955:                     }
16956:                 }
16957:                 $resulttext .= '</ul>';
16958:             } else {
16959:                 $resulttext = $nochgmsg;
16960:             }
16961:         } else {
16962:             $resulttext = '<span class="LC_error">'.
16963:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16964:         }
16965:     } else {
16966:         $resulttext = $nochgmsg;
16967:     }
16968:     return $resulttext;
16969: }
16970: 
16971: sub modify_ssl {
16972:     my ($dom,$lastactref,%domconfig) = @_;
16973:     my (%by_ip,%by_location,@intdoms,@instdoms);
16974:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16975:     my @locations = sort(keys(%by_location));
16976:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16977:     my (%defaultshash,%changes);
16978:     my $action = 'ssl';
16979:     my @prefixes = ('connto','connfrom','replication');
16980:     foreach my $prefix (@prefixes) {
16981:         $defaultshash{$action}{$prefix} = {};
16982:     }
16983:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16984:     my $resulttext;
16985:     my %iphost = &Apache::lonnet::get_iphost();
16986:     my @reptypes = ('certreq','nocertreq');
16987:     my @connecttypes = ('dom','intdom','other');
16988:     my %types = (
16989:                   connto      => \@connecttypes,
16990:                   connfrom    => \@connecttypes,
16991:                   replication => \@reptypes,
16992:                 );
16993:     foreach my $prefix (sort(keys(%types))) {
16994:         foreach my $type (@{$types{$prefix}}) {
16995:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
16996:                 my $value = 'yes';
16997:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
16998:                     $value = $env{'form.'.$prefix.'_'.$type};
16999:                 }
17000:                 if (ref($domconfig{$action}) eq 'HASH') {
17001:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
17002:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
17003:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
17004:                                 $changes{$prefix}{$type} = 1;
17005:                             }
17006:                             $defaultshash{$action}{$prefix}{$type} = $value;
17007:                         } else {
17008:                             $defaultshash{$action}{$prefix}{$type} = $value;
17009:                             $changes{$prefix}{$type} = 1;
17010:                         }
17011:                     } else {
17012:                         $defaultshash{$action}{$prefix}{$type} = $value;
17013:                         $changes{$prefix}{$type} = 1;
17014:                     }
17015:                 } else {
17016:                     $defaultshash{$action}{$prefix}{$type} = $value;
17017:                     $changes{$prefix}{$type} = 1;
17018:                 }
17019:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
17020:                     delete($changes{$prefix}{$type});
17021:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
17022:                     delete($changes{$prefix}{$type});
17023:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
17024:                     delete($changes{$prefix}{$type});
17025:                 }
17026:             } elsif ($prefix eq 'replication') {
17027:                 if (@locations > 0) {
17028:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
17029:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
17030:                     my @okvals;
17031:                     foreach my $val (@vals) {
17032:                         if ($val =~ /:/) {
17033:                             my @items = split(/:/,$val);
17034:                             foreach my $item (@items) {
17035:                                 if (ref($by_location{$item}) eq 'ARRAY') {
17036:                                     push(@okvals,$item);
17037:                                 }
17038:                             }
17039:                         } else {
17040:                             if (ref($by_location{$val}) eq 'ARRAY') {
17041:                                 push(@okvals,$val);
17042:                             }
17043:                         }
17044:                     }
17045:                     @okvals = sort(@okvals);
17046:                     if (ref($domconfig{$action}) eq 'HASH') {
17047:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
17048:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
17049:                                 if ($inuse == 0) {
17050:                                     $changes{$prefix}{$type} = 1;
17051:                                 } else {
17052:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
17053:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
17054:                                     if (@changed > 0) {
17055:                                         $changes{$prefix}{$type} = 1;
17056:                                     }
17057:                                 }
17058:                             } else {
17059:                                 if ($inuse == 1) {
17060:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
17061:                                     $changes{$prefix}{$type} = 1;
17062:                                 }
17063:                             }
17064:                         } else {
17065:                             if ($inuse == 1) {
17066:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
17067:                                 $changes{$prefix}{$type} = 1;
17068:                             }
17069:                         }
17070:                     } else {
17071:                         if ($inuse == 1) {
17072:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
17073:                             $changes{$prefix}{$type} = 1;
17074:                         }
17075:                     }
17076:                 }
17077:             }
17078:         }
17079:     }
17080:     if (keys(%changes)) {
17081:         foreach my $prefix (keys(%changes)) {
17082:             if (ref($changes{$prefix}) eq 'HASH') {
17083:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
17084:                     delete($changes{$prefix});
17085:                 }
17086:             } else {
17087:                 delete($changes{$prefix});
17088:             }
17089:         }
17090:     }
17091:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
17092:     if (keys(%changes) > 0) {
17093:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17094:                                                  $dom);
17095:         if ($putresult eq 'ok') {
17096:             if (ref($defaultshash{$action}) eq 'HASH') {
17097:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
17098:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
17099:                 }
17100:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
17101:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
17102:                 }
17103:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
17104:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
17105:                 }
17106:             }
17107:             my $cachetime = 24*60*60;
17108:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17109:             if (ref($lastactref) eq 'HASH') {
17110:                 $lastactref->{'domdefaults'} = 1;
17111:             }
17112:             if (keys(%changes) > 0) {
17113:                 my %titles = &ssl_titles();
17114:                 $resulttext = &mt('Changes made:').'<ul>';
17115:                 foreach my $prefix (@prefixes) {
17116:                     if (ref($changes{$prefix}) eq 'HASH') {
17117:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
17118:                         foreach my $type (@{$types{$prefix}}) {
17119:                             if (defined($changes{$prefix}{$type})) {
17120:                                 my ($newvalue,$notinuse);
17121:                                 if (ref($defaultshash{$action}) eq 'HASH') {
17122:                                     if (ref($defaultshash{$action}{$prefix})) {
17123:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
17124:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
17125:                                         } else {
17126:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
17127:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
17128:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
17129:                                                 }
17130:                                             } else {
17131:                                                 $notinuse = 1;
17132:                                             }
17133:                                         }
17134:                                     }
17135:                                     if ($notinuse) {
17136:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
17137:                                     } elsif ($newvalue eq '') {
17138:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
17139:                                     } else {
17140:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
17141:                                     }
17142:                                 }
17143:                             }
17144:                         }
17145:                         $resulttext .= '</ul>';
17146:                     }
17147:                 }
17148:             } else {
17149:                 $resulttext = $nochgmsg;
17150:             }
17151:         } else {
17152:             $resulttext = '<span class="LC_error">'.
17153:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17154:         }
17155:     } else {
17156:         $resulttext = $nochgmsg;
17157:     }
17158:     return $resulttext;
17159: }
17160: 
17161: sub modify_trust {
17162:     my ($dom,$lastactref,%domconfig) = @_;
17163:     my (%by_ip,%by_location,@intdoms,@instdoms);
17164:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17165:     my @locations = sort(keys(%by_location));
17166:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
17167:     my @types = ('exc','inc');
17168:     my (%defaultshash,%changes);
17169:     foreach my $prefix (@prefixes) {
17170:         $defaultshash{'trust'}{$prefix} = {};
17171:     }
17172:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17173:     my $resulttext;
17174:     foreach my $prefix (@prefixes) {
17175:         foreach my $type (@types) {
17176:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
17177:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
17178:             my @okvals;
17179:             foreach my $val (@vals) {
17180:                 if ($val =~ /:/) {
17181:                     my @items = split(/:/,$val);
17182:                     foreach my $item (@items) {
17183:                         if (ref($by_location{$item}) eq 'ARRAY') {
17184:                             push(@okvals,$item);
17185:                         }
17186:                     }
17187:                 } else {
17188:                     if (ref($by_location{$val}) eq 'ARRAY') {
17189:                         push(@okvals,$val);
17190:                     }
17191:                 }
17192:             }
17193:             @okvals = sort(@okvals);
17194:             if (ref($domconfig{'trust'}) eq 'HASH') {
17195:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
17196:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
17197:                         if ($inuse == 0) {
17198:                             $changes{$prefix}{$type} = 1;
17199:                         } else {
17200:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17201:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
17202:                             if (@changed > 0) {
17203:                                 $changes{$prefix}{$type} = 1;
17204:                             }
17205:                         }
17206:                     } else {
17207:                         if ($inuse == 1) {
17208:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17209:                             $changes{$prefix}{$type} = 1;
17210:                         }
17211:                     }
17212:                 } else {
17213:                     if ($inuse == 1) {
17214:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17215:                         $changes{$prefix}{$type} = 1;
17216:                     }
17217:                 }
17218:             } else {
17219:                 if ($inuse == 1) {
17220:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17221:                     $changes{$prefix}{$type} = 1;
17222:                 }
17223:             }
17224:         }
17225:     }
17226:     my $nochgmsg = &mt('No changes made to trust settings.');
17227:     if (keys(%changes) > 0) {
17228:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17229:                                                  $dom);
17230:         if ($putresult eq 'ok') {
17231:             if (ref($defaultshash{'trust'}) eq 'HASH') {
17232:                 foreach my $prefix (@prefixes) {
17233:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
17234:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
17235:                     }
17236:                 }
17237:             }
17238:             my $cachetime = 24*60*60;
17239:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17240:             if (ref($lastactref) eq 'HASH') {
17241:                 $lastactref->{'domdefaults'} = 1;
17242:             }
17243:             if (keys(%changes) > 0) {
17244:                 my %lt = &trust_titles();
17245:                 $resulttext = &mt('Changes made:').'<ul>';
17246:                 foreach my $prefix (@prefixes) {
17247:                     if (ref($changes{$prefix}) eq 'HASH') {
17248:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
17249:                         foreach my $type (@types) {
17250:                             if (defined($changes{$prefix}{$type})) {
17251:                                 my ($newvalue,$notinuse);
17252:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
17253:                                     if (ref($defaultshash{'trust'}{$prefix})) {
17254:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
17255:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
17256:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
17257:                                             }
17258:                                         } else {
17259:                                             $notinuse = 1;
17260:                                         }
17261:                                     }
17262:                                 }
17263:                                 if ($notinuse) {
17264:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
17265:                                 } elsif ($newvalue eq '') {
17266:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
17267:                                 } else {
17268:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
17269:                                 }
17270:                             }
17271:                         }
17272:                         $resulttext .= '</ul>';
17273:                     }
17274:                 }
17275:                 $resulttext .= '</ul>';
17276:             } else {
17277:                 $resulttext = $nochgmsg;
17278:             }
17279:         } else {
17280:             $resulttext = '<span class="LC_error">'.
17281:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17282:         }
17283:     } else {
17284:         $resulttext = $nochgmsg;
17285:     }
17286:     return $resulttext;
17287: }
17288: 
17289: sub modify_loadbalancing {
17290:     my ($dom,%domconfig) = @_;
17291:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
17292:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
17293:     my ($othertitle,$usertypes,$types) =
17294:         &Apache::loncommon::sorted_inst_types($dom);
17295:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17296:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
17297:     my @sparestypes = ('primary','default');
17298:     my %typetitles = &sparestype_titles();
17299:     my $resulttext;
17300:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
17301:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
17302:         %existing = %{$domconfig{'loadbalancing'}};
17303:     }
17304:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
17305:                               \%currtargets,\%currrules,\%currcookies);
17306:     my ($saveloadbalancing,%defaultshash,%changes);
17307:     my ($alltypes,$othertypes,$titles) =
17308:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
17309:     my %ruletitles = &offloadtype_text();
17310:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
17311:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
17312:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
17313:         if ($balancer eq '') {
17314:             next;
17315:         }
17316:         if (!exists($servers{$balancer})) {
17317:             if (exists($currbalancer{$balancer})) {
17318:                 push(@{$changes{'delete'}},$balancer);
17319:             }
17320:             next;
17321:         }
17322:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
17323:             push(@{$changes{'delete'}},$balancer);
17324:             next;
17325:         }
17326:         if (!exists($currbalancer{$balancer})) {
17327:             push(@{$changes{'add'}},$balancer);
17328:         }
17329:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
17330:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
17331:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
17332:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
17333:             $saveloadbalancing = 1;
17334:         }
17335:         foreach my $sparetype (@sparestypes) {
17336:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
17337:             my @offloadto;
17338:             foreach my $target (@targets) {
17339:                 if (($servers{$target}) && ($target ne $balancer)) {
17340:                     if ($sparetype eq 'default') {
17341:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
17342:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
17343:                         }
17344:                     }
17345:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
17346:                         push(@offloadto,$target);
17347:                     }
17348:                 }
17349:             }
17350:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
17351:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
17352:                     push(@offloadto,$balancer);
17353:                 }
17354:             }
17355:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
17356:         }
17357:         if ($env{'form.loadbalancing_cookie_'.$i}) {
17358:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
17359:             if (exists($currbalancer{$balancer})) { 
17360:                 unless ($currcookies{$balancer}) {
17361:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
17362:                 }
17363:             }
17364:         } elsif (exists($currbalancer{$balancer})) {
17365:             if ($currcookies{$balancer}) {
17366:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
17367:             }
17368:         }
17369:         if (ref($currtargets{$balancer}) eq 'HASH') {
17370:             foreach my $sparetype (@sparestypes) {
17371:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
17372:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
17373:                     if (@targetdiffs > 0) {
17374:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17375:                     }
17376:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17377:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17378:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17379:                     }
17380:                 }
17381:             }
17382:         } else {
17383:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
17384:                 foreach my $sparetype (@sparestypes) {
17385:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17386:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17387:                             $changes{'curr'}{$balancer}{'targets'} = 1;
17388:                         }
17389:                     }
17390:                 }
17391:             }
17392:         }
17393:         my $ishomedom;
17394:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
17395:             $ishomedom = 1;
17396:         }
17397:         if (ref($alltypes) eq 'ARRAY') {
17398:             foreach my $type (@{$alltypes}) {
17399:                 my $rule;
17400:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
17401:                          (!$ishomedom)) {
17402:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
17403:                 }
17404:                 if ($rule eq 'specific') {
17405:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
17406:                     if (exists($servers{$specifiedhost})) {
17407:                         $rule = $specifiedhost;
17408:                     }
17409:                 }
17410:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
17411:                 if (ref($currrules{$balancer}) eq 'HASH') {
17412:                     if ($rule ne $currrules{$balancer}{$type}) {
17413:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17414:                     }
17415:                 } elsif ($rule ne '') {
17416:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17417:                 }
17418:             }
17419:         }
17420:     }
17421:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
17422:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
17423:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
17424:             $defaultshash{'loadbalancing'} = {};
17425:         }
17426:         my $putresult = &Apache::lonnet::put_dom('configuration',
17427:                                                  \%defaultshash,$dom);
17428:         if ($putresult eq 'ok') {
17429:             if (keys(%changes) > 0) {
17430:                 my %toupdate;
17431:                 if (ref($changes{'delete'}) eq 'ARRAY') {
17432:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
17433:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
17434:                         $toupdate{$balancer} = 1;
17435:                     }
17436:                 }
17437:                 if (ref($changes{'add'}) eq 'ARRAY') {
17438:                     foreach my $balancer (sort(@{$changes{'add'}})) {
17439:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
17440:                         $toupdate{$balancer} = 1;
17441:                     }
17442:                 }
17443:                 if (ref($changes{'curr'}) eq 'HASH') {
17444:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
17445:                         $toupdate{$balancer} = 1;
17446:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
17447:                             if ($changes{'curr'}{$balancer}{'targets'}) {
17448:                                 my %offloadstr;
17449:                                 foreach my $sparetype (@sparestypes) {
17450:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17451:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17452:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17453:                                         }
17454:                                     }
17455:                                 }
17456:                                 if (keys(%offloadstr) == 0) {
17457:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
17458:                                 } else {
17459:                                     my $showoffload;
17460:                                     foreach my $sparetype (@sparestypes) {
17461:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
17462:                                         if (defined($offloadstr{$sparetype})) {
17463:                                             $showoffload .= $offloadstr{$sparetype};
17464:                                         } else {
17465:                                             $showoffload .= &mt('None');
17466:                                         }
17467:                                         $showoffload .= ('&nbsp;'x3);
17468:                                     }
17469:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
17470:                                 }
17471:                             }
17472:                         }
17473:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
17474:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
17475:                                 foreach my $type (@{$alltypes}) {
17476:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
17477:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17478:                                         my $balancetext;
17479:                                         if ($rule eq '') {
17480:                                             $balancetext =  $ruletitles{'default'};
17481:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
17482:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17483:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17484:                                                 foreach my $sparetype (@sparestypes) {
17485:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17486:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17487:                                                     }
17488:                                                 }
17489:                                                 foreach my $item (@{$alltypes}) {
17490:                                                     next if ($item =~  /^_LC_ipchange/);
17491:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
17492:                                                     if ($hasrule eq 'homeserver') {
17493:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
17494:                                                     } else {
17495:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
17496:                                                             if ($servers{$hasrule}) {
17497:                                                                 $toupdate{$hasrule} = 1;
17498:                                                             }
17499:                                                         }
17500:                                                     }
17501:                                                 }
17502:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
17503:                                                     $balancetext =  $ruletitles{$rule};
17504:                                                 } else {
17505:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17506:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
17507:                                                     if ($receiver) {
17508:                                                         $toupdate{$receiver};
17509:                                                     }
17510:                                                 }
17511:                                             } else {
17512:                                                 $balancetext =  $ruletitles{$rule};
17513:                                             }
17514:                                         } else {
17515:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
17516:                                         }
17517:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
17518:                                     }
17519:                                 }
17520:                             }
17521:                         }
17522:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
17523:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
17524:                                                       $balancer).'</li>'; 
17525:                         }
17526:                         if (keys(%toupdate)) {
17527:                             my %thismachine;
17528:                             my $updatedhere;
17529:                             my $cachetime = 60*60*24;
17530:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
17531:                             foreach my $lonhost (keys(%toupdate)) {
17532:                                 if ($thismachine{$lonhost}) {
17533:                                     unless ($updatedhere) {
17534:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
17535:                                                                       $defaultshash{'loadbalancing'},
17536:                                                                       $cachetime);
17537:                                         $updatedhere = 1;
17538:                                     }
17539:                                 } else {
17540:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
17541:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
17542:                                 }
17543:                             }
17544:                         }
17545:                     }
17546:                 }
17547:                 if ($resulttext ne '') {
17548:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
17549:                 } else {
17550:                     $resulttext = $nochgmsg;
17551:                 }
17552:             } else {
17553:                 $resulttext = $nochgmsg;
17554:             }
17555:         } else {
17556:             $resulttext = '<span class="LC_error">'.
17557:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17558:         }
17559:     } else {
17560:         $resulttext = $nochgmsg;
17561:     }
17562:     return $resulttext;
17563: }
17564: 
17565: sub recurse_check {
17566:     my ($chkcats,$categories,$depth,$name) = @_;
17567:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
17568:         my $chg = 0;
17569:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
17570:             my $category = $chkcats->[$depth]{$name}[$j];
17571:             my $item;
17572:             if ($category eq '') {
17573:                 $chg ++;
17574:             } else {
17575:                 my $deeper = $depth + 1;
17576:                 $item = &escape($category).':'.&escape($name).':'.$depth;
17577:                 if ($chg) {
17578:                     $categories->{$item} -= $chg;
17579:                 }
17580:                 &recurse_check($chkcats,$categories,$deeper,$category);
17581:                 $deeper --;
17582:             }
17583:         }
17584:     }
17585:     return;
17586: }
17587: 
17588: sub recurse_cat_deletes {
17589:     my ($item,$coursecategories,$deletions) = @_;
17590:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
17591:     my $subdepth = $depth + 1;
17592:     if (ref($coursecategories) eq 'HASH') {
17593:         foreach my $subitem (keys(%{$coursecategories})) {
17594:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
17595:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
17596:                 delete($coursecategories->{$subitem});
17597:                 $deletions->{$subitem} = 1;
17598:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
17599:             }
17600:         }
17601:     }
17602:     return;
17603: }
17604: 
17605: sub active_dc_picker {
17606:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
17607:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17608:     my @domcoord = keys(%domcoords);
17609:     if (keys(%currhash)) {
17610:         foreach my $dc (keys(%currhash)) {
17611:             unless (exists($domcoords{$dc})) {
17612:                 push(@domcoord,$dc);
17613:             }
17614:         }
17615:     }
17616:     @domcoord = sort(@domcoord);
17617:     my $numdcs = scalar(@domcoord);
17618:     my $rows = 0;
17619:     my $table;
17620:     if ($numdcs > 1) {
17621:         $table = '<table>';
17622:         for (my $i=0; $i<@domcoord; $i++) {
17623:             my $rem = $i%($numinrow);
17624:             if ($rem == 0) {
17625:                 if ($i > 0) {
17626:                     $table .= '</tr>';
17627:                 }
17628:                 $table .= '<tr>';
17629:                 $rows ++;
17630:             }
17631:             my $check = '';
17632:             if ($inputtype eq 'radio') {
17633:                 if (keys(%currhash) == 0) {
17634:                     if (!$i) {
17635:                         $check = ' checked="checked"';
17636:                     }
17637:                 } elsif (exists($currhash{$domcoord[$i]})) {
17638:                     $check = ' checked="checked"';
17639:                 }
17640:             } else {
17641:                 if (exists($currhash{$domcoord[$i]})) {
17642:                     $check = ' checked="checked"';
17643:                 }
17644:             }
17645:             if ($i == @domcoord - 1) {
17646:                 my $colsleft = $numinrow - $rem;
17647:                 if ($colsleft > 1) {
17648:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
17649:                 } else {
17650:                     $table .= '<td class="LC_left_item">';
17651:                 }
17652:             } else {
17653:                 $table .= '<td class="LC_left_item">';
17654:             }
17655:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
17656:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17657:             $table .= '<span class="LC_nobreak"><label>'.
17658:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
17659:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
17660:             if ($user ne $dcname.':'.$dcdom) {
17661:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17662:             }
17663:             $table .= '</label></span></td>';
17664:         }
17665:         $table .= '</tr></table>';
17666:     } elsif ($numdcs == 1) {
17667:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
17668:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17669:         if ($inputtype eq 'radio') {
17670:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
17671:             if ($user ne $dcname.':'.$dcdom) {
17672:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17673:             }
17674:         } else {
17675:             my $check;
17676:             if (exists($currhash{$domcoord[0]})) {
17677:                 $check = ' checked="checked"';
17678:             }
17679:             $table = '<span class="LC_nobreak"><label>'.
17680:                      '<input type="checkbox" name="'.$name.'" '.
17681:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
17682:             if ($user ne $dcname.':'.$dcdom) {
17683:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17684:             }
17685:             $table .= '</label></span>';
17686:             $rows ++;
17687:         }
17688:     }
17689:     return ($numdcs,$table,$rows);
17690: }
17691: 
17692: sub usersession_titles {
17693:     return &Apache::lonlocal::texthash(
17694:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
17695:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
17696:                spares => 'Servers offloaded to, when busy',
17697:                version => 'LON-CAPA version requirement',
17698:                excludedomain => 'Allow all, but exclude specific domains',
17699:                includedomain => 'Deny all, but include specific domains',
17700:                primary => 'Primary (checked first)',
17701:                default => 'Default',
17702:            );
17703: }
17704: 
17705: sub id_for_thisdom {
17706:     my (%servers) = @_;
17707:     my %altids;
17708:     foreach my $server (keys(%servers)) {
17709:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
17710:         if ($serverhome ne $server) {
17711:             $altids{$serverhome} = $server;
17712:         }
17713:     }
17714:     return %altids;
17715: }
17716: 
17717: sub count_servers {
17718:     my ($currbalancer,%servers) = @_;
17719:     my (@spares,$numspares);
17720:     foreach my $lonhost (sort(keys(%servers))) {
17721:         next if ($currbalancer eq $lonhost);
17722:         push(@spares,$lonhost);
17723:     }
17724:     if ($currbalancer) {
17725:         $numspares = scalar(@spares);
17726:     } else {
17727:         $numspares = scalar(@spares) - 1;
17728:     }
17729:     return ($numspares,@spares);
17730: }
17731: 
17732: sub lonbalance_targets_js {
17733:     my ($dom,$types,$servers,$settings) = @_;
17734:     my $select = &mt('Select');
17735:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
17736:     if (ref($servers) eq 'HASH') {
17737:         $alltargets = join("','",sort(keys(%{$servers})));
17738:         my @homedoms;
17739:         foreach my $server (sort(keys(%{$servers}))) {
17740:             if (&Apache::lonnet::host_domain($server) eq $dom) {
17741:                 push(@homedoms,'1');
17742:             } else {
17743:                 push(@homedoms,'0');
17744:             }
17745:         }
17746:         $allishome = join("','",@homedoms);
17747:     }
17748:     if (ref($types) eq 'ARRAY') {
17749:         if (@{$types} > 0) {
17750:             @alltypes = @{$types};
17751:         }
17752:     }
17753:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
17754:     $allinsttypes = join("','",@alltypes);
17755:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
17756:     if (ref($settings) eq 'HASH') {
17757:         %existing = %{$settings};
17758:     }
17759:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
17760:                               \%currtargets,\%currrules,\%currcookies);
17761:     my $balancers = join("','",sort(keys(%currbalancer)));
17762:     return <<"END";
17763: 
17764: <script type="text/javascript">
17765: // <![CDATA[
17766: 
17767: currBalancers = new Array('$balancers');
17768: 
17769: function toggleTargets(balnum) {
17770:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17771:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
17772:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
17773:     var prevbalancer = prevhostitem.value;
17774:     var baltotal = document.getElementById('loadbalancing_total').value;
17775:     prevhostitem.value = balancer;
17776:     if (prevbalancer != '') {
17777:         var prevIdx = currBalancers.indexOf(prevbalancer);
17778:         if (prevIdx != -1) {
17779:             currBalancers.splice(prevIdx,1);
17780:         }
17781:     }
17782:     if (balancer == '') {
17783:         hideSpares(balnum);
17784:     } else {
17785:         var currIdx = currBalancers.indexOf(balancer);
17786:         if (currIdx == -1) {
17787:             currBalancers.push(balancer);
17788:         }
17789:         var homedoms = new Array('$allishome');
17790:         var ishomedom = homedoms[lonhostitem.selectedIndex];
17791:         showSpares(balancer,ishomedom,balnum);
17792:     }
17793:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
17794:     return;
17795: }
17796: 
17797: function showSpares(balancer,ishomedom,balnum) {
17798:     var alltargets = new Array('$alltargets');
17799:     var insttypes = new Array('$allinsttypes');
17800:     var offloadtypes = new Array('primary','default');
17801: 
17802:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
17803:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
17804:  
17805:     for (var i=0; i<offloadtypes.length; i++) {
17806:         var count = 0;
17807:         for (var j=0; j<alltargets.length; j++) {
17808:             if (alltargets[j] != balancer) {
17809:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
17810:                 item.value = alltargets[j];
17811:                 item.style.textAlign='left';
17812:                 item.style.textFace='normal';
17813:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
17814:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
17815:                     item.disabled = '';
17816:                 } else {
17817:                     item.disabled = 'disabled';
17818:                     item.checked = false;
17819:                 }
17820:                 count ++;
17821:             }
17822:         }
17823:     }
17824:     for (var k=0; k<insttypes.length; k++) {
17825:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
17826:             if (ishomedom == 1) {
17827:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17828:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17829:             } else {
17830:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17831:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17832:             }
17833:         } else {
17834:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17835:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17836:         }
17837:         if ((insttypes[k] != '_LC_external') && 
17838:             ((insttypes[k] != '_LC_internetdom') ||
17839:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
17840:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
17841:             item.options.length = 0;
17842:             item.options[0] = new Option("","",true,true);
17843:             var idx = 0;
17844:             for (var m=0; m<alltargets.length; m++) {
17845:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
17846:                     idx ++;
17847:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17848:                 }
17849:             }
17850:         }
17851:     }
17852:     return;
17853: }
17854: 
17855: function hideSpares(balnum) {
17856:     var alltargets = new Array('$alltargets');
17857:     var insttypes = new Array('$allinsttypes');
17858:     var offloadtypes = new Array('primary','default');
17859: 
17860:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
17861:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
17862: 
17863:     var total = alltargets.length - 1;
17864:     for (var i=0; i<offloadtypes; i++) {
17865:         for (var j=0; j<total; j++) {
17866:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
17867:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
17868:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
17869:         }
17870:     }
17871:     for (var k=0; k<insttypes.length; k++) {
17872:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17873:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17874:         if (insttypes[k] != '_LC_external') {
17875:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
17876:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
17877:         }
17878:     }
17879:     return;
17880: }
17881: 
17882: function checkOffloads(item,balnum,type) {
17883:     var alltargets = new Array('$alltargets');
17884:     var offloadtypes = new Array('primary','default');
17885:     if (item.checked) {
17886:         var total = alltargets.length - 1;
17887:         var other;
17888:         if (type == offloadtypes[0]) {
17889:             other = offloadtypes[1];
17890:         } else {
17891:             other = offloadtypes[0];
17892:         }
17893:         for (var i=0; i<total; i++) {
17894:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
17895:             if (server == item.value) {
17896:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
17897:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
17898:                 }
17899:             }
17900:         }
17901:     }
17902:     return;
17903: }
17904: 
17905: function singleServerToggle(balnum,type) {
17906:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
17907:     if (offloadtoSelIdx == 0) {
17908:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
17909:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17910: 
17911:     } else {
17912:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
17913:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17914:     }
17915:     return;
17916: }
17917: 
17918: function balanceruleChange(formname,balnum,type) {
17919:     if (type == '_LC_external') {
17920:         return;
17921:     }
17922:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
17923:     for (var i=0; i<typesRules.length; i++) {
17924:         if (formname.elements[typesRules[i]].checked) {
17925:             if (formname.elements[typesRules[i]].value != 'specific') {
17926:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
17927:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17928:             } else {
17929:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17930:             }
17931:         }
17932:     }
17933:     return;
17934: }
17935: 
17936: function balancerDeleteChange(balnum) {
17937:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17938:     var baltotal = document.getElementById('loadbalancing_total').value;
17939:     var addtarget;
17940:     var removetarget;
17941:     var action = 'delete';
17942:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
17943:         var lonhost = hostitem.value;
17944:         var currIdx = currBalancers.indexOf(lonhost);
17945:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
17946:             if (currIdx != -1) {
17947:                 currBalancers.splice(currIdx,1);
17948:             }
17949:             addtarget = lonhost;
17950:         } else {
17951:             if (currIdx == -1) {
17952:                 currBalancers.push(lonhost);
17953:             }
17954:             removetarget = lonhost;
17955:             action = 'undelete';
17956:         }
17957:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
17958:     }
17959:     return;
17960: }
17961: 
17962: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
17963:     if (baltotal > 1) {
17964:         var offloadtypes = new Array('primary','default');
17965:         var alltargets = new Array('$alltargets');
17966:         var insttypes = new Array('$allinsttypes');
17967:         for (var i=0; i<baltotal; i++) {
17968:             if (i != balnum) {
17969:                 for (var j=0; j<offloadtypes.length; j++) {
17970:                     var total = alltargets.length - 1;
17971:                     for (var k=0; k<total; k++) {
17972:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
17973:                         var server = serveritem.value;
17974:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
17975:                             if (server == addtarget) {
17976:                                 serveritem.disabled = '';
17977:                             }
17978:                         }
17979:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
17980:                             if (server == removetarget) {
17981:                                 serveritem.disabled = 'disabled';
17982:                                 serveritem.checked = false;
17983:                             }
17984:                         }
17985:                     }
17986:                 }
17987:                 for (var j=0; j<insttypes.length; j++) {
17988:                     if (insttypes[j] != '_LC_external') {
17989:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
17990:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
17991:                             var currSel = singleserver.selectedIndex;
17992:                             var currVal = singleserver.options[currSel].value;
17993:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
17994:                                 var numoptions = singleserver.options.length;
17995:                                 var needsnew = 1;
17996:                                 for (var k=0; k<numoptions; k++) {
17997:                                     if (singleserver.options[k] == addtarget) {
17998:                                         needsnew = 0;
17999:                                         break;
18000:                                     }
18001:                                 }
18002:                                 if (needsnew == 1) {
18003:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
18004:                                 }
18005:                             }
18006:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
18007:                                 singleserver.options.length = 0;
18008:                                 if ((currVal) && (currVal != removetarget)) {
18009:                                     singleserver.options[0] = new Option("","",false,false);
18010:                                 } else {
18011:                                     singleserver.options[0] = new Option("","",true,true);
18012:                                 }
18013:                                 var idx = 0;
18014:                                 for (var m=0; m<alltargets.length; m++) {
18015:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
18016:                                         idx ++;
18017:                                         if (currVal == alltargets[m]) {
18018:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
18019:                                         } else {
18020:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
18021:                                         }
18022:                                     }
18023:                                 }
18024:                             }
18025:                         }
18026:                     }
18027:                 }
18028:             }
18029:         }
18030:     }
18031:     return;
18032: }
18033: 
18034: // ]]>
18035: </script>
18036: 
18037: END
18038: }
18039: 
18040: sub new_spares_js {
18041:     my @sparestypes = ('primary','default');
18042:     my $types = join("','",@sparestypes);
18043:     my $select = &mt('Select');
18044:     return <<"END";
18045: 
18046: <script type="text/javascript">
18047: // <![CDATA[
18048: 
18049: function updateNewSpares(formname,lonhost) {
18050:     var types = new Array('$types');
18051:     var include = new Array();
18052:     var exclude = new Array();
18053:     for (var i=0; i<types.length; i++) {
18054:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
18055:         for (var j=0; j<spareboxes.length; j++) {
18056:             if (formname.elements[spareboxes[j]].checked) {
18057:                 exclude.push(formname.elements[spareboxes[j]].value);
18058:             } else {
18059:                 include.push(formname.elements[spareboxes[j]].value);
18060:             }
18061:         }
18062:     }
18063:     for (var i=0; i<types.length; i++) {
18064:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
18065:         var selIdx = newSpare.selectedIndex;
18066:         var currnew = newSpare.options[selIdx].value;
18067:         var okSpares = new Array();
18068:         for (var j=0; j<newSpare.options.length; j++) {
18069:             var possible = newSpare.options[j].value;
18070:             if (possible != '') {
18071:                 if (exclude.indexOf(possible) == -1) {
18072:                     okSpares.push(possible);
18073:                 } else {
18074:                     if (currnew == possible) {
18075:                         selIdx = 0;
18076:                     }
18077:                 }
18078:             }
18079:         }
18080:         for (var k=0; k<include.length; k++) {
18081:             if (okSpares.indexOf(include[k]) == -1) {
18082:                 okSpares.push(include[k]);
18083:             }
18084:         }
18085:         okSpares.sort();
18086:         newSpare.options.length = 0;
18087:         if (selIdx == 0) {
18088:             newSpare.options[0] = new Option("$select","",true,true);
18089:         } else {
18090:             newSpare.options[0] = new Option("$select","",false,false);
18091:         }
18092:         for (var m=0; m<okSpares.length; m++) {
18093:             var idx = m+1;
18094:             var selThis = 0;
18095:             if (selIdx != 0) {
18096:                 if (okSpares[m] == currnew) {
18097:                     selThis = 1;
18098:                 }
18099:             }
18100:             if (selThis == 1) {
18101:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
18102:             } else {
18103:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
18104:             }
18105:         }
18106:     }
18107:     return;
18108: }
18109: 
18110: function checkNewSpares(lonhost,type) {
18111:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
18112:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
18113:     if (chosen != '') { 
18114:         var othertype;
18115:         var othernewSpare;
18116:         if (type == 'primary') {
18117:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
18118:         }
18119:         if (type == 'default') {
18120:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
18121:         }
18122:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
18123:             othernewSpare.selectedIndex = 0;
18124:         }
18125:     }
18126:     return;
18127: }
18128: 
18129: // ]]>
18130: </script>
18131: 
18132: END
18133: 
18134: }
18135: 
18136: sub common_domprefs_js {
18137:     return <<"END";
18138: 
18139: <script type="text/javascript">
18140: // <![CDATA[
18141: 
18142: function getIndicesByName(formname,item) {
18143:     var group = new Array();
18144:     for (var i=0;i<formname.elements.length;i++) {
18145:         if (formname.elements[i].name == item) {
18146:             group.push(formname.elements[i].id);
18147:         }
18148:     }
18149:     return group;
18150: }
18151: 
18152: // ]]>
18153: </script>
18154: 
18155: END
18156: 
18157: }
18158: 
18159: sub recaptcha_js {
18160:     my %lt = &captcha_phrases();
18161:     return <<"END";
18162: 
18163: <script type="text/javascript">
18164: // <![CDATA[
18165: 
18166: function updateCaptcha(caller,context) {
18167:     var privitem;
18168:     var pubitem;
18169:     var privtext;
18170:     var pubtext;
18171:     var versionitem;
18172:     var versiontext;
18173:     if (document.getElementById(context+'_recaptchapub')) {
18174:         pubitem = document.getElementById(context+'_recaptchapub');
18175:     } else {
18176:         return;
18177:     }
18178:     if (document.getElementById(context+'_recaptchapriv')) {
18179:         privitem = document.getElementById(context+'_recaptchapriv');
18180:     } else {
18181:         return;
18182:     }
18183:     if (document.getElementById(context+'_recaptchapubtxt')) {
18184:         pubtext = document.getElementById(context+'_recaptchapubtxt');
18185:     } else {
18186:         return;
18187:     }
18188:     if (document.getElementById(context+'_recaptchaprivtxt')) {
18189:         privtext = document.getElementById(context+'_recaptchaprivtxt');
18190:     } else {
18191:         return;
18192:     }
18193:     if (document.getElementById(context+'_recaptchaversion')) {
18194:         versionitem = document.getElementById(context+'_recaptchaversion');
18195:     } else {
18196:         return;
18197:     }
18198:     if (document.getElementById(context+'_recaptchavertxt')) {
18199:         versiontext = document.getElementById(context+'_recaptchavertxt');
18200:     } else {
18201:         return;
18202:     }
18203:     if (caller.checked) {
18204:         if (caller.value == 'recaptcha') {
18205:             pubitem.type = 'text';
18206:             privitem.type = 'text';
18207:             pubitem.size = '40';
18208:             privitem.size = '40';
18209:             pubtext.innerHTML = "$lt{'pub'}";
18210:             privtext.innerHTML = "$lt{'priv'}";
18211:             versionitem.type = 'text';
18212:             versionitem.size = '3';
18213:             versiontext.innerHTML = "$lt{'ver'}";
18214:         } else {
18215:             pubitem.type = 'hidden';
18216:             privitem.type = 'hidden';
18217:             versionitem.type = 'hidden';
18218:             pubtext.innerHTML = '';
18219:             privtext.innerHTML = '';
18220:             versiontext.innerHTML = '';
18221:         }
18222:     }
18223:     return;
18224: }
18225: 
18226: // ]]>
18227: </script>
18228: 
18229: END
18230: 
18231: }
18232: 
18233: sub toggle_display_js {
18234:     return <<"END";
18235: 
18236: <script type="text/javascript">
18237: // <![CDATA[
18238: 
18239: function toggleDisplay(domForm,caller) {
18240:     if (document.getElementById(caller)) {
18241:         var divitem = document.getElementById(caller);
18242:         var optionsElement = domForm.coursecredits;
18243:         var checkval = 1;
18244:         var dispval = 'block';
18245:         var selfcreateRegExp = /^cancreate_emailverified/;
18246:         if (caller == 'emailoptions') {
18247:             optionsElement = domForm.cancreate_email; 
18248:         }
18249:         if (caller == 'studentsubmission') {
18250:             optionsElement = domForm.postsubmit;
18251:         }
18252:         if (caller == 'cloneinstcode') {
18253:             optionsElement = domForm.canclone;
18254:             checkval = 'instcode';
18255:         }
18256:         if (selfcreateRegExp.test(caller)) {
18257:             optionsElement = domForm.elements[caller];
18258:             checkval = 'other';
18259:             dispval = 'inline'
18260:         }
18261:         if (optionsElement.length) {
18262:             var currval;
18263:             for (var i=0; i<optionsElement.length; i++) {
18264:                 if (optionsElement[i].checked) {
18265:                    currval = optionsElement[i].value;
18266:                 }
18267:             }
18268:             if (currval == checkval) {
18269:                 divitem.style.display = dispval;
18270:             } else {
18271:                 divitem.style.display = 'none';
18272:             }
18273:         }
18274:     }
18275:     return;
18276: }
18277: 
18278: // ]]>
18279: </script>
18280: 
18281: END
18282: 
18283: }
18284: 
18285: sub captcha_phrases {
18286:     return &Apache::lonlocal::texthash (
18287:                  priv => 'Private key',
18288:                  pub  => 'Public key',
18289:                  original  => 'original (CAPTCHA)',
18290:                  recaptcha => 'successor (ReCAPTCHA)',
18291:                  notused   => 'unused',
18292:                  ver => 'ReCAPTCHA version (1 or 2)',
18293:     );
18294: }
18295: 
18296: sub devalidate_remote_domconfs {
18297:     my ($dom,$cachekeys) = @_;
18298:     return unless (ref($cachekeys) eq 'HASH');
18299:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
18300:     my %thismachine;
18301:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
18302:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
18303:     if (keys(%servers)) {
18304:         foreach my $server (keys(%servers)) {
18305:             next if ($thismachine{$server});
18306:             my @cached;
18307:             foreach my $name (@posscached) {
18308:                 if ($cachekeys->{$name}) {
18309:                     push(@cached,&escape($name).':'.&escape($dom));
18310:                 }
18311:             }
18312:             if (@cached) {
18313:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
18314:             }
18315:         }
18316:     }
18317:     return;
18318: }
18319: 
18320: 1;

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