File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.351: download - view: text, annotated - select for diffs
Mon Apr 1 17:24:45 2019 UTC (5 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Fix name for Y/N LTI passback radio buttons in toggleLTI() javascript.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.351 2019/04/01 17:24:45 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_passback_'+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','hostipmail');
 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:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3470:         } elsif ($position eq 'bottom') {
 3471:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3472:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3473:             if (ref($fields) eq 'ARRAY') {
 3474:                 foreach my $field (@{$fields}) {
 3475:                     $currfield{$field} = 'yes';
 3476:                 }
 3477:             }
 3478:             $maxsize = '1.0';
 3479:         }
 3480:     }
 3481:     my ($titles,$short_titles) = &contact_titles();
 3482:     my $rownum = 0;
 3483:     my $css_class;
 3484:     if ($position eq 'top') {
 3485:         foreach my $item (@contacts) {
 3486:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3487:             $datatable .= '<tr'.$css_class.'>'. 
 3488:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3489:                           '</span></td><td class="LC_right_item">'.
 3490:                           '<input type="text" name="'.$item.'" value="'.
 3491:                           $to{$item}.'" /></td></tr>';
 3492:             $rownum ++;
 3493:         }
 3494:     } elsif ($position eq 'bottom') {
 3495:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3496:         $datatable .= '<tr'.$css_class.'>'.
 3497:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3498:                       &mt('(e-mail, subject, and description always shown)').
 3499:                       '</td><td class="LC_left_item">';
 3500:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3501:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3502:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3503:             foreach my $field (@{$fields}) {
 3504:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3505:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3506:                     $datatable .= ' '.&mt('(logged-in users)');
 3507:                 }
 3508:                 $datatable .='</td><td>';
 3509:                 my $clickaction;
 3510:                 if ($field eq 'screenshot') {
 3511:                     $clickaction = ' onclick="screenshotSize(this);"';
 3512:                 }
 3513:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3514:                     foreach my $option (@{$possoptions->{$field}}) {
 3515:                         my $checked;
 3516:                         if ($currfield{$field} eq $option) {
 3517:                             $checked = ' checked="checked"';
 3518:                         }
 3519:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3520:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3521:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3522:                                       '</label></span>'.('&nbsp;'x2);
 3523:                     }
 3524:                 }
 3525:                 if ($field eq 'screenshot') {
 3526:                     my $display;
 3527:                     if ($currfield{$field} eq 'no') {
 3528:                         $display = ' style="display:none"';
 3529:                     }
 3530:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3531:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3532:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3533:                 }
 3534:                 $datatable .= '</td></tr>';
 3535:             }
 3536:             $datatable .= '</table>';
 3537:         }
 3538:         $datatable .= '</td></tr>'."\n";
 3539:         $rownum ++;
 3540:     }
 3541:     unless (($position eq 'top') || ($position eq 'lower')) {
 3542:         foreach my $type (@mailings) {
 3543:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3544:             $datatable .= '<tr'.$css_class.'>'.
 3545:                           '<td><span class="LC_nobreak">'.
 3546:                           $titles->{$type}.': </span></td>'.
 3547:                           '<td class="LC_left_item">';
 3548:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3549:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3550:             }
 3551:             $datatable .= '<span class="LC_nobreak">';
 3552:             foreach my $item (@contacts) {
 3553:                 $datatable .= '<label>'.
 3554:                               '<input type="checkbox" name="'.$type.'"'.
 3555:                               $checked{$type}{$item}.
 3556:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3557:                               '</label>&nbsp;';
 3558:             }
 3559:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3560:                           '<input type="text" name="'.$type.'_others" '.
 3561:                           'value="'.$otheremails{$type}.'"  />';
 3562:             my %locchecked;
 3563:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3564:                 foreach my $loc ('s','b') {
 3565:                     if ($includeloc{$type} eq $loc) {
 3566:                         $locchecked{$loc} = ' checked="checked"';
 3567:                         last;
 3568:                     }
 3569:                 }
 3570:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3571:                               '<input type="text" name="'.$type.'_bcc" '.
 3572:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3573:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3574:                               &mt('Text automatically added to e-mail:').' '.
 3575:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3576:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3577:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3578:                               ('&nbsp;'x2).
 3579:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3580:                               '</span></fieldset>';
 3581:             }
 3582:             $datatable .= '</td></tr>'."\n";
 3583:             $rownum ++;
 3584:         }
 3585:     }
 3586:     if ($position eq 'middle') {
 3587:         my %choices;
 3588:         my $corelink = &core_link_msu();
 3589:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3590:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3591:                                         $corelink);
 3592:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3593:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3594:         my %defaultchecked = ('reporterrors'  => 'on',
 3595:                               'reportupdates' => 'on',
 3596:                               'reportstatus'  => 'on');
 3597:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3598:                                                    \%choices,$rownum);
 3599:         $datatable .= $reports;
 3600:     } elsif ($position eq 'lower') {
 3601:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3602:         my ($threshold,$sysmail,%excluded,%weights);
 3603:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3604:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3605:             $threshold = $lonstatus{'threshold'};
 3606:         } else {
 3607:             $threshold = $defaults->{'threshold'};
 3608:         }
 3609:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3610:             $sysmail = $lonstatus{'sysmail'};
 3611:         } else {
 3612:             $sysmail = $defaults->{'sysmail'};
 3613:         }
 3614:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3615:             foreach my $type ('E','W','N','U') {
 3616:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3617:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3618:                 } else {
 3619:                     $weights{$type} = $defaults->{$type};
 3620:                 }
 3621:             }
 3622:         } else {
 3623:             foreach my $type ('E','W','N','U') {
 3624:                 $weights{$type} = $defaults->{$type};
 3625:             }
 3626:         }
 3627:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3628:             if (@{$lonstatus{'excluded'}} > 0) {
 3629:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3630:             }
 3631:         }
 3632:         $datatable .= '<tr'.$css_class.'>'.
 3633:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3634:                       $titles->{'errorthreshold'}.
 3635:                       '</span></td><td class="LC_left_item">'.
 3636:                       '<input type="text" name="errorthreshold" value="'.
 3637:                       $threshold.'" size="5" /></td></tr>';
 3638:         $rownum ++;
 3639:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3640:         $datatable .= '<tr'.$css_class.'>'.
 3641:                       '<td class="LC_left_item">'.
 3642:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3643:                       '</span></td><td class="LC_left_item"><table><tr>';
 3644:         foreach my $type ('E','W','N','U') {
 3645:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3646:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3647:                           $weights{$type}.'" size="5" /></td>';
 3648:         }
 3649:         $datatable .= '</tr></table></tr>';
 3650:         $rownum ++;
 3651:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3652:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3653:                       $titles->{'errorexcluded'}.'</td>'.
 3654:                       '<td class="LC_left_item"><table>';
 3655:         my $numinrow = 4;
 3656:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3657:         for (my $i=0; $i<@ids; $i++) {
 3658:             my $rem = $i%($numinrow);
 3659:             if ($rem == 0) {
 3660:                 if ($i > 0) {
 3661:                     $datatable .= '</tr>';
 3662:                 }
 3663:                 $datatable .= '<tr>';
 3664:             }
 3665:             my $check;
 3666:             if ($excluded{$ids[$i]}) {
 3667:                 $check = ' checked="checked" ';
 3668:             }
 3669:             $datatable .= '<td class="LC_left_item">'.
 3670:                           '<span class="LC_nobreak"><label>'.
 3671:                           '<input type="checkbox" name="errorexcluded" '.
 3672:                           'value="'.$ids[$i].'"'.$check.' />'.
 3673:                           $ids[$i].'</label></span></td>';
 3674:         }
 3675:         my $colsleft = $numinrow - @ids%($numinrow);
 3676:         if ($colsleft > 1 ) {
 3677:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3678:                           '&nbsp;</td>';
 3679:         } elsif ($colsleft == 1) {
 3680:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3681:         }
 3682:         $datatable .= '</tr></table></td></tr>';
 3683:         $rownum ++;
 3684:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3685:         $datatable .= '<tr'.$css_class.'>'.
 3686:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3687:                       $titles->{'errorsysmail'}.
 3688:                       '</span></td><td class="LC_left_item">'.
 3689:                       '<input type="text" name="errorsysmail" value="'.
 3690:                       $sysmail.'" size="5" /></td></tr>';
 3691:         $rownum ++;
 3692:     } elsif ($position eq 'bottom') {
 3693:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3694:         my (@posstypes,%usertypeshash);
 3695:         if (ref($types) eq 'ARRAY') {
 3696:             @posstypes = @{$types};
 3697:         }
 3698:         if (@posstypes) {
 3699:             if (ref($usertypes) eq 'HASH') {
 3700:                 %usertypeshash = %{$usertypes};
 3701:             }
 3702:             my @overridden;
 3703:             my $numinrow = 4;
 3704:             if (ref($settings) eq 'HASH') {
 3705:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3706:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3707:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3708:                             push(@overridden,$key);
 3709:                             foreach my $item (@contacts) {
 3710:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3711:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3712:                                 }
 3713:                             }
 3714:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3715:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3716:                             $includeloc{'override_'.$key} = '';
 3717:                             $includestr{'override_'.$key} = '';
 3718:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3719:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3720:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3721:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3722:                             }
 3723:                         }
 3724:                     }
 3725:                 }
 3726:             }
 3727:             my $customclass = 'LC_helpdesk_override';
 3728:             my $optionsprefix = 'LC_options_helpdesk_';
 3729: 
 3730:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3731:  
 3732:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3733:                                          $numinrow,$othertitle,'overrides',
 3734:                                          \$rownum,$onclicktypes,$customclass);
 3735:             $rownum ++;
 3736:             $usertypeshash{'default'} = $othertitle;
 3737:             foreach my $status (@posstypes) {
 3738:                 my $css_class;
 3739:                 if ($rownum%2) {
 3740:                     $css_class = 'LC_odd_row ';
 3741:                 }
 3742:                 $css_class .= $customclass;
 3743:                 my $rowid = $optionsprefix.$status;
 3744:                 my $hidden = 1;
 3745:                 my $currstyle = 'display:none';
 3746:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3747:                     $currstyle = 'display:table-row';
 3748:                     $hidden = 0;
 3749:                 }
 3750:                 my $key = 'override_'.$status;
 3751:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3752:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3753:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3754:                                                   \@contacts,$short_titles);
 3755:                 unless ($hidden) {
 3756:                     $rownum ++;
 3757:                 }
 3758:             }
 3759:         }
 3760:     }
 3761:     $$rowtotal += $rownum;
 3762:     return $datatable;
 3763: }
 3764: 
 3765: sub core_link_msu {
 3766:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3767:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3768: }
 3769: 
 3770: sub overridden_helpdesk {
 3771:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3772:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3773:     my $class = 'LC_left_item';
 3774:     if ($css_class) {
 3775:         $css_class = ' class="'.$css_class.'"';
 3776:     }
 3777:     if ($rowid) {
 3778:         $rowid = ' id="'.$rowid.'"';
 3779:     }
 3780:     if ($rowstyle) {
 3781:         $rowstyle = ' style="'.$rowstyle.'"';
 3782:     }
 3783:     my ($output,$description);
 3784:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3785:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3786:               "<td>$description</td>\n".
 3787:               '<td class="'.$class.'" colspan="2">'.
 3788:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3789:               '<span class="LC_nobreak">';
 3790:     if (ref($contacts) eq 'ARRAY') {
 3791:         foreach my $item (@{$contacts}) {
 3792:             my $check;
 3793:             if (ref($checked) eq 'HASH') {
 3794:                $check = $checked->{$item};
 3795:             }
 3796:             my $title;
 3797:             if (ref($short_titles) eq 'HASH') {
 3798:                 $title = $short_titles->{$item}; 
 3799:             }
 3800:             $output .= '<label>'.
 3801:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3802:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3803:         }
 3804:     }
 3805:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3806:                '<input type="text" name="override_'.$type.'_others" '.
 3807:                'value="'.$otheremails.'"  />';
 3808:     my %locchecked;
 3809:     foreach my $loc ('s','b') {
 3810:         if ($includeloc eq $loc) {
 3811:             $locchecked{$loc} = ' checked="checked"';
 3812:             last;
 3813:         }
 3814:     }
 3815:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3816:                '<input type="text" name="override_'.$type.'_bcc" '.
 3817:                'value="'.$bccemails.'"  /></fieldset>'.
 3818:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3819:                &mt('Text automatically added to e-mail:').' '.
 3820:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3821:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3822:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3823:                ('&nbsp;'x2).
 3824:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3825:                '</span></fieldset>'.
 3826:                '</td></tr>'."\n";
 3827:     return $output;
 3828: }
 3829: 
 3830: sub contacts_javascript {
 3831:     return <<"ENDSCRIPT";
 3832: 
 3833: <script type="text/javascript">
 3834: // <![CDATA[
 3835: 
 3836: function screenshotSize(field) {
 3837:     if (document.getElementById('help_screenshotsize')) {
 3838:         if (field.value == 'no') {
 3839:             document.getElementById('help_screenshotsize').style.display="none";
 3840:         } else {
 3841:             document.getElementById('help_screenshotsize').style.display="";
 3842:         }
 3843:     }
 3844:     return;
 3845: }
 3846: 
 3847: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3848:     if (form.elements[checkbox].length != undefined) {
 3849:         var count = 0;
 3850:         if (docount) {
 3851:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3852:                 if (form.elements[checkbox][i].checked) {
 3853:                     count ++;
 3854:                 }
 3855:             }
 3856:         }
 3857:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3858:             var type = form.elements[checkbox][i].value;
 3859:             if (document.getElementById(prefix+type)) {
 3860:                 if (form.elements[checkbox][i].checked) {
 3861:                     document.getElementById(prefix+type).style.display = 'table-row';
 3862:                     if (count % 2 == 1) {
 3863:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3864:                     } else {
 3865:                         document.getElementById(prefix+type).className = target;
 3866:                     }
 3867:                     count ++;
 3868:                 } else {
 3869:                     document.getElementById(prefix+type).style.display = 'none';
 3870:                 }
 3871:             }
 3872:         }
 3873:     }
 3874:     return;
 3875: }
 3876: 
 3877: 
 3878: // ]]>
 3879: </script>
 3880: 
 3881: ENDSCRIPT
 3882: }
 3883: 
 3884: sub print_helpsettings {
 3885:     my ($position,$dom,$settings,$rowtotal) = @_;
 3886:     my $confname = $dom.'-domainconfig';
 3887:     my $formname = 'display';
 3888:     my ($datatable,$itemcount);
 3889:     if ($position eq 'top') {
 3890:         $itemcount = 1;
 3891:         my (%choices,%defaultchecked,@toggles);
 3892:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3893:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3894:                                      &mt('LON-CAPA bug tracker'),600,500));
 3895:         %defaultchecked = ('submitbugs' => 'on');
 3896:         @toggles = ('submitbugs');
 3897:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3898:                                                      \%choices,$itemcount);
 3899:         $$rowtotal ++;
 3900:     } else {
 3901:         my $css_class;
 3902:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3903:         my (%customroles,%ordered,%current);
 3904:         if (ref($settings) eq 'HASH') {
 3905:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3906:                 %current = %{$settings->{'adhoc'}};
 3907:             }
 3908:         }
 3909:         my $count = 0;
 3910:         foreach my $key (sort(keys(%existing))) {
 3911:             if ($key=~/^rolesdef\_(\w+)$/) {
 3912:                 my $rolename = $1;
 3913:                 my (%privs,$order);
 3914:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3915:                 $customroles{$rolename} = \%privs;
 3916:                 if (ref($current{$rolename}) eq 'HASH') {
 3917:                     $order = $current{$rolename}{'order'};
 3918:                 }
 3919:                 if ($order eq '') {
 3920:                     $order = $count;
 3921:                 }
 3922:                 $ordered{$order} = $rolename;
 3923:                 $count++;
 3924:             }
 3925:         }
 3926:         my $maxnum = scalar(keys(%ordered));
 3927:         my @roles_by_num = ();
 3928:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3929:             push(@roles_by_num,$item);
 3930:         }
 3931:         my $context = 'domprefs';
 3932:         my $crstype = 'Course';
 3933:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3934:         my @accesstypes = ('all','dh','da','none');
 3935:         my ($numstatustypes,@jsarray);
 3936:         if (ref($types) eq 'ARRAY') {
 3937:             if (@{$types} > 0) {
 3938:                 $numstatustypes = scalar(@{$types});
 3939:                 push(@accesstypes,'status');
 3940:                 @jsarray = ('bystatus');
 3941:             }
 3942:         }
 3943:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3944:         if (keys(%domhelpdesk)) {
 3945:             push(@accesstypes,('inc','exc'));
 3946:             push(@jsarray,('notinc','notexc'));
 3947:         }
 3948:         my $hiddenstr = join("','",@jsarray);
 3949:         my $context = 'domprefs';
 3950:         my $crstype = 'Course';
 3951:         my $prefix = 'helproles_';
 3952:         my $add_class = 'LC_hidden';
 3953:         foreach my $num (@roles_by_num) {
 3954:             my $role = $ordered{$num};
 3955:             my ($desc,$access,@statuses);
 3956:             if (ref($current{$role}) eq 'HASH') {
 3957:                 $desc = $current{$role}{'desc'};
 3958:                 $access = $current{$role}{'access'};
 3959:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3960:                     @statuses = @{$current{$role}{'insttypes'}};
 3961:                 }
 3962:             }
 3963:             if ($desc eq '') {
 3964:                 $desc = $role;
 3965:             }
 3966:             my $identifier = 'custhelp'.$num;
 3967:             my %full=();
 3968:             my %levels= (
 3969:                          course => {},
 3970:                          domain => {},
 3971:                          system => {},
 3972:                         );
 3973:             my %levelscurrent=(
 3974:                                course => {},
 3975:                                domain => {},
 3976:                                system => {},
 3977:                               );
 3978:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3979:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3980:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3981:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3982:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 3983:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3984:             for (my $k=0; $k<=$maxnum; $k++) {
 3985:                 my $vpos = $k+1;
 3986:                 my $selstr;
 3987:                 if ($k == $num) {
 3988:                     $selstr = ' selected="selected" ';
 3989:                 }
 3990:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3991:             }
 3992:             $datatable .= '</select>'.('&nbsp;'x2).
 3993:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3994:                           '</td>'.
 3995:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3996:                           &mt('Name shown to users:').
 3997:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3998:                           '</fieldset>'.
 3999:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4000:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4001:                           '<fieldset>'.
 4002:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4003:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4004:                                                                    \%levelscurrent,$identifier,
 4005:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4006:                           '</fieldset></td>';
 4007:             $itemcount ++;
 4008:         }
 4009:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4010:         my $newcust = 'custhelp'.$count;
 4011:         my (%privs,%levelscurrent);
 4012:         my %full=();
 4013:         my %levels= (
 4014:                      course => {},
 4015:                      domain => {},
 4016:                      system => {},
 4017:                     );
 4018:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4019:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4020:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4021:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4022:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4023:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4024:         for (my $k=0; $k<$maxnum+1; $k++) {
 4025:             my $vpos = $k+1;
 4026:             my $selstr;
 4027:             if ($k == $maxnum) {
 4028:                 $selstr = ' selected="selected" ';
 4029:             }
 4030:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4031:         }
 4032:         $datatable .= '</select>&nbsp;'."\n".
 4033:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4034:                       '</label></span></td>'.
 4035:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4036:                       '<span class="LC_nobreak">'.
 4037:                       &mt('Internal name:').
 4038:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4039:                       '</span>'.('&nbsp;'x4).
 4040:                       '<span class="LC_nobreak">'.
 4041:                       &mt('Name shown to users:').
 4042:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4043:                       '</span></fieldset>'.
 4044:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4045:                                              $usertypes,$types,\%domhelpdesk).
 4046:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4047:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4048:                                                                 \@templateroles,$newcust).
 4049:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4050:                                                                \%levelscurrent,$newcust).
 4051:                       '</fieldset>'.
 4052:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4053:                       '</td></tr>';
 4054:         $count ++;
 4055:         $$rowtotal += $count;
 4056:     }
 4057:     return $datatable;
 4058: }
 4059: 
 4060: sub adhocbutton {
 4061:     my ($prefix,$num,$field,$visibility) = @_;
 4062:     my %lt = &Apache::lonlocal::texthash(
 4063:                                           show => 'Show details',
 4064:                                           hide => 'Hide details',
 4065:                                         );
 4066:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4067:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4068:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4069:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4070: }
 4071: 
 4072: sub helpsettings_javascript {
 4073:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4074:     return unless(ref($roles_by_num) eq 'ARRAY');
 4075:     my %html_js_lt = &Apache::lonlocal::texthash(
 4076:                                           show => 'Show details',
 4077:                                           hide => 'Hide details',
 4078:                                         );
 4079:     &html_escape(\%html_js_lt);
 4080:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4081:     return <<"ENDSCRIPT";
 4082: <script type="text/javascript">
 4083: // <![CDATA[
 4084: 
 4085: function reorderHelpRoles(form,item) {
 4086:     var changedVal;
 4087: $jstext
 4088:     var newpos = 'helproles_${total}_pos';
 4089:     var maxh = 1 + $total;
 4090:     var current = new Array();
 4091:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4092:     if (item == newpos) {
 4093:         changedVal = newitemVal;
 4094:     } else {
 4095:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4096:         current[newitemVal] = newpos;
 4097:     }
 4098:     for (var i=0; i<helproles.length; i++) {
 4099:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4100:         if (elementName != item) {
 4101:             if (form.elements[elementName]) {
 4102:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4103:                 current[currVal] = elementName;
 4104:             }
 4105:         }
 4106:     }
 4107:     var oldVal;
 4108:     for (var j=0; j<maxh; j++) {
 4109:         if (current[j] == undefined) {
 4110:             oldVal = j;
 4111:         }
 4112:     }
 4113:     if (oldVal < changedVal) {
 4114:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4115:            var elementName = current[k];
 4116:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4117:         }
 4118:     } else {
 4119:         for (var k=changedVal; k<oldVal; k++) {
 4120:             var elementName = current[k];
 4121:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4122:         }
 4123:     }
 4124:     return;
 4125: }
 4126: 
 4127: function helpdeskAccess(num) {
 4128:     var curraccess = null;
 4129:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4130:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4131:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4132:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4133:             }
 4134:         }
 4135:     }
 4136:     var shown = Array();
 4137:     var hidden = Array();
 4138:     if (curraccess == 'none') {
 4139:         hidden = Array('$hiddenstr');
 4140:     } else {
 4141:         if (curraccess == 'status') {
 4142:             shown = Array('bystatus');
 4143:             hidden = Array('notinc','notexc');
 4144:         } else {
 4145:             if (curraccess == 'exc') {
 4146:                 shown = Array('notexc');
 4147:                 hidden = Array('notinc','bystatus');
 4148:             }
 4149:             if (curraccess == 'inc') {
 4150:                 shown = Array('notinc');
 4151:                 hidden = Array('notexc','bystatus');
 4152:             }
 4153:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4154:                 hidden = Array('notinc','notexc','bystatus');
 4155:             }
 4156:         }
 4157:     }
 4158:     if (hidden.length > 0) {
 4159:         for (var i=0; i<hidden.length; i++) {
 4160:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4161:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4162:             }
 4163:         }
 4164:     }
 4165:     if (shown.length > 0) {
 4166:         for (var i=0; i<shown.length; i++) {
 4167:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4168:                 if (shown[i] == 'privs') {
 4169:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4170:                 } else {
 4171:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4172:                 }
 4173:             }
 4174:         }
 4175:     }
 4176:     return;
 4177: }
 4178: 
 4179: function toggleHelpdeskItem(num,field) {
 4180:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4181:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4182:             document.getElementById('helproles_'+num+'_'+field).className =
 4183:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4184:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4185:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4186:             }
 4187:         } else {
 4188:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4189:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4190:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4191:             }
 4192:         }
 4193:     }
 4194:     return;
 4195: }
 4196: 
 4197: // ]]>
 4198: </script>
 4199: 
 4200: ENDSCRIPT
 4201: }
 4202: 
 4203: sub helpdeskroles_access {
 4204:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4205:         $usertypes,$types,$domhelpdesk) = @_;
 4206:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4207:     my %lt = &Apache::lonlocal::texthash(
 4208:                     'rou'    => 'Role usage',
 4209:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4210:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4211:                     'dh'     => 'All with domain helpdesk role',
 4212:                     'da'     => 'All with domain helpdesk assistant role',
 4213:                     'none'   => 'None',
 4214:                     'status' => 'Determined based on institutional status',
 4215:                     'inc'    => 'Include all, but exclude specific personnel',
 4216:                     'exc'    => 'Exclude all, but include specific personnel',
 4217:                   );
 4218:     my %usecheck = (
 4219:                      all => ' checked="checked"',
 4220:                    );
 4221:     my %displaydiv = (
 4222:                       status => 'none',
 4223:                       inc    => 'none',
 4224:                       exc    => 'none',
 4225:                       priv   => 'block',
 4226:                      );
 4227:     my $output;
 4228:     if (ref($current) eq 'HASH') {
 4229:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4230:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4231:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4232:                 delete($usecheck{'all'});
 4233:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4234:                     my $access = $1;
 4235:                     $displaydiv{$access} = 'inline';
 4236:                 } elsif ($current->{access} eq 'none') {
 4237:                     $displaydiv{'priv'} = 'none';
 4238:                 }
 4239:             }
 4240:         }
 4241:     }
 4242:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4243:               '<p>'.$lt{'whi'}.'</p>';
 4244:     foreach my $access (@{$accesstypes}) {
 4245:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4246:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4247:                    $lt{$access}.'</label>';
 4248:         if ($access eq 'status') {
 4249:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4250:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4251:                                                                  $othertitle,$usertypes,$types).
 4252:                        '</div>';
 4253:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4254:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4255:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4256:                        '</div>';
 4257:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4258:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4259:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4260:                        '</div>';
 4261:         }
 4262:         $output .= '</p>';
 4263:     }
 4264:     $output .= '</fieldset>';
 4265:     return $output;
 4266: }
 4267: 
 4268: sub radiobutton_prefs {
 4269:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4270:         $additional,$align) = @_;
 4271:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4272:                    (ref($choices) eq 'HASH'));
 4273: 
 4274:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4275: 
 4276:     foreach my $item (@{$toggles}) {
 4277:         if ($defaultchecked->{$item} eq 'on') {
 4278:             $checkedon{$item} = ' checked="checked" ';
 4279:             $checkedoff{$item} = ' ';
 4280:         } elsif ($defaultchecked->{$item} eq 'off') {
 4281:             $checkedoff{$item} = ' checked="checked" ';
 4282:             $checkedon{$item} = ' ';
 4283:         }
 4284:     }
 4285:     if (ref($settings) eq 'HASH') {
 4286:         foreach my $item (@{$toggles}) {
 4287:             if ($settings->{$item} eq '1') {
 4288:                 $checkedon{$item} =  ' checked="checked" ';
 4289:                 $checkedoff{$item} = ' ';
 4290:             } elsif ($settings->{$item} eq '0') {
 4291:                 $checkedoff{$item} =  ' checked="checked" ';
 4292:                 $checkedon{$item} = ' ';
 4293:             }
 4294:         }
 4295:     }
 4296:     if ($onclick) {
 4297:         $onclick = ' onclick="'.$onclick.'"';
 4298:     }
 4299:     foreach my $item (@{$toggles}) {
 4300:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4301:         $datatable .=
 4302:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4303:             '<span class="LC_nobreak">'.$choices->{$item}.
 4304:             '</span></td>';
 4305:         if ($align eq 'left') {
 4306:             $datatable .= '<td class="LC_left_item">';
 4307:         } else {
 4308:             $datatable .= '<td class="LC_right_item">';
 4309:         }
 4310:         $datatable .=
 4311:             '<span class="LC_nobreak">'.
 4312:             '<label><input type="radio" name="'.
 4313:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4314:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4315:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4316:             '</span>'.$additional.
 4317:             '</td>'.
 4318:             '</tr>';
 4319:         $itemcount ++;
 4320:     }
 4321:     return ($datatable,$itemcount);
 4322: }
 4323: 
 4324: sub print_ltitools {
 4325:     my ($dom,$settings,$rowtotal) = @_;
 4326:     my $rownum = 0;
 4327:     my $css_class;
 4328:     my $itemcount = 1;
 4329:     my $maxnum = 0;
 4330:     my %ordered;
 4331:     if (ref($settings) eq 'HASH') {
 4332:         foreach my $item (keys(%{$settings})) {
 4333:             if (ref($settings->{$item}) eq 'HASH') {
 4334:                 my $num = $settings->{$item}{'order'};
 4335:                 $ordered{$num} = $item;
 4336:             }
 4337:         }
 4338:     }
 4339:     my $confname = $dom.'-domainconfig';
 4340:     my $switchserver = &check_switchserver($dom,$confname);
 4341:     my $maxnum = scalar(keys(%ordered));
 4342:     my $datatable;
 4343:     my %lt = &ltitools_names();
 4344:     my @courseroles = ('cc','in','ta','ep','st');
 4345:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4346:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4347:     if (keys(%ordered)) {
 4348:         my @items = sort { $a <=> $b } keys(%ordered);
 4349:         for (my $i=0; $i<@items; $i++) {
 4350:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4351:             my $item = $ordered{$items[$i]};
 4352:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4353:             if (ref($settings->{$item}) eq 'HASH') {
 4354:                 $title = $settings->{$item}->{'title'};
 4355:                 $url = $settings->{$item}->{'url'};
 4356:                 $key = $settings->{$item}->{'key'};
 4357:                 $secret = $settings->{$item}->{'secret'};
 4358:                 $lifetime = $settings->{$item}->{'lifetime'};
 4359:                 my $image = $settings->{$item}->{'image'};
 4360:                 if ($image ne '') {
 4361:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4362:                 }
 4363:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4364:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4365:                 } else {
 4366:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4367:                 }
 4368:             }
 4369:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4370:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4371:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4372:             for (my $k=0; $k<=$maxnum; $k++) {
 4373:                 my $vpos = $k+1;
 4374:                 my $selstr;
 4375:                 if ($k == $i) {
 4376:                     $selstr = ' selected="selected" ';
 4377:                 }
 4378:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4379:             }
 4380:             $datatable .= '</select>'.('&nbsp;'x2).
 4381:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4382:                 &mt('Delete?').'</label></span></td>'.
 4383:                 '<td colspan="2">'.
 4384:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4385:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4386:                 ('&nbsp;'x2).
 4387:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4388:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4389:                 ('&nbsp;'x2).
 4390:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4391:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4392:                 ('&nbsp;'x2).
 4393:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4394:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4395:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4396:                 '<br /><br />'.
 4397:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4398:                 ' value="'.$url.'" /></span>'.
 4399:                 ('&nbsp;'x2).
 4400:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4401:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4402:                 ('&nbsp;'x2).
 4403:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4404:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4405:                 ('&nbsp;'x2).
 4406:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4407:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4408:                 '<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>'.
 4409:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4410:                 '</fieldset>'.
 4411:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4412:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4413:             my %currdisp;
 4414:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4415:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4416:                     $currdisp{'window'} = ' checked="checked"';
 4417:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4418:                     $currdisp{'tab'} = ' checked="checked"';
 4419:                 } else {
 4420:                     $currdisp{'iframe'} = ' checked="checked"';
 4421:                 }
 4422:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4423:                     $currdisp{'width'} = $1;
 4424:                 }
 4425:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4426:                      $currdisp{'height'} = $1;
 4427:                 }
 4428:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4429:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4430:             } else {
 4431:                 $currdisp{'iframe'} = ' checked="checked"';
 4432:             }
 4433:             foreach my $disp ('iframe','tab','window') {
 4434:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4435:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4436:             }
 4437:             $datatable .= ('&nbsp;'x4);
 4438:             foreach my $dimen ('width','height') {
 4439:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4440:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4441:                               ('&nbsp;'x2);
 4442:             }
 4443:             $datatable .= '</span><br />'.
 4444:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4445:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4446:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4447:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4448:                           '</textarea></div><div style=""></div><br />';
 4449:             my %units = (
 4450:                           'passback' => 'days',
 4451:                           'roster'   => 'seconds',
 4452:                         );
 4453:             foreach my $extra ('passback','roster') {
 4454:                 my $validsty = 'none';
 4455:                 my $currvalid;
 4456:                 my $checkedon = '';
 4457:                 my $checkedoff = ' checked="checked"';
 4458:                 if ($settings->{$item}->{$extra}) {
 4459:                     $checkedon = $checkedoff;
 4460:                     $checkedoff = '';
 4461:                     $validsty = 'inline-block';
 4462:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4463:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4464:                     }
 4465:                 }
 4466:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4467:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4468:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4469:                               &mt('No').'</label>'.('&nbsp;'x2).
 4470:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4471:                               &mt('Yes').'</label></span></div>'.
 4472:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4473:                               '<span class="LC_nobreak">'.
 4474:                               &mt("at least [_1] $units{$extra} after launch",
 4475:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4476:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4477:             }
 4478:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4479:             if ($imgsrc) {
 4480:                 $datatable .= $imgsrc.
 4481:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4482:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4483:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4484:             } else {
 4485:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4486:             }
 4487:             if ($switchserver) {
 4488:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4489:             } else {
 4490:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4491:             }
 4492:             $datatable .= '</span></fieldset>';
 4493:             my (%checkedfields,%rolemaps,$userincdom);
 4494:             if (ref($settings->{$item}) eq 'HASH') {
 4495:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4496:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4497:                 }
 4498:                 $userincdom = $settings->{$item}->{'incdom'};
 4499:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4500:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4501:                     $checkedfields{'roles'} = 1;
 4502:                 }
 4503:             }
 4504:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4505:                           '<span class="LC_nobreak">';
 4506:             my $userfieldstyle = 'display:none;';
 4507:             my $seluserdom = '';
 4508:             my $unseluserdom = ' selected="selected"';
 4509:             foreach my $field (@fields) {
 4510:                 my ($checked,$onclick,$id,$spacer);
 4511:                 if ($checkedfields{$field}) {
 4512:                     $checked = ' checked="checked"';
 4513:                 }
 4514:                 if ($field eq 'user') {
 4515:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4516:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4517:                     if ($checked) {
 4518:                         $userfieldstyle = 'display:inline-block';
 4519:                         if ($userincdom) {
 4520:                             $seluserdom = $unseluserdom;
 4521:                             $unseluserdom = '';
 4522:                         }
 4523:                     }
 4524:                 } else {
 4525:                     $spacer = ('&nbsp;' x2);
 4526:                 }
 4527:                 $datatable .= '<label>'.
 4528:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4529:                               $lt{$field}.'</label>'.$spacer;
 4530:             }
 4531:             $datatable .= '</span>';
 4532:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4533:                           '<span class="LC_nobreak"> : '.
 4534:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4535:                           '<option value="">'.&mt('Select').'</option>'.
 4536:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4537:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4538:                           '</select></span></div>';
 4539:             $datatable .= '</fieldset>'.
 4540:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4541:             foreach my $role (@courseroles) {
 4542:                 my ($selected,$selectnone);
 4543:                 if (!$rolemaps{$role}) {
 4544:                     $selectnone = ' selected="selected"';
 4545:                 }
 4546:                 $datatable .= '<td style="text-align: center">'. 
 4547:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4548:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4549:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4550:                 foreach my $ltirole (@ltiroles) {
 4551:                     unless ($selectnone) {
 4552:                         if ($rolemaps{$role} eq $ltirole) {
 4553:                             $selected = ' selected="selected"';
 4554:                         } else {
 4555:                             $selected = '';
 4556:                         }
 4557:                     }
 4558:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4559:                 }
 4560:                 $datatable .= '</select></td>';
 4561:             }
 4562:             $datatable .= '</tr></table></fieldset>';
 4563:             my %courseconfig;
 4564:             if (ref($settings->{$item}) eq 'HASH') {
 4565:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4566:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4567:                 }
 4568:             }
 4569:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4570:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4571:                 my $checked;
 4572:                 if ($courseconfig{$item}) {
 4573:                     $checked = ' checked="checked"';
 4574:                 }
 4575:                 $datatable .= '<label>'.
 4576:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4577:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4578:             }
 4579:             $datatable .= '</span></fieldset>'.
 4580:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4581:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4582:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4583:                 my %custom = %{$settings->{$item}->{'custom'}};
 4584:                 if (keys(%custom) > 0) {
 4585:                     foreach my $key (sort(keys(%custom))) {
 4586:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4587:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4588:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4589:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4590:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4591:                     }
 4592:                 }
 4593:             }
 4594:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4595:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4596:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4597:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4598:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4599:             $itemcount ++;
 4600:         }
 4601:     }
 4602:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4603:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4604:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4605:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4606:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4607:     for (my $k=0; $k<$maxnum+1; $k++) {
 4608:         my $vpos = $k+1;
 4609:         my $selstr;
 4610:         if ($k == $maxnum) {
 4611:             $selstr = ' selected="selected" ';
 4612:         }
 4613:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4614:     }
 4615:     $datatable .= '</select>&nbsp;'."\n".
 4616:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4617:                   '<td colspan="2">'.
 4618:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4619:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4620:                   ('&nbsp;'x2).
 4621:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4622:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4623:                   ('&nbsp;'x2).
 4624:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4625:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4626:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4627:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4628:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4629:                   '<br />'.
 4630:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4631:                   ('&nbsp;'x2).
 4632:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4633:                   ('&nbsp;'x2).
 4634:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4635:                   ('&nbsp;'x2).
 4636:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4637:                   '<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".
 4638:                   '</fieldset>'.
 4639:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4640:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4641:     my %defaultdisp;
 4642:     $defaultdisp{'iframe'} = ' checked="checked"';
 4643:     foreach my $disp ('iframe','tab','window') {
 4644:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4645:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4646:     }
 4647:     $datatable .= ('&nbsp;'x4);
 4648:     foreach my $dimen ('width','height') {
 4649:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4650:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4651:                       ('&nbsp;'x2);
 4652:     }
 4653:     $datatable .= '</span><br />'.
 4654:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4655:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4656:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4657:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4658:                   '</div><div style=""></div><br />';
 4659:     my %units = (
 4660:                   'passback' => 'days',
 4661:                   'roster'   => 'seconds',
 4662:                 );
 4663:     my %defaulttimes = (
 4664:                      'passback' => '7',
 4665:                      'roster'   => '300',
 4666:                    );
 4667:     foreach my $extra ('passback','roster') {
 4668:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4669:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4670:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4671:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4672:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4673:                       &mt('Yes').'</label></span></div>'.
 4674:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4675:                       '<span class="LC_nobreak">'.
 4676:                       &mt("at least [_1] $units{$extra} after launch",
 4677:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4678:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4679:     }
 4680:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4681:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4682:     if ($switchserver) {
 4683:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4684:     } else {
 4685:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4686:     }
 4687:     $datatable .= '</span></fieldset>'.
 4688:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4689:                   '<span class="LC_nobreak">';
 4690:     foreach my $field (@fields) {
 4691:         my ($id,$onclick,$spacer);
 4692:         if ($field eq 'user') {
 4693:             $id = ' id="ltitools_user_field_add"';
 4694:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4695:         } else {
 4696:             $spacer = ('&nbsp;' x2);
 4697:         }
 4698:         $datatable .= '<label>'.
 4699:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4700:                       $lt{$field}.'</label>'.$spacer;
 4701:     }
 4702:     $datatable .= '</span>'.
 4703:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4704:                   '<span class="LC_nobreak"> : '.
 4705:                   '<select name="ltitools_userincdom_add">'.
 4706:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4707:                   '<option value="0">'.&mt('username').'</option>'.
 4708:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4709:                   '</select></span></div></fieldset>';
 4710:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4711:     foreach my $role (@courseroles) {
 4712:         my ($checked,$checkednone);
 4713:         $datatable .= '<td style="text-align: center">'.
 4714:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4715:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4716:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4717:         foreach my $ltirole (@ltiroles) {
 4718:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4719:         }
 4720:         $datatable .= '</select></td>';
 4721:     }
 4722:     $datatable .= '</tr></table></fieldset>'.
 4723:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4724:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4725:         $datatable .= '<label>'.
 4726:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4727:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4728:     }
 4729:     $datatable .= '</span></fieldset>'.
 4730:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4731:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4732:                   '<tr><td><span class="LC_nobreak">'.
 4733:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4734:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4735:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4736:                   '</table></fieldset>'."\n".
 4737:                   '</td>'."\n".
 4738:                   '</tr>'."\n";
 4739:     $itemcount ++;
 4740:     return $datatable;
 4741: }
 4742: 
 4743: sub ltitools_names {
 4744:     my %lt = &Apache::lonlocal::texthash(
 4745:                                           'title'          => 'Title',
 4746:                                           'version'        => 'Version',
 4747:                                           'msgtype'        => 'Message Type',
 4748:                                           'sigmethod'      => 'Signature Method',
 4749:                                           'url'            => 'URL',
 4750:                                           'key'            => 'Key',
 4751:                                           'lifetime'       => 'Nonce lifetime (s)',
 4752:                                           'secret'         => 'Secret',
 4753:                                           'icon'           => 'Icon',   
 4754:                                           'user'           => 'User',
 4755:                                           'fullname'       => 'Full Name',
 4756:                                           'firstname'      => 'First Name',
 4757:                                           'lastname'       => 'Last Name',
 4758:                                           'email'          => 'E-mail',
 4759:                                           'roles'          => 'Role',
 4760:                                           'window'         => 'Window',
 4761:                                           'tab'            => 'Tab',
 4762:                                           'iframe'         => 'iFrame',
 4763:                                           'height'         => 'Height',
 4764:                                           'width'          => 'Width',
 4765:                                           'linktext'       => 'Default Link Text',
 4766:                                           'explanation'    => 'Default Explanation',
 4767:                                           'passback'       => 'Tool can return grades:',
 4768:                                           'roster'         => 'Tool can retrieve roster:',
 4769:                                           'crstarget'      => 'Display target',
 4770:                                           'crslabel'       => 'Course label',
 4771:                                           'crstitle'       => 'Course title', 
 4772:                                           'crslinktext'    => 'Link Text',
 4773:                                           'crsexplanation' => 'Explanation',
 4774:                                           'crsappend'      => 'Provider URL',
 4775:                                         );
 4776:     return %lt;
 4777: }
 4778: 
 4779: sub print_lti {
 4780:     my ($dom,$settings,$rowtotal) = @_;
 4781:     my $itemcount = 1;
 4782:     my $maxnum = 0;
 4783:     my $css_class;
 4784:     my %ordered;
 4785:     if (ref($settings) eq 'HASH') {
 4786:         foreach my $item (keys(%{$settings})) {
 4787:             if (ref($settings->{$item}) eq 'HASH') {
 4788:                 my $num = $settings->{$item}{'order'};
 4789:                 $ordered{$num} = $item;
 4790:             }
 4791:         }
 4792:     }
 4793:     my $maxnum = scalar(keys(%ordered));
 4794:     my $datatable;
 4795:     my %lt = &lti_names();
 4796:     if (keys(%ordered)) {
 4797:         my @items = sort { $a <=> $b } keys(%ordered);
 4798:         for (my $i=0; $i<@items; $i++) {
 4799:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4800:             my $item = $ordered{$items[$i]};
 4801:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 4802:             if (ref($settings->{$item}) eq 'HASH') {
 4803:                 $key = $settings->{$item}->{'key'};
 4804:                 $secret = $settings->{$item}->{'secret'};
 4805:                 $lifetime = $settings->{$item}->{'lifetime'};
 4806:                 $consumer = $settings->{$item}->{'consumer'};
 4807:                 $requser = $settings->{$item}->{'requser'};
 4808:                 $current = $settings->{$item};
 4809:             }
 4810:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 4811:             my %checkedrequser = (
 4812:                                    yes => ' checked="checked"',
 4813:                                    no  => '',
 4814:                                  );
 4815:             if (!$requser) {
 4816:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 4817:                 $checkedrequser{'yes'} = '';
 4818:             } 
 4819:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 4820:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4821:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 4822:             for (my $k=0; $k<=$maxnum; $k++) {
 4823:                 my $vpos = $k+1;
 4824:                 my $selstr;
 4825:                 if ($k == $i) {
 4826:                     $selstr = ' selected="selected" ';
 4827:                 }
 4828:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4829:             }
 4830:             $datatable .= '</select>'.('&nbsp;'x2).
 4831:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 4832:                 &mt('Delete?').'</label></span></td>'.
 4833:                 '<td colspan="2">'.
 4834:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4835:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 4836:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 4837:                 ('&nbsp;'x2).
 4838:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 4839:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4840:                 ('&nbsp;'x2).
 4841:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 4842:                 'value="'.$lifetime.'" size="3" /></span>'.
 4843:                 ('&nbsp;'x2).
 4844:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4845:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 4846:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 4847:                 '<br /><br />'.
 4848:                 '<span class="LC_nobreak">'.$lt{'key'}.
 4849:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 4850:                 ('&nbsp;'x2).
 4851:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4852:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 4853:                 '<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>'.
 4854:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 4855:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 4856:             $itemcount ++;
 4857:         }
 4858:     }
 4859:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4860:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 4861:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4862:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 4863:                   '<select name="lti_pos_add"'.$chgstr.'>';
 4864:     for (my $k=0; $k<$maxnum+1; $k++) {
 4865:         my $vpos = $k+1;
 4866:         my $selstr;
 4867:         if ($k == $maxnum) {
 4868:             $selstr = ' selected="selected" ';
 4869:         }
 4870:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4871:     }
 4872:     $datatable .= '</select>&nbsp;'."\n".
 4873:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4874:                   '<td colspan="2">'.
 4875:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4876:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 4877:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 4878:                   ('&nbsp;'x2).
 4879:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 4880:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4881:                   ('&nbsp;'x2).
 4882:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 4883:                   ('&nbsp;'x2).
 4884:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4885:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 4886:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 4887:                   '<br /><br />'.
 4888:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 4889:                   ('&nbsp;'x2).
 4890:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 4891:                   '<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".
 4892:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 4893:                   '</td>'."\n".
 4894:                   '</tr>'."\n";
 4895:     $$rowtotal ++;
 4896:     return $datatable;;
 4897: }
 4898: 
 4899: sub lti_names {
 4900:     my %lt = &Apache::lonlocal::texthash(
 4901:                                           'version'   => 'LTI Version',
 4902:                                           'url'       => 'URL',
 4903:                                           'key'       => 'Key',
 4904:                                           'lifetime'  => 'Nonce lifetime (s)',
 4905:                                           'consumer'  => 'Consumer',
 4906:                                           'secret'    => 'Secret',
 4907:                                           'requser'   => "User's identity sent",
 4908:                                           'email'     => 'Email address',
 4909:                                           'sourcedid' => 'User ID',
 4910:                                           'other'     => 'Other',
 4911:                                           'passback'  => 'Can return grades to Consumer:',
 4912:                                           'roster'    => 'Can retrieve roster from Consumer:',
 4913:                                           'topmenu'   => 'Display LON-CAPA page header',
 4914:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 4915:                                         );
 4916:     return %lt;
 4917: }
 4918: 
 4919: sub lti_options {
 4920:     my ($num,$current,$itemcount,%lt) = @_;
 4921:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield);
 4922:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 4923:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 4924:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 4925:     $checked{'mapcrstype'} = {};
 4926:     $checked{'makeuser'} = {};
 4927:     $checked{'selfenroll'} = {};
 4928:     $checked{'crssec'} = {};
 4929:     $checked{'crssecsrc'} = {};
 4930:     $checked{'lcauth'} = {};
 4931:     $checked{'menuitem'} = {};
 4932:     if ($num eq 'add') {
 4933:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 4934:     }
 4935:     my $userfieldsty = 'none';
 4936:     my $crsfieldsty = 'none';
 4937:     my $crssecfieldsty = 'none';
 4938:     my $secsrcfieldsty = 'none';
 4939:     my $passbacksty = 'none';
 4940:     my $optionsty = 'block';
 4941:     my $lcauthparm;
 4942:     my $lcauthparmstyle = 'display:none';
 4943:     my $lcauthparmtext;
 4944:     my $menusty;
 4945:     my $numinrow = 4;
 4946:     my %menutitles = &ltimenu_titles();
 4947: 
 4948:     if (ref($current) eq 'HASH') {
 4949:         if (!$current->{'requser'}) {
 4950:             $optionsty = 'none';
 4951:         }
 4952:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 4953:             $checked{'mapuser'}{'sourcedid'} = '';
 4954:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 4955:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 4956:             } else {
 4957:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 4958:                 $userfield = $current->{'mapuser'};
 4959:                 $userfieldsty = 'inline-block';
 4960:             }
 4961:         }
 4962:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 4963:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 4964:             if ($current->{'mapcrs'} eq 'context_id') {
 4965:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 4966:             } else {
 4967:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 4968:                 $cidfield = $current->{'mapcrs'};
 4969:                 $crsfieldsty = 'inline-block';
 4970:             }
 4971:         }
 4972:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 4973:             foreach my $type (@{$current->{'mapcrstype'}}) {
 4974:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 4975:             }
 4976:         }
 4977:         if ($current->{'makecrs'}) {
 4978:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 4979:         }
 4980:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 4981:             foreach my $role (@{$current->{'makeuser'}}) {
 4982:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 4983:             }
 4984:         }
 4985:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 4986:             $checked{'lcauth'}{$1} = ' checked="checked"';
 4987:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 4988:                 $lcauthparm = $current->{'lcauthparm'};
 4989:                 $lcauthparmstyle = 'display:table-row'; 
 4990:                 if ($current->{'lcauth'} eq 'localauth') {
 4991:                     $lcauthparmtext = &mt('Local auth argument');
 4992:                 } else {
 4993:                     $lcauthparmtext = &mt('Kerberos domain');
 4994:                 }
 4995:             }
 4996:         }
 4997:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 4998:             foreach my $role (@{$current->{'selfenroll'}}) {
 4999:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5000:             }
 5001:         }
 5002:         if (ref($current->{'maproles'}) eq 'HASH') {
 5003:             %rolemaps = %{$current->{'maproles'}};
 5004:         }
 5005:         if ($current->{'section'} ne '') {
 5006:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 5007:             $crssecfieldsty = 'inline-block';
 5008:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5009:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5010:             } else {
 5011:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5012:                 $crssecsrc = $current->{'section'};
 5013:                 $secsrcfieldsty = 'inline-block';
 5014:             }
 5015:         } else {
 5016:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5017:         }
 5018:         if ($current->{'topmenu'}) {
 5019:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5020:         } else {
 5021:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5022:         }
 5023:         if ($current->{'inlinemenu'}) {
 5024:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5025:         } else {
 5026:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5027:         }
 5028:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5029:             $menusty = 'inline-block';
 5030:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5031:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5032:                     if (exists($menutitles{$item})) {
 5033:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5034:                     }
 5035:                 }
 5036:             }
 5037:         } else {
 5038:             $menusty = 'none';
 5039:         }
 5040:     } else {
 5041:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5042:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5043:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5044:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 5045:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5046:         $menusty = 'inline-block'; 
 5047:     }
 5048:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 5049:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5050:                                official   => 'Official',
 5051:                                unofficial => 'Unofficial',
 5052:                                community  => 'Community',
 5053:                                textbook   => 'Textbook',
 5054:                                placement  => 'Placement Test',
 5055:                                lti        => 'LTI Provider',
 5056:     );
 5057:     my @authtypes = ('internal','krb4','krb5','localauth');
 5058:     my %shortauth = (
 5059:                      internal => 'int',
 5060:                      krb4 => 'krb4',
 5061:                      krb5 => 'krb5',
 5062:                      localauth  => 'loc'
 5063:                     );
 5064:     my %authnames = &authtype_names();
 5065:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5066:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5067:     my @courseroles = ('cc','in','ta','ep','st');
 5068:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5069:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5070:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5071:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5072:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5073:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5074:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5075:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5076:     foreach my $option ('sourcedid','email','other') {
 5077:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5078:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5079:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5080:     }
 5081:     $output .= '</span></div>'.
 5082:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5083:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5084:                'value="'.$userfield.'" /></div></fieldset>'. 
 5085:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5086:     foreach my $ltirole (@lticourseroles) {
 5087:         my ($selected,$selectnone);
 5088:         if ($rolemaps{$ltirole} eq '') {
 5089:             $selectnone = ' selected="selected"';
 5090:         }
 5091:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5092:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5093:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5094:         foreach my $role (@courseroles) {
 5095:             unless ($selectnone) {
 5096:                 if ($rolemaps{$ltirole} eq $role) {
 5097:                     $selected = ' selected="selected"';
 5098:                 } else {
 5099:                     $selected = '';
 5100:                 }
 5101:             }
 5102:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5103:                        &Apache::lonnet::plaintext($role,'Course').
 5104:                        '</option>';
 5105:         }
 5106:         $output .= '</select></td>';
 5107:     }
 5108:     $output .= '</tr></table></fieldset>'.
 5109:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5110:     foreach my $ltirole (@ltiroles) {
 5111:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5112:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 5113:     }
 5114:     $output .= '</fieldset>'.
 5115:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5116:                '<table>'.
 5117:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5118:                '</table>'.
 5119:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5120:                '<td class="LC_left_item">';
 5121:     foreach my $auth ('lti',@authtypes) {
 5122:         my $authtext;
 5123:         if ($auth eq 'lti') {
 5124:             $authtext = &mt('None');
 5125:         } else {
 5126:             $authtext = $authnames{$shortauth{$auth}};
 5127:         }
 5128:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5129:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5130:                    $authtext.'</label></span> &nbsp;';
 5131:     }
 5132:     $output .= '</td></tr>'.
 5133:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5134:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5135:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5136:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5137:                '</table></fieldset>'.
 5138:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5139:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5140:                &mt('Unique course identifier').':&nbsp;';
 5141:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5142:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5143:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5144:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5145:     }
 5146:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5147:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5148:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5149:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5150:     foreach my $type (@coursetypes) {
 5151:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5152:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5153:                    ('&nbsp;'x2);
 5154:     }
 5155:     $output .= '</span></fieldset>'.
 5156:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5157:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5158:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5159:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5160:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5161:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5162:                '</fieldset>'.
 5163:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5164:     foreach my $lticrsrole (@lticourseroles) {
 5165:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5166:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5167:     }
 5168:     $output .= '</fieldset>'.
 5169:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 5170:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5171:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5172:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5173:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5174:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5175:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5176:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5177:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5178:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5179:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5180:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5181:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5182:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5183:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5184:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5185:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5186:     foreach my $extra ('roster','passback') {
 5187:         my $checkedon = '';
 5188:         my $checkedoff = ' checked="checked"';
 5189:         if ($extra eq 'passback') {
 5190:             $pb1p1chk = ' checked="checked"';
 5191:             $pb1p0chk = '';
 5192:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 5193:         } else {
 5194:             $onclickpb = ''; 
 5195:         }
 5196:         if (ref($current) eq 'HASH') {
 5197:             if (($current->{$extra})) {
 5198:                 $checkedon = $checkedoff;
 5199:                 $checkedoff = '';
 5200:                 if ($extra eq 'passback') {
 5201:                     $passbacksty = 'inline-block';
 5202:                 }
 5203:                 if ($current->{'passbackformat'} eq '1.0') {
 5204:                     $pb1p0chk =  ' checked="checked"';
 5205:                     $pb1p1chk = '';
 5206:                 }
 5207:             }
 5208:         }
 5209:         $output .= $lt{$extra}.'&nbsp;'.
 5210:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 5211:                    &mt('No').'</label>'.('&nbsp;'x2).
 5212:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 5213:                    &mt('Yes').'</label><br />';
 5214:     }
 5215:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 5216:                '<span class="LC_nobreak">'.&mt('Grade format').
 5217:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 5218:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 5219:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 5220:                &mt('Outcomes Extension (1.0)').'</label></span></div></fieldset>'.
 5221:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 5222:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 5223:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 5224:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5225:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 5226:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 5227:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5228:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 5229:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 5230:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5231:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 5232:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 5233:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 5234:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 5235:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 5236:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 5237:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 5238:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 5239:                    ('&nbsp;'x2);
 5240:     }
 5241:     $output .= '</span></div></fieldset>';
 5242: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 5243: #
 5244: #    $output .= '</fieldset>'.
 5245: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 5246:     return $output;
 5247: }
 5248: 
 5249: sub ltimenu_titles {
 5250:     return &Apache::lonlocal::texthash(
 5251:                                         fullname    => 'Full name',
 5252:                                         coursetitle => 'Course title',
 5253:                                         role        => 'Role',
 5254:                                         logout      => 'Logout',
 5255:                                         grades      => 'Grades',
 5256:     );
 5257: }
 5258: 
 5259: sub print_coursedefaults {
 5260:     my ($position,$dom,$settings,$rowtotal) = @_;
 5261:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 5262:     my $itemcount = 1;
 5263:     my %choices =  &Apache::lonlocal::texthash (
 5264:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 5265:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 5266:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 5267:         coursecredits        => 'Credits can be specified for courses',
 5268:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 5269:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 5270:         texengine            => 'Default method to display mathematics',
 5271:         postsubmit           => 'Disable submit button/keypress following student submission',
 5272:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 5273:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 5274:     );
 5275:     my %staticdefaults = (
 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 = $Apache::lonnet::deftex;
 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:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 6790:                    'errorthreshold'  => 'Error/warning threshold for status e-mail',
 6791:                    'errorsysmail'    => 'Error threshold for e-mail to core group',
 6792:                    'errorweights'    => 'Weights used to compute error count',
 6793:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 6794:                  );
 6795:     my %short_titles = &Apache::lonlocal::texthash (
 6796:                            adminemail   => 'Admin E-mail address',
 6797:                            supportemail => 'Support E-mail',
 6798:                        );   
 6799:     return (\%titles,\%short_titles);
 6800: }
 6801: 
 6802: sub helpform_fields {
 6803:     my %titles =  &Apache::lonlocal::texthash (
 6804:                        'username'   => 'Name',
 6805:                        'user'       => 'Username/domain',
 6806:                        'phone'      => 'Phone',
 6807:                        'cc'         => 'Cc e-mail',
 6808:                        'course'     => 'Course Details',
 6809:                        'section'    => 'Sections',
 6810:                        'screenshot' => 'File upload',
 6811:     );
 6812:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 6813:     my %possoptions = (
 6814:                         username     => ['yes','no','req'],
 6815:                         phone        => ['yes','no','req'],
 6816:                         user         => ['yes','no'],
 6817:                         cc           => ['yes','no'],
 6818:                         course       => ['yes','no'],
 6819:                         section      => ['yes','no'],
 6820:                         screenshot   => ['yes','no'],
 6821:                       );
 6822:     my %fieldoptions = &Apache::lonlocal::texthash (
 6823:                          'yes'  => 'Optional',
 6824:                          'req'  => 'Required',
 6825:                          'no'   => "Not shown",
 6826:     );
 6827:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 6828: }
 6829: 
 6830: sub tool_titles {
 6831:     my %titles = &Apache::lonlocal::texthash (
 6832:                      aboutme    => 'Personal web page',
 6833:                      blog       => 'Blog',
 6834:                      webdav     => 'WebDAV',
 6835:                      portfolio  => 'Portfolio',
 6836:                      official   => 'Official courses (with institutional codes)',
 6837:                      unofficial => 'Unofficial courses',
 6838:                      community  => 'Communities',
 6839:                      textbook   => 'Textbook courses',
 6840:                      placement  => 'Placement tests',
 6841:                  );
 6842:     return %titles;
 6843: }
 6844: 
 6845: sub courserequest_titles {
 6846:     my %titles = &Apache::lonlocal::texthash (
 6847:                                    official   => 'Official',
 6848:                                    unofficial => 'Unofficial',
 6849:                                    community  => 'Communities',
 6850:                                    textbook   => 'Textbook',
 6851:                                    placement  => 'Placement tests',
 6852:                                    lti        => 'LTI Provider',
 6853:                                    norequest  => 'Not allowed',
 6854:                                    approval   => 'Approval by DC',
 6855:                                    validate   => 'With validation',
 6856:                                    autolimit  => 'Numerical limit',
 6857:                                    unlimited  => '(blank for unlimited)',
 6858:                  );
 6859:     return %titles;
 6860: }
 6861: 
 6862: sub authorrequest_titles {
 6863:     my %titles = &Apache::lonlocal::texthash (
 6864:                                    norequest  => 'Not allowed',
 6865:                                    approval   => 'Approval by Dom. Coord.',
 6866:                                    automatic  => 'Automatic approval',
 6867:                  );
 6868:     return %titles;
 6869: }
 6870: 
 6871: sub courserequest_conditions {
 6872:     my %conditions = &Apache::lonlocal::texthash (
 6873:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6874:        validate   => '(Processing of request subject to institutional validation).',
 6875:                  );
 6876:     return %conditions;
 6877: }
 6878: 
 6879: 
 6880: sub print_usercreation {
 6881:     my ($position,$dom,$settings,$rowtotal) = @_;
 6882:     my $numinrow = 4;
 6883:     my $datatable;
 6884:     if ($position eq 'top') {
 6885:         $$rowtotal ++;
 6886:         my $rowcount = 0;
 6887:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6888:         if (ref($rules) eq 'HASH') {
 6889:             if (keys(%{$rules}) > 0) {
 6890:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6891:                                                 $ruleorder,$numinrow,$rowcount);
 6892:                 $$rowtotal ++;
 6893:                 $rowcount ++;
 6894:             }
 6895:         }
 6896:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6897:         if (ref($idrules) eq 'HASH') {
 6898:             if (keys(%{$idrules}) > 0) {
 6899:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6900:                                                 $idruleorder,$numinrow,$rowcount);
 6901:                 $$rowtotal ++;
 6902:                 $rowcount ++;
 6903:             }
 6904:         }
 6905:         if ($rowcount == 0) {
 6906:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6907:             $$rowtotal ++;
 6908:             $rowcount ++;
 6909:         }
 6910:     } elsif ($position eq 'middle') {
 6911:         my @creators = ('author','course','requestcrs');
 6912:         my ($rules,$ruleorder) =
 6913:             &Apache::lonnet::inst_userrules($dom,'username');
 6914:         my %lt = &usercreation_types();
 6915:         my %checked;
 6916:         if (ref($settings) eq 'HASH') {
 6917:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6918:                 foreach my $item (@creators) {
 6919:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6920:                 }
 6921:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6922:                 foreach my $item (@creators) {
 6923:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6924:                         $checked{$item} = 'none';
 6925:                     }
 6926:                 }
 6927:             }
 6928:         }
 6929:         my $rownum = 0;
 6930:         foreach my $item (@creators) {
 6931:             $rownum ++;
 6932:             if ($checked{$item} eq '') {
 6933:                 $checked{$item} = 'any';
 6934:             }
 6935:             my $css_class;
 6936:             if ($rownum%2) {
 6937:                 $css_class = '';
 6938:             } else {
 6939:                 $css_class = ' class="LC_odd_row" ';
 6940:             }
 6941:             $datatable .= '<tr'.$css_class.'>'.
 6942:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6943:                          '</span></td><td style="text-align: right">';
 6944:             my @options = ('any');
 6945:             if (ref($rules) eq 'HASH') {
 6946:                 if (keys(%{$rules}) > 0) {
 6947:                     push(@options,('official','unofficial'));
 6948:                 }
 6949:             }
 6950:             push(@options,'none');
 6951:             foreach my $option (@options) {
 6952:                 my $type = 'radio';
 6953:                 my $check = ' ';
 6954:                 if ($checked{$item} eq $option) {
 6955:                     $check = ' checked="checked" ';
 6956:                 } 
 6957:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6958:                               '<input type="'.$type.'" name="can_createuser_'.
 6959:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6960:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6961:             }
 6962:             $datatable .= '</td></tr>';
 6963:         }
 6964:     } else {
 6965:         my @contexts = ('author','course','domain');
 6966:         my @authtypes = ('int','krb4','krb5','loc','lti');
 6967:         my %checked;
 6968:         if (ref($settings) eq 'HASH') {
 6969:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6970:                 foreach my $item (@contexts) {
 6971:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6972:                         foreach my $auth (@authtypes) {
 6973:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6974:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6975:                             }
 6976:                         }
 6977:                     }
 6978:                 }
 6979:             }
 6980:         } else {
 6981:             foreach my $item (@contexts) {
 6982:                 foreach my $auth (@authtypes) {
 6983:                     $checked{$item}{$auth} = ' checked="checked" ';
 6984:                 }
 6985:             }
 6986:         }
 6987:         my %title = &context_names();
 6988:         my %authname = &authtype_names();
 6989:         my $rownum = 0;
 6990:         my $css_class; 
 6991:         foreach my $item (@contexts) {
 6992:             if ($rownum%2) {
 6993:                 $css_class = '';
 6994:             } else {
 6995:                 $css_class = ' class="LC_odd_row" ';
 6996:             }
 6997:             $datatable .=   '<tr'.$css_class.'>'.
 6998:                             '<td>'.$title{$item}.
 6999:                             '</td><td class="LC_left_item">'.
 7000:                             '<span class="LC_nobreak">';
 7001:             foreach my $auth (@authtypes) {
 7002:                 $datatable .= '<label>'. 
 7003:                               '<input type="checkbox" name="'.$item.'_auth" '.
 7004:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 7005:                               $authname{$auth}.'</label>&nbsp;';
 7006:             }
 7007:             $datatable .= '</span></td></tr>';
 7008:             $rownum ++;
 7009:         }
 7010:         $$rowtotal += $rownum;
 7011:     }
 7012:     return $datatable;
 7013: }
 7014: 
 7015: sub print_selfcreation {
 7016:     my ($position,$dom,$settings,$rowtotal) = @_;
 7017:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 7018:         $emaildomain,$datatable);
 7019:     if (ref($settings) eq 'HASH') {
 7020:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 7021:             $createsettings = $settings->{'cancreate'};
 7022:             if (ref($createsettings) eq 'HASH') {
 7023:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 7024:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 7025:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 7026:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 7027:                         @selfcreate = ('email','login','sso');
 7028:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 7029:                         @selfcreate = ($createsettings->{'selfcreate'});
 7030:                     }
 7031:                 }
 7032:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 7033:                     $processing = $createsettings->{'selfcreateprocessing'};
 7034:                 }
 7035:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 7036:                     $emailoptions = $createsettings->{'emailoptions'};
 7037:                 }
 7038:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 7039:                     $emailverified = $createsettings->{'emailverified'};
 7040:                 }
 7041:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 7042:                     $emaildomain = $createsettings->{'emaildomain'};
 7043:                 }
 7044:             }
 7045:         }
 7046:     }
 7047:     my %radiohash;
 7048:     my $numinrow = 4;
 7049:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 7050:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7051:     if ($position eq 'top') {
 7052:         my %choices = &Apache::lonlocal::texthash (
 7053:                                                       cancreate_login      => 'Institutional Login',
 7054:                                                       cancreate_sso        => 'Institutional Single Sign On',
 7055:                                                   );
 7056:         my @toggles = sort(keys(%choices));
 7057:         my %defaultchecked = (
 7058:                                'cancreate_login' => 'off',
 7059:                                'cancreate_sso'   => 'off',
 7060:                              );
 7061:         my ($onclick,$itemcount);
 7062:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7063:                                                      \%choices,$itemcount,$onclick);
 7064:         $$rowtotal += $itemcount;
 7065:         
 7066:         if (ref($usertypes) eq 'HASH') {
 7067:             if (keys(%{$usertypes}) > 0) {
 7068:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 7069:                                              $dom,$numinrow,$othertitle,
 7070:                                              'statustocreate',$rowtotal);
 7071:                 $$rowtotal ++;
 7072:             }
 7073:         }
 7074:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 7075:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7076:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 7077:         my $rem;
 7078:         my $numperrow = 2;
 7079:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 7080:         $datatable .= '<tr'.$css_class.'>'.
 7081:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 7082:                      '<td class="LC_left_item">'."\n".
 7083:                      '<table>'."\n";
 7084:         for (my $i=0; $i<@fields; $i++) {
 7085:             $rem = $i%($numperrow);
 7086:             if ($rem == 0) {
 7087:                 if ($i > 0) {
 7088:                     $datatable .= '</tr>';
 7089:                 }
 7090:                 $datatable .= '<tr>';
 7091:             }
 7092:             my $currval;
 7093:             if (ref($createsettings) eq 'HASH') {
 7094:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 7095:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 7096:                 }
 7097:             }
 7098:             $datatable .= '<td class="LC_left_item">'.
 7099:                           '<span class="LC_nobreak">'.
 7100:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 7101:                           'value="'.$currval.'" size="10" />&nbsp;'.
 7102:                           $fieldtitles{$fields[$i]}.'</span></td>';
 7103:         }
 7104:         my $colsleft = $numperrow - $rem;
 7105:         if ($colsleft > 1 ) {
 7106:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7107:                          '&nbsp;</td>';
 7108:         } elsif ($colsleft == 1) {
 7109:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7110:         }
 7111:         $datatable .= '</tr></table></td></tr>';
 7112:         $$rowtotal ++;
 7113:     } elsif ($position eq 'middle') {
 7114:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 7115:         my @posstypes;
 7116:         if (ref($types) eq 'ARRAY') {
 7117:             @posstypes = @{$types};
 7118:         }
 7119:         unless (grep(/^default$/,@posstypes)) {
 7120:             push(@posstypes,'default');
 7121:         }
 7122:         my %usertypeshash;
 7123:         if (ref($usertypes) eq 'HASH') {
 7124:             %usertypeshash = %{$usertypes};
 7125:         }
 7126:         $usertypeshash{'default'} = $othertitle;
 7127:         foreach my $status (@posstypes) {
 7128:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 7129:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 7130:             $$rowtotal ++;
 7131:         }
 7132:     } else {
 7133:         my %choices = &Apache::lonlocal::texthash (
 7134:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 7135:                                                   );
 7136:         my @toggles = sort(keys(%choices));
 7137:         my %defaultchecked = (
 7138:                                'cancreate_email' => 'off',
 7139:                              );
 7140:         my $customclass = 'LC_selfcreate_email';
 7141:         my $classprefix = 'LC_canmodify_emailusername_';
 7142:         my $optionsprefix = 'LC_options_emailusername_';
 7143:         my $display = 'none';
 7144:         my $rowstyle = 'display:none';
 7145:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 7146:             $display = 'block';
 7147:             $rowstyle = 'display:table-row';
 7148:         }
 7149:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 7150:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7151:                                                      \%choices,$$rowtotal,$onclick);
 7152:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 7153:                                          $rowstyle);
 7154:         $$rowtotal ++;
 7155:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 7156:                                       $rowstyle);
 7157:         $$rowtotal ++;
 7158:         my (@ordered,@posstypes,%usertypeshash);
 7159:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7160:         my ($emailrules,$emailruleorder) =
 7161:             &Apache::lonnet::inst_userrules($dom,'email');
 7162:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7163:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7164:         if (ref($types) eq 'ARRAY') {
 7165:             @posstypes = @{$types};
 7166:         }
 7167:         if (@posstypes) {
 7168:             unless (grep(/^default$/,@posstypes)) {
 7169:                 push(@posstypes,'default');
 7170:             }
 7171:             if (ref($usertypes) eq 'HASH') {
 7172:                 %usertypeshash = %{$usertypes};
 7173:             }
 7174:             my $currassign;
 7175:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 7176:                 $currassign = {
 7177:                                   selfassign => $domdefaults{'inststatusguest'},
 7178:                               };
 7179:                 @ordered = @{$domdefaults{'inststatusguest'}};
 7180:             } else {
 7181:                 $currassign = { selfassign => [] };
 7182:             }
 7183:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 7184:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 7185:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 7186:                                          $numinrow,$othertitle,'selfassign',
 7187:                                          $rowtotal,$onclicktypes,$customclass,
 7188:                                          $rowstyle);
 7189:             $$rowtotal ++;
 7190:             $usertypeshash{'default'} = $othertitle;
 7191:             foreach my $status (@posstypes) {
 7192:                 my $css_class;
 7193:                 if ($$rowtotal%2) {
 7194:                     $css_class = 'LC_odd_row ';
 7195:                 }
 7196:                 $css_class .= $customclass;
 7197:                 my $rowid = $optionsprefix.$status;
 7198:                 my $hidden = 1;
 7199:                 my $currstyle = 'display:none';
 7200:                 if (grep(/^\Q$status\E$/,@ordered)) {
 7201:                     $currstyle = $rowstyle;
 7202:                     $hidden = 0; 
 7203:                 }
 7204:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7205:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 7206:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 7207:                 unless ($hidden) {
 7208:                     $$rowtotal ++;
 7209:                 }
 7210:             }
 7211:         } else {
 7212:             my $css_class;
 7213:             if ($$rowtotal%2) {
 7214:                 $css_class = 'LC_odd_row ';
 7215:             }
 7216:             $css_class .= $customclass;
 7217:             $usertypeshash{'default'} = $othertitle;
 7218:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7219:                                          $emailrules,$emailruleorder,$settings,'default','',
 7220:                                          $othertitle,$css_class,$rowstyle,$intdom);
 7221:             $$rowtotal ++;
 7222:         }
 7223:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7224:         $numinrow = 1;
 7225:         if (@posstypes) {
 7226:             foreach my $status (@posstypes) {
 7227:                 my $rowid = $classprefix.$status;
 7228:                 my $datarowstyle = 'display:none';
 7229:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 7230:                     $datarowstyle = $rowstyle; 
 7231:                 }
 7232:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 7233:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7234:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 7235:                 unless ($datarowstyle eq 'display:none') {
 7236:                     $$rowtotal ++;
 7237:                 }
 7238:             }
 7239:         } else {
 7240:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 7241:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7242:                                                    $infotitles,'',$customclass,$rowstyle);
 7243:         }
 7244:     }
 7245:     return $datatable;
 7246: }
 7247: 
 7248: sub selfcreate_javascript {
 7249:     return <<"ENDSCRIPT";
 7250: 
 7251: <script type="text/javascript">
 7252: // <![CDATA[
 7253: 
 7254: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 7255:     var x = document.getElementsByClassName(target);
 7256:     var insttypes = 0;
 7257:     var insttypeRegExp = new RegExp(prefix);
 7258:     if ((x.length != undefined) && (x.length > 0)) {
 7259:         if (form.elements[radio].length != undefined) {
 7260:             for (var i=0; i<form.elements[radio].length; i++) {
 7261:                 if (form.elements[radio][i].checked) {
 7262:                     if (form.elements[radio][i].value == 1) {
 7263:                         for (var j=0; j<x.length; j++) {
 7264:                             if (x[j].id == 'undefined') {
 7265:                                 x[j].style.display = 'table-row';
 7266:                             } else if (insttypeRegExp.test(x[j].id)) {
 7267:                                 insttypes ++;
 7268:                             } else {
 7269:                                 x[j].style.display = 'table-row';
 7270:                             }
 7271:                         }
 7272:                     } else {
 7273:                         for (var j=0; j<x.length; j++) {
 7274:                             x[j].style.display = 'none';
 7275:                         }
 7276:                     }
 7277:                     break;
 7278:                 }
 7279:             }
 7280:             if (insttypes > 0) {
 7281:                 toggleDataRow(form,checkbox,target,altprefix);
 7282:                 toggleDataRow(form,checkbox,target,prefix,1);
 7283:             }
 7284:         }
 7285:     }
 7286:     return;
 7287: }
 7288: 
 7289: function toggleDataRow(form,checkbox,target,prefix,docount) {
 7290:     if (form.elements[checkbox].length != undefined) {
 7291:         var count = 0;
 7292:         if (docount) {
 7293:             for (var i=0; i<form.elements[checkbox].length; i++) {
 7294:                 if (form.elements[checkbox][i].checked) {
 7295:                     count ++;
 7296:                 }
 7297:             }
 7298:         }
 7299:         for (var i=0; i<form.elements[checkbox].length; i++) {
 7300:             var type = form.elements[checkbox][i].value;
 7301:             if (document.getElementById(prefix+type)) {
 7302:                 if (form.elements[checkbox][i].checked) {
 7303:                     document.getElementById(prefix+type).style.display = 'table-row';
 7304:                     if (count % 2 == 1) {
 7305:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 7306:                     } else {
 7307:                         document.getElementById(prefix+type).className = target;
 7308:                     }
 7309:                     count ++;
 7310:                 } else {
 7311:                     document.getElementById(prefix+type).style.display = 'none';
 7312:                 }
 7313:             }
 7314:         }
 7315:     }
 7316:     return;
 7317: }
 7318: 
 7319: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 7320:     var caller = radio+'_'+status;
 7321:     if (form.elements[caller].length != undefined) {
 7322:         for (var i=0; i<form.elements[caller].length; i++) {
 7323:             if (form.elements[caller][i].checked) {
 7324:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 7325:                     var curr = form.elements[caller][i].value;
 7326:                     if (prefix) {
 7327:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 7328:                     }
 7329:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 7330:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 7331:                     if (curr == 'custom') {
 7332:                         if (prefix) { 
 7333:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 7334:                         }
 7335:                     } else if (curr == 'inst') {
 7336:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 7337:                     } else if (curr == 'noninst') {
 7338:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 7339:                     }
 7340:                     break;
 7341:                 }
 7342:             }
 7343:         }
 7344:     }
 7345: }
 7346: 
 7347: // ]]>
 7348: </script>
 7349: 
 7350: ENDSCRIPT
 7351: }
 7352: 
 7353: sub noninst_users {
 7354:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 7355:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 7356:     my $class = 'LC_left_item';
 7357:     if ($css_class) {
 7358:         $css_class = ' class="'.$css_class.'"'; 
 7359:     }
 7360:     if ($rowid) {
 7361:         $rowid = ' id="'.$rowid.'"';
 7362:     }
 7363:     if ($rowstyle) {
 7364:         $rowstyle = ' style="'.$rowstyle.'"';
 7365:     }
 7366:     my ($output,$description);
 7367:     if ($type eq 'default') {
 7368:         $description = &mt('Requests for: [_1]',$typetitle);
 7369:     } else {
 7370:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 7371:     }
 7372:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 7373:               "<td>$description</td>\n".           
 7374:               '<td class="'.$class.'" colspan="2">'.
 7375:               '<table><tr>';
 7376:     my %headers = &Apache::lonlocal::texthash( 
 7377:               approve  => 'Processing',
 7378:               email    => 'E-mail',
 7379:               username => 'Username',
 7380:     );
 7381:     foreach my $item ('approve','email','username') {
 7382:         $output .= '<th>'.$headers{$item}.'</th>';
 7383:     }
 7384:     $output .= '</tr><tr>';
 7385:     foreach my $item ('approve','email','username') {
 7386:         $output .= '<td style="vertical-align: top">';
 7387:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 7388:         if ($item eq 'approve') {
 7389:             %choices = &Apache::lonlocal::texthash (
 7390:                                                      automatic => 'Automatically approved',
 7391:                                                      approval  => 'Queued for approval',
 7392:                                                    );
 7393:             @options = ('automatic','approval');
 7394:             $hashref = $processing;
 7395:             $defoption = 'automatic';
 7396:             $name = 'cancreate_emailprocess_'.$type;
 7397:         } elsif ($item eq 'email') {
 7398:             %choices = &Apache::lonlocal::texthash (
 7399:                                                      any     => 'Any e-mail',
 7400:                                                      inst    => 'Institutional only',
 7401:                                                      noninst => 'Non-institutional only',
 7402:                                                      custom  => 'Custom restrictions',
 7403:                                                    );
 7404:             @options = ('any','inst','noninst');
 7405:             my $showcustom;
 7406:             if (ref($emailrules) eq 'HASH') {
 7407:                 if (keys(%{$emailrules}) > 0) {
 7408:                     push(@options,'custom');
 7409:                     $showcustom = 'cancreate_emailrule';
 7410:                     if (ref($settings) eq 'HASH') {
 7411:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 7412:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 7413:                                 if (exists($emailrules->{$rule})) {
 7414:                                     $hascustom ++;
 7415:                                 }
 7416:                             }
 7417:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 7418:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 7419:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 7420:                                     if (exists($emailrules->{$rule})) {
 7421:                                         $hascustom ++;
 7422:                                     }
 7423:                                 }
 7424:                             }
 7425:                         }
 7426:                     }
 7427:                 }
 7428:             }
 7429:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 7430:                                                      "'cancreate_emaildomain','$type'".');"';
 7431:             $hashref = $emailoptions;
 7432:             $defoption = 'any';
 7433:             $name = 'cancreate_emailoptions_'.$type;
 7434:         } elsif ($item eq 'username') {
 7435:             %choices = &Apache::lonlocal::texthash (
 7436:                                                      all    => 'Same as e-mail',
 7437:                                                      first  => 'Omit @domain',
 7438:                                                      free   => 'Free to choose',
 7439:                                                    );
 7440:             @options = ('all','first','free');
 7441:             $hashref = $emailverified;
 7442:             $defoption = 'all';
 7443:             $name = 'cancreate_usernameoptions_'.$type;
 7444:         }
 7445:         foreach my $option (@options) {
 7446:             my $checked;
 7447:             if (ref($hashref) eq 'HASH') {
 7448:                 if ($type eq '') {
 7449:                     if (!exists($hashref->{'default'})) {
 7450:                         if ($option eq $defoption) {
 7451:                             $checked = ' checked="checked"';
 7452:                         }
 7453:                     } else {
 7454:                         if ($hashref->{'default'} eq $option) {
 7455:                             $checked = ' checked="checked"';
 7456:                         }
 7457:                     }
 7458:                 } else {
 7459:                     if (!exists($hashref->{$type})) {
 7460:                         if ($option eq $defoption) {
 7461:                             $checked = ' checked="checked"';
 7462:                         }
 7463:                     } else {
 7464:                         if ($hashref->{$type} eq $option) {
 7465:                             $checked = ' checked="checked"';
 7466:                         }
 7467:                     }
 7468:                 }
 7469:             } elsif (($item eq 'email') && ($hascustom)) {
 7470:                 if ($option eq 'custom') {
 7471:                     $checked = ' checked="checked"';
 7472:                 }
 7473:             } elsif ($option eq $defoption) {
 7474:                 $checked = ' checked="checked"';
 7475:             }
 7476:             $output .= '<span class="LC_nobreak"><label>'.
 7477:                        '<input type="radio" name="'.$name.'"'.
 7478:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 7479:                        $choices{$option}.'</label></span><br />';
 7480:             if ($item eq 'email') {
 7481:                 if ($option eq 'custom') {
 7482:                     my $id = 'cancreate_emailrule_'.$type;
 7483:                     my $display = 'none';
 7484:                     if ($checked) {
 7485:                         $display = 'inline';
 7486:                     }
 7487:                     my $numinrow = 2;
 7488:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 7489:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 7490:                                &user_formats_row('email',$settings,$emailrules,
 7491:                                                  $emailruleorder,$numinrow,'',$type);
 7492:                               '</table></fieldset>';
 7493:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 7494:                     my %text = &Apache::lonlocal::texthash (
 7495:                                                              inst    => 'must end:',
 7496:                                                              noninst => 'cannot end:',
 7497:                                                            );
 7498:                     my $value;
 7499:                     if (ref($emaildomain) eq 'HASH') {
 7500:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 7501:                             $value = $emaildomain->{$type}->{$option}; 
 7502:                         }
 7503:                     }
 7504:                     if ($value eq '') {
 7505:                         $value = '@'.$intdom;
 7506:                     }
 7507:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 7508:                     my $display = 'none';
 7509:                     if ($checked) {
 7510:                         $display = 'inline';
 7511:                     }
 7512:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 7513:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 7514:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 7515:                                '</div>';
 7516:                 }
 7517:             }
 7518:         }
 7519:         $output .= '</td>'."\n";
 7520:     }
 7521:     $output .= "</tr></table></td></tr>\n";
 7522:     return $output;
 7523: }
 7524: 
 7525: sub captcha_choice {
 7526:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 7527:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 7528:         $vertext,$currver);
 7529:     my %lt = &captcha_phrases();
 7530:     $keyentry = 'hidden';
 7531:     if ($context eq 'cancreate') {
 7532:         $rowname = &mt('CAPTCHA validation');
 7533:     } elsif ($context eq 'login') {
 7534:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 7535:     }
 7536:     if (ref($settings) eq 'HASH') {
 7537:         if ($settings->{'captcha'}) {
 7538:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 7539:         } else {
 7540:             $checked{'original'} = ' checked="checked"';
 7541:         }
 7542:         if ($settings->{'captcha'} eq 'recaptcha') {
 7543:             $pubtext = $lt{'pub'};
 7544:             $privtext = $lt{'priv'};
 7545:             $keyentry = 'text';
 7546:             $vertext = $lt{'ver'};
 7547:             $currver = $settings->{'recaptchaversion'};
 7548:             if ($currver ne '2') {
 7549:                 $currver = 1;
 7550:             }
 7551:         }
 7552:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 7553:             $currpub = $settings->{'recaptchakeys'}{'public'};
 7554:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 7555:         }
 7556:     } else {
 7557:         $checked{'original'} = ' checked="checked"';
 7558:     }
 7559:     my $css_class;
 7560:     if ($itemcount%2) {
 7561:         $css_class = 'LC_odd_row';
 7562:     }
 7563:     if ($customcss) {
 7564:         $css_class .= " $customcss";
 7565:     }
 7566:     $css_class =~ s/^\s+//;
 7567:     if ($css_class) {
 7568:         $css_class = ' class="'.$css_class.'"';
 7569:     }
 7570:     if ($rowstyle) {
 7571:         $css_class .= ' style="'.$rowstyle.'"';
 7572:     }
 7573:     my $output = '<tr'.$css_class.'>'.
 7574:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 7575:                  '<table><tr><td>'."\n";
 7576:     foreach my $option ('original','recaptcha','notused') {
 7577:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 7578:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 7579:                    $lt{$option}.'</label></span>';
 7580:         unless ($option eq 'notused') {
 7581:             $output .= ('&nbsp;'x2)."\n";
 7582:         }
 7583:     }
 7584: #
 7585: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 7586: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 7587: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 7588: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 7589: #
 7590:     $output .= '</td></tr>'."\n".
 7591:                '<tr><td class="LC_zero_height">'."\n".
 7592:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 7593:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 7594:                $currpub.'" size="40" /></span><br />'."\n".
 7595:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 7596:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 7597:                $currpriv.'" size="40" /></span><br />'.
 7598:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 7599:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 7600:                $currver.'" size="3" /></span><br />'.
 7601:                '</td></tr></table>'."\n".
 7602:                '</td></tr>';
 7603:     return $output;
 7604: }
 7605: 
 7606: sub user_formats_row {
 7607:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 7608:     my $output;
 7609:     my %text = (
 7610:                    'username' => 'new usernames',
 7611:                    'id'       => 'IDs',
 7612:                );
 7613:     unless ($type eq 'email') {
 7614:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7615:         $output = '<tr '.$css_class.'>'.
 7616:                   '<td><span class="LC_nobreak">'.
 7617:                   &mt("Format rules to check for $text{$type}: ").
 7618:                   '</td><td class="LC_left_item" colspan="2"><table>';
 7619:     }
 7620:     my $rem;
 7621:     if (ref($ruleorder) eq 'ARRAY') {
 7622:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 7623:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 7624:                 my $rem = $i%($numinrow);
 7625:                 if ($rem == 0) {
 7626:                     if ($i > 0) {
 7627:                         $output .= '</tr>';
 7628:                     }
 7629:                     $output .= '<tr>';
 7630:                 }
 7631:                 my $check = ' ';
 7632:                 if (ref($settings) eq 'HASH') {
 7633:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 7634:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 7635:                             $check = ' checked="checked" ';
 7636:                         }
 7637:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 7638:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 7639:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 7640:                                 $check = ' checked="checked" ';
 7641:                             }
 7642:                         }
 7643:                     }
 7644:                 }
 7645:                 my $name = $type.'_rule';
 7646:                 if ($type eq 'email') {
 7647:                     $name .= '_'.$status;
 7648:                 }
 7649:                 $output .= '<td class="LC_left_item">'.
 7650:                            '<span class="LC_nobreak"><label>'.
 7651:                            '<input type="checkbox" name="'.$name.'" '.
 7652:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 7653:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 7654:             }
 7655:         }
 7656:         $rem = @{$ruleorder}%($numinrow);
 7657:     }
 7658:     my $colsleft;
 7659:     if ($rem) {
 7660:         $colsleft = $numinrow - $rem;
 7661:     }
 7662:     if ($colsleft > 1 ) {
 7663:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7664:                    '&nbsp;</td>';
 7665:     } elsif ($colsleft == 1) {
 7666:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7667:     }
 7668:     $output .= '</tr></table>';
 7669:     unless ($type eq 'email') {
 7670:         $output .= '</td></tr>';
 7671:     }
 7672:     return $output;
 7673: }
 7674: 
 7675: sub usercreation_types {
 7676:     my %lt = &Apache::lonlocal::texthash (
 7677:                     author     => 'When adding a co-author',
 7678:                     course     => 'When adding a user to a course',
 7679:                     requestcrs => 'When requesting a course',
 7680:                     any        => 'Any',
 7681:                     official   => 'Institutional only ',
 7682:                     unofficial => 'Non-institutional only',
 7683:                     none       => 'None',
 7684:     );
 7685:     return %lt;
 7686: }
 7687: 
 7688: sub selfcreation_types {
 7689:     my %lt = &Apache::lonlocal::texthash (
 7690:                     selfcreate => 'User creates own account',
 7691:                     any        => 'Any',
 7692:                     official   => 'Institutional only ',
 7693:                     unofficial => 'Non-institutional only',
 7694:                     email      => 'E-mail address',
 7695:                     login      => 'Institutional Login',
 7696:                     sso        => 'SSO',
 7697:              );
 7698: }
 7699: 
 7700: sub authtype_names {
 7701:     my %lt = &Apache::lonlocal::texthash(
 7702:                       int    => 'Internal',
 7703:                       krb4   => 'Kerberos 4',
 7704:                       krb5   => 'Kerberos 5',
 7705:                       loc    => 'Local',
 7706:                       lti    => 'LTI',
 7707:                   );
 7708:     return %lt;
 7709: }
 7710: 
 7711: sub context_names {
 7712:     my %context_title = &Apache::lonlocal::texthash(
 7713:        author => 'Creating users when an Author',
 7714:        course => 'Creating users when in a course',
 7715:        domain => 'Creating users when a Domain Coordinator',
 7716:     );
 7717:     return %context_title;
 7718: }
 7719: 
 7720: sub print_usermodification {
 7721:     my ($position,$dom,$settings,$rowtotal) = @_;
 7722:     my $numinrow = 4;
 7723:     my ($context,$datatable,$rowcount);
 7724:     if ($position eq 'top') {
 7725:         $rowcount = 0;
 7726:         $context = 'author'; 
 7727:         foreach my $role ('ca','aa') {
 7728:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7729:                                                    $numinrow,$rowcount);
 7730:             $$rowtotal ++;
 7731:             $rowcount ++;
 7732:         }
 7733:     } elsif ($position eq 'bottom') {
 7734:         $context = 'course';
 7735:         $rowcount = 0;
 7736:         foreach my $role ('st','ep','ta','in','cr') {
 7737:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7738:                                                    $numinrow,$rowcount);
 7739:             $$rowtotal ++;
 7740:             $rowcount ++;
 7741:         }
 7742:     }
 7743:     return $datatable;
 7744: }
 7745: 
 7746: sub print_defaults {
 7747:     my ($position,$dom,$settings,$rowtotal) = @_;
 7748:     my $rownum = 0;
 7749:     my ($datatable,$css_class,$titles);
 7750:     unless ($position eq 'bottom') {
 7751:         $titles = &defaults_titles($dom);
 7752:     }
 7753:     if ($position eq 'top') {
 7754:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 7755:                      'datelocale_def','portal_def');
 7756:         my %defaults;
 7757:         if (ref($settings) eq 'HASH') {
 7758:             %defaults = %{$settings};
 7759:         } else {
 7760:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7761:             foreach my $item (@items) {
 7762:                 $defaults{$item} = $domdefaults{$item};
 7763:             }
 7764:         }
 7765:         foreach my $item (@items) {
 7766:             if ($rownum%2) {
 7767:                 $css_class = '';
 7768:             } else {
 7769:                 $css_class = ' class="LC_odd_row" ';
 7770:             }
 7771:             $datatable .= '<tr'.$css_class.'>'.
 7772:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7773:                           '</span></td><td class="LC_right_item" colspan="3">';
 7774:             if ($item eq 'auth_def') {
 7775:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 7776:                 my %shortauth = (
 7777:                                  internal => 'int',
 7778:                                  krb4 => 'krb4',
 7779:                                  krb5 => 'krb5',
 7780:                                  localauth  => 'loc',
 7781:                                  lti => 'lti',
 7782:                                 );
 7783:                 my %authnames = &authtype_names();
 7784:                 foreach my $auth (@authtypes) {
 7785:                     my $checked = ' ';
 7786:                     if ($defaults{$item} eq $auth) {
 7787:                         $checked = ' checked="checked" ';
 7788:                     }
 7789:                     $datatable .= '<label><input type="radio" name="'.$item.
 7790:                                   '" value="'.$auth.'"'.$checked.'/>'.
 7791:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 7792:                 }
 7793:             } elsif ($item eq 'timezone_def') {
 7794:                 my $includeempty = 1;
 7795:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 7796:             } elsif ($item eq 'datelocale_def') {
 7797:                 my $includeempty = 1;
 7798:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 7799:             } elsif ($item eq 'lang_def') {
 7800:                 my $includeempty = 1;
 7801:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 7802:             } else {
 7803:                 my $size;
 7804:                 if ($item eq 'portal_def') {
 7805:                     $size = ' size="25"';
 7806:                 }
 7807:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7808:                               $defaults{$item}.'"'.$size.' />';
 7809:             }
 7810:             $datatable .= '</td></tr>';
 7811:             $rownum ++;
 7812:         }
 7813:     } elsif ($position eq 'middle') {
 7814:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7815:         my %defaults;
 7816:         if (ref($settings) eq 'HASH') {
 7817:             %defaults = %{$settings};
 7818:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7819:                 $defaults{'intauth_cost'} = 10;
 7820:             }
 7821:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7822:                 $defaults{'intauth_check'} = 0;
 7823:             }
 7824:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7825:                 $defaults{'intauth_switch'} = 0;
 7826:             }
 7827:         } else {
 7828:             %defaults = (
 7829:                           'intauth_cost'   => 10,
 7830:                           'intauth_check'  => 0,
 7831:                           'intauth_switch' => 0,
 7832:                         );
 7833:         }
 7834:         foreach my $item (@items) {
 7835:             if ($rownum%2) {
 7836:                 $css_class = '';
 7837:             } else {
 7838:                 $css_class = ' class="LC_odd_row" ';
 7839:             }
 7840:             $datatable .= '<tr'.$css_class.'>'.
 7841:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7842:                           '</span></td><td class="LC_left_item" colspan="3">';
 7843:             if ($item eq 'intauth_switch') {
 7844:                 my @options = (0,1,2);
 7845:                 my %optiondesc = &Apache::lonlocal::texthash (
 7846:                                    0 => 'No',
 7847:                                    1 => 'Yes',
 7848:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7849:                                  );
 7850:                 $datatable .= '<table width="100%">';
 7851:                 foreach my $option (@options) {
 7852:                     my $checked = ' ';
 7853:                     if ($defaults{$item} eq $option) {
 7854:                         $checked = ' checked="checked"';
 7855:                     }
 7856:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7857:                                   '<label><input type="radio" name="'.$item.
 7858:                                   '" value="'.$option.'"'.$checked.' />'.
 7859:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7860:                 }
 7861:                 $datatable .= '</table>';
 7862:             } elsif ($item eq 'intauth_check') {
 7863:                 my @options = (0,1,2);
 7864:                 my %optiondesc = &Apache::lonlocal::texthash (
 7865:                                    0 => 'No',
 7866:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7867:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7868:                                  );
 7869:                 $datatable .= '<table width="100%">';
 7870:                 foreach my $option (@options) {
 7871:                     my $checked = ' ';
 7872:                     my $onclick;
 7873:                     if ($defaults{$item} eq $option) {
 7874:                         $checked = ' checked="checked"';
 7875:                     }
 7876:                     if ($option == 2) {
 7877:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7878:                     }
 7879:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7880:                                   '<label><input type="radio" name="'.$item.
 7881:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7882:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7883:                 }
 7884:                 $datatable .= '</table>';
 7885:             } else {
 7886:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7887:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 7888:             }
 7889:             $datatable .= '</td></tr>';
 7890:             $rownum ++;
 7891:         }
 7892:     } else {
 7893:         my %defaults;
 7894:         if (ref($settings) eq 'HASH') {
 7895:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7896:                 my $maxnum = @{$settings->{'inststatusorder'}};
 7897:                 for (my $i=0; $i<$maxnum; $i++) {
 7898:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 7899:                     my $item = $settings->{'inststatusorder'}->[$i];
 7900:                     my $title = $settings->{'inststatustypes'}->{$item};
 7901:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 7902:                     $datatable .= '<tr'.$css_class.'>'.
 7903:                                   '<td><span class="LC_nobreak">'.
 7904:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 7905:                     for (my $k=0; $k<=$maxnum; $k++) {
 7906:                         my $vpos = $k+1;
 7907:                         my $selstr;
 7908:                         if ($k == $i) {
 7909:                             $selstr = ' selected="selected" ';
 7910:                         }
 7911:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7912:                     }
 7913:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 7914:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 7915:                                   &mt('delete').'</span></td>'.
 7916:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 7917:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 7918:                                   '</span></td></tr>';
 7919:                 }
 7920:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 7921:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 7922:                 $datatable .= '<tr '.$css_class.'>'.
 7923:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 7924:                 for (my $k=0; $k<=$maxnum; $k++) {
 7925:                     my $vpos = $k+1;
 7926:                     my $selstr;
 7927:                     if ($k == $maxnum) {
 7928:                         $selstr = ' selected="selected" ';
 7929:                     }
 7930:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7931:                 }
 7932:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 7933:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 7934:                               '&nbsp;'.&mt('(new)').
 7935:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 7936:                               &mt('Name displayed:').
 7937:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 7938:                               '</tr>'."\n";
 7939:                 $rownum ++;
 7940:             }
 7941:         }
 7942:     }
 7943:     $$rowtotal += $rownum;
 7944:     return $datatable;
 7945: }
 7946: 
 7947: sub get_languages_hash {
 7948:     my %langchoices;
 7949:     foreach my $id (&Apache::loncommon::languageids()) {
 7950:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7951:         if ($code ne '') {
 7952:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7953:         }
 7954:     }
 7955:     return %langchoices;
 7956: }
 7957: 
 7958: sub defaults_titles {
 7959:     my ($dom) = @_;
 7960:     my %titles = &Apache::lonlocal::texthash (
 7961:                    'auth_def'      => 'Default authentication type',
 7962:                    'auth_arg_def'  => 'Default authentication argument',
 7963:                    'lang_def'      => 'Default language',
 7964:                    'timezone_def'  => 'Default timezone',
 7965:                    'datelocale_def' => 'Default locale for dates',
 7966:                    'portal_def'     => 'Portal/Default URL',
 7967:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7968:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7969:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7970:                  );
 7971:     if ($dom) {
 7972:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7973:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7974:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7975:         $protocol = 'http' if ($protocol ne 'https');
 7976:         if ($uint_dom) {
 7977:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7978:                                          $uint_dom);
 7979:         }
 7980:     }
 7981:     return (\%titles);
 7982: }
 7983: 
 7984: sub print_scantron {
 7985:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 7986:     if ($position eq 'top') {
 7987:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 7988:     } else {
 7989:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 7990:     }
 7991: }
 7992: 
 7993: sub scantron_javascript {
 7994:     return <<"ENDSCRIPT";
 7995: 
 7996: <script type="text/javascript">
 7997: // <![CDATA[
 7998: 
 7999: function toggleScantron(form) {
 8000:     var csvfieldset = new Array();
 8001:     if (document.getElementById('scantroncsv_cols')) {
 8002:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 8003:     }
 8004:     if (document.getElementById('scantroncsv_options')) {
 8005:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 8006:     }
 8007:     if (csvfieldset.length) {
 8008:         if (document.getElementById('scantronconfcsv')) {
 8009:             var scantroncsv = document.getElementById('scantronconfcsv');
 8010:             if (scantroncsv.checked) {
 8011:                 for (var i=0; i<csvfieldset.length; i++) {
 8012:                     csvfieldset[i].style.display = 'block';
 8013:                 }
 8014:             } else {
 8015:                 for (var i=0; i<csvfieldset.length; i++) {
 8016:                     csvfieldset[i].style.display = 'none';
 8017:                 }
 8018:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 8019:                 if (csvselects.length) {
 8020:                     for (var j=0; j<csvselects.length; j++) {
 8021:                         csvselects[j].selectedIndex = 0;
 8022:                     }
 8023:                 }
 8024:             }
 8025:         }
 8026:     }
 8027:     return;
 8028: }
 8029: // ]]>
 8030: </script>
 8031: 
 8032: ENDSCRIPT
 8033: 
 8034: }
 8035: 
 8036: sub print_scantronformat {
 8037:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 8038:     my $itemcount = 1;
 8039:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 8040:         %confhash);
 8041:     my $switchserver = &check_switchserver($dom,$confname);
 8042:     my %lt = &Apache::lonlocal::texthash (
 8043:                 default => 'Default bubblesheet format file error',
 8044:                 custom  => 'Custom bubblesheet format file error',
 8045:              );
 8046:     my %scantronfiles = (
 8047:         default => 'default.tab',
 8048:         custom => 'custom.tab',
 8049:     );
 8050:     foreach my $key (keys(%scantronfiles)) {
 8051:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 8052:                               .$scantronfiles{$key};
 8053:     }
 8054:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 8055:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 8056:         if (!$switchserver) {
 8057:             my $servadm = $r->dir_config('lonAdmEMail');
 8058:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 8059:             if ($configuserok eq 'ok') {
 8060:                 if ($author_ok eq 'ok') {
 8061:                     my %legacyfile = (
 8062:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 8063:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 8064:                     );
 8065:                     my %md5chk;
 8066:                     foreach my $type (keys(%legacyfile)) {
 8067:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 8068:                         chomp($md5chk{$type});
 8069:                     }
 8070:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 8071:                         foreach my $type (keys(%legacyfile)) {
 8072:                             ($scantronurls{$type},my $error) =
 8073:                                 &legacy_scantronformat($r,$dom,$confname,
 8074:                                                  $type,$legacyfile{$type},
 8075:                                                  $scantronurls{$type},
 8076:                                                  $scantronfiles{$type});
 8077:                             if ($error ne '') {
 8078:                                 $error{$type} = $error;
 8079:                             }
 8080:                         }
 8081:                         if (keys(%error) == 0) {
 8082:                             $is_custom = 1;
 8083:                             $confhash{'scantron'}{'scantronformat'} =
 8084:                                 $scantronurls{'custom'};
 8085:                             my $putresult =
 8086:                                 &Apache::lonnet::put_dom('configuration',
 8087:                                                          \%confhash,$dom);
 8088:                             if ($putresult ne 'ok') {
 8089:                                 $error{'custom'} =
 8090:                                     '<span class="LC_error">'.
 8091:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8092:                             }
 8093:                         }
 8094:                     } else {
 8095:                         ($scantronurls{'default'},my $error) =
 8096:                             &legacy_scantronformat($r,$dom,$confname,
 8097:                                           'default',$legacyfile{'default'},
 8098:                                           $scantronurls{'default'},
 8099:                                           $scantronfiles{'default'});
 8100:                         if ($error eq '') {
 8101:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 8102:                             my $putresult =
 8103:                                 &Apache::lonnet::put_dom('configuration',
 8104:                                                          \%confhash,$dom);
 8105:                             if ($putresult ne 'ok') {
 8106:                                 $error{'default'} =
 8107:                                     '<span class="LC_error">'.
 8108:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8109:                             }
 8110:                         } else {
 8111:                             $error{'default'} = $error;
 8112:                         }
 8113:                     }
 8114:                 }
 8115:             }
 8116:         } else {
 8117:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 8118:         }
 8119:     }
 8120:     if (ref($settings) eq 'HASH') {
 8121:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 8122:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 8123:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 8124:                 $scantronurl = '';
 8125:             } else {
 8126:                 $scantronurl = $settings->{'scantronformat'};
 8127:             }
 8128:             $is_custom = 1;
 8129:         } else {
 8130:             $scantronurl = $scantronurls{'default'};
 8131:         }
 8132:     } else {
 8133:         if ($is_custom) {
 8134:             $scantronurl = $scantronurls{'custom'};
 8135:         } else {
 8136:             $scantronurl = $scantronurls{'default'};
 8137:         }
 8138:     }
 8139:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8140:     $datatable .= '<tr'.$css_class.'>';
 8141:     if (!$is_custom) {
 8142:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 8143:                       '<span class="LC_nobreak">';
 8144:         if ($scantronurl) {
 8145:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 8146:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 8147:         } else {
 8148:             $datatable = &mt('File unavailable for display');
 8149:         }
 8150:         $datatable .= '</span></td>';
 8151:         if (keys(%error) == 0) { 
 8152:             $datatable .= '<td style="vertical-align: bottom">';
 8153:             if (!$switchserver) {
 8154:                 $datatable .= &mt('Upload:').'<br />';
 8155:             }
 8156:         } else {
 8157:             my $errorstr;
 8158:             foreach my $key (sort(keys(%error))) {
 8159:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8160:             }
 8161:             $datatable .= '<td>'.$errorstr;
 8162:         }
 8163:     } else {
 8164:         if (keys(%error) > 0) {
 8165:             my $errorstr;
 8166:             foreach my $key (sort(keys(%error))) {
 8167:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8168:             } 
 8169:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 8170:         } elsif ($scantronurl) {
 8171:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 8172:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8173:             $datatable .= '<td><span class="LC_nobreak">'.
 8174:                           $link.
 8175:                           '<label><input type="checkbox" name="scantronformat_del"'.
 8176:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 8177:                           '<td><span class="LC_nobreak">&nbsp;'.
 8178:                           &mt('Replace:').'</span><br />';
 8179:         }
 8180:     }
 8181:     if (keys(%error) == 0) {
 8182:         if ($switchserver) {
 8183:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 8184:         } else {
 8185:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8186:                          '<input type="file" name="scantronformat" /></span>';
 8187:         }
 8188:     }
 8189:     $datatable .= '</td></tr>';
 8190:     $$rowtotal ++;
 8191:     return $datatable;
 8192: }
 8193: 
 8194: sub legacy_scantronformat {
 8195:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 8196:     my ($url,$error);
 8197:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 8198:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 8199:         (my $result,$url) =
 8200:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 8201:                          '','',$newfile);
 8202:         if ($result ne 'ok') {
 8203:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 8204:         }
 8205:     }
 8206:     return ($url,$error);
 8207: }
 8208: 
 8209: sub print_scantronconfig {
 8210:     my ($dom,$settings,$rowtotal) = @_;
 8211:     my $itemcount = 2;
 8212:     my $is_checked = ' checked="checked"';
 8213:     my %optionson = (
 8214:                      hdr => ' checked="checked"',
 8215:                      pad => ' checked="checked"',
 8216:                      rem => ' checked="checked"',
 8217:                     );
 8218:     my %optionsoff = (
 8219:                       hdr => '',
 8220:                       pad => '',
 8221:                       rem => '',
 8222:                      );
 8223:     my $currcsvsty = 'none';
 8224:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 8225:     my @fields = &scantroncsv_fields();
 8226:     my %titles = &scantronconfig_titles();
 8227:     if (ref($settings) eq 'HASH') {
 8228:         if (ref($settings->{config}) eq 'HASH') {
 8229:             if ($settings->{config}->{dat}) {
 8230:                 $checked{'dat'} = $is_checked;
 8231:             }
 8232:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 8233:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 8234:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 8235:                     if (keys(%csvfields) > 0) {
 8236:                         $checked{'csv'} = $is_checked;
 8237:                         $currcsvsty = 'block';
 8238:                     }
 8239:                 }
 8240:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 8241:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 8242:                     foreach my $option (keys(%optionson)) {
 8243:                         unless ($csvoptions{$option}) {
 8244:                             $optionsoff{$option} = $optionson{$option};
 8245:                             $optionson{$option} = '';
 8246:                         }
 8247:                     }
 8248:                 }
 8249:             }
 8250:         } else {
 8251:             $checked{'dat'} = $is_checked;
 8252:         }
 8253:     } else {
 8254:         $checked{'dat'} = $is_checked;
 8255:     }
 8256:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 8257:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8258:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 8259:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 8260:     foreach my $item ('dat','csv') {
 8261:         my $id;
 8262:         if ($item eq 'csv') {
 8263:             $id = 'id="scantronconfcsv" ';
 8264:         }
 8265:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 8266:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 8267:         if ($item eq 'csv') {
 8268:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 8269:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 8270:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 8271:             foreach my $col (@fields) {
 8272:                 my $selnone;
 8273:                 if ($csvfields{$col} eq '') {
 8274:                     $selnone = ' selected="selected"';
 8275:                 }
 8276:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 8277:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 8278:                               '<option value=""'.$selnone.'></option>';
 8279:                 for (my $i=0; $i<20; $i++) {
 8280:                     my $shown = $i+1;
 8281:                     my $sel;
 8282:                     unless ($selnone) {
 8283:                         if (exists($csvfields{$col})) {
 8284:                             if ($csvfields{$col} == $i) {
 8285:                                 $sel = ' selected="selected"';
 8286:                             }
 8287:                         }
 8288:                     }
 8289:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 8290:                 }
 8291:                 $datatable .= '</select></td></tr>';
 8292:            }
 8293:            $datatable .= '</table></fieldset>'.
 8294:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 8295:                          '<legend>'.&mt('CSV Options').'</legend>';
 8296:            foreach my $option ('hdr','pad','rem') {
 8297:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 8298:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 8299:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 8300:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 8301:            }
 8302:            $datatable .= '</fieldset>';
 8303:            $itemcount ++;
 8304:         }
 8305:     }
 8306:     $datatable .= '</td></tr>';
 8307:     $$rowtotal ++;
 8308:     return $datatable;
 8309: }
 8310: 
 8311: sub scantronconfig_titles {
 8312:     return &Apache::lonlocal::texthash(
 8313:                                           dat => 'Standard format (.dat)',
 8314:                                           csv => 'Comma separated values (.csv)',
 8315:                                           hdr => 'Remove first line in file (contains column titles)',
 8316:                                           pad => 'Prepend 0s to PaperID',
 8317:                                           rem => 'Remove leading spaces (except Question Response columns)',
 8318:                                           CODE => 'CODE',
 8319:                                           ID   => 'Student ID',
 8320:                                           PaperID => 'Paper ID',
 8321:                                           FirstName => 'First Name',
 8322:                                           LastName => 'Last Name',
 8323:                                           FirstQuestion => 'First Question Response',
 8324:                                           Section => 'Section',
 8325:     );
 8326: }
 8327: 
 8328: sub scantroncsv_fields {
 8329:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 8330: }
 8331: 
 8332: sub print_coursecategories {
 8333:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 8334:     my $datatable;
 8335:     if ($position eq 'top') {
 8336:         my (%checked);
 8337:         my @catitems = ('unauth','auth');
 8338:         my @cattypes = ('std','domonly','codesrch','none');
 8339:         $checked{'unauth'} = 'std';
 8340:         $checked{'auth'} = 'std';
 8341:         if (ref($settings) eq 'HASH') {
 8342:             foreach my $type (@cattypes) {
 8343:                 if ($type eq $settings->{'unauth'}) {
 8344:                     $checked{'unauth'} = $type;
 8345:                 }
 8346:                 if ($type eq $settings->{'auth'}) {
 8347:                     $checked{'auth'} = $type;
 8348:                 }
 8349:             }
 8350:         }
 8351:         my %lt = &Apache::lonlocal::texthash (
 8352:                                                unauth   => 'Catalog type for unauthenticated users',
 8353:                                                auth     => 'Catalog type for authenticated users',
 8354:                                                none     => 'No catalog',
 8355:                                                std      => 'Standard catalog',
 8356:                                                domonly  => 'Domain-only catalog',
 8357:                                                codesrch => "Code search form",
 8358:                                              );
 8359:        my $itemcount = 0;
 8360:        foreach my $item (@catitems) {
 8361:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8362:            $datatable .= '<tr '.$css_class.'>'.
 8363:                          '<td>'.$lt{$item}.'</td>'.
 8364:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 8365:            foreach my $type (@cattypes) {
 8366:                my $ischecked;
 8367:                if ($checked{$item} eq $type) {
 8368:                    $ischecked=' checked="checked"';
 8369:                }
 8370:                $datatable .= '<label>'.
 8371:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 8372:                              ' />'.$lt{$type}.'</label>&nbsp;';
 8373:            }
 8374:            $datatable .= '</span></td></tr>';
 8375:            $itemcount ++;
 8376:         }
 8377:         $$rowtotal += $itemcount;
 8378:     } elsif ($position eq 'middle') {
 8379:         my $toggle_cats_crs = ' ';
 8380:         my $toggle_cats_dom = ' checked="checked" ';
 8381:         my $can_cat_crs = ' ';
 8382:         my $can_cat_dom = ' checked="checked" ';
 8383:         my $toggle_catscomm_comm = ' ';
 8384:         my $toggle_catscomm_dom = ' checked="checked" ';
 8385:         my $can_catcomm_comm = ' ';
 8386:         my $can_catcomm_dom = ' checked="checked" ';
 8387:         my $toggle_catsplace_place = ' ';
 8388:         my $toggle_catsplace_dom = ' checked="checked" ';
 8389:         my $can_catplace_place = ' ';
 8390:         my $can_catplace_dom = ' checked="checked" ';
 8391: 
 8392:         if (ref($settings) eq 'HASH') {
 8393:             if ($settings->{'togglecats'} eq 'crs') {
 8394:                 $toggle_cats_crs = $toggle_cats_dom;
 8395:                 $toggle_cats_dom = ' ';
 8396:             }
 8397:             if ($settings->{'categorize'} eq 'crs') {
 8398:                 $can_cat_crs = $can_cat_dom;
 8399:                 $can_cat_dom = ' ';
 8400:             }
 8401:             if ($settings->{'togglecatscomm'} eq 'comm') {
 8402:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 8403:                 $toggle_catscomm_dom = ' ';
 8404:             }
 8405:             if ($settings->{'categorizecomm'} eq 'comm') {
 8406:                 $can_catcomm_comm = $can_catcomm_dom;
 8407:                 $can_catcomm_dom = ' ';
 8408:             }
 8409:             if ($settings->{'togglecatsplace'} eq 'place') {
 8410:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 8411:                 $toggle_catsplace_dom = ' ';
 8412:             }
 8413:             if ($settings->{'categorizeplace'} eq 'place') {
 8414:                 $can_catplace_place = $can_catplace_dom;
 8415:                 $can_catplace_dom = ' ';
 8416:             }
 8417:         }
 8418:         my %title = &Apache::lonlocal::texthash (
 8419:                      togglecats      => 'Show/Hide a course in catalog',
 8420:                      togglecatscomm  => 'Show/Hide a community in catalog',
 8421:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 8422:                      categorize      => 'Assign a category to a course',
 8423:                      categorizecomm  => 'Assign a category to a community',
 8424:                      categorizeplace => 'Assign a category to a placement test',
 8425:                     );
 8426:         my %level = &Apache::lonlocal::texthash (
 8427:                      dom   => 'Set in Domain',
 8428:                      crs   => 'Set in Course',
 8429:                      comm  => 'Set in Community',
 8430:                      place => 'Set in Placement Test',
 8431:                     );
 8432:         $datatable = '<tr class="LC_odd_row">'.
 8433:                   '<td>'.$title{'togglecats'}.'</td>'.
 8434:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8435:                   '<input type="radio" name="togglecats"'.
 8436:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8437:                   '<label><input type="radio" name="togglecats"'.
 8438:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8439:                   '</tr><tr>'.
 8440:                   '<td>'.$title{'categorize'}.'</td>'.
 8441:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8442:                   '<label><input type="radio" name="categorize"'.
 8443:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8444:                   '<label><input type="radio" name="categorize"'.
 8445:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8446:                   '</tr><tr class="LC_odd_row">'.
 8447:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 8448:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8449:                   '<input type="radio" name="togglecatscomm"'.
 8450:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8451:                   '<label><input type="radio" name="togglecatscomm"'.
 8452:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8453:                   '</tr><tr>'.
 8454:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 8455:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8456:                   '<label><input type="radio" name="categorizecomm"'.
 8457:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8458:                   '<label><input type="radio" name="categorizecomm"'.
 8459:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8460:                   '</tr><tr class="LC_odd_row">'.
 8461:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 8462:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8463:                   '<input type="radio" name="togglecatsplace"'.
 8464:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8465:                   '<label><input type="radio" name="togglecatscomm"'.
 8466:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 8467:                   '</tr><tr>'.
 8468:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 8469:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8470:                   '<label><input type="radio" name="categorizeplace"'.
 8471:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8472:                   '<label><input type="radio" name="categorizeplace"'.
 8473:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 8474:                   '</tr>';
 8475:         $$rowtotal += 6;
 8476:     } else {
 8477:         my $css_class;
 8478:         my $itemcount = 1;
 8479:         my $cathash; 
 8480:         if (ref($settings) eq 'HASH') {
 8481:             $cathash = $settings->{'cats'};
 8482:         }
 8483:         if (ref($cathash) eq 'HASH') {
 8484:             my (@cats,@trails,%allitems,%idx,@jsarray);
 8485:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 8486:                                                    \%allitems,\%idx,\@jsarray);
 8487:             my $maxdepth = scalar(@cats);
 8488:             my $colattrib = '';
 8489:             if ($maxdepth > 2) {
 8490:                 $colattrib = ' colspan="2" ';
 8491:             }
 8492:             my @path;
 8493:             if (@cats > 0) {
 8494:                 if (ref($cats[0]) eq 'ARRAY') {
 8495:                     my $numtop = @{$cats[0]};
 8496:                     my $maxnum = $numtop;
 8497:                     my %default_names = (
 8498:                           instcode    => &mt('Official courses'),
 8499:                           communities => &mt('Communities'),
 8500:                           placement   => &mt('Placement Tests'),
 8501:                     );
 8502: 
 8503:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 8504:                         ($cathash->{'instcode::0'} eq '') ||
 8505:                         (!grep(/^communities$/,@{$cats[0]})) || 
 8506:                         ($cathash->{'communities::0'} eq '') ||
 8507:                         (!grep(/^placement$/,@{$cats[0]})) ||
 8508:                         ($cathash->{'placement::0'} eq '')) {
 8509:                         $maxnum ++;
 8510:                     }
 8511:                     my $lastidx;
 8512:                     for (my $i=0; $i<$numtop; $i++) {
 8513:                         my $parent = $cats[0][$i];
 8514:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8515:                         my $item = &escape($parent).'::0';
 8516:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 8517:                         $lastidx = $idx{$item};
 8518:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8519:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 8520:                         for (my $k=0; $k<=$maxnum; $k++) {
 8521:                             my $vpos = $k+1;
 8522:                             my $selstr;
 8523:                             if ($k == $i) {
 8524:                                 $selstr = ' selected="selected" ';
 8525:                             }
 8526:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8527:                         }
 8528:                         $datatable .= '</select></span></td><td>';
 8529:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 8530:                             $datatable .=  '<span class="LC_nobreak">'
 8531:                                            .$default_names{$parent}.'</span>';
 8532:                             if ($parent eq 'instcode') {
 8533:                                 $datatable .= '<br /><span class="LC_nobreak">('
 8534:                                               .&mt('with institutional codes')
 8535:                                               .')</span></td><td'.$colattrib.'>';
 8536:                             } else {
 8537:                                 $datatable .= '<table><tr><td>';
 8538:                             }
 8539:                             $datatable .= '<span class="LC_nobreak">'
 8540:                                           .'<label><input type="radio" name="'
 8541:                                           .$parent.'" value="1" checked="checked" />'
 8542:                                           .&mt('Display').'</label>';
 8543:                             if ($parent eq 'instcode') {
 8544:                                 $datatable .= '&nbsp;';
 8545:                             } else {
 8546:                                 $datatable .= '</span></td></tr><tr><td>'
 8547:                                               .'<span class="LC_nobreak">';
 8548:                             }
 8549:                             $datatable .= '<label><input type="radio" name="'
 8550:                                           .$parent.'" value="0" />'
 8551:                                           .&mt('Do not display').'</label></span>';
 8552:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 8553:                                 $datatable .= '</td></tr></table>';
 8554:                             }
 8555:                             $datatable .= '</td>';
 8556:                         } else {
 8557:                             $datatable .= $parent
 8558:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 8559:                                           .'<input type="checkbox" name="deletecategory" '
 8560:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 8561:                         }
 8562:                         my $depth = 1;
 8563:                         push(@path,$parent);
 8564:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 8565:                         pop(@path);
 8566:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 8567:                         $itemcount ++;
 8568:                     }
 8569:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8570:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 8571:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 8572:                     for (my $k=0; $k<=$maxnum; $k++) {
 8573:                         my $vpos = $k+1;
 8574:                         my $selstr;
 8575:                         if ($k == $numtop) {
 8576:                             $selstr = ' selected="selected" ';
 8577:                         }
 8578:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8579:                     }
 8580:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 8581:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 8582:                                   .'</tr>'."\n";
 8583:                     $itemcount ++;
 8584:                     foreach my $default ('instcode','communities','placement') {
 8585:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 8586:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8587:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 8588:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 8589:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 8590:                             for (my $k=0; $k<=$maxnum; $k++) {
 8591:                                 my $vpos = $k+1;
 8592:                                 my $selstr;
 8593:                                 if ($k == $maxnum) {
 8594:                                     $selstr = ' selected="selected" ';
 8595:                                 }
 8596:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8597:                             }
 8598:                             $datatable .= '</select></span></td>'.
 8599:                                           '<td><span class="LC_nobreak">'.
 8600:                                           $default_names{$default}.'</span>';
 8601:                             if ($default eq 'instcode') {
 8602:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 8603:                                               .&mt('with institutional codes').')</span>';
 8604:                             }
 8605:                             $datatable .= '</td>'
 8606:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 8607:                                           .&mt('Display').'</label>&nbsp;'
 8608:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 8609:                                           .&mt('Do not display').'</label></span></td></tr>';
 8610:                         }
 8611:                     }
 8612:                 }
 8613:             } else {
 8614:                 $datatable .= &initialize_categories($itemcount);
 8615:             }
 8616:         } else {
 8617:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 8618:                           .&initialize_categories($itemcount);
 8619:         }
 8620:         $$rowtotal += $itemcount;
 8621:     }
 8622:     return $datatable;
 8623: }
 8624: 
 8625: sub print_serverstatuses {
 8626:     my ($dom,$settings,$rowtotal) = @_;
 8627:     my $datatable;
 8628:     my @pages = &serverstatus_pages();
 8629:     my (%namedaccess,%machineaccess);
 8630:     foreach my $type (@pages) {
 8631:         $namedaccess{$type} = '';
 8632:         $machineaccess{$type}= '';
 8633:     }
 8634:     if (ref($settings) eq 'HASH') {
 8635:         foreach my $type (@pages) {
 8636:             if (exists($settings->{$type})) {
 8637:                 if (ref($settings->{$type}) eq 'HASH') {
 8638:                     foreach my $key (keys(%{$settings->{$type}})) {
 8639:                         if ($key eq 'namedusers') {
 8640:                             $namedaccess{$type} = $settings->{$type}->{$key};
 8641:                         } elsif ($key eq 'machines') {
 8642:                             $machineaccess{$type} = $settings->{$type}->{$key};
 8643:                         }
 8644:                     }
 8645:                 }
 8646:             }
 8647:         }
 8648:     }
 8649:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8650:     my $rownum = 0;
 8651:     my $css_class;
 8652:     foreach my $type (@pages) {
 8653:         $rownum ++;
 8654:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 8655:         $datatable .= '<tr'.$css_class.'>'.
 8656:                       '<td><span class="LC_nobreak">'.
 8657:                       $titles->{$type}.'</span></td>'.
 8658:                       '<td class="LC_left_item">'.
 8659:                       '<input type="text" name="'.$type.'_namedusers" '.
 8660:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 8661:                       '<td class="LC_right_item">'.
 8662:                       '<span class="LC_nobreak">'.
 8663:                       '<input type="text" name="'.$type.'_machines" '.
 8664:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 8665:                       '</span></td></tr>'."\n";
 8666:     }
 8667:     $$rowtotal += $rownum;
 8668:     return $datatable;
 8669: }
 8670: 
 8671: sub serverstatus_pages {
 8672:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 8673:             'checksums','clusterstatus','certstatus','metadata_keywords',
 8674:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 8675:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 8676: }
 8677: 
 8678: sub defaults_javascript {
 8679:     my ($settings) = @_;
 8680:     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.');
 8681:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 8682:     &js_escape(\$intauthcheck);
 8683:     &js_escape(\$intauthcost);
 8684:     my $intauthjs = <<"ENDSCRIPT";
 8685: 
 8686: function warnIntAuth(field) {
 8687:     if (field.name == 'intauth_check') {
 8688:         if (field.value == '2') {
 8689:             alert('$intauthcheck');
 8690:         }
 8691:     }
 8692:     if (field.name == 'intauth_cost') {
 8693:         field.value.replace(/\s/g,'');
 8694:         if (field.value != '') {
 8695:             var regexdigit=/^\\d+\$/;
 8696:             if (!regexdigit.test(field.value)) {
 8697:                 alert('$intauthcost');
 8698:             }
 8699:         }
 8700:     }
 8701:     return;
 8702: }
 8703: 
 8704: ENDSCRIPT
 8705: 
 8706:     if (ref($settings) ne 'HASH') {
 8707:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8708:     }
 8709:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 8710:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 8711:         if ($maxnum eq '') {
 8712:             $maxnum = 0;
 8713:         }
 8714:         $maxnum ++;
 8715:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 8716:         return <<"ENDSCRIPT";
 8717: <script type="text/javascript">
 8718: // <![CDATA[
 8719: function reorderTypes(form,caller) {
 8720:     var changedVal;
 8721: $jstext 
 8722:     var newpos = 'addinststatus_pos';
 8723:     var current = new Array;
 8724:     var maxh = $maxnum;
 8725:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8726:     var oldVal;
 8727:     if (caller == newpos) {
 8728:         changedVal = newitemVal;
 8729:     } else {
 8730:         var curritem = 'inststatus_pos_'+caller;
 8731:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 8732:         current[newitemVal] = newpos;
 8733:     }
 8734:     for (var i=0; i<inststatuses.length; i++) {
 8735:         if (inststatuses[i] != caller) {
 8736:             var elementName = 'inststatus_pos_'+inststatuses[i];
 8737:             if (form.elements[elementName]) {
 8738:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8739:                 current[currVal] = elementName;
 8740:             }
 8741:         }
 8742:     }
 8743:     for (var j=0; j<maxh; j++) {
 8744:         if (current[j] == undefined) {
 8745:             oldVal = j;
 8746:         }
 8747:     }
 8748:     if (oldVal < changedVal) {
 8749:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8750:            var elementName = current[k];
 8751:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8752:         }
 8753:     } else {
 8754:         for (var k=changedVal; k<oldVal; k++) {
 8755:             var elementName = current[k];
 8756:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8757:         }
 8758:     }
 8759:     return;
 8760: }
 8761: 
 8762: $intauthjs
 8763: 
 8764: // ]]>
 8765: </script>
 8766: 
 8767: ENDSCRIPT
 8768:     } else {
 8769:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8770:     }
 8771: }
 8772: 
 8773: sub coursecategories_javascript {
 8774:     my ($settings) = @_;
 8775:     my ($output,$jstext,$cathash);
 8776:     if (ref($settings) eq 'HASH') {
 8777:         $cathash = $settings->{'cats'};
 8778:     }
 8779:     if (ref($cathash) eq 'HASH') {
 8780:         my (@cats,@jsarray,%idx);
 8781:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 8782:         if (@jsarray > 0) {
 8783:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 8784:             for (my $i=0; $i<@jsarray; $i++) {
 8785:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 8786:                     my $catstr = join('","',@{$jsarray[$i]});
 8787:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 8788:                 }
 8789:             }
 8790:         }
 8791:     } else {
 8792:         $jstext  = '    var categories = Array(1);'."\n".
 8793:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 8794:     }
 8795:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 8796:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 8797:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 8798:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 8799:     &js_escape(\$instcode_reserved);
 8800:     &js_escape(\$communities_reserved);
 8801:     &js_escape(\$placement_reserved);
 8802:     &js_escape(\$choose_again);
 8803:     $output = <<"ENDSCRIPT";
 8804: <script type="text/javascript">
 8805: // <![CDATA[
 8806: function reorderCats(form,parent,item,idx) {
 8807:     var changedVal;
 8808: $jstext
 8809:     var newpos = 'addcategory_pos';
 8810:     if (parent == '') {
 8811:         var has_instcode = 0;
 8812:         var maxtop = categories[idx].length;
 8813:         for (var j=0; j<maxtop; j++) {
 8814:             if (categories[idx][j] == 'instcode::0') {
 8815:                 has_instcode == 1;
 8816:             }
 8817:         }
 8818:         if (has_instcode == 0) {
 8819:             categories[idx][maxtop] = 'instcode_pos';
 8820:         }
 8821:     } else {
 8822:         newpos += '_'+parent;
 8823:     }
 8824:     var maxh = 1 + categories[idx].length;
 8825:     var current = new Array;
 8826:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8827:     if (item == newpos) {
 8828:         changedVal = newitemVal;
 8829:     } else {
 8830:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 8831:         current[newitemVal] = newpos;
 8832:     }
 8833:     for (var i=0; i<categories[idx].length; i++) {
 8834:         var elementName = categories[idx][i];
 8835:         if (elementName != item) {
 8836:             if (form.elements[elementName]) {
 8837:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8838:                 current[currVal] = elementName;
 8839:             }
 8840:         }
 8841:     }
 8842:     var oldVal;
 8843:     for (var j=0; j<maxh; j++) {
 8844:         if (current[j] == undefined) {
 8845:             oldVal = j;
 8846:         }
 8847:     }
 8848:     if (oldVal < changedVal) {
 8849:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8850:            var elementName = current[k];
 8851:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8852:         }
 8853:     } else {
 8854:         for (var k=changedVal; k<oldVal; k++) {
 8855:             var elementName = current[k];
 8856:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8857:         }
 8858:     }
 8859:     return;
 8860: }
 8861: 
 8862: function categoryCheck(form) {
 8863:     if (form.elements['addcategory_name'].value == 'instcode') {
 8864:         alert('$instcode_reserved\\n$choose_again');
 8865:         return false;
 8866:     }
 8867:     if (form.elements['addcategory_name'].value == 'communities') {
 8868:         alert('$communities_reserved\\n$choose_again');
 8869:         return false;
 8870:     }
 8871:     if (form.elements['addcategory_name'].value == 'placement') {
 8872:         alert('$placement_reserved\\n$choose_again');
 8873:         return false;
 8874:     }
 8875:     return true;
 8876: }
 8877: 
 8878: // ]]>
 8879: </script>
 8880: 
 8881: ENDSCRIPT
 8882:     return $output;
 8883: }
 8884: 
 8885: sub initialize_categories {
 8886:     my ($itemcount) = @_;
 8887:     my ($datatable,$css_class,$chgstr);
 8888:     my %default_names = (
 8889:                       instcode    => 'Official courses (with institutional codes)',
 8890:                       communities => 'Communities',
 8891:                       placement   => 'Placement Tests',
 8892:                         );
 8893:     my %selnum = (
 8894:                    instcode    => '0',
 8895:                    communities => '1',
 8896:                    placement   => '2',
 8897:                  );
 8898:     my %selected;
 8899:     foreach my $default ('instcode','communities','placement') {
 8900:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8901:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 8902:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
 8903:         $selected{$selnum{$default}} = ' selected="selected"';
 8904:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8905:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
 8906:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
 8907:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
 8908:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
 8909:                      .'<option value="3">4</option></select>&nbsp;'
 8910:                      .$default_names{$default}
 8911:                      .'</span></td><td><span class="LC_nobreak">'
 8912:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 8913:                      .&mt('Display').'</label>&nbsp;<label>'
 8914:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 8915:                  .'</label></span></td></tr>';
 8916:         $itemcount ++;
 8917:     }
 8918:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8919:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 8920:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8921:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 8922:                   .'<option value="0">1</option>'
 8923:                   .'<option value="1">2</option>'
 8924:                   .'<option value="2">3</option>'
 8925:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
 8926:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 8927:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 8928:                   .'</td></tr>';
 8929:     return $datatable;
 8930: }
 8931: 
 8932: sub build_category_rows {
 8933:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 8934:     my ($text,$name,$item,$chgstr);
 8935:     if (ref($cats) eq 'ARRAY') {
 8936:         my $maxdepth = scalar(@{$cats});
 8937:         if (ref($cats->[$depth]) eq 'HASH') {
 8938:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8939:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8940:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8941:                 $text .= '<td><table class="LC_data_table">';
 8942:                 my ($idxnum,$parent_name,$parent_item);
 8943:                 my $higher = $depth - 1;
 8944:                 if ($higher == 0) {
 8945:                     $parent_name = &escape($parent).'::'.$higher;
 8946:                 } else {
 8947:                     if (ref($path) eq 'ARRAY') {
 8948:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8949:                     }
 8950:                 }
 8951:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8952:                 for (my $j=0; $j<=$numchildren; $j++) {
 8953:                     if ($j < $numchildren) {
 8954:                         $name = $cats->[$depth]{$parent}[$j];
 8955:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8956:                         $idxnum = $idx->{$item};
 8957:                     } else {
 8958:                         $name = $parent_name;
 8959:                         $item = $parent_item;
 8960:                     }
 8961:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8962:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8963:                     for (my $i=0; $i<=$numchildren; $i++) {
 8964:                         my $vpos = $i+1;
 8965:                         my $selstr;
 8966:                         if ($j == $i) {
 8967:                             $selstr = ' selected="selected" ';
 8968:                         }
 8969:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8970:                     }
 8971:                     $text .= '</select>&nbsp;';
 8972:                     if ($j < $numchildren) {
 8973:                         my $deeper = $depth+1;
 8974:                         $text .= $name.'&nbsp;'
 8975:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8976:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8977:                         if(ref($path) eq 'ARRAY') {
 8978:                             push(@{$path},$name);
 8979:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8980:                             pop(@{$path});
 8981:                         }
 8982:                     } else {
 8983:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 8984:                         if ($j == $numchildren) {
 8985:                             $text .= $name;
 8986:                         } else {
 8987:                             $text .= $item;
 8988:                         }
 8989:                         $text .= '" value="" />';
 8990:                     }
 8991:                     $text .= '</td></tr>';
 8992:                 }
 8993:                 $text .= '</table></td>';
 8994:             } else {
 8995:                 my $higher = $depth-1;
 8996:                 if ($higher == 0) {
 8997:                     $name = &escape($parent).'::'.$higher;
 8998:                 } else {
 8999:                     if (ref($path) eq 'ARRAY') {
 9000:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 9001:                     }
 9002:                 }
 9003:                 my $colspan;
 9004:                 if ($parent ne 'instcode') {
 9005:                     $colspan = $maxdepth - $depth - 1;
 9006:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 9007:                 }
 9008:             }
 9009:         }
 9010:     }
 9011:     return $text;
 9012: }
 9013: 
 9014: sub modifiable_userdata_row {
 9015:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 9016:         $rowid,$customcss,$rowstyle) = @_;
 9017:     my ($role,$rolename,$statustype);
 9018:     $role = $item;
 9019:     if ($context eq 'cancreate') {
 9020:         if ($item =~ /^(emailusername)_(.+)$/) {
 9021:             $role = $1;
 9022:             $statustype = $2;
 9023:             if (ref($usertypes) eq 'HASH') {
 9024:                 if ($usertypes->{$statustype}) {
 9025:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 9026:                 } else {
 9027:                     $rolename = &mt('Data provided by user');
 9028:                 }
 9029:             }
 9030:         }
 9031:     } elsif ($context eq 'selfcreate') {
 9032:         if (ref($usertypes) eq 'HASH') {
 9033:             $rolename = $usertypes->{$role};
 9034:         } else {
 9035:             $rolename = $role;
 9036:         }
 9037:     } elsif ($context eq 'lti') {
 9038:         $rolename = &mt('Institutional data used (if available)');
 9039:     } else {
 9040:         if ($role eq 'cr') {
 9041:             $rolename = &mt('Custom role');
 9042:         } else {
 9043:             $rolename = &Apache::lonnet::plaintext($role);
 9044:         }
 9045:     }
 9046:     my (@fields,%fieldtitles);
 9047:     if (ref($fieldsref) eq 'ARRAY') {
 9048:         @fields = @{$fieldsref};
 9049:     } else {
 9050:         @fields = ('lastname','firstname','middlename','generation',
 9051:                    'permanentemail','id');
 9052:     }
 9053:     if ((ref($titlesref) eq 'HASH')) {
 9054:         %fieldtitles = %{$titlesref};
 9055:     } else {
 9056:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9057:     }
 9058:     my $output;
 9059:     my $css_class;
 9060:     if ($rowcount%2) {
 9061:         $css_class = 'LC_odd_row';
 9062:     }
 9063:     if ($customcss) {
 9064:         $css_class .= " $customcss";
 9065:     }
 9066:     $css_class =~ s/^\s+//;
 9067:     if ($css_class) {
 9068:         $css_class = ' class="'.$css_class.'"';
 9069:     }
 9070:     if ($rowstyle) {
 9071:         $css_class .= ' style="'.$rowstyle.'"';
 9072:     }
 9073:     if ($rowid) {
 9074:         $rowid = ' id="'.$rowid.'"';
 9075:     }
 9076:     $output = '<tr '.$css_class.$rowid.'>'.
 9077:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 9078:               '<td class="LC_left_item" colspan="2"><table>';
 9079:     my $rem;
 9080:     my %checks;
 9081:     my %current;
 9082:     if (ref($settings) eq 'HASH') {
 9083:         my $hashref;
 9084:         if ($context eq 'lti') {
 9085:             if (ref($settings) eq 'HASH') {
 9086:                 $hashref = $settings->{'instdata'};
 9087:             }
 9088:         } elsif (ref($settings->{$context}) eq 'HASH') {
 9089:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 9090:                 $hashref = $settings->{'lti_instdata'};
 9091:             }
 9092:             if ($role eq 'emailusername') {
 9093:                 if ($statustype) {
 9094:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 9095:                         $hashref = $settings->{$context}->{$role}->{$statustype};
 9096:                     }
 9097:                 }
 9098:             }
 9099:         }
 9100:         if (ref($hashref) eq 'HASH') { 
 9101:             foreach my $field (@fields) {
 9102:                 if ($hashref->{$field}) {
 9103:                     if ($role eq 'emailusername') {
 9104:                         $checks{$field} = $hashref->{$field};
 9105:                     } else {
 9106:                         $checks{$field} = ' checked="checked" ';
 9107:                     }
 9108:                 }
 9109:             }
 9110:         }
 9111:     }
 9112:  
 9113:     my $total = scalar(@fields);
 9114:     for (my $i=0; $i<$total; $i++) {
 9115:         $rem = $i%($numinrow);
 9116:         if ($rem == 0) {
 9117:             if ($i > 0) {
 9118:                 $output .= '</tr>';
 9119:             }
 9120:             $output .= '<tr>';
 9121:         }
 9122:         my $check = ' ';
 9123:         unless ($role eq 'emailusername') {
 9124:             if (exists($checks{$fields[$i]})) {
 9125:                 $check = $checks{$fields[$i]}
 9126:             } elsif ($context ne 'lti') {
 9127:                 if ($role eq 'st') {
 9128:                     if (ref($settings) ne 'HASH') {
 9129:                         $check = ' checked="checked" '; 
 9130:                     }
 9131:                 }
 9132:             }
 9133:         }
 9134:         $output .= '<td class="LC_left_item">'.
 9135:                    '<span class="LC_nobreak">';
 9136:         my $prefix = 'canmodify';
 9137:         if ($role eq 'emailusername') {
 9138:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 9139:                 $checks{$fields[$i]} = 'omit';
 9140:             }
 9141:             foreach my $option ('required','optional','omit') {
 9142:                 my $checked='';
 9143:                 if ($checks{$fields[$i]} eq $option) {
 9144:                     $checked='checked="checked" ';
 9145:                 }
 9146:                 $output .= '<label>'.
 9147:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 9148:                            &mt($option).'</label>'.('&nbsp;' x2);
 9149:             }
 9150:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 9151:         } else {
 9152:             if ($context eq 'lti') {
 9153:                 $prefix = 'lti';
 9154:             }
 9155:             $output .= '<label>'.
 9156:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
 9157:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 9158:                        '</label>';
 9159:         }
 9160:         $output .= '</span></td>';
 9161:     }
 9162:     $rem = $total%$numinrow;
 9163:     my $colsleft;
 9164:     if ($rem) {
 9165:         $colsleft = $numinrow - $rem;
 9166:     }
 9167:     if ($colsleft > 1) {
 9168:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9169:                    '&nbsp;</td>';
 9170:     } elsif ($colsleft == 1) {
 9171:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9172:     }
 9173:     $output .= '</tr></table></td></tr>';
 9174:     return $output;
 9175: }
 9176: 
 9177: sub insttypes_row {
 9178:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 9179:         $customcss,$rowstyle) = @_;
 9180:     my %lt = &Apache::lonlocal::texthash (
 9181:                       cansearch => 'Users allowed to search',
 9182:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 9183:                       lockablenames => 'User preference to lock name',
 9184:                       selfassign    => 'Self-reportable affiliations',
 9185:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 9186:              );
 9187:     my $showdom;
 9188:     if ($context eq 'cansearch') {
 9189:         $showdom = ' ('.$dom.')';
 9190:     }
 9191:     my $class = 'LC_left_item';
 9192:     if ($context eq 'statustocreate') {
 9193:         $class = 'LC_right_item';
 9194:     }
 9195:     my $css_class;
 9196:     if ($$rowtotal%2) {
 9197:         $css_class = 'LC_odd_row';
 9198:     }
 9199:     if ($customcss) {
 9200:         $css_class .= ' '.$customcss;
 9201:     }
 9202:     $css_class =~ s/^\s+//;
 9203:     if ($css_class) {
 9204:         $css_class = ' class="'.$css_class.'"';
 9205:     }
 9206:     if ($rowstyle) {
 9207:         $css_class .= ' style="'.$rowstyle.'"';
 9208:     }
 9209:     if ($onclick) {
 9210:         $onclick = 'onclick="'.$onclick.'" ';
 9211:     }
 9212:     my $output = '<tr'.$css_class.'>'.
 9213:                  '<td>'.$lt{$context}.$showdom.
 9214:                  '</td><td class="'.$class.'" colspan="2"><table>';
 9215:     my $rem;
 9216:     if (ref($types) eq 'ARRAY') {
 9217:         for (my $i=0; $i<@{$types}; $i++) {
 9218:             if (defined($usertypes->{$types->[$i]})) {
 9219:                 my $rem = $i%($numinrow);
 9220:                 if ($rem == 0) {
 9221:                     if ($i > 0) {
 9222:                         $output .= '</tr>';
 9223:                     }
 9224:                     $output .= '<tr>';
 9225:                 }
 9226:                 my $check = ' ';
 9227:                 if (ref($settings) eq 'HASH') {
 9228:                     if (ref($settings->{$context}) eq 'ARRAY') {
 9229:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 9230:                             $check = ' checked="checked" ';
 9231:                         }
 9232:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 9233:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 9234:                             $check = ' checked="checked" ';
 9235:                         }
 9236:                     } elsif ($context eq 'statustocreate') {
 9237:                         $check = ' checked="checked" ';
 9238:                     }
 9239:                 }
 9240:                 $output .= '<td class="LC_left_item">'.
 9241:                            '<span class="LC_nobreak"><label>'.
 9242:                            '<input type="checkbox" name="'.$context.'" '.
 9243:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 9244:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 9245:             }
 9246:         }
 9247:         $rem = @{$types}%($numinrow);
 9248:     }
 9249:     my $colsleft = $numinrow - $rem;
 9250:     if ($context eq 'overrides') {
 9251:         if ($colsleft > 1) {
 9252:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9253:         } else {
 9254:             $output .= '<td class="LC_left_item">';
 9255:         }
 9256:         $output .= '&nbsp;';  
 9257:     } else {
 9258:         if ($rem == 0) {
 9259:             $output .= '<tr>';
 9260:         }
 9261:         if ($colsleft > 1) {
 9262:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9263:         } else {
 9264:             $output .= '<td class="LC_left_item">';
 9265:         }
 9266:         my $defcheck = ' ';
 9267:         if (ref($settings) eq 'HASH') {  
 9268:             if (ref($settings->{$context}) eq 'ARRAY') {
 9269:                 if (grep(/^default$/,@{$settings->{$context}})) {
 9270:                     $defcheck = ' checked="checked" ';
 9271:                 }
 9272:             } elsif ($context eq 'statustocreate') {
 9273:                 $defcheck = ' checked="checked" ';
 9274:             }
 9275:         }
 9276:         $output .= '<span class="LC_nobreak"><label>'.
 9277:                    '<input type="checkbox" name="'.$context.'" '.
 9278:                    'value="default"'.$defcheck.$onclick.' />'.
 9279:                    $othertitle.'</label></span>';
 9280:     }
 9281:     $output .= '</td></tr></table></td></tr>';
 9282:     return $output;
 9283: }
 9284: 
 9285: sub sorted_searchtitles {
 9286:     my %searchtitles = &Apache::lonlocal::texthash(
 9287:                          'uname' => 'username',
 9288:                          'lastname' => 'last name',
 9289:                          'lastfirst' => 'last name, first name',
 9290:                      );
 9291:     my @titleorder = ('uname','lastname','lastfirst');
 9292:     return (\%searchtitles,\@titleorder);
 9293: }
 9294: 
 9295: sub sorted_searchtypes {
 9296:     my %srchtypes_desc = (
 9297:                            exact    => 'is exact match',
 9298:                            contains => 'contains ..',
 9299:                            begins   => 'begins with ..',
 9300:                          );
 9301:     my @srchtypeorder = ('exact','begins','contains');
 9302:     return (\%srchtypes_desc,\@srchtypeorder);
 9303: }
 9304: 
 9305: sub usertype_update_row {
 9306:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 9307:     my $datatable;
 9308:     my $numinrow = 4;
 9309:     foreach my $type (@{$types}) {
 9310:         if (defined($usertypes->{$type})) {
 9311:             $$rownums ++;
 9312:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 9313:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 9314:                           '</td><td class="LC_left_item"><table>';
 9315:             for (my $i=0; $i<@{$fields}; $i++) {
 9316:                 my $rem = $i%($numinrow);
 9317:                 if ($rem == 0) {
 9318:                     if ($i > 0) {
 9319:                         $datatable .= '</tr>';
 9320:                     }
 9321:                     $datatable .= '<tr>';
 9322:                 }
 9323:                 my $check = ' ';
 9324:                 if (ref($settings) eq 'HASH') {
 9325:                     if (ref($settings->{'fields'}) eq 'HASH') {
 9326:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 9327:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 9328:                                 $check = ' checked="checked" ';
 9329:                             }
 9330:                         }
 9331:                     }
 9332:                 }
 9333: 
 9334:                 if ($i == @{$fields}-1) {
 9335:                     my $colsleft = $numinrow - $rem;
 9336:                     if ($colsleft > 1) {
 9337:                         $datatable .= '<td colspan="'.$colsleft.'">';
 9338:                     } else {
 9339:                         $datatable .= '<td>';
 9340:                     }
 9341:                 } else {
 9342:                     $datatable .= '<td>';
 9343:                 }
 9344:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9345:                               '<input type="checkbox" name="updateable_'.$type.
 9346:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 9347:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 9348:             }
 9349:             $datatable .= '</tr></table></td></tr>';
 9350:         }
 9351:     }
 9352:     return $datatable;
 9353: }
 9354: 
 9355: sub modify_login {
 9356:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9357:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 9358:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 9359:     %title = ( coursecatalog => 'Display course catalog',
 9360:                adminmail => 'Display administrator E-mail address',
 9361:                helpdesk  => 'Display "Contact Helpdesk" link',
 9362:                newuser => 'Link for visitors to create a user account',
 9363:                loginheader => 'Log-in box header');
 9364:     @offon = ('off','on');
 9365:     if (ref($domconfig{login}) eq 'HASH') {
 9366:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 9367:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 9368:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 9369:             }
 9370:         }
 9371:     }
 9372:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 9373:                                            \%domconfig,\%loginhash);
 9374:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9375:     foreach my $item (@toggles) {
 9376:         $loginhash{login}{$item} = $env{'form.'.$item};
 9377:     }
 9378:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 9379:     if (ref($colchanges{'login'}) eq 'HASH') {  
 9380:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 9381:                                          \%loginhash);
 9382:     }
 9383: 
 9384:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9385:     my %domservers = &Apache::lonnet::get_servers($dom);
 9386:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 9387:     if (keys(%servers) > 1) {
 9388:         foreach my $lonhost (keys(%servers)) {
 9389:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 9390:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 9391:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 9392:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 9393:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 9394:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9395:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9396:                         $changes{'loginvia'}{$lonhost} = 1;
 9397:                     } else {
 9398:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 9399:                         $changes{'loginvia'}{$lonhost} = 1;
 9400:                     }
 9401:                 } else {
 9402:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9403:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9404:                         $changes{'loginvia'}{$lonhost} = 1;
 9405:                     }
 9406:                 }
 9407:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 9408:                     foreach my $item (@loginvia_attribs) {
 9409:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 9410:                     }
 9411:                 } else {
 9412:                     foreach my $item (@loginvia_attribs) {
 9413:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9414:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9415:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 9416:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9417:                                 $new = '/';
 9418:                             }
 9419:                         }
 9420:                         if (($item eq 'custompath') && 
 9421:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9422:                             $new = '';
 9423:                         }
 9424:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 9425:                             $changes{'loginvia'}{$lonhost} = 1;
 9426:                         }
 9427:                         if ($item eq 'exempt') {
 9428:                             $new = &check_exempt_addresses($new);
 9429:                         }
 9430:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9431:                     }
 9432:                 }
 9433:             } else {
 9434:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9435:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9436:                     $changes{'loginvia'}{$lonhost} = 1;
 9437:                     foreach my $item (@loginvia_attribs) {
 9438:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9439:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9440:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9441:                                 $new = '/';
 9442:                             }
 9443:                         }
 9444:                         if (($item eq 'custompath') && 
 9445:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9446:                             $new = '';
 9447:                         }
 9448:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9449:                     }
 9450:                 }
 9451:             }
 9452:         }
 9453:     }
 9454: 
 9455:     my $servadm = $r->dir_config('lonAdmEMail');
 9456:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 9457:     if (ref($domconfig{'login'}) eq 'HASH') {
 9458:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 9459:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 9460:                 if ($lang eq 'nolang') {
 9461:                     push(@currlangs,$lang);
 9462:                 } elsif (defined($langchoices{$lang})) {
 9463:                     push(@currlangs,$lang);
 9464:                 } else {
 9465:                     next;
 9466:                 }
 9467:             }
 9468:         }
 9469:     }
 9470:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 9471:     if (@currlangs > 0) {
 9472:         foreach my $lang (@currlangs) {
 9473:             if (grep(/^\Q$lang\E$/,@delurls)) {
 9474:                 $changes{'helpurl'}{$lang} = 1;
 9475:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 9476:                 $changes{'helpurl'}{$lang} = 1;
 9477:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 9478:                 push(@newlangs,$lang);
 9479:             } else {
 9480:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9481:             }
 9482:         }
 9483:     }
 9484:     unless (grep(/^nolang$/,@currlangs)) {
 9485:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 9486:             $changes{'helpurl'}{'nolang'} = 1;
 9487:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 9488:             push(@newlangs,'nolang');
 9489:         }
 9490:     }
 9491:     if ($env{'form.loginhelpurl_add_lang'}) {
 9492:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 9493:             ($env{'form.loginhelpurl_add_file.filename'})) {
 9494:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 9495:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 9496:         }
 9497:     }
 9498:     if ((@newlangs > 0) || ($addedfile)) {
 9499:         my $error;
 9500:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9501:         if ($configuserok eq 'ok') {
 9502:             if ($switchserver) {
 9503:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 9504:             } elsif ($author_ok eq 'ok') {
 9505:                 my @allnew = @newlangs;
 9506:                 if ($addedfile ne '') {
 9507:                     push(@allnew,$addedfile);
 9508:                 }
 9509:                 foreach my $lang (@allnew) {
 9510:                     my $formelem = 'loginhelpurl_'.$lang;
 9511:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 9512:                         $formelem = 'loginhelpurl_add_file';
 9513:                     }
 9514:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9515:                                                                "help/$lang",'','',$newfile{$lang});
 9516:                     if ($result eq 'ok') {
 9517:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 9518:                         $changes{'helpurl'}{$lang} = 1;
 9519:                     } else {
 9520:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 9521:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9522:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 9523:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 9524:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9525:                         }
 9526:                     }
 9527:                 }
 9528:             } else {
 9529:                 $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);
 9530:             }
 9531:         } else {
 9532:             $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);
 9533:         }
 9534:         if ($error) {
 9535:             &Apache::lonnet::logthis($error);
 9536:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9537:         }
 9538:     }
 9539: 
 9540:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 9541:     if (ref($domconfig{'login'}) eq 'HASH') {
 9542:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 9543:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 9544:                 if ($domservers{$lonhost}) {
 9545:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9546:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 9547:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 9548:                     }
 9549:                 }
 9550:             }
 9551:         }
 9552:     }
 9553:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 9554:     foreach my $lonhost (sort(keys(%domservers))) {
 9555:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9556:             $changes{'headtag'}{$lonhost} = 1;
 9557:         } else {
 9558:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 9559:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 9560:             }
 9561:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 9562:                 push(@newhosts,$lonhost);
 9563:             } elsif ($currheadtagurls{$lonhost}) {
 9564:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 9565:                 if ($currexempt{$lonhost}) {
 9566:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 9567:                         $changes{'headtag'}{$lonhost} = 1;
 9568:                     }
 9569:                 } elsif ($possexempt{$lonhost}) {
 9570:                     $changes{'headtag'}{$lonhost} = 1;
 9571:                 }
 9572:                 if ($possexempt{$lonhost}) {
 9573:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9574:                 }
 9575:             }
 9576:         }
 9577:     }
 9578:     if (@newhosts) {
 9579:         my $error;
 9580:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9581:         if ($configuserok eq 'ok') {
 9582:             if ($switchserver) {
 9583:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 9584:             } elsif ($author_ok eq 'ok') {
 9585:                 foreach my $lonhost (@newhosts) {
 9586:                     my $formelem = 'loginheadtag_'.$lonhost;
 9587:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9588:                                                                           "login/headtag/$lonhost",'','',
 9589:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 9590:                     if ($result eq 'ok') {
 9591:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 9592:                         $changes{'headtag'}{$lonhost} = 1;
 9593:                         if ($possexempt{$lonhost}) {
 9594:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9595:                         }
 9596:                     } else {
 9597:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 9598:                                            $newheadtagurls{$lonhost},$result);
 9599:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9600:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 9601:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 9602:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 9603:                         }
 9604:                     }
 9605:                 }
 9606:             } else {
 9607:                 $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);
 9608:             }
 9609:         } else {
 9610:             $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);
 9611:         }
 9612:         if ($error) {
 9613:             &Apache::lonnet::logthis($error);
 9614:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9615:         }
 9616:     }
 9617:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 9618: 
 9619:     my $defaulthelpfile = '/adm/loginproblems.html';
 9620:     my $defaulttext = &mt('Default in use');
 9621: 
 9622:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 9623:                                              $dom);
 9624:     if ($putresult eq 'ok') {
 9625:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9626:         my %defaultchecked = (
 9627:                     'coursecatalog' => 'on',
 9628:                     'helpdesk'      => 'on',
 9629:                     'adminmail'     => 'off',
 9630:                     'newuser'       => 'off',
 9631:         );
 9632:         if (ref($domconfig{'login'}) eq 'HASH') {
 9633:             foreach my $item (@toggles) {
 9634:                 if ($defaultchecked{$item} eq 'on') { 
 9635:                     if (($domconfig{'login'}{$item} eq '0') &&
 9636:                         ($env{'form.'.$item} eq '1')) {
 9637:                         $changes{$item} = 1;
 9638:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9639:                               $domconfig{'login'}{$item} eq '1') &&
 9640:                              ($env{'form.'.$item} eq '0')) {
 9641:                         $changes{$item} = 1;
 9642:                     }
 9643:                 } elsif ($defaultchecked{$item} eq 'off') {
 9644:                     if (($domconfig{'login'}{$item} eq '1') &&
 9645:                         ($env{'form.'.$item} eq '0')) {
 9646:                         $changes{$item} = 1;
 9647:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9648:                               $domconfig{'login'}{$item} eq '0') &&
 9649:                              ($env{'form.'.$item} eq '1')) {
 9650:                         $changes{$item} = 1;
 9651:                     }
 9652:                 }
 9653:             }
 9654:         }
 9655:         if (keys(%changes) > 0 || $colchgtext) {
 9656:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9657:             if (ref($lastactref) eq 'HASH') {
 9658:                 $lastactref->{'domainconfig'} = 1;
 9659:             }
 9660:             $resulttext = &mt('Changes made:').'<ul>';
 9661:             foreach my $item (sort(keys(%changes))) {
 9662:                 if ($item eq 'loginvia') {
 9663:                     if (ref($changes{$item}) eq 'HASH') {
 9664:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 9665:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9666:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 9667:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 9668:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 9669:                                     $protocol = 'http' if ($protocol ne 'https');
 9670:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 9671: 
 9672:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 9673:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 9674:                                     } else {
 9675:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 9676:                                     }
 9677:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 9678:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 9679:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 9680:                                     }
 9681:                                     $resulttext .= '</li>';
 9682:                                 } else {
 9683:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 9684:                                 }
 9685:                             } else {
 9686:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 9687:                             }
 9688:                         }
 9689:                         $resulttext .= '</ul></li>';
 9690:                     }
 9691:                 } elsif ($item eq 'helpurl') {
 9692:                     if (ref($changes{$item}) eq 'HASH') {
 9693:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 9694:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 9695:                                 my ($chg,$link);
 9696:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 9697:                                 if ($lang eq 'nolang') {
 9698:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 9699:                                 } else {
 9700:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 9701:                                 }
 9702:                                 $resulttext .= '<li>'.$chg.'</li>';
 9703:                             } else {
 9704:                                 my $chg;
 9705:                                 if ($lang eq 'nolang') {
 9706:                                     $chg = &mt('custom log-in help file for no preferred language');
 9707:                                 } else {
 9708:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 9709:                                 }
 9710:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 9711:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 9712:                                                       '?inhibitmenu=yes',$chg,600,500).
 9713:                                                '</li>';
 9714:                             }
 9715:                         }
 9716:                     }
 9717:                 } elsif ($item eq 'headtag') {
 9718:                     if (ref($changes{$item}) eq 'HASH') {
 9719:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9720:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9721:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 9722:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9723:                                 $resulttext .= '<li><a href="'.
 9724:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 9725:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 9726:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 9727:                                 if ($possexempt{$lonhost}) {
 9728:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 9729:                                 } else {
 9730:                                     $resulttext .= &mt('included for any client IP');
 9731:                                 }
 9732:                                 $resulttext .= '</li>';
 9733:                             }
 9734:                         }
 9735:                     }
 9736:                 } elsif ($item eq 'captcha') {
 9737:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9738:                         my $chgtxt;
 9739:                         if ($loginhash{'login'}{$item} eq 'notused') {
 9740:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 9741:                         } else {
 9742:                             my %captchas = &captcha_phrases();
 9743:                             if ($captchas{$loginhash{'login'}{$item}}) {
 9744:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 9745:                             } else {
 9746:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 9747:                             }
 9748:                         }
 9749:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9750:                     }
 9751:                 } elsif ($item eq 'recaptchakeys') {
 9752:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9753:                         my ($privkey,$pubkey);
 9754:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 9755:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 9756:                             $privkey = $loginhash{'login'}{$item}{'private'};
 9757:                         }
 9758:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 9759:                         if (!$pubkey) {
 9760:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 9761:                         } else {
 9762:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9763:                         }
 9764:                         if (!$privkey) {
 9765:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 9766:                         } else {
 9767:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 9768:                         }
 9769:                         $chgtxt .= '</ul>';
 9770:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9771:                     }
 9772:                 } elsif ($item eq 'recaptchaversion') {
 9773:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9774:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 9775:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 9776:                                            '</li>';
 9777:                         }
 9778:                     }
 9779:                 } else {
 9780:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 9781:                 }
 9782:             }
 9783:             $resulttext .= $colchgtext.'</ul>';
 9784:         } else {
 9785:             $resulttext = &mt('No changes made to log-in page settings');
 9786:         }
 9787:     } else {
 9788:         $resulttext = '<span class="LC_error">'.
 9789: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9790:     }
 9791:     if ($errors) {
 9792:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9793:                        $errors.'</ul>';
 9794:     }
 9795:     return $resulttext;
 9796: }
 9797: 
 9798: sub check_exempt_addresses {
 9799:     my ($iplist) = @_;
 9800:     $iplist =~ s/^\s+//;
 9801:     $iplist =~ s/\s+$//;
 9802:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 9803:     my (@okips,$new);
 9804:     foreach my $ip (@poss_ips) {
 9805:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 9806:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 9807:                 push(@okips,$ip);
 9808:             }
 9809:         }
 9810:     }
 9811:     if (@okips > 0) {
 9812:         $new = join(',',@okips);
 9813:     } else {
 9814:         $new = '';
 9815:     }
 9816:     return $new;
 9817: }
 9818: 
 9819: sub color_font_choices {
 9820:     my %choices =
 9821:         &Apache::lonlocal::texthash (
 9822:             img => "Header",
 9823:             bgs => "Background colors",
 9824:             links => "Link colors",
 9825:             images => "Images",
 9826:             font => "Font color",
 9827:             fontmenu => "Font menu",
 9828:             pgbg => "Page",
 9829:             tabbg => "Header",
 9830:             sidebg => "Border",
 9831:             link => "Link",
 9832:             alink => "Active link",
 9833:             vlink => "Visited link",
 9834:         );
 9835:     return %choices;
 9836: }
 9837: 
 9838: sub modify_rolecolors {
 9839:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 9840:     my ($resulttext,%rolehash);
 9841:     $rolehash{'rolecolors'} = {};
 9842:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 9843:         if ($domconfig{'rolecolors'} eq '') {
 9844:             $domconfig{'rolecolors'} = {};
 9845:         }
 9846:     }
 9847:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 9848:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 9849:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 9850:                                              $dom);
 9851:     if ($putresult eq 'ok') {
 9852:         if (keys(%changes) > 0) {
 9853:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9854:             if (ref($lastactref) eq 'HASH') {
 9855:                 $lastactref->{'domainconfig'} = 1;
 9856:             }
 9857:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 9858:                                              $rolehash{'rolecolors'});
 9859:         } else {
 9860:             $resulttext = &mt('No changes made to default color schemes');
 9861:         }
 9862:     } else {
 9863:         $resulttext = '<span class="LC_error">'.
 9864: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9865:     }
 9866:     if ($errors) {
 9867:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9868:                        $errors.'</ul>';
 9869:     }
 9870:     return $resulttext;
 9871: }
 9872: 
 9873: sub modify_colors {
 9874:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 9875:     my (%changes,%choices);
 9876:     my @bgs;
 9877:     my @links = ('link','alink','vlink');
 9878:     my @logintext;
 9879:     my @images;
 9880:     my $servadm = $r->dir_config('lonAdmEMail');
 9881:     my $errors;
 9882:     my %defaults;
 9883:     foreach my $role (@{$roles}) {
 9884:         if ($role eq 'login') {
 9885:             %choices = &login_choices();
 9886:             @logintext = ('textcol','bgcol');
 9887:         } else {
 9888:             %choices = &color_font_choices();
 9889:         }
 9890:         if ($role eq 'login') {
 9891:             @images = ('img','logo','domlogo','login');
 9892:             @bgs = ('pgbg','mainbg','sidebg');
 9893:         } else {
 9894:             @images = ('img');
 9895:             @bgs = ('pgbg','tabbg','sidebg');
 9896:         }
 9897:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 9898:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 9899:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 9900:         }
 9901:         if ($role eq 'login') {
 9902:             foreach my $item (@logintext) {
 9903:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9904:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9905:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9906:                 }
 9907:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 9908:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9909:                 }
 9910:             }
 9911:         } else {
 9912:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 9913:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 9914:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 9915:             }
 9916:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 9917:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 9918:             }
 9919:         }
 9920:         foreach my $item (@bgs) {
 9921:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9922:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9923:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9924:             }
 9925:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 9926:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9927:             }
 9928:         }
 9929:         foreach my $item (@links) {
 9930:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9931:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9932:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9933:             }
 9934:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 9935:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9936:             }
 9937:         }
 9938:         my ($configuserok,$author_ok,$switchserver) = 
 9939:             &config_check($dom,$confname,$servadm);
 9940:         my ($width,$height) = &thumb_dimensions();
 9941:         if (ref($domconfig->{$role}) ne 'HASH') {
 9942:             $domconfig->{$role} = {};
 9943:         }
 9944:         foreach my $img (@images) {
 9945:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 9946:                 if (defined($env{'form.login_showlogo_'.$img})) {
 9947:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 9948:                 } else { 
 9949:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 9950:                 }
 9951:             } 
 9952: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 9953: 		 && !defined($domconfig->{$role}{$img})
 9954: 		 && !$env{'form.'.$role.'_del_'.$img}
 9955: 		 && $env{'form.'.$role.'_import_'.$img}) {
 9956: 		# import the old configured image from the .tab setting
 9957: 		# if they haven't provided a new one 
 9958: 		$domconfig->{$role}{$img} = 
 9959: 		    $env{'form.'.$role.'_import_'.$img};
 9960: 	    }
 9961:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 9962:                 my $error;
 9963:                 if ($configuserok eq 'ok') {
 9964:                     if ($switchserver) {
 9965:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 9966:                     } else {
 9967:                         if ($author_ok eq 'ok') {
 9968:                             my ($result,$logourl) = 
 9969:                                 &publishlogo($r,'upload',$role.'_'.$img,
 9970:                                            $dom,$confname,$img,$width,$height);
 9971:                             if ($result eq 'ok') {
 9972:                                 $confhash->{$role}{$img} = $logourl;
 9973:                                 $changes{$role}{'images'}{$img} = 1;
 9974:                             } else {
 9975:                                 $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);
 9976:                             }
 9977:                         } else {
 9978:                             $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);
 9979:                         }
 9980:                     }
 9981:                 } else {
 9982:                     $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);
 9983:                 }
 9984:                 if ($error) {
 9985:                     &Apache::lonnet::logthis($error);
 9986:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9987:                 }
 9988:             } elsif ($domconfig->{$role}{$img} ne '') {
 9989:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 9990:                     my $error;
 9991:                     if ($configuserok eq 'ok') {
 9992: # is confname an author?
 9993:                         if ($switchserver eq '') {
 9994:                             if ($author_ok eq 'ok') {
 9995:                                 my ($result,$logourl) = 
 9996:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 9997:                                             $dom,$confname,$img,$width,$height);
 9998:                                 if ($result eq 'ok') {
 9999:                                     $confhash->{$role}{$img} = $logourl;
10000: 				    $changes{$role}{'images'}{$img} = 1;
10001:                                 }
10002:                             }
10003:                         }
10004:                     }
10005:                 }
10006:             }
10007:         }
10008:         if (ref($domconfig) eq 'HASH') {
10009:             if (ref($domconfig->{$role}) eq 'HASH') {
10010:                 foreach my $img (@images) {
10011:                     if ($domconfig->{$role}{$img} ne '') {
10012:                         if ($env{'form.'.$role.'_del_'.$img}) {
10013:                             $confhash->{$role}{$img} = '';
10014:                             $changes{$role}{'images'}{$img} = 1;
10015:                         } else {
10016:                             if ($confhash->{$role}{$img} eq '') {
10017:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
10018:                             }
10019:                         }
10020:                     } else {
10021:                         if ($env{'form.'.$role.'_del_'.$img}) {
10022:                             $confhash->{$role}{$img} = '';
10023:                             $changes{$role}{'images'}{$img} = 1;
10024:                         } 
10025:                     }
10026:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
10027:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
10028:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
10029:                                 $domconfig->{$role}{'showlogo'}{$img}) {
10030:                                 $changes{$role}{'showlogo'}{$img} = 1; 
10031:                             }
10032:                         } else {
10033:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10034:                                 $changes{$role}{'showlogo'}{$img} = 1;
10035:                             }
10036:                         }
10037:                     }
10038:                 }
10039:                 if ($domconfig->{$role}{'font'} ne '') {
10040:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
10041:                         $changes{$role}{'font'} = 1;
10042:                     }
10043:                 } else {
10044:                     if ($confhash->{$role}{'font'}) {
10045:                         $changes{$role}{'font'} = 1;
10046:                     }
10047:                 }
10048:                 if ($role ne 'login') {
10049:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
10050:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
10051:                             $changes{$role}{'fontmenu'} = 1;
10052:                         }
10053:                     } else {
10054:                         if ($confhash->{$role}{'fontmenu'}) {
10055:                             $changes{$role}{'fontmenu'} = 1;
10056:                         }
10057:                     }
10058:                 }
10059:                 foreach my $item (@bgs) {
10060:                     if ($domconfig->{$role}{$item} ne '') {
10061:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10062:                             $changes{$role}{'bgs'}{$item} = 1;
10063:                         } 
10064:                     } else {
10065:                         if ($confhash->{$role}{$item}) {
10066:                             $changes{$role}{'bgs'}{$item} = 1;
10067:                         }
10068:                     }
10069:                 }
10070:                 foreach my $item (@links) {
10071:                     if ($domconfig->{$role}{$item} ne '') {
10072:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10073:                             $changes{$role}{'links'}{$item} = 1;
10074:                         }
10075:                     } else {
10076:                         if ($confhash->{$role}{$item}) {
10077:                             $changes{$role}{'links'}{$item} = 1;
10078:                         }
10079:                     }
10080:                 }
10081:                 foreach my $item (@logintext) {
10082:                     if ($domconfig->{$role}{$item} ne '') {
10083:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10084:                             $changes{$role}{'logintext'}{$item} = 1;
10085:                         }
10086:                     } else {
10087:                         if ($confhash->{$role}{$item}) {
10088:                             $changes{$role}{'logintext'}{$item} = 1;
10089:                         }
10090:                     }
10091:                 }
10092:             } else {
10093:                 &default_change_checker($role,\@images,\@links,\@bgs,
10094:                                         \@logintext,$confhash,\%changes); 
10095:             }
10096:         } else {
10097:             &default_change_checker($role,\@images,\@links,\@bgs,
10098:                                     \@logintext,$confhash,\%changes); 
10099:         }
10100:     }
10101:     return ($errors,%changes);
10102: }
10103: 
10104: sub config_check {
10105:     my ($dom,$confname,$servadm) = @_;
10106:     my ($configuserok,$author_ok,$switchserver,%currroles);
10107:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
10108:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
10109:                                                    $confname,$servadm);
10110:     if ($configuserok eq 'ok') {
10111:         $switchserver = &check_switchserver($dom,$confname);
10112:         if ($switchserver eq '') {
10113:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
10114:         }
10115:     }
10116:     return ($configuserok,$author_ok,$switchserver);
10117: }
10118: 
10119: sub default_change_checker {
10120:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
10121:     foreach my $item (@{$links}) {
10122:         if ($confhash->{$role}{$item}) {
10123:             $changes->{$role}{'links'}{$item} = 1;
10124:         }
10125:     }
10126:     foreach my $item (@{$bgs}) {
10127:         if ($confhash->{$role}{$item}) {
10128:             $changes->{$role}{'bgs'}{$item} = 1;
10129:         }
10130:     }
10131:     foreach my $item (@{$logintext}) {
10132:         if ($confhash->{$role}{$item}) {
10133:             $changes->{$role}{'logintext'}{$item} = 1;
10134:         }
10135:     }
10136:     foreach my $img (@{$images}) {
10137:         if ($env{'form.'.$role.'_del_'.$img}) {
10138:             $confhash->{$role}{$img} = '';
10139:             $changes->{$role}{'images'}{$img} = 1;
10140:         }
10141:         if ($role eq 'login') {
10142:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10143:                 $changes->{$role}{'showlogo'}{$img} = 1;
10144:             }
10145:         }
10146:     }
10147:     if ($confhash->{$role}{'font'}) {
10148:         $changes->{$role}{'font'} = 1;
10149:     }
10150: }
10151: 
10152: sub display_colorchgs {
10153:     my ($dom,$changes,$roles,$confhash) = @_;
10154:     my (%choices,$resulttext);
10155:     if (!grep(/^login$/,@{$roles})) {
10156:         $resulttext = &mt('Changes made:').'<br />';
10157:     }
10158:     foreach my $role (@{$roles}) {
10159:         if ($role eq 'login') {
10160:             %choices = &login_choices();
10161:         } else {
10162:             %choices = &color_font_choices();
10163:         }
10164:         if (ref($changes->{$role}) eq 'HASH') {
10165:             if ($role ne 'login') {
10166:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
10167:             }
10168:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
10169:                 if ($role ne 'login') {
10170:                     $resulttext .= '<ul>';
10171:                 }
10172:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
10173:                     if ($role ne 'login') {
10174:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
10175:                     }
10176:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
10177:                         if (($role eq 'login') && ($key eq 'showlogo')) {
10178:                             if ($confhash->{$role}{$key}{$item}) {
10179:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
10180:                             } else {
10181:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
10182:                             }
10183:                         } elsif ($confhash->{$role}{$item} eq '') {
10184:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
10185:                         } else {
10186:                             my $newitem = $confhash->{$role}{$item};
10187:                             if ($key eq 'images') {
10188:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
10189:                             }
10190:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
10191:                         }
10192:                     }
10193:                     if ($role ne 'login') {
10194:                         $resulttext .= '</ul></li>';
10195:                     }
10196:                 } else {
10197:                     if ($confhash->{$role}{$key} eq '') {
10198:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
10199:                     } else {
10200:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
10201:                     }
10202:                 }
10203:                 if ($role ne 'login') {
10204:                     $resulttext .= '</ul>';
10205:                 }
10206:             }
10207:         }
10208:     }
10209:     return $resulttext;
10210: }
10211: 
10212: sub thumb_dimensions {
10213:     return ('200','50');
10214: }
10215: 
10216: sub check_dimensions {
10217:     my ($inputfile) = @_;
10218:     my ($fullwidth,$fullheight);
10219:     if ($inputfile =~ m|^[/\w.\-]+$|) {
10220:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
10221:             my $imageinfo = <PIPE>;
10222:             if (!close(PIPE)) {
10223:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
10224:             }
10225:             chomp($imageinfo);
10226:             my ($fullsize) = 
10227:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
10228:             if ($fullsize) {
10229:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
10230:             }
10231:         }
10232:     }
10233:     return ($fullwidth,$fullheight);
10234: }
10235: 
10236: sub check_configuser {
10237:     my ($uhome,$dom,$confname,$servadm) = @_;
10238:     my ($configuserok,%currroles);
10239:     if ($uhome eq 'no_host') {
10240:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
10241:         my $configpass = &LONCAPA::Enrollment::create_password();
10242:         $configuserok = 
10243:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
10244:                              $configpass,'','','','','',undef,$servadm);
10245:     } else {
10246:         $configuserok = 'ok';
10247:         %currroles = 
10248:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
10249:     }
10250:     return ($configuserok,%currroles);
10251: }
10252: 
10253: sub check_authorstatus {
10254:     my ($dom,$confname,%currroles) = @_;
10255:     my $author_ok;
10256:     if (!$currroles{':'.$dom.':au'}) {
10257:         my $start = time;
10258:         my $end = 0;
10259:         $author_ok = 
10260:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
10261:                                         'au',$end,$start,'','','domconfig');
10262:     } else {
10263:         $author_ok = 'ok';
10264:     }
10265:     return $author_ok;
10266: }
10267: 
10268: sub publishlogo {
10269:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
10270:     my ($output,$fname,$logourl,$madethumb);
10271:     if ($action eq 'upload') {
10272:         $fname=$env{'form.'.$formname.'.filename'};
10273:         chop($env{'form.'.$formname});
10274:     } else {
10275:         ($fname) = ($formname =~ /([^\/]+)$/);
10276:     }
10277:     if ($savefileas ne '') {
10278:         $fname = $savefileas;
10279:     }
10280:     $fname=&Apache::lonnet::clean_filename($fname);
10281: # See if there is anything left
10282:     unless ($fname) { return ('error: no uploaded file'); }
10283:     $fname="$subdir/$fname";
10284:     my $docroot=$r->dir_config('lonDocRoot');
10285:     my $filepath="$docroot/priv";
10286:     my $relpath = "$dom/$confname";
10287:     my ($fnamepath,$file,$fetchthumb);
10288:     $file=$fname;
10289:     if ($fname=~m|/|) {
10290:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
10291:     }
10292:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
10293:     my $count;
10294:     for ($count=5;$count<=$#parts;$count++) {
10295:         $filepath.="/$parts[$count]";
10296:         if ((-e $filepath)!=1) {
10297:             mkdir($filepath,02770);
10298:         }
10299:     }
10300:     # Check for bad extension and disallow upload
10301:     if ($file=~/\.(\w+)$/ &&
10302:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
10303:         $output = 
10304:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
10305:     } elsif ($file=~/\.(\w+)$/ &&
10306:         !defined(&Apache::loncommon::fileembstyle($1))) {
10307:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
10308:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
10309:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
10310:     } elsif (-d "$filepath/$file") {
10311:         $output = &mt('Filename is a directory name - rename the file and re-upload');
10312:     } else {
10313:         my $source = $filepath.'/'.$file;
10314:         my $logfile;
10315:         if (!open($logfile,">>",$source.'.log')) {
10316:             return (&mt('No write permission to Authoring Space'));
10317:         }
10318:         print $logfile
10319: "\n================= Publish ".localtime()." ================\n".
10320: $env{'user.name'}.':'.$env{'user.domain'}."\n";
10321: # Save the file
10322:         if (!open(FH,">",$source)) {
10323:             &Apache::lonnet::logthis('Failed to create '.$source);
10324:             return (&mt('Failed to create file'));
10325:         }
10326:         if ($action eq 'upload') {
10327:             if (!print FH ($env{'form.'.$formname})) {
10328:                 &Apache::lonnet::logthis('Failed to write to '.$source);
10329:                 return (&mt('Failed to write file'));
10330:             }
10331:         } else {
10332:             my $original = &Apache::lonnet::filelocation('',$formname);
10333:             if(!copy($original,$source)) {
10334:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
10335:                 return (&mt('Failed to write file'));
10336:             }
10337:         }
10338:         close(FH);
10339:         chmod(0660, $source); # Permissions to rw-rw---.
10340: 
10341:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
10342:         my $copyfile=$targetdir.'/'.$file;
10343: 
10344:         my @parts=split(/\//,$targetdir);
10345:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
10346:         for (my $count=5;$count<=$#parts;$count++) {
10347:             $path.="/$parts[$count]";
10348:             if (!-e $path) {
10349:                 print $logfile "\nCreating directory ".$path;
10350:                 mkdir($path,02770);
10351:             }
10352:         }
10353:         my $versionresult;
10354:         if (-e $copyfile) {
10355:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
10356:         } else {
10357:             $versionresult = 'ok';
10358:         }
10359:         if ($versionresult eq 'ok') {
10360:             if (copy($source,$copyfile)) {
10361:                 print $logfile "\nCopied original source to ".$copyfile."\n";
10362:                 $output = 'ok';
10363:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
10364:                 push(@{$modified_urls},[$copyfile,$source]);
10365:                 my $metaoutput = 
10366:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
10367:                 unless ($registered_cleanup) {
10368:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10369:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10370:                     $registered_cleanup=1;
10371:                 }
10372:             } else {
10373:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
10374:                 $output = &mt('Failed to copy file to RES space').", $!";
10375:             }
10376:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
10377:                 my $inputfile = $filepath.'/'.$file;
10378:                 my $outfile = $filepath.'/'.'tn-'.$file;
10379:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
10380:                 if ($fullwidth ne '' && $fullheight ne '') { 
10381:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
10382:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
10383:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
10384:                         system({$args[0]} @args);
10385:                         chmod(0660, $filepath.'/tn-'.$file);
10386:                         if (-e $outfile) {
10387:                             my $copyfile=$targetdir.'/tn-'.$file;
10388:                             if (copy($outfile,$copyfile)) {
10389:                                 print $logfile "\nCopied source to ".$copyfile."\n";
10390:                                 my $thumb_metaoutput = 
10391:                                     &write_metadata($dom,$confname,$formname,
10392:                                                     $targetdir,'tn-'.$file,$logfile);
10393:                                 push(@{$modified_urls},[$copyfile,$outfile]);
10394:                                 unless ($registered_cleanup) {
10395:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10396:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10397:                                     $registered_cleanup=1;
10398:                                 }
10399:                                 $madethumb = 1;
10400:                             } else {
10401:                                 print $logfile "\nUnable to write ".$copyfile.
10402:                                                ':'.$!."\n";
10403:                             }
10404:                         }
10405:                     }
10406:                 }
10407:             }
10408:         } else {
10409:             $output = $versionresult;
10410:         }
10411:     }
10412:     return ($output,$logourl,$madethumb);
10413: }
10414: 
10415: sub logo_versioning {
10416:     my ($targetdir,$file,$logfile) = @_;
10417:     my $target = $targetdir.'/'.$file;
10418:     my ($maxversion,$fn,$extn,$output);
10419:     $maxversion = 0;
10420:     if ($file =~ /^(.+)\.(\w+)$/) {
10421:         $fn=$1;
10422:         $extn=$2;
10423:     }
10424:     opendir(DIR,$targetdir);
10425:     while (my $filename=readdir(DIR)) {
10426:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
10427:             $maxversion=($1>$maxversion)?$1:$maxversion;
10428:         }
10429:     }
10430:     $maxversion++;
10431:     print $logfile "\nCreating old version ".$maxversion."\n";
10432:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
10433:     if (copy($target,$copyfile)) {
10434:         print $logfile "Copied old target to ".$copyfile."\n";
10435:         $copyfile=$copyfile.'.meta';
10436:         if (copy($target.'.meta',$copyfile)) {
10437:             print $logfile "Copied old target metadata to ".$copyfile."\n";
10438:             $output = 'ok';
10439:         } else {
10440:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
10441:             $output = &mt('Failed to copy old meta').", $!, ";
10442:         }
10443:     } else {
10444:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
10445:         $output = &mt('Failed to copy old target').", $!, ";
10446:     }
10447:     return $output;
10448: }
10449: 
10450: sub write_metadata {
10451:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
10452:     my (%metadatafields,%metadatakeys,$output);
10453:     $metadatafields{'title'}=$formname;
10454:     $metadatafields{'creationdate'}=time;
10455:     $metadatafields{'lastrevisiondate'}=time;
10456:     $metadatafields{'copyright'}='public';
10457:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
10458:                                          $env{'user.domain'};
10459:     $metadatafields{'authorspace'}=$confname.':'.$dom;
10460:     $metadatafields{'domain'}=$dom;
10461:     {
10462:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
10463:         my $mfh;
10464:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
10465:             foreach (sort(keys(%metadatafields))) {
10466:                 unless ($_=~/\./) {
10467:                     my $unikey=$_;
10468:                     $unikey=~/^([A-Za-z]+)/;
10469:                     my $tag=$1;
10470:                     $tag=~tr/A-Z/a-z/;
10471:                     print $mfh "\n\<$tag";
10472:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
10473:                         my $value=$metadatafields{$unikey.'.'.$_};
10474:                         $value=~s/\"/\'\'/g;
10475:                         print $mfh ' '.$_.'="'.$value.'"';
10476:                     }
10477:                     print $mfh '>'.
10478:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
10479:                             .'</'.$tag.'>';
10480:                 }
10481:             }
10482:             $output = 'ok';
10483:             print $logfile "\nWrote metadata";
10484:             close($mfh);
10485:         } else {
10486:             print $logfile "\nFailed to open metadata file";
10487:             $output = &mt('Could not write metadata');
10488:         }
10489:     }
10490:     return $output;
10491: }
10492: 
10493: sub notifysubscribed {
10494:     foreach my $targetsource (@{$modified_urls}){
10495:         next unless (ref($targetsource) eq 'ARRAY');
10496:         my ($target,$source)=@{$targetsource};
10497:         if ($source ne '') {
10498:             if (open(my $logfh,">>",$source.'.log')) {
10499:                 print $logfh "\nCleanup phase: Notifications\n";
10500:                 my @subscribed=&subscribed_hosts($target);
10501:                 foreach my $subhost (@subscribed) {
10502:                     print $logfh "\nNotifying host ".$subhost.':';
10503:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
10504:                     print $logfh $reply;
10505:                 }
10506:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
10507:                 foreach my $subhost (@subscribedmeta) {
10508:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
10509:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
10510:                                                         $subhost);
10511:                     print $logfh $reply;
10512:                 }
10513:                 print $logfh "\n============ Done ============\n";
10514:                 close($logfh);
10515:             }
10516:         }
10517:     }
10518:     return OK;
10519: }
10520: 
10521: sub subscribed_hosts {
10522:     my ($target) = @_;
10523:     my @subscribed;
10524:     if (open(my $fh,"<","$target.subscription")) {
10525:         while (my $subline=<$fh>) {
10526:             if ($subline =~ /^($match_lonid):/) {
10527:                 my $host = $1;
10528:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
10529:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
10530:                         push(@subscribed,$host);
10531:                     }
10532:                 }
10533:             }
10534:         }
10535:     }
10536:     return @subscribed;
10537: }
10538: 
10539: sub check_switchserver {
10540:     my ($dom,$confname) = @_;
10541:     my ($allowed,$switchserver);
10542:     my $home = &Apache::lonnet::homeserver($confname,$dom);
10543:     if ($home eq 'no_host') {
10544:         $home = &Apache::lonnet::domain($dom,'primary');
10545:     }
10546:     my @ids=&Apache::lonnet::current_machine_ids();
10547:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
10548:     if (!$allowed) {
10549: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
10550:     }
10551:     return $switchserver;
10552: }
10553: 
10554: sub modify_quotas {
10555:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10556:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
10557:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
10558:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
10559:         $validationfieldsref);
10560:     if ($action eq 'quotas') {
10561:         $context = 'tools'; 
10562:     } else {
10563:         $context = $action;
10564:     }
10565:     if ($context eq 'requestcourses') {
10566:         @usertools = ('official','unofficial','community','textbook','placement','lti');
10567:         @options =('norequest','approval','validate','autolimit');
10568:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
10569:         %titles = &courserequest_titles();
10570:         $toolregexp = join('|',@usertools);
10571:         %conditions = &courserequest_conditions();
10572:         $confname = $dom.'-domainconfig';
10573:         my $servadm = $r->dir_config('lonAdmEMail');
10574:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10575:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
10576:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
10577:     } elsif ($context eq 'requestauthor') {
10578:         @usertools = ('author');
10579:         %titles = &authorrequest_titles();
10580:     } else {
10581:         @usertools = ('aboutme','blog','webdav','portfolio');
10582:         %titles = &tool_titles();
10583:     }
10584:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10585:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10586:     foreach my $key (keys(%env)) {
10587:         if ($context eq 'requestcourses') {
10588:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
10589:                 my $item = $1;
10590:                 my $type = $2;
10591:                 if ($type =~ /^limit_(.+)/) {
10592:                     $limithash{$item}{$1} = $env{$key};
10593:                 } else {
10594:                     $confhash{$item}{$type} = $env{$key};
10595:                 }
10596:             }
10597:         } elsif ($context eq 'requestauthor') {
10598:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
10599:                 $confhash{$1} = $env{$key};
10600:             }
10601:         } else {
10602:             if ($key =~ /^form\.quota_(.+)$/) {
10603:                 $confhash{'defaultquota'}{$1} = $env{$key};
10604:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
10605:                 $confhash{'authorquota'}{$1} = $env{$key};
10606:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
10607:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
10608:             }
10609:         }
10610:     }
10611:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10612:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
10613:         @approvalnotify = sort(@approvalnotify);
10614:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
10615:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
10616:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
10617:         foreach my $type (@hasuniquecode) {
10618:             if (grep(/^\Q$type\E$/,@crstypes)) {
10619:                 $confhash{'uniquecode'}{$type} = 1;
10620:             }
10621:         }
10622:         my (%newbook,%allpos);
10623:         if ($context eq 'requestcourses') {
10624:             foreach my $type ('textbooks','templates') {
10625:                 @{$allpos{$type}} = (); 
10626:                 my $invalid;
10627:                 if ($type eq 'textbooks') {
10628:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
10629:                 } else {
10630:                     $invalid = &mt('Invalid LON-CAPA course for template');
10631:                 }
10632:                 if ($env{'form.'.$type.'_addbook'}) {
10633:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
10634:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
10635:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
10636:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
10637:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10638:                         } else {
10639:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
10640:                             my $position = $env{'form.'.$type.'_addbook_pos'};
10641:                             $position =~ s/\D+//g;
10642:                             if ($position ne '') {
10643:                                 $allpos{$type}[$position] = $newbook{$type};
10644:                             }
10645:                         }
10646:                     } else {
10647:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10648:                     }
10649:                 }
10650:             } 
10651:         }
10652:         if (ref($domconfig{$action}) eq 'HASH') {
10653:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
10654:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
10655:                     $changes{'notify'}{'approval'} = 1;
10656:                 }
10657:             } else {
10658:                 if ($confhash{'notify'}{'approval'}) {
10659:                     $changes{'notify'}{'approval'} = 1;
10660:                 }
10661:             }
10662:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
10663:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
10664:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
10665:                         unless ($confhash{'uniquecode'}{$crstype}) {
10666:                             $changes{'uniquecode'} = 1;
10667:                         }
10668:                     }
10669:                     unless ($changes{'uniquecode'}) {
10670:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
10671:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
10672:                                 $changes{'uniquecode'} = 1;
10673:                             }
10674:                         }
10675:                     }
10676:                } else {
10677:                    $changes{'uniquecode'} = 1;
10678:                }
10679:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
10680:                 $changes{'uniquecode'} = 1;
10681:             }
10682:             if ($context eq 'requestcourses') {
10683:                 foreach my $type ('textbooks','templates') {
10684:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
10685:                         my %deletions;
10686:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
10687:                         if (@todelete) {
10688:                             map { $deletions{$_} = 1; } @todelete;
10689:                         }
10690:                         my %imgdeletions;
10691:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
10692:                         if (@todeleteimages) {
10693:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
10694:                         }
10695:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
10696:                         for (my $i=0; $i<=$maxnum; $i++) {
10697:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
10698:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
10699:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
10700:                                 if ($deletions{$key}) {
10701:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
10702:                                         #FIXME need to obsolete item in RES space
10703:                                     }
10704:                                     next;
10705:                                 } else {
10706:                                     my $newpos = $env{'form.'.$itemid};
10707:                                     $newpos =~ s/\D+//g;
10708:                                     foreach my $item ('subject','title','publisher','author') {
10709:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10710:                                                  ($type eq 'templates'));
10711:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
10712:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
10713:                                             $changes{$type}{$key} = 1;
10714:                                         }
10715:                                     }
10716:                                     $allpos{$type}[$newpos] = $key;
10717:                                 }
10718:                                 if ($imgdeletions{$key}) {
10719:                                     $changes{$type}{$key} = 1;
10720:                                     #FIXME need to obsolete item in RES space
10721:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
10722:                                     my ($cdom,$cnum) = split(/_/,$key);
10723:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10724:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10725:                                     } else {
10726:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
10727:                                                                                       $cdom,$cnum,$type,$configuserok,
10728:                                                                                       $switchserver,$author_ok);
10729:                                         if ($imgurl) {
10730:                                             $confhash{$type}{$key}{'image'} = $imgurl;
10731:                                             $changes{$type}{$key} = 1; 
10732:                                         }
10733:                                         if ($error) {
10734:                                             &Apache::lonnet::logthis($error);
10735:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10736:                                         }
10737:                                     }
10738:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
10739:                                     $confhash{$type}{$key}{'image'} = 
10740:                                         $domconfig{$action}{$type}{$key}{'image'};
10741:                                 }
10742:                             }
10743:                         }
10744:                     }
10745:                 }
10746:             }
10747:         } else {
10748:             if ($confhash{'notify'}{'approval'}) {
10749:                 $changes{'notify'}{'approval'} = 1;
10750:             }
10751:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
10752:                 $changes{'uniquecode'} = 1;
10753:             }
10754:         }
10755:         if ($context eq 'requestcourses') {
10756:             foreach my $type ('textbooks','templates') {
10757:                 if ($newbook{$type}) {
10758:                     $changes{$type}{$newbook{$type}} = 1;
10759:                     foreach my $item ('subject','title','publisher','author') {
10760:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10761:                                  ($type eq 'template'));
10762:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
10763:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
10764:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
10765:                         }
10766:                     }
10767:                     if ($type eq 'textbooks') {
10768:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
10769:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
10770:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10771:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10772:                             } else {
10773:                                 my ($imageurl,$error) =
10774:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
10775:                                                             $configuserok,$switchserver,$author_ok);
10776:                                 if ($imageurl) {
10777:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
10778:                                 }
10779:                                 if ($error) {
10780:                                     &Apache::lonnet::logthis($error);
10781:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10782:                                 }
10783:                             }
10784:                         }
10785:                     }
10786:                 }
10787:                 if (@{$allpos{$type}} > 0) {
10788:                     my $idx = 0;
10789:                     foreach my $item (@{$allpos{$type}}) {
10790:                         if ($item ne '') {
10791:                             $confhash{$type}{$item}{'order'} = $idx;
10792:                             if (ref($domconfig{$action}) eq 'HASH') {
10793:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
10794:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
10795:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
10796:                                             $changes{$type}{$item} = 1;
10797:                                         }
10798:                                     }
10799:                                 }
10800:                             }
10801:                             $idx ++;
10802:                         }
10803:                     }
10804:                 }
10805:             }
10806:             if (ref($validationitemsref) eq 'ARRAY') {
10807:                 foreach my $item (@{$validationitemsref}) {
10808:                     if ($item eq 'fields') {
10809:                         my @changed;
10810:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
10811:                         if (@{$confhash{'validation'}{$item}} > 0) {
10812:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
10813:                         }
10814:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10815:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10816:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
10817:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
10818:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
10819:                                 } else {
10820:                                     @changed = @{$confhash{'validation'}{$item}};
10821:                                 }
10822:                             } else {
10823:                                 @changed = @{$confhash{'validation'}{$item}};
10824:                             }
10825:                         } else {
10826:                             @changed = @{$confhash{'validation'}{$item}};
10827:                         }
10828:                         if (@changed) {
10829:                             if ($confhash{'validation'}{$item}) {
10830:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
10831:                             } else {
10832:                                 $changes{'validation'}{$item} = &mt('None');
10833:                             }
10834:                         }
10835:                     } else {
10836:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
10837:                         if ($item eq 'markup') {
10838:                             if ($env{'form.requestcourses_validation_'.$item}) {
10839:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10840:                             }
10841:                         }
10842:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10843:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10844:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
10845:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10846:                                 }
10847:                             } else {
10848:                                 if ($confhash{'validation'}{$item} ne '') {
10849:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10850:                                 }
10851:                             }
10852:                         } else {
10853:                             if ($confhash{'validation'}{$item} ne '') {
10854:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10855:                             }
10856:                         }
10857:                     }
10858:                 }
10859:             }
10860:             if ($env{'form.validationdc'}) {
10861:                 my $newval = $env{'form.validationdc'};
10862:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10863:                 if (exists($domcoords{$newval})) {
10864:                     $confhash{'validation'}{'dc'} = $newval;
10865:                 }
10866:             }
10867:             if (ref($confhash{'validation'}) eq 'HASH') {
10868:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10869:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10870:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10871:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
10872:                                 if ($confhash{'validation'}{'dc'} eq '') {
10873:                                     $changes{'validation'}{'dc'} = &mt('None');
10874:                                 } else {
10875:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10876:                                 }
10877:                             }
10878:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
10879:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10880:                         }
10881:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
10882:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10883:                     }
10884:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
10885:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10886:                 }
10887:             } else {
10888:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10889:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10890:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10891:                             $changes{'validation'}{'dc'} = &mt('None');
10892:                         }
10893:                     }
10894:                 }
10895:             }
10896:         }
10897:     } else {
10898:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
10899:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
10900:     }
10901:     foreach my $item (@usertools) {
10902:         foreach my $type (@{$types},'default','_LC_adv') {
10903:             my $unset; 
10904:             if ($context eq 'requestcourses') {
10905:                 $unset = '0';
10906:                 if ($type eq '_LC_adv') {
10907:                     $unset = '';
10908:                 }
10909:                 if ($confhash{$item}{$type} eq 'autolimit') {
10910:                     $confhash{$item}{$type} .= '=';
10911:                     unless ($limithash{$item}{$type} =~ /\D/) {
10912:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
10913:                     }
10914:                 }
10915:             } elsif ($context eq 'requestauthor') {
10916:                 $unset = '0';
10917:                 if ($type eq '_LC_adv') {
10918:                     $unset = '';
10919:                 }
10920:             } else {
10921:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
10922:                     $confhash{$item}{$type} = 1;
10923:                 } else {
10924:                     $confhash{$item}{$type} = 0;
10925:                 }
10926:             }
10927:             if (ref($domconfig{$action}) eq 'HASH') {
10928:                 if ($action eq 'requestauthor') {
10929:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
10930:                         $changes{$type} = 1;
10931:                     }
10932:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
10933:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
10934:                         $changes{$item}{$type} = 1;
10935:                     }
10936:                 } else {
10937:                     if ($context eq 'requestcourses') {
10938:                         if ($confhash{$item}{$type} ne $unset) {
10939:                             $changes{$item}{$type} = 1;
10940:                         }
10941:                     } else {
10942:                         if (!$confhash{$item}{$type}) {
10943:                             $changes{$item}{$type} = 1;
10944:                         }
10945:                     }
10946:                 }
10947:             } else {
10948:                 if ($context eq 'requestcourses') {
10949:                     if ($confhash{$item}{$type} ne $unset) {
10950:                         $changes{$item}{$type} = 1;
10951:                     }
10952:                 } elsif ($context eq 'requestauthor') {
10953:                     if ($confhash{$type} ne $unset) {
10954:                         $changes{$type} = 1;
10955:                     }
10956:                 } else {
10957:                     if (!$confhash{$item}{$type}) {
10958:                         $changes{$item}{$type} = 1;
10959:                     }
10960:                 }
10961:             }
10962:         }
10963:     }
10964:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10965:         if (ref($domconfig{'quotas'}) eq 'HASH') {
10966:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10967:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
10968:                     if (exists($confhash{'defaultquota'}{$key})) {
10969:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
10970:                             $changes{'defaultquota'}{$key} = 1;
10971:                         }
10972:                     } else {
10973:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
10974:                     }
10975:                 }
10976:             } else {
10977:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
10978:                     if (exists($confhash{'defaultquota'}{$key})) {
10979:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
10980:                             $changes{'defaultquota'}{$key} = 1;
10981:                         }
10982:                     } else {
10983:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
10984:                     }
10985:                 }
10986:             }
10987:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10988:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
10989:                     if (exists($confhash{'authorquota'}{$key})) {
10990:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
10991:                             $changes{'authorquota'}{$key} = 1;
10992:                         }
10993:                     } else {
10994:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
10995:                     }
10996:                 }
10997:             }
10998:         }
10999:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
11000:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
11001:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11002:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
11003:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
11004:                             $changes{'defaultquota'}{$key} = 1;
11005:                         }
11006:                     } else {
11007:                         if (!exists($domconfig{'quotas'}{$key})) {
11008:                             $changes{'defaultquota'}{$key} = 1;
11009:                         }
11010:                     }
11011:                 } else {
11012:                     $changes{'defaultquota'}{$key} = 1;
11013:                 }
11014:             }
11015:         }
11016:         if (ref($confhash{'authorquota'}) eq 'HASH') {
11017:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
11018:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11019:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
11020:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
11021:                             $changes{'authorquota'}{$key} = 1;
11022:                         }
11023:                     } else {
11024:                         $changes{'authorquota'}{$key} = 1;
11025:                     }
11026:                 } else {
11027:                     $changes{'authorquota'}{$key} = 1;
11028:                 }
11029:             }
11030:         }
11031:     }
11032: 
11033:     if ($context eq 'requestauthor') {
11034:         $domdefaults{'requestauthor'} = \%confhash;
11035:     } else {
11036:         foreach my $key (keys(%confhash)) {
11037:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
11038:                 $domdefaults{$key} = $confhash{$key};
11039:             }
11040:         }
11041:     }
11042: 
11043:     my %quotahash = (
11044:                       $action => { %confhash }
11045:                     );
11046:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
11047:                                              $dom);
11048:     if ($putresult eq 'ok') {
11049:         if (keys(%changes) > 0) {
11050:             my $cachetime = 24*60*60;
11051:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11052:             if (ref($lastactref) eq 'HASH') {
11053:                 $lastactref->{'domdefaults'} = 1;
11054:             }
11055:             $resulttext = &mt('Changes made:').'<ul>';
11056:             unless (($context eq 'requestcourses') ||
11057:                     ($context eq 'requestauthor')) {
11058:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
11059:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
11060:                     foreach my $type (@{$types},'default') {
11061:                         if (defined($changes{'defaultquota'}{$type})) {
11062:                             my $typetitle = $usertypes->{$type};
11063:                             if ($type eq 'default') {
11064:                                 $typetitle = $othertitle;
11065:                             }
11066:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
11067:                         }
11068:                     }
11069:                     $resulttext .= '</ul></li>';
11070:                 }
11071:                 if (ref($changes{'authorquota'}) eq 'HASH') {
11072:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
11073:                     foreach my $type (@{$types},'default') {
11074:                         if (defined($changes{'authorquota'}{$type})) {
11075:                             my $typetitle = $usertypes->{$type};
11076:                             if ($type eq 'default') {
11077:                                 $typetitle = $othertitle;
11078:                             }
11079:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
11080:                         }
11081:                     }
11082:                     $resulttext .= '</ul></li>';
11083:                 }
11084:             }
11085:             my %newenv;
11086:             foreach my $item (@usertools) {
11087:                 my (%haschgs,%inconf);
11088:                 if ($context eq 'requestauthor') {
11089:                     %haschgs = %changes;
11090:                     %inconf = %confhash;
11091:                 } else {
11092:                     if (ref($changes{$item}) eq 'HASH') {
11093:                         %haschgs = %{$changes{$item}};
11094:                     }
11095:                     if (ref($confhash{$item}) eq 'HASH') {
11096:                         %inconf = %{$confhash{$item}};
11097:                     }
11098:                 }
11099:                 if (keys(%haschgs) > 0) {
11100:                     my $newacc = 
11101:                         &Apache::lonnet::usertools_access($env{'user.name'},
11102:                                                           $env{'user.domain'},
11103:                                                           $item,'reload',$context);
11104:                     if (($context eq 'requestcourses') ||
11105:                         ($context eq 'requestauthor')) {
11106:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
11107:                             $newenv{'environment.canrequest.'.$item} = $newacc;
11108:                         }
11109:                     } else {
11110:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
11111:                             $newenv{'environment.availabletools.'.$item} = $newacc;
11112:                         }
11113:                     }
11114:                     unless ($context eq 'requestauthor') {
11115:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
11116:                     }
11117:                     foreach my $type (@{$types},'default','_LC_adv') {
11118:                         if ($haschgs{$type}) {
11119:                             my $typetitle = $usertypes->{$type};
11120:                             if ($type eq 'default') {
11121:                                 $typetitle = $othertitle;
11122:                             } elsif ($type eq '_LC_adv') {
11123:                                 $typetitle = 'LON-CAPA Advanced Users'; 
11124:                             }
11125:                             if ($inconf{$type}) {
11126:                                 if ($context eq 'requestcourses') {
11127:                                     my $cond;
11128:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
11129:                                         if ($1 eq '') {
11130:                                             $cond = &mt('(Automatic processing of any request).');
11131:                                         } else {
11132:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
11133:                                         }
11134:                                     } else { 
11135:                                         $cond = $conditions{$inconf{$type}};
11136:                                     }
11137:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
11138:                                 } elsif ($context eq 'requestauthor') {
11139:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
11140:                                                              $titles{$inconf{$type}},$typetitle);
11141: 
11142:                                 } else {
11143:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
11144:                                 }
11145:                             } else {
11146:                                 if ($type eq '_LC_adv') {
11147:                                     if ($inconf{$type} eq '0') {
11148:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11149:                                     } else { 
11150:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
11151:                                     }
11152:                                 } else {
11153:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11154:                                 }
11155:                             }
11156:                         }
11157:                     }
11158:                     unless ($context eq 'requestauthor') {
11159:                         $resulttext .= '</ul></li>';
11160:                     }
11161:                 }
11162:             }
11163:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
11164:                 if (ref($changes{'notify'}) eq 'HASH') {
11165:                     if ($changes{'notify'}{'approval'}) {
11166:                         if (ref($confhash{'notify'}) eq 'HASH') {
11167:                             if ($confhash{'notify'}{'approval'}) {
11168:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
11169:                             } else {
11170:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
11171:                             }
11172:                         }
11173:                     }
11174:                 }
11175:             }
11176:             if ($action eq 'requestcourses') {
11177:                 my @offon = ('off','on');
11178:                 if ($changes{'uniquecode'}) {
11179:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
11180:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
11181:                         $resulttext .= '<li>'.
11182:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
11183:                                        '</li>';
11184:                     } else {
11185:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
11186:                                        '</li>';
11187:                     }
11188:                 }
11189:                 foreach my $type ('textbooks','templates') {
11190:                     if (ref($changes{$type}) eq 'HASH') {
11191:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
11192:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
11193:                             my %coursehash = &Apache::lonnet::coursedescription($key);
11194:                             my $coursetitle = $coursehash{'description'};
11195:                             my $position = $confhash{$type}{$key}{'order'} + 1;
11196:                             $resulttext .= '<li>';
11197:                             foreach my $item ('subject','title','publisher','author') {
11198:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
11199:                                          ($type eq 'templates'));
11200:                                 my $name = $item.':';
11201:                                 $name =~ s/^(\w)/\U$1/;
11202:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
11203:                             }
11204:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
11205:                             if ($type eq 'textbooks') {
11206:                                 if ($confhash{$type}{$key}{'image'}) {
11207:                                     $resulttext .= ' '.&mt('Image: [_1]',
11208:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
11209:                                                    ' alt="Textbook cover" />').'<br />';
11210:                                 }
11211:                             }
11212:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
11213:                         }
11214:                         $resulttext .= '</ul></li>';
11215:                     }
11216:                 }
11217:                 if (ref($changes{'validation'}) eq 'HASH') {
11218:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
11219:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
11220:                         foreach my $item (@{$validationitemsref}) {
11221:                             if (exists($changes{'validation'}{$item})) {
11222:                                 if ($item eq 'markup') {
11223:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11224:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
11225:                                 } else {
11226:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11227:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
11228:                                 }
11229:                             }
11230:                         }
11231:                         if (exists($changes{'validation'}{'dc'})) {
11232:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
11233:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
11234:                         }
11235:                     }
11236:                 }
11237:             }
11238:             $resulttext .= '</ul>';
11239:             if (keys(%newenv)) {
11240:                 &Apache::lonnet::appenv(\%newenv);
11241:             }
11242:         } else {
11243:             if ($context eq 'requestcourses') {
11244:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
11245:             } elsif ($context eq 'requestauthor') {
11246:                 $resulttext = &mt('No changes made to rights to request author space.');
11247:             } else {
11248:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
11249:             }
11250:         }
11251:     } else {
11252:         $resulttext = '<span class="LC_error">'.
11253: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11254:     }
11255:     if ($errors) {
11256:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
11257:                        '<ul>'.$errors.'</ul></p>';
11258:     }
11259:     return $resulttext;
11260: }
11261: 
11262: sub process_textbook_image {
11263:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
11264:     my $filename = $env{'form.'.$caller.'.filename'};
11265:     my ($error,$url);
11266:     my ($width,$height) = (50,50);
11267:     if ($configuserok eq 'ok') {
11268:         if ($switchserver) {
11269:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
11270:                          $switchserver);
11271:         } elsif ($author_ok eq 'ok') {
11272:             my ($result,$imageurl) =
11273:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11274:                              "$type/$cdom/$cnum/cover",$width,$height);
11275:             if ($result eq 'ok') {
11276:                 $url = $imageurl;
11277:             } else {
11278:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11279:             }
11280:         } else {
11281:             $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);
11282:         }
11283:     } else {
11284:         $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);
11285:     }
11286:     return ($url,$error);
11287: }
11288: 
11289: sub modify_ltitools {
11290:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11291:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11292:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11293:     my $confname = $dom.'-domainconfig';
11294:     my $servadm = $r->dir_config('lonAdmEMail');
11295:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11296:     my (%posslti,%possfield);
11297:     my @courseroles = ('cc','in','ta','ep','st');
11298:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
11299:     map { $posslti{$_} = 1; } @ltiroles;
11300:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
11301:     map { $possfield{$_} = 1; } @allfields;
11302:     my %lt = &ltitools_names(); 
11303:     if ($env{'form.ltitools_add'}) {
11304:         my $title = $env{'form.ltitools_add_title'};
11305:         $title =~ s/(`)/'/g;
11306:         ($newid,my $error) = &get_ltitools_id($dom,$title);
11307:         if ($newid) {
11308:             my $position = $env{'form.ltitools_add_pos'};
11309:             $position =~ s/\D+//g;
11310:             if ($position ne '') {
11311:                 $allpos[$position] = $newid;
11312:             }
11313:             $changes{$newid} = 1;
11314:             foreach my $item ('title','url','key','secret','lifetime') {
11315:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
11316:                 if ($item eq 'lifetime') {
11317:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
11318:                 }
11319:                 if ($env{'form.ltitools_add_'.$item}) {
11320:                     if (($item eq 'key') || ($item eq 'secret')) {
11321:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11322:                     } else {
11323:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11324:                     }
11325:                 }
11326:             }
11327:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
11328:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
11329:             }
11330:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
11331:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
11332:             }
11333:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
11334:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
11335:             } else {
11336:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
11337:             }
11338:             foreach my $item ('width','height','linktext','explanation') {
11339:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
11340:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
11341:                 if (($item eq 'width') || ($item eq 'height')) {
11342:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
11343:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
11344:                     }
11345:                 } else {
11346:                     if ($env{'form.ltitools_add_'.$item} ne '') {
11347:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
11348:                     }
11349:                 }
11350:             }
11351:             if ($env{'form.ltitools_add_target'} eq 'window') {
11352:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11353:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
11354:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11355:             } else {
11356:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
11357:             }
11358:             foreach my $item ('passback','roster') {
11359:                 if ($env{'form.ltitools_'.$item.'_add'}) {
11360:                     $confhash{$newid}{$item} = 1;
11361:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
11362:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
11363:                         $lifetime =~ s/^\s+|\s+$//g;
11364:                         if ($lifetime =~ /^\d+\.?\d*$/) {
11365:                             $confhash{$newid}{$item.'valid'} = $lifetime;
11366:                         }
11367:                     }
11368:                 }
11369:             }
11370:             if ($env{'form.ltitools_add_image.filename'} ne '') {
11371:                 my ($imageurl,$error) =
11372:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
11373:                                             $configuserok,$switchserver,$author_ok);
11374:                 if ($imageurl) {
11375:                     $confhash{$newid}{'image'} = $imageurl;
11376:                 }
11377:                 if ($error) {
11378:                     &Apache::lonnet::logthis($error);
11379:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11380:                 }
11381:             }
11382:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
11383:             foreach my $field (@fields) {
11384:                 if ($possfield{$field}) {
11385:                     if ($field eq 'roles') {
11386:                         foreach my $role (@courseroles) {
11387:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
11388:                             if (($choice ne '') && ($posslti{$choice})) {
11389:                                 $confhash{$newid}{'roles'}{$role} = $choice;
11390:                                 if ($role eq 'cc') {
11391:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
11392:                                 }
11393:                             }
11394:                         }
11395:                     } else {
11396:                         $confhash{$newid}{'fields'}{$field} = 1;
11397:                     }
11398:                 }
11399:             }
11400:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
11401:                 if ($confhash{$newid}{'fields'}{'user'}) {
11402:                     if ($env{'form.ltitools_userincdom_add'}) {
11403:                         $confhash{$newid}{'incdom'} = 1;
11404:                     }
11405:                 }
11406:             }
11407:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
11408:             foreach my $item (@courseconfig) {
11409:                 $confhash{$newid}{'crsconf'}{$item} = 1;
11410:             }
11411:             if ($env{'form.ltitools_add_custom'}) {
11412:                 my $name = $env{'form.ltitools_add_custom_name'};
11413:                 my $value = $env{'form.ltitools_add_custom_value'};
11414:                 $value =~ s/(`)/'/g;
11415:                 $name =~ s/(`)/'/g;
11416:                 $confhash{$newid}{'custom'}{$name} = $value;
11417:             }
11418:         } else {
11419:             my $error = &mt('Failed to acquire unique ID for new external tool');   
11420:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11421:         }
11422:     }
11423:     if (ref($domconfig{$action}) eq 'HASH') {
11424:         my %deletions;
11425:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
11426:         if (@todelete) {
11427:             map { $deletions{$_} = 1; } @todelete;
11428:         }
11429:         my %customadds;
11430:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
11431:         if (@newcustom) {
11432:             map { $customadds{$_} = 1; } @newcustom;
11433:         } 
11434:         my %imgdeletions;
11435:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
11436:         if (@todeleteimages) {
11437:             map { $imgdeletions{$_} = 1; } @todeleteimages;
11438:         }
11439:         my $maxnum = $env{'form.ltitools_maxnum'};
11440:         for (my $i=0; $i<=$maxnum; $i++) {
11441:             my $itemid = $env{'form.ltitools_id_'.$i};
11442:             $itemid =~ s/\D+//g;
11443:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11444:                 if ($deletions{$itemid}) {
11445:                     if ($domconfig{$action}{$itemid}{'image'}) {
11446:                         #FIXME need to obsolete item in RES space
11447:                     }
11448:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
11449:                     next;
11450:                 } else {
11451:                     my $newpos = $env{'form.ltitools_'.$itemid};
11452:                     $newpos =~ s/\D+//g;
11453:                     foreach my $item ('title','url','lifetime') {
11454:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11455:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
11456:                             $changes{$itemid} = 1;
11457:                         }
11458:                     }
11459:                     foreach my $item ('key','secret') {
11460:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11461:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
11462:                             $changes{$itemid} = 1;
11463:                         }
11464:                     }
11465:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
11466:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
11467:                     }
11468:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
11469:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
11470:                     }
11471:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
11472:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
11473:                     } else {
11474:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
11475:                     }
11476:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
11477:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
11478:                             $changes{$itemid} = 1;
11479:                         }
11480:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
11481:                         $changes{$itemid} = 1;
11482:                     }
11483:                     foreach my $size ('width','height') {
11484:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
11485:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
11486:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
11487:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
11488:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11489:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
11490:                                     $changes{$itemid} = 1;
11491:                                 }
11492:                             } else {
11493:                                 $changes{$itemid} = 1;
11494:                             }
11495:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11496:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
11497:                                 $changes{$itemid} = 1;
11498:                             }
11499:                         }
11500:                     }
11501:                     foreach my $item ('linktext','explanation') {
11502:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
11503:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
11504:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
11505:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11506:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11507:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
11508:                                     $changes{$itemid} = 1;
11509:                                 }
11510:                             } else {
11511:                                 $changes{$itemid} = 1;
11512:                             }
11513:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11514:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
11515:                                 $changes{$itemid} = 1;
11516:                             }
11517:                         }
11518:                     }
11519:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
11520:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11521:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
11522:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11523:                     } else {
11524:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
11525:                     }
11526:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11527:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
11528:                             $changes{$itemid} = 1;
11529:                         }
11530:                     } else {
11531:                         $changes{$itemid} = 1;
11532:                     }
11533:                     foreach my $extra ('passback','roster') {
11534:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
11535:                             $confhash{$itemid}{$extra} = 1;
11536:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
11537:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
11538:                                 $lifetime =~ s/^\s+|\s+$//g;
11539:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
11540:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
11541:                                 }
11542:                             }
11543:                         }
11544:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
11545:                             $changes{$itemid} = 1;
11546:                         }
11547:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
11548:                             $changes{$itemid} = 1;
11549:                         }
11550:                     }
11551:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
11552:                     foreach my $item ('label','title','target','linktext','explanation','append') {
11553:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
11554:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
11555:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
11556:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
11557:                                     $changes{$itemid} = 1;
11558:                                 }
11559:                             } else {
11560:                                 $changes{$itemid} = 1;
11561:                             }
11562:                         }
11563:                     }
11564:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
11565:                     foreach my $field (@fields) {
11566:                         if ($possfield{$field}) {
11567:                             if ($field eq 'roles') {
11568:                                 foreach my $role (@courseroles) {
11569:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
11570:                                     if (($choice ne '') && ($posslti{$choice})) {
11571:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
11572:                                         if ($role eq 'cc') {
11573:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
11574:                                         }
11575:                                     }
11576:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
11577:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
11578:                                             $changes{$itemid} = 1;
11579:                                         }
11580:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
11581:                                         $changes{$itemid} = 1;
11582:                                     }
11583:                                 }
11584:                             } else {
11585:                                 $confhash{$itemid}{'fields'}{$field} = 1;
11586:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
11587:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
11588:                                         $changes{$itemid} = 1;
11589:                                     }
11590:                                 } else {
11591:                                     $changes{$itemid} = 1;
11592:                                 }
11593:                             }
11594:                         }
11595:                     }
11596:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11597:                         if ($confhash{$itemid}{'fields'}{'user'}) {
11598:                             if ($env{'form.ltitools_userincdom_'.$i}) {
11599:                                 $confhash{$itemid}{'incdom'} = 1;
11600:                             }
11601:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
11602:                                 $changes{$itemid} = 1;
11603:                             }
11604:                         }
11605:                     }
11606:                     $allpos[$newpos] = $itemid;
11607:                 }
11608:                 if ($imgdeletions{$itemid}) {
11609:                     $changes{$itemid} = 1;
11610:                     #FIXME need to obsolete item in RES space
11611:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
11612:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
11613:                                                                  $itemid,$configuserok,$switchserver,
11614:                                                                  $author_ok);
11615:                     if ($imgurl) {
11616:                         $confhash{$itemid}{'image'} = $imgurl;
11617:                         $changes{$itemid} = 1;
11618:                     }
11619:                     if ($error) {
11620:                         &Apache::lonnet::logthis($error);
11621:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11622:                     }
11623:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
11624:                     $confhash{$itemid}{'image'} =
11625:                        $domconfig{$action}{$itemid}{'image'};
11626:                 }
11627:                 if ($customadds{$i}) {
11628:                     my $name = $env{'form.ltitools_custom_name_'.$i};
11629:                     $name =~ s/(`)/'/g;
11630:                     $name =~ s/^\s+//;
11631:                     $name =~ s/\s+$//;
11632:                     my $value = $env{'form.ltitools_custom_value_'.$i};
11633:                     $value =~ s/(`)/'/g;
11634:                     $value =~ s/^\s+//;
11635:                     $value =~ s/\s+$//;
11636:                     if ($name ne '') {
11637:                         $confhash{$itemid}{'custom'}{$name} = $value;
11638:                         $changes{$itemid} = 1;
11639:                     }
11640:                 }
11641:                 my %customdels;
11642:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
11643:                 if (@customdeletions) {
11644:                     $changes{$itemid} = 1;
11645:                 }
11646:                 map { $customdels{$_} = 1; } @customdeletions;
11647:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
11648:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
11649:                         unless ($customdels{$key}) {
11650:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
11651:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
11652:                             }
11653:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
11654:                                 $changes{$itemid} = 1;
11655:                             }
11656:                         }
11657:                     }
11658:                 }
11659:                 unless ($changes{$itemid}) {
11660:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
11661:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
11662:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
11663:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
11664:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
11665:                                         $changes{$itemid} = 1;
11666:                                         last;
11667:                                     }
11668:                                 }
11669:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
11670:                                 $changes{$itemid} = 1;
11671:                             }
11672:                         }
11673:                         last if ($changes{$itemid});
11674:                     }
11675:                 }
11676:             }
11677:         }
11678:     }
11679:     if (@allpos > 0) {
11680:         my $idx = 0;
11681:         foreach my $itemid (@allpos) {
11682:             if ($itemid ne '') {
11683:                 $confhash{$itemid}{'order'} = $idx;
11684:                 if (ref($domconfig{$action}) eq 'HASH') {
11685:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11686:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
11687:                             $changes{$itemid} = 1;
11688:                         }
11689:                     }
11690:                 }
11691:                 $idx ++;
11692:             }
11693:         }
11694:     }
11695:     my %ltitoolshash = (
11696:                           $action => { %confhash }
11697:                        );
11698:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
11699:                                              $dom);
11700:     if ($putresult eq 'ok') {
11701:         my %ltienchash = (
11702:                              $action => { %encconfig }
11703:                          );
11704:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
11705:         if (keys(%changes) > 0) {
11706:             my $cachetime = 24*60*60;
11707:             my %ltiall = %confhash;
11708:             foreach my $id (keys(%ltiall)) {
11709:                 if (ref($encconfig{$id}) eq 'HASH') {
11710:                     foreach my $item ('key','secret') {
11711:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
11712:                     }
11713:                 }
11714:             }
11715:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
11716:             if (ref($lastactref) eq 'HASH') {
11717:                 $lastactref->{'ltitools'} = 1;
11718:             }
11719:             $resulttext = &mt('Changes made:').'<ul>';
11720:             my %bynum;
11721:             foreach my $itemid (sort(keys(%changes))) {
11722:                 my $position = $confhash{$itemid}{'order'};
11723:                 $bynum{$position} = $itemid;
11724:             }
11725:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
11726:                 my $itemid = $bynum{$pos}; 
11727:                 if (ref($confhash{$itemid}) ne 'HASH') {
11728:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
11729:                 } else {
11730:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
11731:                     if ($confhash{$itemid}{'image'}) {
11732:                         $resulttext .= '&nbsp;'.
11733:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
11734:                                        ' alt="'.&mt('Tool Provider icon').'" />';
11735:                     }
11736:                     $resulttext .= '</li><ul>';
11737:                     my $position = $pos + 1;
11738:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
11739:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
11740:                         if ($confhash{$itemid}{$item} ne '') {
11741:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
11742:                         }
11743:                     }
11744:                     if ($encconfig{$itemid}{'key'} ne '') {
11745:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
11746:                     }
11747:                     if ($encconfig{$itemid}{'secret'} ne '') {
11748:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
11749:                         my $num = length($encconfig{$itemid}{'secret'});
11750:                         $resulttext .= ('*'x$num).'</li>';
11751:                     }
11752:                     $resulttext .= '<li>'.&mt('Configurable in course:');
11753:                     my @possconfig = ('label','title','target','linktext','explanation','append');
11754:                     my $numconfig = 0; 
11755:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
11756:                         foreach my $item (@possconfig) {
11757:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
11758:                                 $numconfig ++;
11759:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
11760:                             }
11761:                         }
11762:                     }
11763:                     if (!$numconfig) {
11764:                         $resulttext .= &mt('None');
11765:                     }
11766:                     $resulttext .= '</li>';
11767:                     foreach my $item ('passback','roster') {
11768:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
11769:                         if ($confhash{$itemid}{$item}) {
11770:                             $resulttext .= &mt('Yes');
11771:                             if ($confhash{$itemid}{$item.'valid'}) {
11772:                                 if ($item eq 'passback') {
11773:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
11774:                                                            $confhash{$itemid}{$item.'valid'});
11775:                                 } else {
11776:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
11777:                                                            $confhash{$itemid}{$item.'valid'});
11778:                                 }
11779:                             }
11780:                         } else {
11781:                             $resulttext .= &mt('No');
11782:                         }
11783:                         $resulttext .= '</li>';
11784:                     }
11785:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
11786:                         my $displaylist;
11787:                         if ($confhash{$itemid}{'display'}{'target'}) {
11788:                             $displaylist = &mt('Display target').':&nbsp;'.
11789:                                            $confhash{$itemid}{'display'}{'target'}.',';
11790:                         }
11791:                         foreach my $size ('width','height') { 
11792:                             if ($confhash{$itemid}{'display'}{$size}) {
11793:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
11794:                                                 $confhash{$itemid}{'display'}{$size}.',';
11795:                             }
11796:                         }
11797:                         if ($displaylist) {
11798:                             $displaylist =~ s/,$//;
11799:                             $resulttext .= '<li>'.$displaylist.'</li>';
11800:                         }
11801:                         foreach my $item ('linktext','explanation') {
11802:                             if ($confhash{$itemid}{'display'}{$item}) {
11803:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
11804:                             }
11805:                         }
11806:                     }
11807:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11808:                         my $fieldlist;
11809:                         foreach my $field (@allfields) {
11810:                             if ($confhash{$itemid}{'fields'}{$field}) {
11811:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
11812:                             }
11813:                         }
11814:                         if ($fieldlist) {
11815:                             $fieldlist =~ s/,$//;
11816:                             if ($confhash{$itemid}{'fields'}{'user'}) {
11817:                                 if ($confhash{$itemid}{'incdom'}) {
11818:                                     $fieldlist .= ' ('.&mt('username:domain').')';
11819:                                 } else {
11820:                                     $fieldlist .= ' ('.&mt('username').')';
11821:                                 }
11822:                             }
11823:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
11824:                         }
11825:                     }
11826:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
11827:                         my $rolemaps;
11828:                         foreach my $role (@courseroles) {
11829:                             if ($confhash{$itemid}{'roles'}{$role}) {
11830:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
11831:                                              $confhash{$itemid}{'roles'}{$role}.',';
11832:                             }
11833:                         }
11834:                         if ($rolemaps) {
11835:                             $rolemaps =~ s/,$//; 
11836:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
11837:                         }
11838:                     }
11839:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
11840:                         my $customlist;
11841:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
11842:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
11843:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
11844:                             } 
11845:                         }
11846:                         if ($customlist) {
11847:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
11848:                         }
11849:                     } 
11850:                     $resulttext .= '</ul></li>';
11851:                 }
11852:             }
11853:             $resulttext .= '</ul>';
11854:         } else {
11855:             $resulttext = &mt('No changes made.');
11856:         }
11857:     } else {
11858:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11859:     }
11860:     if ($errors) {
11861:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11862:                        $errors.'</ul>';
11863:     }
11864:     return $resulttext;
11865: }
11866: 
11867: sub process_ltitools_image {
11868:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
11869:     my $filename = $env{'form.'.$caller.'.filename'};
11870:     my ($error,$url);
11871:     my ($width,$height) = (21,21);
11872:     if ($configuserok eq 'ok') {
11873:         if ($switchserver) {
11874:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
11875:                          $switchserver);
11876:         } elsif ($author_ok eq 'ok') {
11877:             my ($result,$imageurl,$madethumb) =
11878:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11879:                              "ltitools/$itemid/icon",$width,$height);
11880:             if ($result eq 'ok') {
11881:                 if ($madethumb) {
11882:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
11883:                     my $imagethumb = "$path/tn-".$imagefile;
11884:                     $url = $imagethumb;
11885:                 } else {
11886:                     $url = $imageurl;
11887:                 }
11888:             } else {
11889:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11890:             }
11891:         } else {
11892:             $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);
11893:         }
11894:     } else {
11895:         $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);
11896:     }
11897:     return ($url,$error);
11898: }
11899: 
11900: sub get_ltitools_id {
11901:     my ($cdom,$title) = @_;
11902:     # get lock on ltitools db
11903:     my $lockhash = {
11904:                       lock => $env{'user.name'}.
11905:                               ':'.$env{'user.domain'},
11906:                    };
11907:     my $tries = 0;
11908:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11909:     my ($id,$error);
11910:  
11911:     while (($gotlock ne 'ok') && ($tries<10)) {
11912:         $tries ++;
11913:         sleep (0.1);
11914:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11915:     }
11916:     if ($gotlock eq 'ok') {
11917:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
11918:         if ($currids{'lock'}) {
11919:             delete($currids{'lock'});
11920:             if (keys(%currids)) {
11921:                 my @curr = sort { $a <=> $b } keys(%currids);
11922:                 if ($curr[-1] =~ /^\d+$/) {
11923:                     $id = 1 + $curr[-1];
11924:                 }
11925:             } else {
11926:                 $id = 1;
11927:             }
11928:             if ($id) {
11929:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
11930:                     $error = 'nostore';
11931:                 }
11932:             } else {
11933:                 $error = 'nonumber';
11934:             }
11935:         }
11936:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
11937:     } else {
11938:         $error = 'nolock';
11939:     }
11940:     return ($id,$error);
11941: }
11942: 
11943: sub modify_lti {
11944:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11945:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11946:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11947:     my (%posslti,%posslticrs,%posscrstype);
11948:     my @courseroles = ('cc','in','ta','ep','st');
11949:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
11950:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
11951:     my @coursetypes = ('official','unofficial','community','textbook','placement');
11952:     my %coursetypetitles = &Apache::lonlocal::texthash (
11953:                                official   => 'Official',
11954:                                unofficial => 'Unofficial',
11955:                                community  => 'Community',
11956:                                textbook   => 'Textbook',
11957:                                placement  => 'Placement Test',
11958:     );
11959:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11960:     my %lt = &lti_names();
11961:     map { $posslti{$_} = 1; } @ltiroles;
11962:     map { $posslticrs{$_} = 1; } @lticourseroles;
11963:     map { $posscrstype{$_} = 1; } @coursetypes;
11964: 
11965:     my %menutitles = &ltimenu_titles();
11966: 
11967:     my (@items,%deletions,%itemids);
11968:     if ($env{'form.lti_add'}) {
11969:         my $consumer = $env{'form.lti_consumer_add'};
11970:         $consumer =~ s/(`)/'/g;
11971:         ($newid,my $error) = &get_lti_id($dom,$consumer);
11972:         if ($newid) {
11973:             $itemids{'add'} = $newid;
11974:             push(@items,'add');
11975:             $changes{$newid} = 1;
11976:         } else {
11977:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
11978:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11979:         }
11980:     }
11981:     if (ref($domconfig{$action}) eq 'HASH') {
11982:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
11983:         if (@todelete) {
11984:             map { $deletions{$_} = 1; } @todelete;
11985:         }
11986:         my $maxnum = $env{'form.lti_maxnum'};
11987:         for (my $i=0; $i<=$maxnum; $i++) {
11988:             my $itemid = $env{'form.lti_id_'.$i};
11989:             $itemid =~ s/\D+//g;
11990:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11991:                 if ($deletions{$itemid}) {
11992:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
11993:                 } else {
11994:                    push(@items,$i);
11995:                    $itemids{$i} = $itemid;
11996:                 }
11997:             }
11998:         }
11999:     }
12000:     foreach my $idx (@items) {
12001:         my $itemid = $itemids{$idx};
12002:         next unless ($itemid);
12003:         my $position = $env{'form.lti_pos_'.$idx};
12004:         $position =~ s/\D+//g;
12005:         if ($position ne '') {
12006:             $allpos[$position] = $itemid;
12007:         }
12008:         foreach my $item ('consumer','key','secret','lifetime','requser') {
12009:             my $formitem = 'form.lti_'.$item.'_'.$idx;
12010:             $env{$formitem} =~ s/(`)/'/g;
12011:             if ($item eq 'lifetime') {
12012:                 $env{$formitem} =~ s/[^\d.]//g;
12013:             }
12014:             if ($env{$formitem} ne '') {
12015:                 if (($item eq 'key') || ($item eq 'secret')) {
12016:                     $encconfig{$itemid}{$item} = $env{$formitem};
12017:                 } else {
12018:                     $confhash{$itemid}{$item} = $env{$formitem};
12019:                     unless (($idx eq 'add') || ($changes{$itemid})) {
12020:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
12021:                             $changes{$itemid} = 1;
12022:                         }
12023:                     }
12024:                 }
12025:             }
12026:         }
12027:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
12028:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
12029:         }
12030:         if ($confhash{$itemid}{'requser'}) {
12031:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
12032:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
12033:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
12034:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
12035:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
12036:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
12037:                 $mapuser =~ s/(`)/'/g;
12038:                 $mapuser =~ s/^\s+|\s+$//g; 
12039:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
12040:             }
12041:             foreach my $ltirole (@lticourseroles) {
12042:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
12043:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
12044:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
12045:                 }
12046:             }
12047:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
12048:             my @makeuser;
12049:             foreach my $ltirole (sort(@possmakeuser)) {
12050:                 if ($posslti{$ltirole}) {
12051:                     push(@makeuser,$ltirole);
12052:                 }
12053:             }
12054:             $confhash{$itemid}{'makeuser'} = \@makeuser;
12055:             if (@makeuser) {
12056:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
12057:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
12058:                     $confhash{$itemid}{'lcauth'} = $lcauth;
12059:                     if ($lcauth ne 'internal') {
12060:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
12061:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
12062:                         $lcauthparm =~ s/`//g;
12063:                         if ($lcauthparm ne '') {
12064:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
12065:                         }
12066:                     }
12067:                 } else {
12068:                     $confhash{$itemid}{'lcauth'} = 'lti';
12069:                 }
12070:             }
12071:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
12072:             if (@possinstdata) {
12073:                 foreach my $field (@possinstdata) {
12074:                     if (exists($fieldtitles{$field})) {
12075:                         push(@{$confhash{$itemid}{'instdata'}});
12076:                     }
12077:                 }
12078:             }
12079:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
12080:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
12081:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
12082:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
12083:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
12084:                 $mapcrs =~ s/(`)/'/g;
12085:                 $mapcrs =~ s/^\s+|\s+$//g;
12086:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
12087:             }
12088:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
12089:             my @crstypes;
12090:             foreach my $type (sort(@posstypes)) {
12091:                 if ($posscrstype{$type}) {
12092:                     push(@crstypes,$type);
12093:                 }
12094:             }
12095:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
12096:             if ($env{'form.lti_makecrs_'.$idx}) {
12097:                 $confhash{$itemid}{'makecrs'} = 1;
12098:             }
12099:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
12100:             my @selfenroll;
12101:             foreach my $type (sort(@possenroll)) {
12102:                 if ($posslticrs{$type}) {
12103:                     push(@selfenroll,$type);
12104:                 }
12105:             }
12106:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
12107:             if ($env{'form.lti_crssec_'.$idx}) {
12108:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
12109:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
12110:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
12111:                     my $section = $env{'form.lti_customsection_'.$idx};
12112:                     $section =~ s/(`)/'/g;
12113:                     $section =~ s/^\s+|\s+$//g;
12114:                     if ($section ne '') {
12115:                         $confhash{$itemid}{'section'} = $section;
12116:                     }
12117:                 }
12118:             }
12119:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
12120:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
12121:                     $confhash{$itemid}{$field} = 1;
12122:                 }
12123:             }
12124:             if ($env{'form.lti_passback_'.$idx}) {
12125:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
12126:                     $confhash{$itemid}{'passbackformat'} = '1.0';
12127:                 } else {
12128:                     $confhash{$itemid}{'passbackformat'} = '1.1';
12129:                 }
12130:             }
12131:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
12132:                 $confhash{$itemid}{lcmenu} = [];
12133:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
12134:                 foreach my $field (@possmenu) {
12135:                     if (exists($menutitles{$field})) {
12136:                         if ($field eq 'grades') {
12137:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
12138:                         }
12139:                         push(@{$confhash{$itemid}{lcmenu}},$field);
12140:                     }
12141:                 }
12142:             }
12143:             unless (($idx eq 'add') || ($changes{$itemid})) {
12144:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu') {
12145:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
12146:                         $changes{$itemid} = 1;
12147:                     }
12148:                 }
12149:                 unless ($changes{$itemid}) {
12150:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
12151:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
12152:                             $changes{$itemid} = 1;
12153:                         }
12154:                     }
12155:                 }
12156:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
12157:                     unless ($changes{$itemid}) {
12158:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
12159:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12160:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
12161:                                                                                $confhash{$itemid}{$field});
12162:                                 if (@diffs) {
12163:                                     $changes{$itemid} = 1;
12164:                                 }
12165:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
12166:                                 $changes{$itemid} = 1;
12167:                             }
12168:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12169:                             if (@{$confhash{$itemid}{$field}} > 0) {
12170:                                 $changes{$itemid} = 1;
12171:                             }
12172:                         }
12173:                     }
12174:                 }
12175:                 unless ($changes{$itemid}) {
12176:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
12177:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12178:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
12179:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
12180:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
12181:                                     $changes{$itemid} = 1;
12182:                                     last;
12183:                                 }
12184:                             }
12185:                             unless ($changes{$itemid}) {
12186:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
12187:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
12188:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
12189:                                         $changes{$itemid} = 1;
12190:                                         last;
12191:                                     }
12192:                                 }
12193:                             }
12194:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
12195:                             $changes{$itemid} = 1;
12196:                         }
12197:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12198:                         unless ($changes{$itemid}) {
12199:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
12200:                                 $changes{$itemid} = 1;
12201:                             }
12202:                         }
12203:                     }
12204:                 }
12205:             }
12206:         }
12207:     }
12208:     if (@allpos > 0) {
12209:         my $idx = 0;
12210:         foreach my $itemid (@allpos) {
12211:             if ($itemid ne '') {
12212:                 $confhash{$itemid}{'order'} = $idx;
12213:                 if (ref($domconfig{$action}) eq 'HASH') {
12214:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12215:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
12216:                             $changes{$itemid} = 1;
12217:                         }
12218:                     }
12219:                 }
12220:                 $idx ++;
12221:             }
12222:         }
12223:     }
12224:     my %ltihash = (
12225:                           $action => { %confhash }
12226:                        );
12227:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
12228:                                              $dom);
12229:     if ($putresult eq 'ok') {
12230:         my %ltienchash = (
12231:                              $action => { %encconfig }
12232:                          );
12233:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
12234:         if (keys(%changes) > 0) {
12235:             my $cachetime = 24*60*60;
12236:             my %ltiall = %confhash;
12237:             foreach my $id (keys(%ltiall)) {
12238:                 if (ref($encconfig{$id}) eq 'HASH') {
12239:                     foreach my $item ('key','secret') {
12240:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
12241:                     }
12242:                 }
12243:             }
12244:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
12245:             if (ref($lastactref) eq 'HASH') {
12246:                 $lastactref->{'lti'} = 1;
12247:             }
12248:             $resulttext = &mt('Changes made:').'<ul>';
12249:             my %bynum;
12250:             foreach my $itemid (sort(keys(%changes))) {
12251:                 my $position = $confhash{$itemid}{'order'};
12252:                 $bynum{$position} = $itemid;
12253:             }
12254:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12255:                 my $itemid = $bynum{$pos};
12256:                 if (ref($confhash{$itemid}) ne 'HASH') {
12257:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12258:                 } else {
12259:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
12260:                     my $position = $pos + 1;
12261:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12262:                     foreach my $item ('version','lifetime') {
12263:                         if ($confhash{$itemid}{$item} ne '') {
12264:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
12265:                         }
12266:                     }
12267:                     if ($encconfig{$itemid}{'key'} ne '') {
12268:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
12269:                     }
12270:                     if ($encconfig{$itemid}{'secret'} ne '') {
12271:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
12272:                         my $num = length($encconfig{$itemid}{'secret'});
12273:                         $resulttext .= ('*'x$num).'</li>';
12274:                     }
12275:                     if ($confhash{$itemid}{'requser'}) {
12276:                         if ($confhash{$itemid}{'mapuser'}) {
12277:                             my $shownmapuser;
12278:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
12279:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
12280:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
12281:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
12282:                             } else {
12283:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
12284:                             }
12285:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
12286:                         }
12287:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12288:                             my $rolemaps;
12289:                             foreach my $role (@ltiroles) {
12290:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
12291:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
12292:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
12293:                                                                             'Course').',';
12294:                                 }
12295:                             }
12296:                             if ($rolemaps) {
12297:                                 $rolemaps =~ s/,$//;
12298:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
12299:                             }
12300:                         }
12301:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
12302:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
12303:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
12304:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
12305:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
12306:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
12307:                                 } else {
12308:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
12309:                                                        $confhash{$itemid}{'lcauth'});
12310:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
12311:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
12312:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
12313:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
12314:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
12315:                                         }
12316:                                     } else {
12317:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
12318:                                     }
12319:                                 }
12320:                                 $resulttext .= '</li>';
12321:                             } else {
12322:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
12323:                             }
12324:                         }
12325:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
12326:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
12327:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
12328:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
12329:                             } else {
12330:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
12331:                             }
12332:                         }
12333:                         if ($confhash{$itemid}{'mapcrs'}) {
12334:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
12335:                         }
12336:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
12337:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
12338:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
12339:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
12340:                                                '</li>';
12341:                             } else {
12342:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
12343:                             }
12344:                         }
12345:                         if ($confhash{$itemid}{'makecrs'}) {
12346:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
12347:                         } else {
12348:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
12349:                         }
12350:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
12351:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
12352:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
12353:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
12354:                                                '</li>';
12355:                             } else {
12356:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
12357:                             }
12358:                         }
12359:                         if ($confhash{$itemid}{'section'}) {
12360:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
12361:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
12362:                                                      ' (course_section_sourcedid)'.'</li>';  
12363:                             } else {
12364:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
12365:                                                       $confhash{$itemid}{'section'}.'</li>';
12366:                             }
12367:                         } else {
12368:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
12369:                         }
12370:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
12371:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
12372:                             if ($confhash{$itemid}{$item}) {
12373:                                 $resulttext .= &mt('Yes');
12374:                                 if ($item eq 'passback') {
12375:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
12376:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
12377:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
12378:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
12379:                                     }
12380:                                 }
12381:                             } else {
12382:                                 $resulttext .= &mt('No');
12383:                             }
12384:                             $resulttext .= '</li>';
12385:                         }
12386:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
12387:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
12388:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
12389:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
12390:                             } else {
12391:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
12392:                             }
12393:                         }
12394:                     }
12395:                     $resulttext .= '</ul></li>';
12396:                 }
12397:             }
12398:             $resulttext .= '</ul>';
12399:         } else {
12400:             $resulttext = &mt('No changes made.');
12401:         }
12402:     } else {
12403:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12404:     }
12405:     if ($errors) {
12406:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12407:                        $errors.'</ul>';
12408:     }
12409:     return $resulttext;
12410: }
12411: 
12412: sub get_lti_id {
12413:     my ($domain,$consumer) = @_;
12414:     # get lock on lti db
12415:     my $lockhash = {
12416:                       lock => $env{'user.name'}.
12417:                               ':'.$env{'user.domain'},
12418:                    };
12419:     my $tries = 0;
12420:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12421:     my ($id,$error);
12422: 
12423:     while (($gotlock ne 'ok') && ($tries<10)) {
12424:         $tries ++;
12425:         sleep (0.1);
12426:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12427:     }
12428:     if ($gotlock eq 'ok') {
12429:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
12430:         if ($currids{'lock'}) {
12431:             delete($currids{'lock'});
12432:             if (keys(%currids)) {
12433:                 my @curr = sort { $a <=> $b } keys(%currids);
12434:                 if ($curr[-1] =~ /^\d+$/) {
12435:                     $id = 1 + $curr[-1];
12436:                 }
12437:             } else {
12438:                 $id = 1;
12439:             }
12440:             if ($id) {
12441:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
12442:                     $error = 'nostore';
12443:                 }
12444:             } else {
12445:                 $error = 'nonumber';
12446:             }
12447:         }
12448:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
12449:     } else {
12450:         $error = 'nolock';
12451:     }
12452:     return ($id,$error);
12453: }
12454: 
12455: sub modify_autoenroll {
12456:     my ($dom,$lastactref,%domconfig) = @_;
12457:     my ($resulttext,%changes);
12458:     my %currautoenroll;
12459:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
12460:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
12461:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
12462:         }
12463:     }
12464:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
12465:     my %title = ( run => 'Auto-enrollment active',
12466:                   sender => 'Sender for notification messages',
12467:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
12468:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
12469:     my @offon = ('off','on');
12470:     my $sender_uname = $env{'form.sender_uname'};
12471:     my $sender_domain = $env{'form.sender_domain'};
12472:     if ($sender_domain eq '') {
12473:         $sender_uname = '';
12474:     } elsif ($sender_uname eq '') {
12475:         $sender_domain = '';
12476:     }
12477:     my $coowners = $env{'form.autoassign_coowners'};
12478:     my $failsafe = $env{'form.autoenroll_failsafe'};
12479:     $failsafe =~ s{^\s+|\s+$}{}g;
12480:     if ($failsafe =~ /\D/) {
12481:         undef($failsafe);
12482:     }
12483:     my %autoenrollhash =  (
12484:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
12485:                                        'sender_uname' => $sender_uname,
12486:                                        'sender_domain' => $sender_domain,
12487:                                        'co-owners' => $coowners,
12488:                                        'autofailsafe' => $failsafe,
12489:                                 }
12490:                      );
12491:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
12492:                                              $dom);
12493:     if ($putresult eq 'ok') {
12494:         if (exists($currautoenroll{'run'})) {
12495:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
12496:                  $changes{'run'} = 1;
12497:              }
12498:         } elsif ($autorun) {
12499:             if ($env{'form.autoenroll_run'} ne '1') {
12500:                  $changes{'run'} = 1;
12501:             }
12502:         }
12503:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
12504:             $changes{'sender'} = 1;
12505:         }
12506:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
12507:             $changes{'sender'} = 1;
12508:         }
12509:         if ($currautoenroll{'co-owners'} ne '') {
12510:             if ($currautoenroll{'co-owners'} ne $coowners) {
12511:                 $changes{'coowners'} = 1;
12512:             }
12513:         } elsif ($coowners) {
12514:             $changes{'coowners'} = 1;
12515:         }
12516:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
12517:             $changes{'autofailsafe'} = 1;
12518:         }
12519:         if (keys(%changes) > 0) {
12520:             $resulttext = &mt('Changes made:').'<ul>';
12521:             if ($changes{'run'}) {
12522:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
12523:             }
12524:             if ($changes{'sender'}) {
12525:                 if ($sender_uname eq '' || $sender_domain eq '') {
12526:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
12527:                 } else {
12528:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
12529:                 }
12530:             }
12531:             if ($changes{'coowners'}) {
12532:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
12533:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12534:                 if (ref($lastactref) eq 'HASH') {
12535:                     $lastactref->{'domainconfig'} = 1;
12536:                 }
12537:             }
12538:             if ($changes{'autofailsafe'}) {
12539:                 if ($failsafe ne '') {
12540:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
12541:                 } else {
12542:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
12543:                 }
12544:                 &Apache::lonnet::get_domain_defaults($dom,1);
12545:                 if (ref($lastactref) eq 'HASH') {
12546:                     $lastactref->{'domdefaults'} = 1;
12547:                 }
12548:             }
12549:             $resulttext .= '</ul>';
12550:         } else {
12551:             $resulttext = &mt('No changes made to auto-enrollment settings');
12552:         }
12553:     } else {
12554:         $resulttext = '<span class="LC_error">'.
12555: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12556:     }
12557:     return $resulttext;
12558: }
12559: 
12560: sub modify_autoupdate {
12561:     my ($dom,%domconfig) = @_;
12562:     my ($resulttext,%currautoupdate,%fields,%changes);
12563:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
12564:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
12565:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
12566:         }
12567:     }
12568:     my @offon = ('off','on');
12569:     my %title = &Apache::lonlocal::texthash (
12570:                    run => 'Auto-update:',
12571:                    classlists => 'Updates to user information in classlists?'
12572:                 );
12573:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12574:     my %fieldtitles = &Apache::lonlocal::texthash (
12575:                         id => 'Student/Employee ID',
12576:                         permanentemail => 'E-mail address',
12577:                         lastname => 'Last Name',
12578:                         firstname => 'First Name',
12579:                         middlename => 'Middle Name',
12580:                         generation => 'Generation',
12581:                       );
12582:     $othertitle = &mt('All users');
12583:     if (keys(%{$usertypes}) >  0) {
12584:         $othertitle = &mt('Other users');
12585:     }
12586:     foreach my $key (keys(%env)) {
12587:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
12588:             my ($usertype,$item) = ($1,$2);
12589:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
12590:                 if ($usertype eq 'default') {   
12591:                     push(@{$fields{$1}},$2);
12592:                 } elsif (ref($types) eq 'ARRAY') {
12593:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
12594:                         push(@{$fields{$1}},$2);
12595:                     }
12596:                 }
12597:             }
12598:         }
12599:     }
12600:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
12601:     @lockablenames = sort(@lockablenames);
12602:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
12603:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12604:         if (@changed) {
12605:             $changes{'lockablenames'} = 1;
12606:         }
12607:     } else {
12608:         if (@lockablenames) {
12609:             $changes{'lockablenames'} = 1;
12610:         }
12611:     }
12612:     my %updatehash = (
12613:                       autoupdate => { run => $env{'form.autoupdate_run'},
12614:                                       classlists => $env{'form.classlists'},
12615:                                       fields => {%fields},
12616:                                       lockablenames => \@lockablenames,
12617:                                     }
12618:                      );
12619:     foreach my $key (keys(%currautoupdate)) {
12620:         if (($key eq 'run') || ($key eq 'classlists')) {
12621:             if (exists($updatehash{autoupdate}{$key})) {
12622:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
12623:                     $changes{$key} = 1;
12624:                 }
12625:             }
12626:         } elsif ($key eq 'fields') {
12627:             if (ref($currautoupdate{$key}) eq 'HASH') {
12628:                 foreach my $item (@{$types},'default') {
12629:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
12630:                         my $change = 0;
12631:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
12632:                             if (!exists($fields{$item})) {
12633:                                 $change = 1;
12634:                                 last;
12635:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
12636:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
12637:                                     $change = 1;
12638:                                     last;
12639:                                 }
12640:                             }
12641:                         }
12642:                         if ($change) {
12643:                             push(@{$changes{$key}},$item);
12644:                         }
12645:                     } 
12646:                 }
12647:             }
12648:         } elsif ($key eq 'lockablenames') {
12649:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
12650:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12651:                 if (@changed) {
12652:                     $changes{'lockablenames'} = 1;
12653:                 }
12654:             } else {
12655:                 if (@lockablenames) {
12656:                     $changes{'lockablenames'} = 1;
12657:                 }
12658:             }
12659:         }
12660:     }
12661:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
12662:         if (@lockablenames) {
12663:             $changes{'lockablenames'} = 1;
12664:         }
12665:     }
12666:     foreach my $item (@{$types},'default') {
12667:         if (defined($fields{$item})) {
12668:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
12669:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
12670:                     my $change = 0;
12671:                     if (ref($fields{$item}) eq 'ARRAY') {
12672:                         foreach my $type (@{$fields{$item}}) {
12673:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
12674:                                 $change = 1;
12675:                                 last;
12676:                             }
12677:                         }
12678:                     }
12679:                     if ($change) {
12680:                         push(@{$changes{'fields'}},$item);
12681:                     }
12682:                 } else {
12683:                     push(@{$changes{'fields'}},$item);
12684:                 }
12685:             } else {
12686:                 push(@{$changes{'fields'}},$item);
12687:             }
12688:         }
12689:     }
12690:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
12691:                                              $dom);
12692:     if ($putresult eq 'ok') {
12693:         if (keys(%changes) > 0) {
12694:             $resulttext = &mt('Changes made:').'<ul>';
12695:             foreach my $key (sort(keys(%changes))) {
12696:                 if ($key eq 'lockablenames') {
12697:                     $resulttext .= '<li>';
12698:                     if (@lockablenames) {
12699:                         $usertypes->{'default'} = $othertitle;
12700:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
12701:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
12702:                     } else {
12703:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
12704:                     }
12705:                     $resulttext .= '</li>';
12706:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
12707:                     foreach my $item (@{$changes{$key}}) {
12708:                         my @newvalues;
12709:                         foreach my $type (@{$fields{$item}}) {
12710:                             push(@newvalues,$fieldtitles{$type});
12711:                         }
12712:                         my $newvaluestr;
12713:                         if (@newvalues > 0) {
12714:                             $newvaluestr = join(', ',@newvalues);
12715:                         } else {
12716:                             $newvaluestr = &mt('none');
12717:                         }
12718:                         if ($item eq 'default') {
12719:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
12720:                         } else {
12721:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
12722:                         }
12723:                     }
12724:                 } else {
12725:                     my $newvalue;
12726:                     if ($key eq 'run') {
12727:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
12728:                     } else {
12729:                         $newvalue = $offon[$env{'form.'.$key}];
12730:                     }
12731:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
12732:                 }
12733:             }
12734:             $resulttext .= '</ul>';
12735:         } else {
12736:             $resulttext = &mt('No changes made to autoupdates');
12737:         }
12738:     } else {
12739:         $resulttext = '<span class="LC_error">'.
12740: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12741:     }
12742:     return $resulttext;
12743: }
12744: 
12745: sub modify_autocreate {
12746:     my ($dom,%domconfig) = @_;
12747:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
12748:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
12749:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
12750:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
12751:         }
12752:     }
12753:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
12754:                  req => 'Auto-creation of validated requests for official courses',
12755:                  xmldc => 'Identity of course creator of courses from XML files',
12756:                );
12757:     my @types = ('xml','req');
12758:     foreach my $item (@types) {
12759:         $newvals{$item} = $env{'form.autocreate_'.$item};
12760:         $newvals{$item} =~ s/\D//g;
12761:         $newvals{$item} = 0 if ($newvals{$item} eq '');
12762:     }
12763:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
12764:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12765:     unless (exists($domcoords{$newvals{'xmldc'}})) {
12766:         $newvals{'xmldc'} = '';
12767:     } 
12768:     %autocreatehash =  (
12769:                         autocreate => { xml => $newvals{'xml'},
12770:                                         req => $newvals{'req'},
12771:                                       }
12772:                        );
12773:     if ($newvals{'xmldc'} ne '') {
12774:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
12775:     }
12776:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
12777:                                              $dom);
12778:     if ($putresult eq 'ok') {
12779:         my @items = @types;
12780:         if ($newvals{'xml'}) {
12781:             push(@items,'xmldc');
12782:         }
12783:         foreach my $item (@items) {
12784:             if (exists($currautocreate{$item})) {
12785:                 if ($currautocreate{$item} ne $newvals{$item}) {
12786:                     $changes{$item} = 1;
12787:                 }
12788:             } elsif ($newvals{$item}) {
12789:                 $changes{$item} = 1;
12790:             }
12791:         }
12792:         if (keys(%changes) > 0) {
12793:             my @offon = ('off','on'); 
12794:             $resulttext = &mt('Changes made:').'<ul>';
12795:             foreach my $item (@types) {
12796:                 if ($changes{$item}) {
12797:                     my $newtxt = $offon[$newvals{$item}];
12798:                     $resulttext .= '<li>'.
12799:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
12800:                                        '<b>','</b>').
12801:                                    '</li>';
12802:                 }
12803:             }
12804:             if ($changes{'xmldc'}) {
12805:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
12806:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
12807:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
12808:             }
12809:             $resulttext .= '</ul>';
12810:         } else {
12811:             $resulttext = &mt('No changes made to auto-creation settings');
12812:         }
12813:     } else {
12814:         $resulttext = '<span class="LC_error">'.
12815:             &mt('An error occurred: [_1]',$putresult).'</span>';
12816:     }
12817:     return $resulttext;
12818: }
12819: 
12820: sub modify_directorysrch {
12821:     my ($dom,$lastactref,%domconfig) = @_;
12822:     my ($resulttext,%changes);
12823:     my %currdirsrch;
12824:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
12825:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
12826:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
12827:         }
12828:     }
12829:     my %title = ( available => 'Institutional directory search available',
12830:                   localonly => 'Other domains can search institution',
12831:                   lcavailable => 'LON-CAPA directory search available',
12832:                   lclocalonly => 'Other domains can search LON-CAPA domain',
12833:                   searchby => 'Search types',
12834:                   searchtypes => 'Search latitude');
12835:     my @offon = ('off','on');
12836:     my @otherdoms = ('Yes','No');
12837: 
12838:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
12839:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
12840:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
12841: 
12842:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12843:     if (keys(%{$usertypes}) == 0) {
12844:         @cansearch = ('default');
12845:     } else {
12846:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
12847:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
12848:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
12849:                     push(@{$changes{'cansearch'}},$type);
12850:                 }
12851:             }
12852:             foreach my $type (@cansearch) {
12853:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
12854:                     push(@{$changes{'cansearch'}},$type);
12855:                 }
12856:             }
12857:         } else {
12858:             push(@{$changes{'cansearch'}},@cansearch);
12859:         }
12860:     }
12861: 
12862:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
12863:         foreach my $by (@{$currdirsrch{'searchby'}}) {
12864:             if (!grep(/^\Q$by\E$/,@searchby)) {
12865:                 push(@{$changes{'searchby'}},$by);
12866:             }
12867:         }
12868:         foreach my $by (@searchby) {
12869:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
12870:                 push(@{$changes{'searchby'}},$by);
12871:             }
12872:         }
12873:     } else {
12874:         push(@{$changes{'searchby'}},@searchby);
12875:     }
12876: 
12877:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
12878:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
12879:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
12880:                 push(@{$changes{'searchtypes'}},$type);
12881:             }
12882:         }
12883:         foreach my $type (@searchtypes) {
12884:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
12885:                 push(@{$changes{'searchtypes'}},$type);
12886:             }
12887:         }
12888:     } else {
12889:         if (exists($currdirsrch{'searchtypes'})) {
12890:             foreach my $type (@searchtypes) {  
12891:                 if ($type ne $currdirsrch{'searchtypes'}) { 
12892:                     push(@{$changes{'searchtypes'}},$type);
12893:                 }
12894:             }
12895:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
12896:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
12897:             }   
12898:         } else {
12899:             push(@{$changes{'searchtypes'}},@searchtypes); 
12900:         }
12901:     }
12902: 
12903:     my %dirsrch_hash =  (
12904:             directorysrch => { available => $env{'form.dirsrch_available'},
12905:                                cansearch => \@cansearch,
12906:                                localonly => $env{'form.dirsrch_instlocalonly'},
12907:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
12908:                                lcavailable => $env{'form.dirsrch_domavailable'},
12909:                                searchby => \@searchby,
12910:                                searchtypes => \@searchtypes,
12911:                              }
12912:             );
12913:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
12914:                                              $dom);
12915:     if ($putresult eq 'ok') {
12916:         if (exists($currdirsrch{'available'})) {
12917:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
12918:                  $changes{'available'} = 1;
12919:              }
12920:         } else {
12921:             if ($env{'form.dirsrch_available'} eq '1') {
12922:                 $changes{'available'} = 1;
12923:             }
12924:         }
12925:         if (exists($currdirsrch{'lcavailable'})) {
12926:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
12927:                 $changes{'lcavailable'} = 1;
12928:             }
12929:         } else {
12930:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
12931:                 $changes{'lcavailable'} = 1;
12932:             }
12933:         }
12934:         if (exists($currdirsrch{'localonly'})) {
12935:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
12936:                 $changes{'localonly'} = 1;
12937:             }
12938:         } else {
12939:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
12940:                 $changes{'localonly'} = 1;
12941:             }
12942:         }
12943:         if (exists($currdirsrch{'lclocalonly'})) {
12944:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
12945:                 $changes{'lclocalonly'} = 1;
12946:             }
12947:         } else {
12948:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
12949:                 $changes{'lclocalonly'} = 1;
12950:             }
12951:         }
12952:         if (keys(%changes) > 0) {
12953:             $resulttext = &mt('Changes made:').'<ul>';
12954:             if ($changes{'available'}) {
12955:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
12956:             }
12957:             if ($changes{'lcavailable'}) {
12958:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
12959:             }
12960:             if ($changes{'localonly'}) {
12961:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
12962:             }
12963:             if ($changes{'lclocalonly'}) {
12964:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
12965:             }
12966:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
12967:                 my $chgtext;
12968:                 if (ref($usertypes) eq 'HASH') {
12969:                     if (keys(%{$usertypes}) > 0) {
12970:                         foreach my $type (@{$types}) {
12971:                             if (grep(/^\Q$type\E$/,@cansearch)) {
12972:                                 $chgtext .= $usertypes->{$type}.'; ';
12973:                             }
12974:                         }
12975:                         if (grep(/^default$/,@cansearch)) {
12976:                             $chgtext .= $othertitle;
12977:                         } else {
12978:                             $chgtext =~ s/\; $//;
12979:                         }
12980:                         $resulttext .=
12981:                             '<li>'.
12982:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
12983:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
12984:                             '</li>';
12985:                     }
12986:                 }
12987:             }
12988:             if (ref($changes{'searchby'}) eq 'ARRAY') {
12989:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
12990:                 my $chgtext;
12991:                 foreach my $type (@{$titleorder}) {
12992:                     if (grep(/^\Q$type\E$/,@searchby)) {
12993:                         if (defined($searchtitles->{$type})) {
12994:                             $chgtext .= $searchtitles->{$type}.'; ';
12995:                         }
12996:                     }
12997:                 }
12998:                 $chgtext =~ s/\; $//;
12999:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
13000:             }
13001:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
13002:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
13003:                 my $chgtext;
13004:                 foreach my $type (@{$srchtypeorder}) {
13005:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
13006:                         if (defined($srchtypes_desc->{$type})) {
13007:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
13008:                         }
13009:                     }
13010:                 }
13011:                 $chgtext =~ s/\; $//;
13012:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
13013:             }
13014:             $resulttext .= '</ul>';
13015:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
13016:             if (ref($lastactref) eq 'HASH') {
13017:                 $lastactref->{'directorysrch'} = 1;
13018:             }
13019:         } else {
13020:             $resulttext = &mt('No changes made to directory search settings');
13021:         }
13022:     } else {
13023:         $resulttext = '<span class="LC_error">'.
13024:                       &mt('An error occurred: [_1]',$putresult).'</span>';
13025:     }
13026:     return $resulttext;
13027: }
13028: 
13029: sub modify_contacts {
13030:     my ($dom,$lastactref,%domconfig) = @_;
13031:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
13032:     if (ref($domconfig{'contacts'}) eq 'HASH') {
13033:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
13034:             $currsetting{$key} = $domconfig{'contacts'}{$key};
13035:         }
13036:     }
13037:     my (%others,%to,%bcc,%includestr,%includeloc);
13038:     my @contacts = ('supportemail','adminemail');
13039:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
13040:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
13041:     my @toggles = ('reporterrors','reportupdates','reportstatus');
13042:     my @lonstatus = ('threshold','sysmail','weights','excluded');
13043:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
13044:     foreach my $type (@mailings) {
13045:         @{$newsetting{$type}} = 
13046:             &Apache::loncommon::get_env_multiple('form.'.$type);
13047:         foreach my $item (@contacts) {
13048:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
13049:                 $contacts_hash{contacts}{$type}{$item} = 1;
13050:             } else {
13051:                 $contacts_hash{contacts}{$type}{$item} = 0;
13052:             }
13053:         }
13054:         $others{$type} = $env{'form.'.$type.'_others'};
13055:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
13056:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13057:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
13058:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
13059:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
13060:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
13061:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
13062:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13063:             }
13064:         }
13065:     }
13066:     foreach my $item (@contacts) {
13067:         $to{$item} = $env{'form.'.$item};
13068:         $contacts_hash{'contacts'}{$item} = $to{$item};
13069:     }
13070:     foreach my $item (@toggles) {
13071:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13072:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
13073:         }
13074:     }
13075:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
13076:     foreach my $item (@lonstatus) {
13077:         if ($item eq 'excluded') {
13078:             my (%serverhomes,@excluded);
13079:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
13080:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
13081:             if (@possexcluded) {
13082:                 foreach my $id (sort(@possexcluded)) {
13083:                     if ($serverhomes{$id}) {
13084:                         push(@excluded,$id);
13085:                     }
13086:                 }
13087:             }
13088:             if (@excluded) {
13089:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
13090:             }
13091:         } elsif ($item eq 'weights') {
13092:             foreach my $type ('E','W','N') {
13093:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
13094:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
13095:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
13096:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
13097:                             $env{'form.error'.$item.'_'.$type};
13098:                     }
13099:                 }
13100:             }
13101:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
13102:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
13103:             if ($env{'form.error'.$item} =~ /^\d+$/) {
13104:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
13105:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
13106:                 }
13107:             }
13108:         }
13109:     }
13110:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
13111:         foreach my $field (@{$fields}) {
13112:             if (ref($possoptions->{$field}) eq 'ARRAY') {
13113:                 my $value = $env{'form.helpform_'.$field};
13114:                 $value =~ s/^\s+|\s+$//g;
13115:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
13116:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
13117:                     if ($field eq 'screenshot') {
13118:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
13119:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
13120:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
13121:                         }
13122:                     }
13123:                 }
13124:             }
13125:         }
13126:     }
13127:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13128:     my (@statuses,%usertypeshash,@overrides);
13129:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
13130:         @statuses = @{$types};
13131:         if (ref($usertypes) eq 'HASH') {
13132:             %usertypeshash = %{$usertypes};
13133:         }
13134:     }
13135:     if (@statuses) {
13136:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
13137:         foreach my $type (@possoverrides) {
13138:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
13139:                 push(@overrides,$type);
13140:             }
13141:         }
13142:         if (@overrides) {
13143:             foreach my $type (@overrides) {
13144:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
13145:                 foreach my $item (@contacts) {
13146:                     if (grep(/^\Q$item\E$/,@standard)) {
13147:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
13148:                         $newsetting{'override_'.$type}{$item} = 1;
13149:                     } else {
13150:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
13151:                         $newsetting{'override_'.$type}{$item} = 0;
13152:                     }
13153:                 }
13154:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
13155:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13156:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
13157:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13158:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
13159:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
13160:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
13161:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13162:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
13163:                 }
13164:             }     
13165:         }
13166:     }
13167:     if (keys(%currsetting) > 0) {
13168:         foreach my $item (@contacts) {
13169:             if ($to{$item} ne $currsetting{$item}) {
13170:                 $changes{$item} = 1;
13171:             }
13172:         }
13173:         foreach my $type (@mailings) {
13174:             foreach my $item (@contacts) {
13175:                 if (ref($currsetting{$type}) eq 'HASH') {
13176:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
13177:                         push(@{$changes{$type}},$item);
13178:                     }
13179:                 } else {
13180:                     push(@{$changes{$type}},@{$newsetting{$type}});
13181:                 }
13182:             }
13183:             if ($others{$type} ne $currsetting{$type}{'others'}) {
13184:                 push(@{$changes{$type}},'others');
13185:             }
13186:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13187:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
13188:                     push(@{$changes{$type}},'bcc'); 
13189:                 }
13190:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
13191:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
13192:                     push(@{$changes{$type}},'include');
13193:                 }
13194:             }
13195:         }
13196:         if (ref($fields) eq 'ARRAY') {
13197:             if (ref($currsetting{'helpform'}) eq 'HASH') {
13198:                 foreach my $field (@{$fields}) {
13199:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
13200:                         push(@{$changes{'helpform'}},$field);
13201:                     }
13202:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13203:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
13204:                             push(@{$changes{'helpform'}},'maxsize');
13205:                         }
13206:                     }
13207:                 }
13208:             } else {
13209:                 foreach my $field (@{$fields}) {
13210:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13211:                         push(@{$changes{'helpform'}},$field);
13212:                     }
13213:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13214:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13215:                             push(@{$changes{'helpform'}},'maxsize');
13216:                         }
13217:                     }
13218:                 }
13219:             }
13220:         }
13221:         if (@statuses) {
13222:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
13223:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
13224:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
13225:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
13226:                             foreach my $item (@contacts,'bcc','others','include') {
13227:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
13228:                                     push(@{$changes{'overrides'}},$key);
13229:                                     last;
13230:                                 }
13231:                             }
13232:                         } else {
13233:                             push(@{$changes{'overrides'}},$key);
13234:                         }
13235:                     }
13236:                 }
13237:                 foreach my $key (@overrides) {
13238:                     unless (exists($currsetting{'overrides'}{$key})) {
13239:                         push(@{$changes{'overrides'}},$key);
13240:                     }
13241:                 }
13242:             } else {
13243:                 foreach my $key (@overrides) {
13244:                     push(@{$changes{'overrides'}},$key); 
13245:                 }
13246:             }
13247:         }
13248:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
13249:             foreach my $key ('excluded','weights','threshold','sysmail') {
13250:                 if ($key eq 'excluded') {
13251:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13252:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
13253:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13254:                             (@{$currsetting{'lonstatus'}{$key}})) {
13255:                             my @diffs =
13256:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
13257:                                                                    $currsetting{'lonstatus'}{$key});
13258:                             if (@diffs) {
13259:                                 push(@{$changes{'lonstatus'}},$key);
13260:                             }
13261:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
13262:                             push(@{$changes{'lonstatus'}},$key);
13263:                         }
13264:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13265:                              (@{$currsetting{'lonstatus'}{$key}})) {
13266:                         push(@{$changes{'lonstatus'}},$key);
13267:                     }
13268:                 } elsif ($key eq 'weights') {
13269:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13270:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
13271:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13272:                             foreach my $type ('E','W','N','U') {
13273:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
13274:                                         $currsetting{'lonstatus'}{$key}{$type}) {
13275:                                     push(@{$changes{'lonstatus'}},$key);
13276:                                     last;
13277:                                 }
13278:                             }
13279:                         } else {
13280:                             foreach my $type ('E','W','N','U') {
13281:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
13282:                                     push(@{$changes{'lonstatus'}},$key);
13283:                                     last;
13284:                                 }
13285:                             }
13286:                         }
13287:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13288:                         foreach my $type ('E','W','N','U') {
13289:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
13290:                                 push(@{$changes{'lonstatus'}},$key);
13291:                                 last;
13292:                             }
13293:                         }
13294:                     }
13295:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
13296:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13297:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13298:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
13299:                                 push(@{$changes{'lonstatus'}},$key);
13300:                             }
13301:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
13302:                             push(@{$changes{'lonstatus'}},$key);
13303:                         }
13304:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13305:                         push(@{$changes{'lonstatus'}},$key);
13306:                     }
13307:                 }
13308:             }
13309:         } else {
13310:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13311:                 foreach my $key ('excluded','weights','threshold','sysmail') {
13312:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13313:                         push(@{$changes{'lonstatus'}},$key);
13314:                     }
13315:                 }
13316:             }
13317:         }
13318:     } else {
13319:         my %default;
13320:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
13321:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
13322:         $default{'errormail'} = 'adminemail';
13323:         $default{'packagesmail'} = 'adminemail';
13324:         $default{'helpdeskmail'} = 'supportemail';
13325:         $default{'otherdomsmail'} = 'supportemail';
13326:         $default{'lonstatusmail'} = 'adminemail';
13327:         $default{'requestsmail'} = 'adminemail';
13328:         $default{'updatesmail'} = 'adminemail';
13329:         $default{'hostipmail'} = 'adminemail';
13330:         foreach my $item (@contacts) {
13331:            if ($to{$item} ne $default{$item}) {
13332:                $changes{$item} = 1;
13333:            }
13334:         }
13335:         foreach my $type (@mailings) {
13336:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
13337:                 push(@{$changes{$type}},@{$newsetting{$type}});
13338:             }
13339:             if ($others{$type} ne '') {
13340:                 push(@{$changes{$type}},'others');
13341:             }
13342:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13343:                 if ($bcc{$type} ne '') {
13344:                     push(@{$changes{$type}},'bcc');
13345:                 }
13346:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
13347:                     push(@{$changes{$type}},'include');
13348:                 }
13349:             }
13350:         }
13351:         if (ref($fields) eq 'ARRAY') {
13352:             foreach my $field (@{$fields}) {
13353:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13354:                     push(@{$changes{'helpform'}},$field);
13355:                 }
13356:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13357:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13358:                         push(@{$changes{'helpform'}},'maxsize');
13359:                     }
13360:                 }
13361:             }
13362:         }
13363:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13364:             foreach my $key ('excluded','weights','threshold','sysmail') {
13365:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13366:                     push(@{$changes{'lonstatus'}},$key);
13367:                 }
13368:             }
13369:         }
13370:     }
13371:     foreach my $item (@toggles) {
13372:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
13373:             $changes{$item} = 1;
13374:         } elsif ((!$env{'form.'.$item}) &&
13375:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
13376:             $changes{$item} = 1;
13377:         }
13378:     }
13379:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
13380:                                              $dom);
13381:     if ($putresult eq 'ok') {
13382:         if (keys(%changes) > 0) {
13383:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13384:             if (ref($lastactref) eq 'HASH') {
13385:                 $lastactref->{'domainconfig'} = 1;
13386:             }
13387:             my ($titles,$short_titles)  = &contact_titles();
13388:             $resulttext = &mt('Changes made:').'<ul>';
13389:             foreach my $item (@contacts) {
13390:                 if ($changes{$item}) {
13391:                     $resulttext .= '<li>'.$titles->{$item}.
13392:                                     &mt(' set to: ').
13393:                                     '<span class="LC_cusr_emph">'.
13394:                                     $to{$item}.'</span></li>';
13395:                 }
13396:             }
13397:             foreach my $type (@mailings) {
13398:                 if (ref($changes{$type}) eq 'ARRAY') {
13399:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13400:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
13401:                     } else {
13402:                         $resulttext .= '<li>'.$titles->{$type}.': ';
13403:                     }
13404:                     my @text;
13405:                     foreach my $item (@{$newsetting{$type}}) {
13406:                         push(@text,$short_titles->{$item});
13407:                     }
13408:                     if ($others{$type} ne '') {
13409:                         push(@text,$others{$type});
13410:                     }
13411:                     if (@text) {
13412:                         $resulttext .= '<span class="LC_cusr_emph">'.
13413:                                        join(', ',@text).'</span>';
13414:                     }
13415:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13416:                         if ($bcc{$type} ne '') {
13417:                             my $bcctext;
13418:                             if (@text) {
13419:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
13420:                             } else {
13421:                                 $bcctext = '(Bcc)';
13422:                             }
13423:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
13424:                         } elsif (!@text) {
13425:                             $resulttext .= &mt('No one');
13426:                         }   
13427:                         if ($includestr{$type} ne '') {
13428:                             if ($includeloc{$type} eq 'b') {
13429:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
13430:                             } elsif ($includeloc{$type} eq 's') {
13431:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
13432:                             }
13433:                         }
13434:                     } elsif (!@text) {
13435:                         $resulttext .= &mt('No recipients');
13436:                     }
13437:                     $resulttext .= '</li>';
13438:                 }
13439:             }
13440:             if (ref($changes{'overrides'}) eq 'ARRAY') {
13441:                 my @deletions;
13442:                 foreach my $type (@{$changes{'overrides'}}) {
13443:                     if ($usertypeshash{$type}) {
13444:                         if (grep(/^\Q$type\E/,@overrides)) {
13445:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
13446:                                                       $usertypeshash{$type}).'<ul><li>';
13447:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
13448:                                 my @text;
13449:                                 foreach my $item (@contacts) {
13450:                                     if ($newsetting{'override_'.$type}{$item}) { 
13451:                                         push(@text,$short_titles->{$item});
13452:                                     }
13453:                                 }
13454:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
13455:                                     push(@text,$newsetting{'override_'.$type}{'others'});
13456:                                 }
13457:   
13458:                                 if (@text) {
13459:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
13460:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
13461:                                 }
13462:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
13463:                                     my $bcctext;
13464:                                     if (@text) {
13465:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
13466:                                     } else {
13467:                                         $bcctext = '(Bcc)';
13468:                                     }
13469:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
13470:                                 } elsif (!@text) {
13471:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
13472:                                 }
13473:                                 $resulttext .= '</li>';
13474:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
13475:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
13476:                                     if ($loc eq 'b') {
13477:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
13478:                                     } elsif ($loc eq 's') {
13479:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
13480:                                     }
13481:                                 }
13482:                             }
13483:                             $resulttext .= '</li></ul></li>';
13484:                         } else {
13485:                             push(@deletions,$usertypeshash{$type});
13486:                         }
13487:                     }
13488:                 }
13489:                 if (@deletions) {
13490:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
13491:                                               join(', ',@deletions)).'</li>';
13492:                 }
13493:             }
13494:             my @offon = ('off','on');
13495:             my $corelink = &core_link_msu();
13496:             if ($changes{'reporterrors'}) {
13497:                 $resulttext .= '<li>'.
13498:                                &mt('E-mail error reports to [_1] set to "'.
13499:                                    $offon[$env{'form.reporterrors'}].'".',
13500:                                    $corelink).
13501:                                '</li>';
13502:             }
13503:             if ($changes{'reportupdates'}) {
13504:                 $resulttext .= '<li>'.
13505:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
13506:                                     $offon[$env{'form.reportupdates'}].'".',
13507:                                     $corelink).
13508:                                 '</li>';
13509:             }
13510:             if ($changes{'reportstatus'}) {
13511:                 $resulttext .= '<li>'.
13512:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
13513:                                     $offon[$env{'form.reportstatus'}].'".',
13514:                                     $corelink).
13515:                                 '</li>';
13516:             }
13517:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
13518:                 $resulttext .= '<li>'.
13519:                                &mt('Nightly status check e-mail settings').':<ul>';
13520:                 my (%defval,%use_def,%shown);
13521:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
13522:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
13523:                 $defval{'weights'} =
13524:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
13525:                 $defval{'excluded'} = &mt('None');
13526:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
13527:                     foreach my $item ('threshold','sysmail','weights','excluded') {
13528:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
13529:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
13530:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
13531:                             } elsif ($item eq 'weights') {
13532:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
13533:                                     foreach my $type ('E','W','N','U') {
13534:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
13535:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
13536:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
13537:                                         } else {
13538:                                             $shown{$item} .= $lonstatus_defs->{$type};
13539:                                         }
13540:                                         $shown{$item} .= ', ';
13541:                                     }
13542:                                     $shown{$item} =~ s/, $//;
13543:                                 } else {
13544:                                     $shown{$item} = $defval{$item};
13545:                                 }
13546:                             } elsif ($item eq 'excluded') {
13547:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
13548:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
13549:                                 } else {
13550:                                     $shown{$item} = $defval{$item};
13551:                                 }
13552:                             }
13553:                         } else {
13554:                             $shown{$item} = $defval{$item};
13555:                         }
13556:                     }
13557:                 } else {
13558:                     foreach my $item ('threshold','weights','excluded','sysmail') {
13559:                         $shown{$item} = $defval{$item};
13560:                     }
13561:                 }
13562:                 foreach my $item ('threshold','weights','excluded','sysmail') {
13563:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
13564:                                           $shown{$item}).'</li>';
13565:                 }
13566:                 $resulttext .= '</ul></li>';
13567:             }
13568:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
13569:                 my (@optional,@required,@unused,$maxsizechg);
13570:                 foreach my $field (@{$changes{'helpform'}}) {
13571:                     if ($field eq 'maxsize') {
13572:                         $maxsizechg = 1;
13573:                         next;
13574:                     }
13575:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
13576:                         push(@optional,$field);
13577:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
13578:                         push(@unused,$field);
13579:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
13580:                         push(@required,$field);
13581:                     }
13582:                 }
13583:                 if (@optional) {
13584:                     $resulttext .= '<li>'.
13585:                                    &mt('Help form fields changed to "Optional": [_1].',
13586:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
13587:                                    '</li>';
13588:                 }
13589:                 if (@required) {
13590:                     $resulttext .= '<li>'.
13591:                                    &mt('Help form fields changed to "Required": [_1].',
13592:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
13593:                                    '</li>';
13594:                 }
13595:                 if (@unused) {
13596:                     $resulttext .= '<li>'.
13597:                                    &mt('Help form fields changed to "Not shown": [_1].',
13598:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
13599:                                    '</li>';
13600:                 }
13601:                 if ($maxsizechg) {
13602:                     $resulttext .= '<li>'.
13603:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
13604:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
13605:                                    '</li>';
13606:                 }
13607:             }
13608:             $resulttext .= '</ul>';
13609:         } else {
13610:             $resulttext = &mt('No changes made to contacts and form settings');
13611:         }
13612:     } else {
13613:         $resulttext = '<span class="LC_error">'.
13614:             &mt('An error occurred: [_1].',$putresult).'</span>';
13615:     }
13616:     return $resulttext;
13617: }
13618: 
13619: sub modify_usercreation {
13620:     my ($dom,%domconfig) = @_;
13621:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
13622:     my $warningmsg;
13623:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13624:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13625:             if ($key eq 'cancreate') {
13626:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13627:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13628:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
13629:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13630:                         } else {
13631:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13632:                         }
13633:                     }
13634:                 }
13635:             } elsif ($key eq 'email_rule') {
13636:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13637:             } else {
13638:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13639:             }
13640:         }
13641:     }
13642:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
13643:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
13644:     my @contexts = ('author','course','requestcrs');
13645:     foreach my $item(@contexts) {
13646:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
13647:     }
13648:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13649:         foreach my $item (@contexts) {
13650:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
13651:                 push(@{$changes{'cancreate'}},$item);
13652:             }
13653:         }
13654:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
13655:         foreach my $item (@contexts) {
13656:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
13657:                 if ($cancreate{$item} ne 'any') {
13658:                     push(@{$changes{'cancreate'}},$item);
13659:                 }
13660:             } else {
13661:                 if ($cancreate{$item} ne 'none') {
13662:                     push(@{$changes{'cancreate'}},$item);
13663:                 }
13664:             }
13665:         }
13666:     } else {
13667:         foreach my $item (@contexts)  {
13668:             push(@{$changes{'cancreate'}},$item);
13669:         }
13670:     }
13671: 
13672:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
13673:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
13674:             if (!grep(/^\Q$type\E$/,@username_rule)) {
13675:                 push(@{$changes{'username_rule'}},$type);
13676:             }
13677:         }
13678:         foreach my $type (@username_rule) {
13679:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
13680:                 push(@{$changes{'username_rule'}},$type);
13681:             }
13682:         }
13683:     } else {
13684:         push(@{$changes{'username_rule'}},@username_rule);
13685:     }
13686: 
13687:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
13688:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
13689:             if (!grep(/^\Q$type\E$/,@id_rule)) {
13690:                 push(@{$changes{'id_rule'}},$type);
13691:             }
13692:         }
13693:         foreach my $type (@id_rule) {
13694:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
13695:                 push(@{$changes{'id_rule'}},$type);
13696:             }
13697:         }
13698:     } else {
13699:         push(@{$changes{'id_rule'}},@id_rule);
13700:     }
13701: 
13702:     my @authen_contexts = ('author','course','domain');
13703:     my @authtypes = ('int','krb4','krb5','loc','lti');
13704:     my %authhash;
13705:     foreach my $item (@authen_contexts) {
13706:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
13707:         foreach my $auth (@authtypes) {
13708:             if (grep(/^\Q$auth\E$/,@authallowed)) {
13709:                 $authhash{$item}{$auth} = 1;
13710:             } else {
13711:                 $authhash{$item}{$auth} = 0;
13712:             }
13713:         }
13714:     }
13715:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
13716:         foreach my $item (@authen_contexts) {
13717:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
13718:                 foreach my $auth (@authtypes) {
13719:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
13720:                         push(@{$changes{'authtypes'}},$item);
13721:                         last;
13722:                     }
13723:                 }
13724:             }
13725:         }
13726:     } else {
13727:         foreach my $item (@authen_contexts) {
13728:             push(@{$changes{'authtypes'}},$item);
13729:         }
13730:     }
13731: 
13732:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
13733:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
13734:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
13735:     $save_usercreate{'id_rule'} = \@id_rule;
13736:     $save_usercreate{'username_rule'} = \@username_rule,
13737:     $save_usercreate{'authtypes'} = \%authhash;
13738: 
13739:     my %usercreation_hash =  (
13740:         usercreation     => \%save_usercreate,
13741:     );
13742: 
13743:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
13744:                                              $dom);
13745: 
13746:     if ($putresult eq 'ok') {
13747:         if (keys(%changes) > 0) {
13748:             $resulttext = &mt('Changes made:').'<ul>';
13749:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13750:                 my %lt = &usercreation_types();
13751:                 foreach my $type (@{$changes{'cancreate'}}) {
13752:                     my $chgtext = $lt{$type}.', ';
13753:                     if ($cancreate{$type} eq 'none') {
13754:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
13755:                     } elsif ($cancreate{$type} eq 'any') {
13756:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
13757:                     } elsif ($cancreate{$type} eq 'official') {
13758:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
13759:                     } elsif ($cancreate{$type} eq 'unofficial') {
13760:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
13761:                     }
13762:                     $resulttext .= '<li>'.$chgtext.'</li>';
13763:                 }
13764:             }
13765:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
13766:                 my ($rules,$ruleorder) = 
13767:                     &Apache::lonnet::inst_userrules($dom,'username');
13768:                 my $chgtext = '<ul>';
13769:                 foreach my $type (@username_rule) {
13770:                     if (ref($rules->{$type}) eq 'HASH') {
13771:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
13772:                     }
13773:                 }
13774:                 $chgtext .= '</ul>';
13775:                 if (@username_rule > 0) {
13776:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
13777:                 } else {
13778:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
13779:                 }
13780:             }
13781:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
13782:                 my ($idrules,$idruleorder) = 
13783:                     &Apache::lonnet::inst_userrules($dom,'id');
13784:                 my $chgtext = '<ul>';
13785:                 foreach my $type (@id_rule) {
13786:                     if (ref($idrules->{$type}) eq 'HASH') {
13787:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
13788:                     }
13789:                 }
13790:                 $chgtext .= '</ul>';
13791:                 if (@id_rule > 0) {
13792:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
13793:                 } else {
13794:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
13795:                 }
13796:             }
13797:             my %authname = &authtype_names();
13798:             my %context_title = &context_names();
13799:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
13800:                 my $chgtext = '<ul>';
13801:                 foreach my $type (@{$changes{'authtypes'}}) {
13802:                     my @allowed;
13803:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
13804:                     foreach my $auth (@authtypes) {
13805:                         if ($authhash{$type}{$auth}) {
13806:                             push(@allowed,$authname{$auth});
13807:                         }
13808:                     }
13809:                     if (@allowed > 0) {
13810:                         $chgtext .= join(', ',@allowed).'</li>';
13811:                     } else {
13812:                         $chgtext .= &mt('none').'</li>';
13813:                     }
13814:                 }
13815:                 $chgtext .= '</ul>';
13816:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
13817:                 $resulttext .= '</li>';
13818:             }
13819:             $resulttext .= '</ul>';
13820:         } else {
13821:             $resulttext = &mt('No changes made to user creation settings');
13822:         }
13823:     } else {
13824:         $resulttext = '<span class="LC_error">'.
13825:             &mt('An error occurred: [_1]',$putresult).'</span>';
13826:     }
13827:     if ($warningmsg ne '') {
13828:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13829:     }
13830:     return $resulttext;
13831: }
13832: 
13833: sub modify_selfcreation {
13834:     my ($dom,$lastactref,%domconfig) = @_;
13835:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
13836:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
13837:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13838:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
13839:     if (ref($typesref) eq 'ARRAY') {
13840:         @types = @{$typesref};
13841:     }
13842:     if (ref($usertypesref) eq 'HASH') {
13843:         %usertypes = %{$usertypesref};
13844:     }
13845:     $usertypes{'default'} = $othertitle;
13846: #
13847: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
13848: #
13849:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13850:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13851:             if ($key eq 'cancreate') {
13852:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13853:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13854:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
13855:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
13856:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
13857:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
13858:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
13859:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
13860:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13861:                         } else {
13862:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13863:                         }
13864:                     }
13865:                 }
13866:             } elsif ($key eq 'email_rule') {
13867:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13868:             } else {
13869:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13870:             }
13871:         }
13872:     }
13873: #
13874: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
13875: #
13876:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13877:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13878:             if ($key eq 'selfcreate') {
13879:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
13880:             } else {
13881:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
13882:             }
13883:         }
13884:     }
13885: #
13886: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
13887: #
13888:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13889:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
13890:             if ($key eq 'inststatusguest') {
13891:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
13892:             } else {
13893:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
13894:             }
13895:         }
13896:     }
13897: 
13898:     my @contexts = ('selfcreate');
13899:     @{$cancreate{'selfcreate'}} = ();
13900:     %{$cancreate{'emailusername'}} = ();
13901:     if (@types) {
13902:         @{$cancreate{'statustocreate'}} = ();
13903:     }
13904:     %{$cancreate{'selfcreateprocessing'}} = ();
13905:     %{$cancreate{'shibenv'}} = ();
13906:     %{$cancreate{'emailverified'}} = ();
13907:     %{$cancreate{'emailoptions'}} = ();
13908:     %{$cancreate{'emaildomain'}} = ();
13909:     my %selfcreatetypes = (
13910:                              sso   => 'users authenticated by institutional single sign on',
13911:                              login => 'users authenticated by institutional log-in',
13912:                              email => 'users verified by e-mail',
13913:                           );
13914: #
13915: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
13916: # is permitted.
13917: #
13918:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
13919: 
13920:     my (@statuses,%email_rule);
13921:     foreach my $item ('login','sso','email') {
13922:         if ($item eq 'email') {
13923:             if ($env{'form.cancreate_email'}) {
13924:                 if (@types) {
13925:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
13926:                     foreach my $status (@poss_statuses) {
13927:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
13928:                             push(@statuses,$status);
13929:                         }
13930:                     }
13931:                     $save_inststatus{'inststatusguest'} = \@statuses;
13932:                 } else {
13933:                     push(@statuses,'default');
13934:                 }
13935:                 if (@statuses) {
13936:                     my %curr_rule;
13937:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
13938:                         foreach my $type (@statuses) {
13939:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
13940:                         }
13941:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
13942:                         foreach my $type (@statuses) {
13943:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
13944:                         }
13945:                     }
13946:                     push(@{$cancreate{'selfcreate'}},'email');
13947:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
13948:                     my %curremaildom;
13949:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
13950:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
13951:                     }
13952:                     foreach my $type (@statuses) {
13953:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
13954:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
13955:                         }
13956:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
13957:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
13958:                         }
13959:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
13960: #
13961: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
13962: #
13963:                             my $chosen = $1;
13964:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
13965:                                 my $emaildom;
13966:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
13967:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
13968:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
13969:                                     if (ref($curremaildom{$type}) eq 'HASH') {
13970:                                         if (exists($curremaildom{$type}{$chosen})) {
13971:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
13972:                                                 push(@{$changes{'cancreate'}},'emaildomain');
13973:                                             }
13974:                                         } elsif ($emaildom ne '') {
13975:                                             push(@{$changes{'cancreate'}},'emaildomain');
13976:                                         }
13977:                                     } elsif ($emaildom ne '') {
13978:                                         push(@{$changes{'cancreate'}},'emaildomain');
13979:                                     } 
13980:                                 }
13981:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13982:                             } elsif ($chosen eq 'custom') {
13983:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
13984:                                 $email_rule{$type} = [];
13985:                                 if (ref($emailrules) eq 'HASH') {
13986:                                     foreach my $rule (@possemail_rules) {
13987:                                         if (exists($emailrules->{$rule})) {
13988:                                             push(@{$email_rule{$type}},$rule);
13989:                                         }
13990:                                     }
13991:                                 }
13992:                                 if (@{$email_rule{$type}}) {
13993:                                     $cancreate{'emailoptions'}{$type} = 'custom';
13994:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
13995:                                         if (@{$curr_rule{$type}} > 0) {
13996:                                             foreach my $rule (@{$curr_rule{$type}}) {
13997:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
13998:                                                     push(@{$changes{'email_rule'}},$type);
13999:                                                 }
14000:                                             }
14001:                                         }
14002:                                         foreach my $type (@{$email_rule{$type}}) {
14003:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
14004:                                                 push(@{$changes{'email_rule'}},$type);
14005:                                             }
14006:                                         }
14007:                                     } else {
14008:                                         push(@{$changes{'email_rule'}},$type);
14009:                                     }
14010:                                 }
14011:                             } else {
14012:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
14013:                             }
14014:                         }
14015:                     }
14016:                     if (@types) {
14017:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
14018:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
14019:                             if (@changed) {
14020:                                 push(@{$changes{'inststatus'}},'inststatusguest');
14021:                             }
14022:                         } else {
14023:                             push(@{$changes{'inststatus'}},'inststatusguest');
14024:                         }
14025:                     }
14026:                 } else {
14027:                     delete($env{'form.cancreate_email'});
14028:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
14029:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
14030:                             push(@{$changes{'inststatus'}},'inststatusguest');
14031:                         }
14032:                     }
14033:                 }
14034:             } else {
14035:                 $save_inststatus{'inststatusguest'} = [];
14036:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
14037:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
14038:                         push(@{$changes{'inststatus'}},'inststatusguest');
14039:                     }
14040:                 }
14041:             }
14042:         } else {
14043:             if ($env{'form.cancreate_'.$item}) {
14044:                 push(@{$cancreate{'selfcreate'}},$item);
14045:             }
14046:         }
14047:     }
14048:     my (%userinfo,%savecaptcha);
14049:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
14050: #
14051: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
14052: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
14053: #
14054: 
14055:     if ($env{'form.cancreate_email'}) {
14056:         push(@contexts,'emailusername');
14057:         if (@statuses) {
14058:             foreach my $type (@statuses) {
14059:                 if (ref($infofields) eq 'ARRAY') {
14060:                     foreach my $field (@{$infofields}) {
14061:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
14062:                             $cancreate{'emailusername'}{$type}{$field} = $1;
14063:                         }
14064:                     }
14065:                 }
14066:             }
14067:         }
14068: #
14069: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
14070: # queued requests for self-creation of account verified by e-mail.
14071: #
14072: 
14073:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
14074:         @approvalnotify = sort(@approvalnotify);
14075:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
14076:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
14077:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
14078:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
14079:                     push(@{$changes{'cancreate'}},'notify');
14080:                 }
14081:             } else {
14082:                 if ($cancreate{'notify'}{'approval'}) {
14083:                     push(@{$changes{'cancreate'}},'notify');
14084:                 }
14085:             }
14086:         } elsif ($cancreate{'notify'}{'approval'}) {
14087:             push(@{$changes{'cancreate'}},'notify');
14088:         }
14089: 
14090:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
14091:     }
14092: #  
14093: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
14094: # institutional log-in.
14095: #
14096:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
14097:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
14098:                ($domdefaults{'auth_def'} eq 'localauth'))) {
14099:             $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.').' '.
14100:                           &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.');
14101:         }
14102:     }
14103:     my @fields = ('lastname','firstname','middlename','generation',
14104:                   'permanentemail','id');
14105:     my @shibfields = (@fields,'inststatus');
14106:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14107: #
14108: # Where usernames may created for institutional log-in and/or institutional single sign on:
14109: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
14110: # may self-create accounts 
14111: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
14112: # which the user may supply, if institutional data is unavailable.
14113: #
14114:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
14115:         if (@types) {
14116:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
14117:             push(@contexts,'statustocreate');
14118:             foreach my $type (@types) {
14119:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
14120:                 foreach my $field (@fields) {
14121:                     if (grep(/^\Q$field\E$/,@modifiable)) {
14122:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
14123:                     } else {
14124:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
14125:                     }
14126:                 }
14127:             }
14128:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
14129:                 foreach my $type (@types) {
14130:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
14131:                         foreach my $field (@fields) {
14132:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
14133:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
14134:                                 push(@{$changes{'selfcreate'}},$type);
14135:                                 last;
14136:                             }
14137:                         }
14138:                     }
14139:                 }
14140:             } else {
14141:                 foreach my $type (@types) {
14142:                     push(@{$changes{'selfcreate'}},$type);
14143:                 }
14144:             }
14145:         }
14146:         foreach my $field (@shibfields) {
14147:             if ($env{'form.shibenv_'.$field} ne '') {
14148:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
14149:             }
14150:         }
14151:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
14152:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
14153:                 foreach my $field (@shibfields) {
14154:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
14155:                         push(@{$changes{'cancreate'}},'shibenv');
14156:                     }
14157:                 }
14158:             } else {
14159:                 foreach my $field (@shibfields) {
14160:                     if ($env{'form.shibenv_'.$field}) {
14161:                         push(@{$changes{'cancreate'}},'shibenv');
14162:                         last;
14163:                     }
14164:                 }
14165:             }
14166:         }
14167:     }
14168:     foreach my $item (@contexts) {
14169:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
14170:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
14171:                 if (ref($cancreate{$item}) eq 'ARRAY') {
14172:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
14173:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14174:                             push(@{$changes{'cancreate'}},$item);
14175:                         }
14176:                     }
14177:                 }
14178:             }
14179:             if (ref($cancreate{$item}) eq 'ARRAY') {
14180:                 foreach my $type (@{$cancreate{$item}}) {
14181:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
14182:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14183:                             push(@{$changes{'cancreate'}},$item);
14184:                         }
14185:                     }
14186:                 }
14187:             }
14188:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
14189:             if (ref($cancreate{$item}) eq 'HASH') {
14190:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
14191:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
14192:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
14193:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
14194:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14195:                                     push(@{$changes{'cancreate'}},$item);
14196:                                 }
14197:                             }
14198:                         }
14199:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14200:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
14201:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14202:                                 push(@{$changes{'cancreate'}},$item);
14203:                             }
14204:                         }
14205:                     }
14206:                 }
14207:                 foreach my $type (keys(%{$cancreate{$item}})) {
14208:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
14209:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
14210:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
14211:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
14212:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14213:                                         push(@{$changes{'cancreate'}},$item);
14214:                                     }
14215:                                 }
14216:                             } else {
14217:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14218:                                     push(@{$changes{'cancreate'}},$item);
14219:                                 }
14220:                             }
14221:                         }
14222:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14223:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
14224:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14225:                                 push(@{$changes{'cancreate'}},$item);
14226:                             }
14227:                         }
14228:                     }
14229:                 }
14230:             }
14231:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
14232:             if (ref($cancreate{$item}) eq 'ARRAY') {
14233:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
14234:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14235:                         push(@{$changes{'cancreate'}},$item);
14236:                     }
14237:                 }
14238:             }
14239:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14240:             if (ref($cancreate{$item}) eq 'HASH') {
14241:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14242:                     push(@{$changes{'cancreate'}},$item);
14243:                 }
14244:             }
14245:         } elsif ($item eq 'emailusername') {
14246:             if (ref($cancreate{$item}) eq 'HASH') {
14247:                 foreach my $type (keys(%{$cancreate{$item}})) {
14248:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
14249:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
14250:                             if ($cancreate{$item}{$type}{$field}) {
14251:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14252:                                     push(@{$changes{'cancreate'}},$item);
14253:                                 }
14254:                                 last;
14255:                             }
14256:                         }
14257:                     }
14258:                 }
14259:             }
14260:         }
14261:     }
14262: #
14263: # Populate %save_usercreate hash with updates to self-creation configuration.
14264: #
14265:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
14266:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
14267:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
14268:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
14269:     if (ref($cancreate{'notify'}) eq 'HASH') {
14270:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
14271:     }
14272:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
14273:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
14274:     }
14275:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
14276:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
14277:     }
14278:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
14279:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
14280:     }
14281:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
14282:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
14283:     }
14284:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
14285:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
14286:     }
14287:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
14288:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
14289:     }
14290:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
14291:     $save_usercreate{'email_rule'} = \%email_rule;
14292: 
14293:     my %userconfig_hash = (
14294:             usercreation     => \%save_usercreate,
14295:             usermodification => \%save_usermodify,
14296:             inststatus       => \%save_inststatus,
14297:     );
14298: 
14299:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
14300:                                              $dom);
14301: #
14302: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
14303: #
14304:     if ($putresult eq 'ok') {
14305:         if (keys(%changes) > 0) {
14306:             $resulttext = &mt('Changes made:').'<ul>';
14307:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
14308:                 my %lt = &selfcreation_types();
14309:                 foreach my $type (@{$changes{'cancreate'}}) {
14310:                     my $chgtext = '';
14311:                     if ($type eq 'selfcreate') {
14312:                         if (@{$cancreate{$type}} == 0) {
14313:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
14314:                         } else {
14315:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
14316:                                         '<ul>';
14317:                             foreach my $case (@{$cancreate{$type}}) {
14318:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
14319:                             }
14320:                             $chgtext .= '</ul>';
14321:                             if (ref($cancreate{$type}) eq 'ARRAY') {
14322:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
14323:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
14324:                                         if (@{$cancreate{'statustocreate'}} == 0) {
14325:                                             $chgtext .= '<span class="LC_warning">'.
14326:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
14327:                                                         '</span><br />';
14328:                                         }
14329:                                     }
14330:                                 }
14331:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
14332:                                     if (!@statuses) {
14333:                                         $chgtext .= '<span class="LC_warning">'.
14334:                                                     &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.").
14335:                                                     '</span><br />';
14336: 
14337:                                     }
14338:                                 }
14339:                             }
14340:                         }
14341:                     } elsif ($type eq 'shibenv') {
14342:                         if (keys(%{$cancreate{$type}}) == 0) {
14343:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
14344:                         } else {
14345:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
14346:                                         '<ul>';
14347:                             foreach my $field (@shibfields) {
14348:                                 next if ($cancreate{$type}{$field} eq '');
14349:                                 if ($field eq 'inststatus') {
14350:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
14351:                                 } else {
14352:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
14353:                                 }
14354:                             }
14355:                             $chgtext .= '</ul>';
14356:                         }
14357:                     } elsif ($type eq 'statustocreate') {
14358:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
14359:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
14360:                             if (@{$cancreate{'selfcreate'}} > 0) {
14361:                                 if (@{$cancreate{'statustocreate'}} == 0) {
14362:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
14363:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
14364:                                         $chgtext .= '<br />'.
14365:                                                     '<span class="LC_warning">'.
14366:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
14367:                                                     '</span>';
14368:                                     }
14369:                                 } elsif (keys(%usertypes) > 0) {
14370:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
14371:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
14372:                                     } else {
14373:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
14374:                                     }
14375:                                     $chgtext .= '<ul>';
14376:                                     foreach my $case (@{$cancreate{$type}}) {
14377:                                         if ($case eq 'default') {
14378:                                             $chgtext .= '<li>'.$othertitle.'</li>';
14379:                                         } else {
14380:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
14381:                                         }
14382:                                     }
14383:                                     $chgtext .= '</ul>';
14384:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
14385:                                         $chgtext .= '<span class="LC_warning">'.
14386:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
14387:                                                     '</span>';
14388:                                     }
14389:                                 }
14390:                             } else {
14391:                                 if (@{$cancreate{$type}} == 0) {
14392:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
14393:                                 } else {
14394:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
14395:                                 }
14396:                             }
14397:                             $chgtext .= '<br />';
14398:                         }
14399:                     } elsif ($type eq 'selfcreateprocessing') {
14400:                         my %choices = &Apache::lonlocal::texthash (
14401:                                                                     automatic => 'Automatic approval',
14402:                                                                     approval  => 'Queued for approval',
14403:                                                                   );
14404:                         if (@types) {
14405:                             if (@statuses) {
14406:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
14407:                                             '<ul>';
14408:                                 foreach my $status (@statuses) {
14409:                                     if ($status eq 'default') {
14410:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
14411:                                     } else {
14412:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
14413:                                     }
14414:                                 }
14415:                                 $chgtext .= '</ul>';
14416:                             }
14417:                         } else {
14418:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
14419:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
14420:                         }
14421:                     } elsif ($type eq 'emailverified') {
14422:                         my %options = &Apache::lonlocal::texthash (
14423:                                                                     all   => 'Same as e-mail',
14424:                                                                     first => 'Omit @domain',
14425:                                                                     free  => 'Free to choose',
14426:                                                                   );
14427:                         if (@types) {
14428:                             if (@statuses) {
14429:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
14430:                                             '<ul>';
14431:                                 foreach my $status (@statuses) {
14432:                                     if ($type eq 'default') {
14433:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14434:                                     } else {
14435:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14436:                                     }
14437:                                 }
14438:                                 $chgtext .= '</ul>';
14439:                             }
14440:                         } else {
14441:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
14442:                                             $options{$cancreate{'emailverified'}{'default'}});
14443:                         }
14444:                     } elsif ($type eq 'emailoptions') {
14445:                         my %options = &Apache::lonlocal::texthash (
14446:                                                                     any     => 'Any e-mail',
14447:                                                                     inst    => 'Institutional only',
14448:                                                                     noninst => 'Non-institutional only',
14449:                                                                     custom  => 'Custom restrictions',
14450:                                                                   );
14451:                         if (@types) {
14452:                             if (@statuses) {
14453:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
14454:                                             '<ul>';
14455:                                 foreach my $status (@statuses) {
14456:                                     if ($type eq 'default') {
14457:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14458:                                     } else {
14459:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14460:                                     }
14461:                                 }
14462:                                 $chgtext .= '</ul>';
14463:                             }
14464:                         } else {
14465:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
14466:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
14467:                             } else {
14468:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
14469:                                                 $options{$cancreate{'emailoptions'}{'default'}});
14470:                             }
14471:                         }
14472:                     } elsif ($type eq 'emaildomain') {
14473:                         my $output;
14474:                         if (@statuses) {
14475:                             foreach my $type (@statuses) {
14476:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
14477:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
14478:                                         if ($type eq 'default') {
14479:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14480:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14481:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14482:                                             } else {
14483:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
14484:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
14485:                                             }
14486:                                         } else {
14487:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14488:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14489:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14490:                                             } else {
14491:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
14492:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
14493:                                             }
14494:                                         }
14495:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
14496:                                         if ($type eq 'default') {
14497:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14498:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14499:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14500:                                             } else {
14501:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
14502:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
14503:                                             }
14504:                                         } else {
14505:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14506:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14507:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14508:                                             } else {
14509:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
14510:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
14511:                                             }
14512:                                         }
14513:                                     }
14514:                                 }
14515:                             }
14516:                         }
14517:                         if ($output ne '') {
14518:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
14519:                                         '<ul>'.$output.'</ul>';
14520:                         }
14521:                     } elsif ($type eq 'captcha') {
14522:                         if ($savecaptcha{$type} eq 'notused') {
14523:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
14524:                         } else {
14525:                             my %captchas = &captcha_phrases();
14526:                             if ($captchas{$savecaptcha{$type}}) {
14527:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
14528:                             } else {
14529:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
14530:                             }
14531:                         }
14532:                     } elsif ($type eq 'recaptchakeys') {
14533:                         my ($privkey,$pubkey);
14534:                         if (ref($savecaptcha{$type}) eq 'HASH') {
14535:                             $pubkey = $savecaptcha{$type}{'public'};
14536:                             $privkey = $savecaptcha{$type}{'private'};
14537:                         }
14538:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
14539:                         if (!$pubkey) {
14540:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
14541:                         } else {
14542:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
14543:                         }
14544:                         if (!$privkey) {
14545:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
14546:                         } else {
14547:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
14548:                         }
14549:                         $chgtext .= '</ul>';
14550:                     } elsif ($type eq 'recaptchaversion') {
14551:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
14552:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
14553:                         }
14554:                     } elsif ($type eq 'emailusername') {
14555:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
14556:                             if (@statuses) {
14557:                                 foreach my $type (@statuses) {
14558:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
14559:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
14560:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
14561:                                                     '<ul>';
14562:                                             foreach my $field (@{$infofields}) {
14563:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
14564:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
14565:                                                 }
14566:                                             }
14567:                                             $chgtext .= '</ul>';
14568:                                         } else {
14569:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14570:                                         }
14571:                                     } else {
14572:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14573:                                     }
14574:                                 }
14575:                             }
14576:                         }
14577:                     } elsif ($type eq 'notify') {
14578:                         my $numapprove = 0;
14579:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
14580:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
14581:                                 if ($cancreate{'notify'}{'approval'}) {
14582:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
14583:                                     $numapprove ++;
14584:                                 }
14585:                             }
14586:                         }
14587:                         unless ($numapprove) {
14588:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
14589:                         }
14590:                     }
14591:                     if ($chgtext) {
14592:                         $resulttext .= '<li>'.$chgtext.'</li>';
14593:                     }
14594:                 }
14595:             }
14596:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
14597:                 my ($emailrules,$emailruleorder) =
14598:                     &Apache::lonnet::inst_userrules($dom,'email');
14599:                 foreach my $type (@{$changes{'email_rule'}}) {
14600:                     if (ref($email_rule{$type}) eq 'ARRAY') {
14601:                         my $chgtext = '<ul>';
14602:                         foreach my $rule (@{$email_rule{$type}}) {
14603:                             if (ref($emailrules->{$rule}) eq 'HASH') {
14604:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
14605:                             }
14606:                         }
14607:                         $chgtext .= '</ul>';
14608:                         my $typename;
14609:                         if (@types) {
14610:                             if ($type eq 'default') {
14611:                                 $typename = $othertitle;
14612:                             } else {
14613:                                 $typename = $usertypes{$type};
14614:                             } 
14615:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
14616:                         }
14617:                         if (@{$email_rule{$type}} > 0) {
14618:                             $resulttext .= '<li>'.
14619:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
14620:                                                $usertypes{$type}).
14621:                                            $chgtext.
14622:                                            '</li>';
14623:                         } else {
14624:                             $resulttext .= '<li>'.
14625:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
14626:                                            '</li>'.
14627:                                            &mt('(Affiliation: [_1])',$typename);
14628:                         }
14629:                     }
14630:                 }
14631:             }
14632:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
14633:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
14634:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
14635:                         my $chgtext = '<ul>';
14636:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
14637:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
14638:                         }
14639:                         $chgtext .= '</ul>';
14640:                         $resulttext .= '<li>'.
14641:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
14642:                                           $chgtext.
14643:                                        '</li>';
14644:                     } else {
14645:                         $resulttext .= '<li>'.
14646:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
14647:                                        '</li>';
14648:                     }
14649:                 }
14650:             }
14651:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
14652:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
14653:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14654:                 foreach my $type (@{$changes{'selfcreate'}}) {
14655:                     my $typename = $type;
14656:                     if (keys(%usertypes) > 0) {
14657:                         if ($usertypes{$type} ne '') {
14658:                             $typename = $usertypes{$type};
14659:                         }
14660:                     }
14661:                     my @modifiable;
14662:                     $resulttext .= '<li>'.
14663:                                     &mt('Self-creation of account by users with status: [_1]',
14664:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
14665:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
14666:                     foreach my $field (@fields) {
14667:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
14668:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
14669:                         }
14670:                     }
14671:                     if (@modifiable > 0) {
14672:                         $resulttext .= join(', ',@modifiable);
14673:                     } else {
14674:                         $resulttext .= &mt('none');
14675:                     }
14676:                     $resulttext .= '</li>';
14677:                 }
14678:                 $resulttext .= '</ul></li>';
14679:             }
14680:             $resulttext .= '</ul>';
14681:             my $cachetime = 24*60*60;
14682:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
14683:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14684:             if (ref($lastactref) eq 'HASH') {
14685:                 $lastactref->{'domdefaults'} = 1;
14686:             }
14687:         } else {
14688:             $resulttext = &mt('No changes made to self-creation settings');
14689:         }
14690:     } else {
14691:         $resulttext = '<span class="LC_error">'.
14692:             &mt('An error occurred: [_1]',$putresult).'</span>';
14693:     }
14694:     if ($warningmsg ne '') {
14695:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
14696:     }
14697:     return $resulttext;
14698: }
14699: 
14700: sub process_captcha {
14701:     my ($container,$changes,$newsettings,$current) = @_;
14702:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
14703:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
14704:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
14705:         $newsettings->{'captcha'} = 'original';
14706:     }
14707:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
14708:         if ($container eq 'cancreate') {
14709:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14710:                 push(@{$changes->{'cancreate'}},'captcha');
14711:             } elsif (!defined($changes->{'cancreate'})) {
14712:                 $changes->{'cancreate'} = ['captcha'];
14713:             }
14714:         } else {
14715:             $changes->{'captcha'} = 1;
14716:         }
14717:     }
14718:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
14719:     if ($newsettings->{'captcha'} eq 'recaptcha') {
14720:         $newpub = $env{'form.'.$container.'_recaptchapub'};
14721:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
14722:         $newpub =~ s/[^\w\-]//g;
14723:         $newpriv =~ s/[^\w\-]//g;
14724:         $newsettings->{'recaptchakeys'} = {
14725:                                              public  => $newpub,
14726:                                              private => $newpriv,
14727:                                           };
14728:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
14729:         $newversion =~ s/\D//g;
14730:         if ($newversion ne '2') {
14731:             $newversion = 1;
14732:         }
14733:         $newsettings->{'recaptchaversion'} = $newversion;
14734:     }
14735:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
14736:         $currpub = $current->{'recaptchakeys'}{'public'};
14737:         $currpriv = $current->{'recaptchakeys'}{'private'};
14738:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
14739:             $newsettings->{'recaptchakeys'} = {
14740:                                                  public  => '',
14741:                                                  private => '',
14742:                                               }
14743:         }
14744:     }
14745:     if ($current->{'captcha'} eq 'recaptcha') {
14746:         $currversion = $current->{'recaptchaversion'};
14747:         if ($currversion ne '2') {
14748:             $currversion = 1;
14749:         }
14750:     }
14751:     if ($currversion ne $newversion) {
14752:         if ($container eq 'cancreate') {
14753:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14754:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
14755:             } elsif (!defined($changes->{'cancreate'})) {
14756:                 $changes->{'cancreate'} = ['recaptchaversion'];
14757:             }
14758:         } else {
14759:             $changes->{'recaptchaversion'} = 1;
14760:         }
14761:     }
14762:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
14763:         if ($container eq 'cancreate') {
14764:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14765:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
14766:             } elsif (!defined($changes->{'cancreate'})) {
14767:                 $changes->{'cancreate'} = ['recaptchakeys'];
14768:             }
14769:         } else {
14770:             $changes->{'recaptchakeys'} = 1;
14771:         }
14772:     }
14773:     return;
14774: }
14775: 
14776: sub modify_usermodification {
14777:     my ($dom,%domconfig) = @_;
14778:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
14779:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
14780:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
14781:             if ($key eq 'selfcreate') {
14782:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
14783:             } else {  
14784:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
14785:             }
14786:         }
14787:     }
14788:     my @contexts = ('author','course');
14789:     my %context_title = (
14790:                            author => 'In author context',
14791:                            course => 'In course context',
14792:                         );
14793:     my @fields = ('lastname','firstname','middlename','generation',
14794:                   'permanentemail','id');
14795:     my %roles = (
14796:                   author => ['ca','aa'],
14797:                   course => ['st','ep','ta','in','cr'],
14798:                 );
14799:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14800:     foreach my $context (@contexts) {
14801:         foreach my $role (@{$roles{$context}}) {
14802:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
14803:             foreach my $item (@fields) {
14804:                 if (grep(/^\Q$item\E$/,@modifiable)) {
14805:                     $modifyhash{$context}{$role}{$item} = 1;
14806:                 } else {
14807:                     $modifyhash{$context}{$role}{$item} = 0;
14808:                 }
14809:             }
14810:         }
14811:         if (ref($curr_usermodification{$context}) eq 'HASH') {
14812:             foreach my $role (@{$roles{$context}}) {
14813:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
14814:                     foreach my $field (@fields) {
14815:                         if ($modifyhash{$context}{$role}{$field} ne 
14816:                                 $curr_usermodification{$context}{$role}{$field}) {
14817:                             push(@{$changes{$context}},$role);
14818:                             last;
14819:                         }
14820:                     }
14821:                 }
14822:             }
14823:         } else {
14824:             foreach my $context (@contexts) {
14825:                 foreach my $role (@{$roles{$context}}) {
14826:                     push(@{$changes{$context}},$role);
14827:                 }
14828:             }
14829:         }
14830:     }
14831:     my %usermodification_hash =  (
14832:                                    usermodification => \%modifyhash,
14833:                                  );
14834:     my $putresult = &Apache::lonnet::put_dom('configuration',
14835:                                              \%usermodification_hash,$dom);
14836:     if ($putresult eq 'ok') {
14837:         if (keys(%changes) > 0) {
14838:             $resulttext = &mt('Changes made: ').'<ul>';
14839:             foreach my $context (@contexts) {
14840:                 if (ref($changes{$context}) eq 'ARRAY') {
14841:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
14842:                     if (ref($changes{$context}) eq 'ARRAY') {
14843:                         foreach my $role (@{$changes{$context}}) {
14844:                             my $rolename;
14845:                             if ($role eq 'cr') {
14846:                                 $rolename = &mt('Custom');
14847:                             } else {
14848:                                 $rolename = &Apache::lonnet::plaintext($role);
14849:                             }
14850:                             my @modifiable;
14851:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
14852:                             foreach my $field (@fields) {
14853:                                 if ($modifyhash{$context}{$role}{$field}) {
14854:                                     push(@modifiable,$fieldtitles{$field});
14855:                                 }
14856:                             }
14857:                             if (@modifiable > 0) {
14858:                                 $resulttext .= join(', ',@modifiable);
14859:                             } else {
14860:                                 $resulttext .= &mt('none'); 
14861:                             }
14862:                             $resulttext .= '</li>';
14863:                         }
14864:                         $resulttext .= '</ul></li>';
14865:                     }
14866:                 }
14867:             }
14868:             $resulttext .= '</ul>';
14869:         } else {
14870:             $resulttext = &mt('No changes made to user modification settings');
14871:         }
14872:     } else {
14873:         $resulttext = '<span class="LC_error">'.
14874:             &mt('An error occurred: [_1]',$putresult).'</span>';
14875:     }
14876:     return $resulttext;
14877: }
14878: 
14879: sub modify_defaults {
14880:     my ($dom,$lastactref,%domconfig) = @_;
14881:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
14882:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14883:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
14884:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
14885:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
14886:     foreach my $item (@items) {
14887:         $newvalues{$item} = $env{'form.'.$item};
14888:         if ($item eq 'auth_def') {
14889:             if ($newvalues{$item} ne '') {
14890:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
14891:                     push(@errors,$item);
14892:                 }
14893:             }
14894:         } elsif ($item eq 'lang_def') {
14895:             if ($newvalues{$item} ne '') {
14896:                 if ($newvalues{$item} =~ /^(\w+)/) {
14897:                     my $langcode = $1;
14898:                     if ($langcode ne 'x_chef') {
14899:                         if (code2language($langcode) eq '') {
14900:                             push(@errors,$item);
14901:                         }
14902:                     }
14903:                 } else {
14904:                     push(@errors,$item);
14905:                 }
14906:             }
14907:         } elsif ($item eq 'timezone_def') {
14908:             if ($newvalues{$item} ne '') {
14909:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
14910:                     push(@errors,$item);   
14911:                 }
14912:             }
14913:         } elsif ($item eq 'datelocale_def') {
14914:             if ($newvalues{$item} ne '') {
14915:                 my @datelocale_ids = DateTime::Locale->ids();
14916:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
14917:                     push(@errors,$item);
14918:                 }
14919:             }
14920:         } elsif ($item eq 'portal_def') {
14921:             if ($newvalues{$item} ne '') {
14922:                 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])\/?$/) {
14923:                     push(@errors,$item);
14924:                 }
14925:             }
14926:         } elsif ($item eq 'intauth_cost') {
14927:             if ($newvalues{$item} ne '') {
14928:                 if ($newvalues{$item} =~ /\D/) {
14929:                     push(@errors,$item);
14930:                 }
14931:             }
14932:         } elsif ($item eq 'intauth_check') {
14933:             if ($newvalues{$item} ne '') {
14934:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14935:                     push(@errors,$item);
14936:                 }
14937:             }
14938:         } elsif ($item eq 'intauth_switch') {
14939:             if ($newvalues{$item} ne '') {
14940:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
14941:                     push(@errors,$item);
14942:                 }
14943:             }
14944:         }
14945:         if (grep(/^\Q$item\E$/,@errors)) {
14946:             $newvalues{$item} = $domdefaults{$item};
14947:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
14948:             $changes{$item} = 1;
14949:         }
14950:         $domdefaults{$item} = $newvalues{$item};
14951:     }
14952:     my %defaults_hash = (
14953:                          defaults => \%newvalues,
14954:                         );
14955:     my $title = &defaults_titles();
14956: 
14957:     my $currinststatus;
14958:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
14959:         $currinststatus = $domconfig{'inststatus'};
14960:     } else {
14961:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14962:         $currinststatus = {
14963:                              inststatustypes => $usertypes,
14964:                              inststatusorder => $types,
14965:                              inststatusguest => [],
14966:                           };
14967:     }
14968:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
14969:     my @allpos;
14970:     my %alltypes;
14971:     my @inststatusguest;
14972:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
14973:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
14974:             unless (grep(/^\Q$type\E$/,@todelete)) {
14975:                 push(@inststatusguest,$type);
14976:             }
14977:         }
14978:     }
14979:     my ($currtitles,$currorder);
14980:     if (ref($currinststatus) eq 'HASH') {
14981:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
14982:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
14983:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
14984:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
14985:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
14986:                     }
14987:                 }
14988:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
14989:                     my $position = $env{'form.inststatus_pos_'.$type};
14990:                     $position =~ s/\D+//g;
14991:                     $allpos[$position] = $type;
14992:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
14993:                     $alltypes{$type} =~ s/`//g;
14994:                 }
14995:             }
14996:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
14997:             $currtitles =~ s/,$//;
14998:         }
14999:     }
15000:     if ($env{'form.addinststatus'}) {
15001:         my $newtype = $env{'form.addinststatus'};
15002:         $newtype =~ s/\W//g;
15003:         unless (exists($alltypes{$newtype})) {
15004:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
15005:             $alltypes{$newtype} =~ s/`//g; 
15006:             my $position = $env{'form.addinststatus_pos'};
15007:             $position =~ s/\D+//g;
15008:             if ($position ne '') {
15009:                 $allpos[$position] = $newtype;
15010:             }
15011:         }
15012:     }
15013:     my @orderedstatus;
15014:     foreach my $type (@allpos) {
15015:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
15016:             push(@orderedstatus,$type);
15017:         }
15018:     }
15019:     foreach my $type (keys(%alltypes)) {
15020:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
15021:             delete($alltypes{$type});
15022:         }
15023:     }
15024:     $defaults_hash{'inststatus'} = {
15025:                                      inststatustypes => \%alltypes,
15026:                                      inststatusorder => \@orderedstatus,
15027:                                      inststatusguest => \@inststatusguest,
15028:                                    };
15029:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
15030:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
15031:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
15032:         }
15033:     }
15034:     if ($currorder ne join(',',@orderedstatus)) {
15035:         $changes{'inststatus'}{'inststatusorder'} = 1;
15036:     }
15037:     my $newtitles;
15038:     foreach my $item (@orderedstatus) {
15039:         $newtitles .= $alltypes{$item}.',';
15040:     }
15041:     $newtitles =~ s/,$//;
15042:     if ($currtitles ne $newtitles) {
15043:         $changes{'inststatus'}{'inststatustypes'} = 1;
15044:     }
15045:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
15046:                                              $dom);
15047:     if ($putresult eq 'ok') {
15048:         if (keys(%changes) > 0) {
15049:             $resulttext = &mt('Changes made:').'<ul>';
15050:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
15051:             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";
15052:             foreach my $item (sort(keys(%changes))) {
15053:                 if ($item eq 'inststatus') {
15054:                     if (ref($changes{'inststatus'}) eq 'HASH') {
15055:                         if (@orderedstatus) {
15056:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
15057:                             foreach my $type (@orderedstatus) { 
15058:                                 $resulttext .= $alltypes{$type}.', ';
15059:                             }
15060:                             $resulttext =~ s/, $//;
15061:                             $resulttext .= '</li>';
15062:                         } else {
15063:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
15064:                         }
15065:                     }
15066:                 } else {
15067:                     my $value = $env{'form.'.$item};
15068:                     if ($value eq '') {
15069:                         $value = &mt('none');
15070:                     } elsif ($item eq 'auth_def') {
15071:                         my %authnames = &authtype_names();
15072:                         my %shortauth = (
15073:                                           internal   => 'int',
15074:                                           krb4       => 'krb4',
15075:                                           krb5       => 'krb5',
15076:                                           localauth  => 'loc',
15077:                                           lti        => 'lti',
15078:                         );
15079:                         $value = $authnames{$shortauth{$value}};
15080:                     } elsif ($item eq 'intauth_switch') {
15081:                         my %optiondesc = &Apache::lonlocal::texthash (
15082:                                             0 => 'No',
15083:                                             1 => 'Yes',
15084:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
15085:                                          );
15086:                         if ($value =~ /^(0|1|2)$/) {
15087:                             $value = $optiondesc{$value};
15088:                         } else {
15089:                             $value = &mt('none -- defaults to No');
15090:                         }
15091:                     } elsif ($item eq 'intauth_check') {
15092:                         my %optiondesc = &Apache::lonlocal::texthash (
15093:                                              0 => 'No',
15094:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
15095:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
15096:                                          );
15097:                         if ($value =~ /^(0|1|2)$/) {
15098:                             $value = $optiondesc{$value};
15099:                         } else {
15100:                             $value = &mt('none -- defaults to No');
15101:                         }
15102:                     }
15103:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
15104:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
15105:                 }
15106:             }
15107:             $resulttext .= '</ul>';
15108:             $mailmsgtext .= "\n";
15109:             my $cachetime = 24*60*60;
15110:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15111:             if (ref($lastactref) eq 'HASH') {
15112:                 $lastactref->{'domdefaults'} = 1;
15113:             }
15114:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
15115:                 my $notify = 1;
15116:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
15117:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
15118:                         $notify = 0;
15119:                     }
15120:                 }
15121:                 if ($notify) {
15122:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
15123:                                                "LON-CAPA Domain Settings Change - $dom",
15124:                                                $mailmsgtext);
15125:                 }
15126:             }
15127:         } else {
15128:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
15129:         }
15130:     } else {
15131:         $resulttext = '<span class="LC_error">'.
15132:             &mt('An error occurred: [_1]',$putresult).'</span>';
15133:     }
15134:     if (@errors > 0) {
15135:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
15136:         foreach my $item (@errors) {
15137:             $resulttext .= ' "'.$title->{$item}.'",';
15138:         }
15139:         $resulttext =~ s/,$//;
15140:     }
15141:     return $resulttext;
15142: }
15143: 
15144: sub modify_scantron {
15145:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15146:     my ($resulttext,%confhash,%changes,$errors);
15147:     my $custom = 'custom.tab';
15148:     my $default = 'default.tab';
15149:     my $servadm = $r->dir_config('lonAdmEMail');
15150:     my ($configuserok,$author_ok,$switchserver) =
15151:         &config_check($dom,$confname,$servadm);
15152:     if ($env{'form.scantronformat.filename'} ne '') {
15153:         my $error;
15154:         if ($configuserok eq 'ok') {
15155:             if ($switchserver) {
15156:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
15157:             } else {
15158:                 if ($author_ok eq 'ok') {
15159:                     my ($result,$scantronurl) =
15160:                         &publishlogo($r,'upload','scantronformat',$dom,
15161:                                      $confname,'scantron','','',$custom);
15162:                     if ($result eq 'ok') {
15163:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
15164:                         $changes{'scantronformat'} = 1;
15165:                     } else {
15166:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
15167:                     }
15168:                 } else {
15169:                     $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);
15170:                 }
15171:             }
15172:         } else {
15173:             $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);
15174:         }
15175:         if ($error) {
15176:             &Apache::lonnet::logthis($error);
15177:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15178:         }
15179:     }
15180:     if (ref($domconfig{'scantron'}) eq 'HASH') {
15181:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
15182:             if ($env{'form.scantronformat_del'}) {
15183:                 $confhash{'scantron'}{'scantronformat'} = '';
15184:                 $changes{'scantronformat'} = 1;
15185:             } else {
15186:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
15187:             }
15188:         }
15189:     }
15190:     my @options = ('hdr','pad','rem');
15191:     my @fields = &scantroncsv_fields();
15192:     my %titles = &scantronconfig_titles();
15193:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
15194:     my ($newdat,$currdat,%newcol,%currcol);
15195:     if (grep(/^dat$/,@formats)) {
15196:         $confhash{'scantron'}{config}{dat} = 1;
15197:         $newdat = 1;
15198:     } else {
15199:         $newdat = 0;
15200:     }
15201:     if (grep(/^csv$/,@formats)) {
15202:         my %bynum;
15203:         foreach my $field (@fields) {
15204:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
15205:                 my $posscol = $1;
15206:                 if (($posscol < 20) && (!$bynum{$posscol})) {
15207:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
15208:                     $bynum{$posscol} = $field;
15209:                     $newcol{$field} = $posscol;
15210:                 }
15211:             }
15212:         }
15213:         if (keys(%newcol)) {
15214:             foreach my $option (@options) {
15215:                 if ($env{'form.scantroncsv_'.$option}) {
15216:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
15217:                 }
15218:             }
15219:         }
15220:     }
15221:     $currdat = 1;
15222:     if (ref($domconfig{'scantron'}) eq 'HASH') {
15223:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
15224:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
15225:                 $currdat = 0;
15226:             }
15227:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
15228:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
15229:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
15230:                 }
15231:             }
15232:         }
15233:     }
15234:     if ($currdat != $newdat) {
15235:         $changes{'config'} = 1;
15236:     } else {
15237:         foreach my $field (@fields) {
15238:             if ($currcol{$field} ne '') {
15239:                 if ($currcol{$field} ne $newcol{$field}) {
15240:                     $changes{'config'} = 1;
15241:                     last;
15242:                 }
15243:             } elsif ($newcol{$field} ne '') {
15244:                 $changes{'config'} = 1;
15245:                 last;
15246:             }
15247:         }
15248:     }
15249:     if (keys(%confhash) > 0) {
15250:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
15251:                                                  $dom);
15252:         if ($putresult eq 'ok') {
15253:             if (keys(%changes) > 0) {
15254:                 if (ref($confhash{'scantron'}) eq 'HASH') {
15255:                     $resulttext = &mt('Changes made:').'<ul>';
15256:                     if ($changes{'scantronformat'}) {
15257:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
15258:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
15259:                         } else {
15260:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
15261:                         }
15262:                     }
15263:                     if ($changes{'config'}) {
15264:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
15265:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
15266:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
15267:                             }
15268:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
15269:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
15270:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
15271:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
15272:                                         foreach my $field (@fields) {
15273:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
15274:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
15275:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
15276:                                             }
15277:                                         }
15278:                                         $resulttext .= '</ul></li>';
15279:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
15280:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
15281:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
15282:                                                 foreach my $option (@options) {
15283:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
15284:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
15285:                                                     }
15286:                                                 }
15287:                                                 $resulttext .= '</ul></li>';
15288:                                             }
15289:                                         }
15290:                                     }
15291:                                 }
15292:                             }
15293:                         } else {
15294:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
15295:                         }
15296:                     }
15297:                     $resulttext .= '</ul>';
15298:                 } else {
15299:                     $resulttext = &mt('Changes made to bubblesheet format file.');
15300:                 }
15301:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15302:                 if (ref($lastactref) eq 'HASH') {
15303:                     $lastactref->{'domainconfig'} = 1;
15304:                 }
15305:             } else {
15306:                 $resulttext = &mt('No changes made to bubblesheet format settings');
15307:             }
15308:         } else {
15309:             $resulttext = '<span class="LC_error">'.
15310:                 &mt('An error occurred: [_1]',$putresult).'</span>';
15311:         }
15312:     } else {
15313:         $resulttext = &mt('No changes made to bubblesheet format file'); 
15314:     }
15315:     if ($errors) {
15316:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15317:                        $errors.'</ul>';
15318:     }
15319:     return $resulttext;
15320: }
15321: 
15322: sub modify_coursecategories {
15323:     my ($dom,$lastactref,%domconfig) = @_;
15324:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
15325:         $cathash);
15326:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
15327:     my @catitems = ('unauth','auth');
15328:     my @cattypes = ('std','domonly','codesrch','none');
15329:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
15330:         $cathash = $domconfig{'coursecategories'}{'cats'};
15331:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
15332:             $changes{'togglecats'} = 1;
15333:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
15334:         }
15335:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
15336:             $changes{'categorize'} = 1;
15337:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
15338:         }
15339:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
15340:             $changes{'togglecatscomm'} = 1;
15341:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
15342:         }
15343:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
15344:             $changes{'categorizecomm'} = 1;
15345:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
15346: 
15347:         }
15348:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
15349:             $changes{'togglecatsplace'} = 1;
15350:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
15351:         }
15352:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
15353:             $changes{'categorizeplace'} = 1;
15354:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
15355:         }
15356:         foreach my $item (@catitems) {
15357:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
15358:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
15359:                     $changes{$item} = 1;
15360:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
15361:                 }
15362:             }
15363:         }
15364:     } else {
15365:         $changes{'togglecats'} = 1;
15366:         $changes{'categorize'} = 1;
15367:         $changes{'togglecatscomm'} = 1;
15368:         $changes{'categorizecomm'} = 1;
15369:         $changes{'togglecatsplace'} = 1;
15370:         $changes{'categorizeplace'} = 1;
15371:         $domconfig{'coursecategories'} = {
15372:                                              togglecats => $env{'form.togglecats'},
15373:                                              categorize => $env{'form.categorize'},
15374:                                              togglecatscomm => $env{'form.togglecatscomm'},
15375:                                              categorizecomm => $env{'form.categorizecomm'},
15376:                                              togglecatsplace => $env{'form.togglecatsplace'},
15377:                                              categorizeplace => $env{'form.categorizeplace'},
15378:                                          };
15379:         foreach my $item (@catitems) {
15380:             if ($env{'form.coursecat_'.$item} ne 'std') {
15381:                 $changes{$item} = 1;
15382:             }
15383:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
15384:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
15385:             }
15386:         }
15387:     }
15388:     if (ref($cathash) eq 'HASH') {
15389:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
15390:             push (@deletecategory,'instcode::0');
15391:         }
15392:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
15393:             push(@deletecategory,'communities::0');
15394:         }
15395:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
15396:             push(@deletecategory,'placement::0');
15397:         }
15398:     }
15399:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
15400:     if (ref($cathash) eq 'HASH') {
15401:         if (@deletecategory > 0) {
15402:             #FIXME Need to remove category from all courses using a deleted category 
15403:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
15404:             foreach my $item (@deletecategory) {
15405:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
15406:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
15407:                     $deletions{$item} = 1;
15408:                     &recurse_cat_deletes($item,$cathash,\%deletions);
15409:                 }
15410:             }
15411:         }
15412:         foreach my $item (keys(%{$cathash})) {
15413:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15414:             if ($cathash->{$item} ne $env{'form.'.$item}) {
15415:                 $reorderings{$item} = 1;
15416:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
15417:             }
15418:             if ($env{'form.addcategory_name_'.$item} ne '') {
15419:                 my $newcat = $env{'form.addcategory_name_'.$item};
15420:                 my $newdepth = $depth+1;
15421:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
15422:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
15423:                 $adds{$newitem} = 1; 
15424:             }
15425:             if ($env{'form.subcat_'.$item} ne '') {
15426:                 my $newcat = $env{'form.subcat_'.$item};
15427:                 my $newdepth = $depth+1;
15428:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
15429:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
15430:                 $adds{$newitem} = 1;
15431:             }
15432:         }
15433:     }
15434:     if ($env{'form.instcode'} eq '1') {
15435:         if (ref($cathash) eq 'HASH') {
15436:             my $newitem = 'instcode::0';
15437:             if ($cathash->{$newitem} eq '') {  
15438:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
15439:                 $adds{$newitem} = 1;
15440:             }
15441:         } else {
15442:             my $newitem = 'instcode::0';
15443:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
15444:             $adds{$newitem} = 1;
15445:         }
15446:     }
15447:     if ($env{'form.communities'} eq '1') {
15448:         if (ref($cathash) eq 'HASH') {
15449:             my $newitem = 'communities::0';
15450:             if ($cathash->{$newitem} eq '') {
15451:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
15452:                 $adds{$newitem} = 1;
15453:             }
15454:         } else {
15455:             my $newitem = 'communities::0';
15456:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
15457:             $adds{$newitem} = 1;
15458:         }
15459:     }
15460:     if ($env{'form.placement'} eq '1') {
15461:         if (ref($cathash) eq 'HASH') {
15462:             my $newitem = 'placement::0';
15463:             if ($cathash->{$newitem} eq '') {
15464:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
15465:                 $adds{$newitem} = 1;
15466:             }
15467:         } else {
15468:             my $newitem = 'placement::0';
15469:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
15470:             $adds{$newitem} = 1;
15471:         }
15472:     }
15473:     if ($env{'form.addcategory_name'} ne '') {
15474:         if (($env{'form.addcategory_name'} ne 'instcode') &&
15475:             ($env{'form.addcategory_name'} ne 'communities') &&
15476:             ($env{'form.addcategory_name'} ne 'placement')) {
15477:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
15478:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
15479:             $adds{$newitem} = 1;
15480:         }
15481:     }
15482:     my $putresult;
15483:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15484:         if (keys(%deletions) > 0) {
15485:             foreach my $key (keys(%deletions)) {
15486:                 if ($predelallitems{$key} ne '') {
15487:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
15488:                 }
15489:             }
15490:         }
15491:         my (@chkcats,@chktrails,%chkallitems);
15492:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
15493:         if (ref($chkcats[0]) eq 'ARRAY') {
15494:             my $depth = 0;
15495:             my $chg = 0;
15496:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
15497:                 my $name = $chkcats[0][$i];
15498:                 my $item;
15499:                 if ($name eq '') {
15500:                     $chg ++;
15501:                 } else {
15502:                     $item = &escape($name).'::0';
15503:                     if ($chg) {
15504:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
15505:                     }
15506:                     $depth ++; 
15507:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
15508:                     $depth --;
15509:                 }
15510:             }
15511:         }
15512:     }
15513:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15514:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
15515:         if ($putresult eq 'ok') {
15516:             my %title = (
15517:                          togglecats     => 'Show/Hide a course in catalog',
15518:                          categorize     => 'Assign a category to a course',
15519:                          togglecatscomm => 'Show/Hide a community in catalog',
15520:                          categorizecomm => 'Assign a category to a community',
15521:                         );
15522:             my %level = (
15523:                          dom  => 'set in Domain ("Modify Course/Community")',
15524:                          crs  => 'set in Course ("Course Configuration")',
15525:                          comm => 'set in Community ("Community Configuration")',
15526:                          none     => 'No catalog',
15527:                          std      => 'Standard catalog',
15528:                          domonly  => 'Domain-only catalog',
15529:                          codesrch => 'Code search form',
15530:                         );
15531:             $resulttext = &mt('Changes made:').'<ul>';
15532:             if ($changes{'togglecats'}) {
15533:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
15534:             }
15535:             if ($changes{'categorize'}) {
15536:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
15537:             }
15538:             if ($changes{'togglecatscomm'}) {
15539:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
15540:             }
15541:             if ($changes{'categorizecomm'}) {
15542:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
15543:             }
15544:             if ($changes{'unauth'}) {
15545:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
15546:             }
15547:             if ($changes{'auth'}) {
15548:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
15549:             }
15550:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15551:                 my $cathash;
15552:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
15553:                     $cathash = $domconfig{'coursecategories'}{'cats'};
15554:                 } else {
15555:                     $cathash = {};
15556:                 } 
15557:                 my (@cats,@trails,%allitems);
15558:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
15559:                 if (keys(%deletions) > 0) {
15560:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
15561:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
15562:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
15563:                     }
15564:                     $resulttext .= '</ul></li>';
15565:                 }
15566:                 if (keys(%reorderings) > 0) {
15567:                     my %sort_by_trail;
15568:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
15569:                     foreach my $key (keys(%reorderings)) {
15570:                         if ($allitems{$key} ne '') {
15571:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15572:                         }
15573:                     }
15574:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15575:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15576:                     }
15577:                     $resulttext .= '</ul></li>';
15578:                 }
15579:                 if (keys(%adds) > 0) {
15580:                     my %sort_by_trail;
15581:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
15582:                     foreach my $key (keys(%adds)) {
15583:                         if ($allitems{$key} ne '') {
15584:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15585:                         }
15586:                     }
15587:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15588:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15589:                     }
15590:                     $resulttext .= '</ul></li>';
15591:                 }
15592:             }
15593:             $resulttext .= '</ul>';
15594:             if ($changes{'unauth'} || $changes{'auth'}) {
15595:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15596:                 if ($changes{'auth'}) {
15597:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
15598:                 }
15599:                 if ($changes{'unauth'}) {
15600:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
15601:                 }
15602:                 my $cachetime = 24*60*60;
15603:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15604:                 if (ref($lastactref) eq 'HASH') {
15605:                     $lastactref->{'domdefaults'} = 1;
15606:                 }
15607:             }
15608:         } else {
15609:             $resulttext = '<span class="LC_error">'.
15610:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15611:         }
15612:     } else {
15613:         $resulttext = &mt('No changes made to course and community categories');
15614:     }
15615:     return $resulttext;
15616: }
15617: 
15618: sub modify_serverstatuses {
15619:     my ($dom,%domconfig) = @_;
15620:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
15621:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
15622:         %currserverstatus = %{$domconfig{'serverstatuses'}};
15623:     }
15624:     my @pages = &serverstatus_pages();
15625:     foreach my $type (@pages) {
15626:         $newserverstatus{$type}{'namedusers'} = '';
15627:         $newserverstatus{$type}{'machines'} = '';
15628:         if (defined($env{'form.'.$type.'_namedusers'})) {
15629:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
15630:             my @okusers;
15631:             foreach my $user (@users) {
15632:                 my ($uname,$udom) = split(/:/,$user);
15633:                 if (($udom =~ /^$match_domain$/) &&   
15634:                     (&Apache::lonnet::domain($udom)) &&
15635:                     ($uname =~ /^$match_username$/)) {
15636:                     if (!grep(/^\Q$user\E/,@okusers)) {
15637:                         push(@okusers,$user);
15638:                     }
15639:                 }
15640:             }
15641:             if (@okusers > 0) {
15642:                  @okusers = sort(@okusers);
15643:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
15644:             }
15645:         }
15646:         if (defined($env{'form.'.$type.'_machines'})) {
15647:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
15648:             my @okmachines;
15649:             foreach my $ip (@machines) {
15650:                 my @parts = split(/\./,$ip);
15651:                 next if (@parts < 4);
15652:                 my $badip = 0;
15653:                 for (my $i=0; $i<4; $i++) {
15654:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
15655:                         $badip = 1;
15656:                         last;
15657:                     }
15658:                 }
15659:                 if (!$badip) {
15660:                     push(@okmachines,$ip);     
15661:                 }
15662:             }
15663:             @okmachines = sort(@okmachines);
15664:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
15665:         }
15666:     }
15667:     my %serverstatushash =  (
15668:                                 serverstatuses => \%newserverstatus,
15669:                             );
15670:     foreach my $type (@pages) {
15671:         foreach my $setting ('namedusers','machines') {
15672:             my (@current,@new);
15673:             if (ref($currserverstatus{$type}) eq 'HASH') {
15674:                 if ($currserverstatus{$type}{$setting} ne '') { 
15675:                     @current = split(/,/,$currserverstatus{$type}{$setting});
15676:                 }
15677:             }
15678:             if ($newserverstatus{$type}{$setting} ne '') {
15679:                 @new = split(/,/,$newserverstatus{$type}{$setting});
15680:             }
15681:             if (@current > 0) {
15682:                 if (@new > 0) {
15683:                     foreach my $item (@current) {
15684:                         if (!grep(/^\Q$item\E$/,@new)) {
15685:                             $changes{$type}{$setting} = 1;
15686:                             last;
15687:                         }
15688:                     }
15689:                     foreach my $item (@new) {
15690:                         if (!grep(/^\Q$item\E$/,@current)) {
15691:                             $changes{$type}{$setting} = 1;
15692:                             last;
15693:                         }
15694:                     }
15695:                 } else {
15696:                     $changes{$type}{$setting} = 1;
15697:                 }
15698:             } elsif (@new > 0) {
15699:                 $changes{$type}{$setting} = 1;
15700:             }
15701:         }
15702:     }
15703:     if (keys(%changes) > 0) {
15704:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
15705:         my $putresult = &Apache::lonnet::put_dom('configuration',
15706:                                                  \%serverstatushash,$dom);
15707:         if ($putresult eq 'ok') {
15708:             $resulttext .= &mt('Changes made:').'<ul>';
15709:             foreach my $type (@pages) {
15710:                 if (ref($changes{$type}) eq 'HASH') {
15711:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
15712:                     if ($changes{$type}{'namedusers'}) {
15713:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
15714:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
15715:                         } else {
15716:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
15717:                         }
15718:                     }
15719:                     if ($changes{$type}{'machines'}) {
15720:                         if ($newserverstatus{$type}{'machines'} eq '') {
15721:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
15722:                         } else {
15723:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
15724:                         }
15725: 
15726:                     }
15727:                     $resulttext .= '</ul></li>';
15728:                 }
15729:             }
15730:             $resulttext .= '</ul>';
15731:         } else {
15732:             $resulttext = '<span class="LC_error">'.
15733:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
15734: 
15735:         }
15736:     } else {
15737:         $resulttext = &mt('No changes made to access to server status pages');
15738:     }
15739:     return $resulttext;
15740: }
15741: 
15742: sub modify_helpsettings {
15743:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15744:     my ($resulttext,$errors,%changes,%helphash);
15745:     my %defaultchecked = ('submitbugs' => 'on');
15746:     my @offon = ('off','on');
15747:     my @toggles = ('submitbugs');
15748:     my %current = ('submitbugs' => '',
15749:                    'adhoc'      => {},
15750:                   );
15751:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
15752:         %current = %{$domconfig{'helpsettings'}};
15753:     }
15754:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15755:     foreach my $item (@toggles) {
15756:         if ($defaultchecked{$item} eq 'on') { 
15757:             if ($current{$item} eq '') {
15758:                 if ($env{'form.'.$item} eq '0') {
15759:                     $changes{$item} = 1;
15760:                 }
15761:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15762:                 $changes{$item} = 1;
15763:             }
15764:         } elsif ($defaultchecked{$item} eq 'off') {
15765:             if ($current{$item} eq '') {
15766:                 if ($env{'form.'.$item} eq '1') {
15767:                     $changes{$item} = 1;
15768:                 }
15769:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15770:                 $changes{$item} = 1;
15771:             }
15772:         }
15773:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
15774:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
15775:         }
15776:     }
15777:     my $maxnum = $env{'form.helproles_maxnum'};
15778:     my $confname = $dom.'-domainconfig';
15779:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
15780:     my (@allpos,%newsettings,%changedprivs,$newrole);
15781:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15782:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
15783:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
15784:     my %lt = &Apache::lonlocal::texthash(
15785:                     s      => 'system',
15786:                     d      => 'domain',
15787:                     order  => 'Display order',
15788:                     access => 'Role usage',
15789:                     all    => 'All with domain helpdesk or helpdesk assistant role',
15790:                     dh     => 'All with domain helpdesk role',
15791:                     da     => 'All with domain helpdesk assistant role',
15792:                     none   => 'None',
15793:                     status => 'Determined based on institutional status',
15794:                     inc    => 'Include all, but exclude specific personnel',
15795:                     exc    => 'Exclude all, but include specific personnel',
15796:     );
15797:     for (my $num=0; $num<=$maxnum; $num++) {
15798:         my ($prefix,$identifier,$rolename,%curr);
15799:         if ($num == $maxnum) {
15800:             next unless ($env{'form.newcusthelp'} == $maxnum);
15801:             $identifier = 'custhelp'.$num;
15802:             $prefix = 'helproles_'.$num;
15803:             $rolename = $env{'form.custhelpname'.$num};
15804:             $rolename=~s/[^A-Za-z0-9]//gs;
15805:             next if ($rolename eq '');
15806:             next if (exists($existing{'rolesdef_'.$rolename}));
15807:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15808:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15809:                                                      $newprivs{'c'},$confname,$dom);
15810:             if ($result ne 'ok') {
15811:                 $errors .= '<li><span class="LC_error">'.
15812:                            &mt('An error occurred storing the new custom role: [_1]',
15813:                            $result).'</span></li>';
15814:                 next;
15815:             } else {
15816:                 $changedprivs{$rolename} = \%newprivs;
15817:                 $newrole = $rolename;
15818:             }
15819:         } else {
15820:             $prefix = 'helproles_'.$num;
15821:             $rolename = $env{'form.'.$prefix};
15822:             next if ($rolename eq '');
15823:             next unless (exists($existing{'rolesdef_'.$rolename}));
15824:             $identifier = 'custhelp'.$num;
15825:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15826:             my %currprivs;
15827:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
15828:                 split(/\_/,$existing{'rolesdef_'.$rolename});
15829:             foreach my $level ('c','d','s') {
15830:                 if ($newprivs{$level} ne $currprivs{$level}) {
15831:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15832:                                                              $newprivs{'c'},$confname,$dom);
15833:                     if ($result ne 'ok') {
15834:                         $errors .= '<li><span class="LC_error">'.
15835:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
15836:                                        $rolename,$result).'</span></li>';
15837:                     } else {
15838:                         $changedprivs{$rolename} = \%newprivs;
15839:                     }
15840:                     last;
15841:                 }
15842:             }
15843:             if (ref($current{'adhoc'}) eq 'HASH') {
15844:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15845:                     %curr = %{$current{'adhoc'}{$rolename}};
15846:                 }
15847:             }
15848:         }
15849:         my $newpos = $env{'form.'.$prefix.'_pos'};
15850:         $newpos =~ s/\D+//g;
15851:         $allpos[$newpos] = $rolename;
15852:         my $newdesc = $env{'form.'.$prefix.'_desc'};
15853:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
15854:         if ($curr{'desc'}) {
15855:             if ($curr{'desc'} ne $newdesc) {
15856:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
15857:                 $newsettings{$rolename}{'desc'} = $newdesc;
15858:             }
15859:         } elsif ($newdesc ne '') {
15860:             $changes{'customrole'}{$rolename}{'desc'} = 1;
15861:             $newsettings{$rolename}{'desc'} = $newdesc;
15862:         }
15863:         my $access = $env{'form.'.$prefix.'_access'};
15864:         if (grep(/^\Q$access\E$/,@accesstypes)) {
15865:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
15866:             if ($access eq 'status') {
15867:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
15868:                 if (scalar(@statuses) == 0) {
15869:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
15870:                 } else {
15871:                     my (@shownstatus,$numtypes);
15872:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15873:                     if (ref($types) eq 'ARRAY') {
15874:                         $numtypes = scalar(@{$types});
15875:                         foreach my $type (sort(@statuses)) {
15876:                             if ($type eq 'default') {
15877:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15878:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
15879:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15880:                                 push(@shownstatus,$usertypes->{$type});
15881:                             }
15882:                         }
15883:                     }
15884:                     if (grep(/^default$/,@statuses)) {
15885:                         push(@shownstatus,$othertitle);
15886:                     }
15887:                     if (scalar(@shownstatus) == 1+$numtypes) {
15888:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
15889:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
15890:                     } else {
15891:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
15892:                         if (ref($curr{'status'}) eq 'ARRAY') {
15893:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15894:                             if (@diffs) {
15895:                                 $changes{'customrole'}{$rolename}{$access} = 1;
15896:                             }
15897:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15898:                             $changes{'customrole'}{$rolename}{$access} = 1;
15899:                         }
15900:                     }
15901:                 }
15902:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
15903:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
15904:                 my @newspecstaff;
15905:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15906:                 foreach my $person (sort(@personnel)) {
15907:                     if ($domhelpdesk{$person}) {
15908:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
15909:                     }
15910:                 }
15911:                 if (ref($curr{$access}) eq 'ARRAY') {
15912:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15913:                     if (@diffs) {
15914:                         $changes{'customrole'}{$rolename}{$access} = 1;
15915:                     }
15916:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15917:                     $changes{'customrole'}{$rolename}{$access} = 1;
15918:                 }
15919:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15920:                     my ($uname,$udom) = split(/:/,$person);
15921:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
15922:                 }
15923:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
15924:             }
15925:         } else {
15926:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
15927:         }
15928:         unless ($curr{'access'} eq $access) {
15929:             $changes{'customrole'}{$rolename}{'access'} = 1;
15930:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
15931:         }
15932:     }
15933:     if (@allpos > 0) {
15934:         my $idx = 0;
15935:         foreach my $rolename (@allpos) {
15936:             if ($rolename ne '') {
15937:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
15938:                 if (ref($current{'adhoc'}) eq 'HASH') {
15939:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15940:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
15941:                             $changes{'customrole'}{$rolename}{'order'} = 1;
15942:                             $newsettings{$rolename}{'order'} = $idx+1;
15943:                         }
15944:                     }
15945:                 }
15946:                 $idx ++;
15947:             }
15948:         }
15949:     }
15950:     my $putresult;
15951:     if (keys(%changes) > 0) {
15952:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
15953:         if ($putresult eq 'ok') {
15954:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
15955:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
15956:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
15957:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
15958:                 }
15959:             }
15960:             my $cachetime = 24*60*60;
15961:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15962:             if (ref($lastactref) eq 'HASH') {
15963:                 $lastactref->{'domdefaults'} = 1;
15964:             }
15965:         } else {
15966:             $errors .= '<li><span class="LC_error">'.
15967:                        &mt('An error occurred storing the settings: [_1]',
15968:                            $putresult).'</span></li>';
15969:         }
15970:     }
15971:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
15972:         $resulttext = &mt('Changes made:').'<ul>';
15973:         my (%shownprivs,@levelorder);
15974:         @levelorder = ('c','d','s');
15975:         if ((keys(%changes)) && ($putresult eq 'ok')) {
15976:             foreach my $item (sort(keys(%changes))) {
15977:                 if ($item eq 'submitbugs') {
15978:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
15979:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
15980:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
15981:                 } elsif ($item eq 'customrole') {
15982:                     if (ref($changes{'customrole'}) eq 'HASH') {
15983:                         my @keyorder = ('order','desc','access','status','exc','inc');
15984:                         my %keytext = &Apache::lonlocal::texthash(
15985:                                                                    order  => 'Order',
15986:                                                                    desc   => 'Role description',
15987:                                                                    access => 'Role usage',
15988:                                                                    status => 'Allowed institutional types',
15989:                                                                    exc    => 'Allowed personnel',
15990:                                                                    inc    => 'Disallowed personnel',
15991:                         );
15992:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
15993:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
15994:                                 if ($role eq $newrole) {
15995:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
15996:                                                               $role).'<ul>';
15997:                                 } else {
15998:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15999:                                                               $role).'<ul>';
16000:                                 }
16001:                                 foreach my $key (@keyorder) {
16002:                                     if ($changes{'customrole'}{$role}{$key}) {
16003:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
16004:                                                                   $keytext{$key},$newsettings{$role}{$key}).
16005:                                                        '</li>';
16006:                                     }
16007:                                 }
16008:                                 if (ref($changedprivs{$role}) eq 'HASH') {
16009:                                     $shownprivs{$role} = 1;
16010:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
16011:                                     foreach my $level (@levelorder) {
16012:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
16013:                                             next if ($item eq '');
16014:                                             my ($priv) = split(/\&/,$item,2);
16015:                                             if (&Apache::lonnet::plaintext($priv)) {
16016:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
16017:                                                 unless ($level eq 'c') {
16018:                                                     $resulttext .= ' ('.$lt{$level}.')';
16019:                                                 }
16020:                                                 $resulttext .= '</li>';
16021:                                             }
16022:                                         }
16023:                                     }
16024:                                     $resulttext .= '</ul>';
16025:                                 }
16026:                                 $resulttext .= '</ul></li>';
16027:                             }
16028:                         }
16029:                     }
16030:                 }
16031:             }
16032:         }
16033:         if (keys(%changedprivs)) {
16034:             foreach my $role (sort(keys(%changedprivs))) {
16035:                 unless ($shownprivs{$role}) {
16036:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
16037:                                               $role).'<ul>'.
16038:                                    '<li>'.&mt('Privileges set to :').'<ul>';
16039:                     foreach my $level (@levelorder) {
16040:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
16041:                             next if ($item eq '');
16042:                             my ($priv) = split(/\&/,$item,2);
16043:                             if (&Apache::lonnet::plaintext($priv)) {
16044:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
16045:                                 unless ($level eq 'c') {
16046:                                     $resulttext .= ' ('.$lt{$level}.')';
16047:                                 }
16048:                                 $resulttext .= '</li>';
16049:                             }
16050:                         }
16051:                     }
16052:                     $resulttext .= '</ul></li></ul></li>';
16053:                 }
16054:             }
16055:         }
16056:         $resulttext .= '</ul>';
16057:     } else {
16058:         $resulttext = &mt('No changes made to help settings');
16059:     }
16060:     if ($errors) {
16061:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
16062:                                     $errors.'</ul>';
16063:     }
16064:     return $resulttext;
16065: }
16066: 
16067: sub modify_coursedefaults {
16068:     my ($dom,$lastactref,%domconfig) = @_;
16069:     my ($resulttext,$errors,%changes,%defaultshash);
16070:     my %defaultchecked = (
16071:                            'canuse_pdfforms' => 'off',
16072:                            'uselcmath'       => 'on',
16073:                            'usejsme'         => 'on'
16074:                          );
16075:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
16076:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
16077:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
16078:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
16079:                    'mysqltables_textbook','mysqltables_placement');
16080:     my @types = ('official','unofficial','community','textbook','placement');
16081:     my %staticdefaults = (
16082:                            anonsurvey_threshold => 10,
16083:                            uploadquota          => 500,
16084:                            postsubmit           => 60,
16085:                            mysqltables          => 172800,
16086:                          );
16087:     my %texoptions = (
16088:                         MathJax  => 'MathJax',
16089:                         mimetex  => &mt('Convert to Images'),
16090:                         tth      => &mt('TeX to HTML'),
16091:                      );
16092:     $defaultshash{'coursedefaults'} = {};
16093: 
16094:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
16095:         if ($domconfig{'coursedefaults'} eq '') {
16096:             $domconfig{'coursedefaults'} = {};
16097:         }
16098:     }
16099: 
16100:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
16101:         foreach my $item (@toggles) {
16102:             if ($defaultchecked{$item} eq 'on') {
16103:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
16104:                     ($env{'form.'.$item} eq '0')) {
16105:                     $changes{$item} = 1;
16106:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
16107:                     $changes{$item} = 1;
16108:                 }
16109:             } elsif ($defaultchecked{$item} eq 'off') {
16110:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
16111:                     ($env{'form.'.$item} eq '1')) {
16112:                     $changes{$item} = 1;
16113:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
16114:                     $changes{$item} = 1;
16115:                 }
16116:             }
16117:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
16118:         }
16119:         foreach my $item (@numbers) {
16120:             my ($currdef,$newdef);
16121:             $newdef = $env{'form.'.$item};
16122:             if ($item eq 'anonsurvey_threshold') {
16123:                 $currdef = $domconfig{'coursedefaults'}{$item};
16124:                 $newdef =~ s/\D//g;
16125:                 if ($newdef eq '' || $newdef < 1) {
16126:                     $newdef = 1;
16127:                 }
16128:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
16129:             } else {
16130:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
16131:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
16132:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
16133:                 }
16134:                 $newdef =~ s/[^\w.\-]//g;
16135:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
16136:             }
16137:             if ($currdef ne $newdef) {
16138:                 if ($item eq 'anonsurvey_threshold') {
16139:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
16140:                         $changes{$item} = 1;
16141:                     }
16142:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
16143:                     my $setting = $1;
16144:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
16145:                         $changes{$setting} = 1;
16146:                     }
16147:                 }
16148:             }
16149:         }
16150:         my $texengine;
16151:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
16152:             $texengine = $env{'form.texengine'};
16153:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
16154:             if ($currdef eq '') {
16155:                 unless ($texengine eq $Apache::lonnet::deftex) {
16156:                     $changes{'texengine'} = 1;
16157:                 }
16158:             } elsif ($currdef ne $texengine) {
16159:                 $changes{'texengine'} = 1;
16160:             }
16161:         }
16162:         if ($texengine ne '') {
16163:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
16164:         }
16165:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
16166:         my @currclonecode;
16167:         if (ref($currclone) eq 'HASH') {
16168:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
16169:                 @currclonecode = @{$currclone->{'instcode'}};
16170:             }
16171:         }
16172:         my $newclone;
16173:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
16174:             $newclone = $env{'form.canclone'};
16175:         }
16176:         if ($newclone eq 'instcode') {
16177:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
16178:             my (%codedefaults,@code_order,@clonecode);
16179:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
16180:                                                     \@code_order);
16181:             foreach my $item (@code_order) {
16182:                 if (grep(/^\Q$item\E$/,@newcodes)) {
16183:                     push(@clonecode,$item);
16184:                 }
16185:             }
16186:             if (@clonecode) {
16187:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
16188:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
16189:                 if (@diffs) {
16190:                     $changes{'canclone'} = 1;
16191:                 }
16192:             } else {
16193:                 $newclone eq '';
16194:             }
16195:         } elsif ($newclone ne '') {
16196:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
16197:         }
16198:         if ($newclone ne $currclone) {
16199:             $changes{'canclone'} = 1;
16200:         }
16201:         my %credits;
16202:         foreach my $type (@types) {
16203:             unless ($type eq 'community') {
16204:                 $credits{$type} = $env{'form.'.$type.'_credits'};
16205:                 $credits{$type} =~ s/[^\d.]+//g;
16206:             }
16207:         }
16208:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
16209:             ($env{'form.coursecredits'} eq '1')) {
16210:             $changes{'coursecredits'} = 1;
16211:             foreach my $type (keys(%credits)) {
16212:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
16213:             }
16214:         } else {
16215:             if ($env{'form.coursecredits'} eq '1') {
16216:                 foreach my $type (@types) {
16217:                     unless ($type eq 'community') {
16218:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
16219:                             $changes{'coursecredits'} = 1;
16220:                         }
16221:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
16222:                     }
16223:                 }
16224:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16225:                 foreach my $type (@types) {
16226:                     unless ($type eq 'community') {
16227:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
16228:                             $changes{'coursecredits'} = 1;
16229:                             last;
16230:                         }
16231:                     }
16232:                 }
16233:             }
16234:         }
16235:         if ($env{'form.postsubmit'} eq '1') {
16236:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
16237:             my %currtimeout;
16238:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16239:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
16240:                     $changes{'postsubmit'} = 1;
16241:                 }
16242:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16243:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
16244:                 }
16245:             } else {
16246:                 $changes{'postsubmit'} = 1;
16247:             }
16248:             foreach my $type (@types) {
16249:                 my $timeout = $env{'form.'.$type.'_timeout'};
16250:                 $timeout =~ s/\D//g;
16251:                 if ($timeout == $staticdefaults{'postsubmit'}) {
16252:                     $timeout = '';
16253:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
16254:                     $timeout = '0';
16255:                 }
16256:                 unless ($timeout eq '') {
16257:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
16258:                 }
16259:                 if (exists($currtimeout{$type})) {
16260:                     if ($timeout ne $currtimeout{$type}) {
16261:                         $changes{'postsubmit'} = 1;
16262:                     }
16263:                 } elsif ($timeout ne '') {
16264:                     $changes{'postsubmit'} = 1;
16265:                 }
16266:             }
16267:         } else {
16268:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
16269:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16270:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
16271:                     $changes{'postsubmit'} = 1;
16272:                 }
16273:             } else {
16274:                 $changes{'postsubmit'} = 1;
16275:             }
16276:         }
16277:     }
16278:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16279:                                              $dom);
16280:     if ($putresult eq 'ok') {
16281:         if (keys(%changes) > 0) {
16282:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16283:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
16284:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
16285:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
16286:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
16287:                     if ($changes{$item}) {
16288:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
16289:                     }
16290:                 }
16291:                 if ($changes{'coursecredits'}) {
16292:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16293:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
16294:                             $domdefaults{$type.'credits'} =
16295:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
16296:                         }
16297:                     }
16298:                 }
16299:                 if ($changes{'postsubmit'}) {
16300:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16301:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
16302:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16303:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
16304:                                 $domdefaults{$type.'postsubtimeout'} =
16305:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
16306:                             }
16307:                         }
16308:                     }
16309:                 }
16310:                 if ($changes{'uploadquota'}) {
16311:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
16312:                         foreach my $type (@types) {
16313:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
16314:                         }
16315:                     }
16316:                 }
16317:                 if ($changes{'canclone'}) {
16318:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
16319:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
16320:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
16321:                             if (@clonecodes) {
16322:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
16323:                             }
16324:                         }
16325:                     } else {
16326:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
16327:                     }
16328:                 }
16329:                 my $cachetime = 24*60*60;
16330:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16331:                 if (ref($lastactref) eq 'HASH') {
16332:                     $lastactref->{'domdefaults'} = 1;
16333:                 }
16334:             }
16335:             $resulttext = &mt('Changes made:').'<ul>';
16336:             foreach my $item (sort(keys(%changes))) {
16337:                 if ($item eq 'canuse_pdfforms') {
16338:                     if ($env{'form.'.$item} eq '1') {
16339:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
16340:                     } else {
16341:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
16342:                     }
16343:                 } elsif ($item eq 'uselcmath') {
16344:                     if ($env{'form.'.$item} eq '1') {
16345:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
16346:                     } else {
16347:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
16348:                     }
16349:                 } elsif ($item eq 'usejsme') {
16350:                     if ($env{'form.'.$item} eq '1') {
16351:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
16352:                     } else {
16353:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
16354:                     }
16355:                 } elsif ($item eq 'texengine') {
16356:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
16357:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
16358:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
16359:                     }
16360:                 } elsif ($item eq 'anonsurvey_threshold') {
16361:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
16362:                 } elsif ($item eq 'uploadquota') {
16363:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
16364:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
16365:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
16366:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
16367:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
16368:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
16369:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
16370:                                        '</ul>'.
16371:                                        '</li>';
16372:                     } else {
16373:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
16374:                     }
16375:                 } elsif ($item eq 'mysqltables') {
16376:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
16377:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
16378:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
16379:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
16380:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
16381:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
16382:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
16383:                                        '</ul>'.
16384:                                        '</li>';
16385:                     } else {
16386:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
16387:                     }
16388:                 } elsif ($item eq 'postsubmit') {
16389:                     if ($domdefaults{'postsubmit'} eq 'off') {
16390:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
16391:                     } else {
16392:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
16393:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
16394:                             $resulttext .= &mt('durations:').'<ul>';
16395:                             foreach my $type (@types) {
16396:                                 $resulttext .= '<li>';
16397:                                 my $timeout;
16398:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
16399:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
16400:                                 }
16401:                                 my $display;
16402:                                 if ($timeout eq '0') {
16403:                                     $display = &mt('unlimited');
16404:                                 } elsif ($timeout eq '') {
16405:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
16406:                                 } else {
16407:                                     $display = &mt('[quant,_1,second]',$timeout);
16408:                                 }
16409:                                 if ($type eq 'community') {
16410:                                     $resulttext .= &mt('Communities');
16411:                                 } elsif ($type eq 'official') {
16412:                                     $resulttext .= &mt('Official courses');
16413:                                 } elsif ($type eq 'unofficial') {
16414:                                     $resulttext .= &mt('Unofficial courses');
16415:                                 } elsif ($type eq 'textbook') {
16416:                                     $resulttext .= &mt('Textbook courses');
16417:                                 } elsif ($type eq 'placement') {
16418:                                     $resulttext .= &mt('Placement tests');
16419:                                 }
16420:                                 $resulttext .= ' -- '.$display.'</li>';
16421:                             }
16422:                             $resulttext .= '</ul>';
16423:                         }
16424:                         $resulttext .= '</li>';
16425:                     }
16426:                 } elsif ($item eq 'coursecredits') {
16427:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16428:                         if (($domdefaults{'officialcredits'} eq '') &&
16429:                             ($domdefaults{'unofficialcredits'} eq '') &&
16430:                             ($domdefaults{'textbookcredits'} eq '')) {
16431:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
16432:                         } else {
16433:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
16434:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
16435:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
16436:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
16437:                                            '</ul>'.
16438:                                            '</li>';
16439:                         }
16440:                     } else {
16441:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
16442:                     }
16443:                 } elsif ($item eq 'canclone') {
16444:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
16445:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
16446:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
16447:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
16448:                         }
16449:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
16450:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
16451:                     } else {
16452:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
16453:                     }
16454:                 }
16455:             }
16456:             $resulttext .= '</ul>';
16457:         } else {
16458:             $resulttext = &mt('No changes made to course defaults');
16459:         }
16460:     } else {
16461:         $resulttext = '<span class="LC_error">'.
16462:             &mt('An error occurred: [_1]',$putresult).'</span>';
16463:     }
16464:     return $resulttext;
16465: }
16466: 
16467: sub modify_selfenrollment {
16468:     my ($dom,$lastactref,%domconfig) = @_;
16469:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
16470:     my @types = ('official','unofficial','community','textbook','placement');
16471:     my %titles = &tool_titles();
16472:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
16473:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
16474:     $ordered{'default'} = ['types','registered','approval','limit'];
16475: 
16476:     my (%roles,%shown,%toplevel);
16477:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
16478: 
16479:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
16480:         if ($domconfig{'selfenrollment'} eq '') {
16481:             $domconfig{'selfenrollment'} = {};
16482:         }
16483:     }
16484:     %toplevel = (
16485:                   admin      => 'Configuration Rights',
16486:                   default    => 'Default settings',
16487:                   validation => 'Validation of self-enrollment requests',
16488:                 );
16489:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
16490: 
16491:     if (ref($ordered{'admin'}) eq 'ARRAY') {
16492:         foreach my $item (@{$ordered{'admin'}}) {
16493:             foreach my $type (@types) {
16494:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
16495:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
16496:                 } else {
16497:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
16498:                 }
16499:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
16500:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
16501:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
16502:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
16503:                             push(@{$changes{'admin'}{$type}},$item);
16504:                         }
16505:                     } else {
16506:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
16507:                             push(@{$changes{'admin'}{$type}},$item);
16508:                         }
16509:                     }
16510:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
16511:                     push(@{$changes{'admin'}{$type}},$item);
16512:                 }
16513:             }
16514:         }
16515:     }
16516: 
16517:     foreach my $item (@{$ordered{'default'}}) {
16518:         foreach my $type (@types) {
16519:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
16520:             if ($item eq 'types') {
16521:                 unless (($value eq 'all') || ($value eq 'dom')) {
16522:                     $value = '';
16523:                 }
16524:             } elsif ($item eq 'registered') {
16525:                 unless ($value eq '1') {
16526:                     $value = 0;
16527:                 }
16528:             } elsif ($item eq 'approval') {
16529:                 unless ($value =~ /^[012]$/) {
16530:                     $value = 0;
16531:                 }
16532:             } else {
16533:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16534:                     $value = 'none';
16535:                 }
16536:             }
16537:             $selfenrollhash{'default'}{$type}{$item} = $value;
16538:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
16539:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16540:                     if ($selfenrollhash{'default'}{$type}{$item} ne
16541:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
16542:                          push(@{$changes{'default'}{$type}},$item);
16543:                     }
16544:                 } else {
16545:                     push(@{$changes{'default'}{$type}},$item);
16546:                 }
16547:             } else {
16548:                 push(@{$changes{'default'}{$type}},$item);
16549:             }
16550:             if ($item eq 'limit') {
16551:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16552:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
16553:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
16554:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
16555:                     }
16556:                 } else {
16557:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
16558:                 }
16559:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16560:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
16561:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
16562:                          push(@{$changes{'default'}{$type}},'cap');
16563:                     }
16564:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
16565:                     push(@{$changes{'default'}{$type}},'cap');
16566:                 }
16567:             }
16568:         }
16569:     }
16570: 
16571:     foreach my $item (@{$itemsref}) {
16572:         if ($item eq 'fields') {
16573:             my @changed;
16574:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
16575:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
16576:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
16577:             }
16578:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16579:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
16580:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
16581:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
16582:                 } else {
16583:                     @changed = @{$selfenrollhash{'validation'}{$item}};
16584:                 }
16585:             } else {
16586:                 @changed = @{$selfenrollhash{'validation'}{$item}};
16587:             }
16588:             if (@changed) {
16589:                 if ($selfenrollhash{'validation'}{$item}) { 
16590:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
16591:                 } else {
16592:                     $changes{'validation'}{$item} = &mt('None');
16593:                 }
16594:             }
16595:         } else {
16596:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
16597:             if ($item eq 'markup') {
16598:                if ($env{'form.selfenroll_validation_'.$item}) {
16599:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
16600:                }
16601:             }
16602:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16603:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
16604:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
16605:                 }
16606:             }
16607:         }
16608:     }
16609: 
16610:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
16611:                                              $dom);
16612:     if ($putresult eq 'ok') {
16613:         if (keys(%changes) > 0) {
16614:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16615:             $resulttext = &mt('Changes made:').'<ul>';
16616:             foreach my $key ('admin','default','validation') {
16617:                 if (ref($changes{$key}) eq 'HASH') {
16618:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
16619:                     if ($key eq 'validation') {
16620:                         foreach my $item (@{$itemsref}) {
16621:                             if (exists($changes{$key}{$item})) {
16622:                                 if ($item eq 'markup') {
16623:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16624:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
16625:                                 } else {  
16626:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16627:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
16628:                                 }
16629:                             }
16630:                         }
16631:                     } else {
16632:                         foreach my $type (@types) {
16633:                             if ($type eq 'community') {
16634:                                 $roles{'1'} = &mt('Community personnel');
16635:                             } else {
16636:                                 $roles{'1'} = &mt('Course personnel');
16637:                             }
16638:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
16639:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
16640:                                     if ($key eq 'admin') {
16641:                                         my @mgrdc = ();
16642:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16643:                                             foreach my $item (@{$ordered{'admin'}}) {
16644:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
16645:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
16646:                                                         push(@mgrdc,$item);
16647:                                                     }
16648:                                                 }
16649:                                             }
16650:                                             if (@mgrdc) {
16651:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
16652:                                             } else {
16653:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
16654:                                             }
16655:                                         }
16656:                                     } else {
16657:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16658:                                             foreach my $item (@{$ordered{$key}}) {
16659:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16660:                                                     $domdefaults{$type.'selfenroll'.$item} =
16661:                                                         $selfenrollhash{$key}{$type}{$item};
16662:                                                 }
16663:                                             }
16664:                                         }
16665:                                     }
16666:                                 }
16667:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
16668:                                 foreach my $item (@{$ordered{$key}}) {
16669:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16670:                                         $resulttext .= '<li>';
16671:                                         if ($key eq 'admin') {
16672:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
16673:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
16674:                                         } else {
16675:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
16676:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
16677:                                         }
16678:                                         $resulttext .= '</li>';
16679:                                     }
16680:                                 }
16681:                                 $resulttext .= '</ul></li>';
16682:                             }
16683:                         }
16684:                         $resulttext .= '</ul></li>'; 
16685:                     }
16686:                 }
16687:             }
16688:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
16689:                 my $cachetime = 24*60*60;
16690:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16691:                 if (ref($lastactref) eq 'HASH') {
16692:                     $lastactref->{'domdefaults'} = 1;
16693:                 }
16694:             }
16695:             $resulttext .= '</ul>';
16696:         } else {
16697:             $resulttext = &mt('No changes made to self-enrollment settings');
16698:         }
16699:     } else {
16700:         $resulttext = '<span class="LC_error">'.
16701:             &mt('An error occurred: [_1]',$putresult).'</span>';
16702:     }
16703:     return $resulttext;
16704: }
16705: 
16706: sub modify_usersessions {
16707:     my ($dom,$lastactref,%domconfig) = @_;
16708:     my @hostingtypes = ('version','excludedomain','includedomain');
16709:     my @offloadtypes = ('primary','default');
16710:     my %types = (
16711:                   remote => \@hostingtypes,
16712:                   hosted => \@hostingtypes,
16713:                   spares => \@offloadtypes,
16714:                 );
16715:     my @prefixes = ('remote','hosted','spares');
16716:     my @lcversions = &Apache::lonnet::all_loncaparevs();
16717:     my (%by_ip,%by_location,@intdoms,@instdoms);
16718:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16719:     my @locations = sort(keys(%by_location));
16720:     my (%defaultshash,%changes);
16721:     foreach my $prefix (@prefixes) {
16722:         $defaultshash{'usersessions'}{$prefix} = {};
16723:     }
16724:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16725:     my $resulttext;
16726:     my %iphost = &Apache::lonnet::get_iphost();
16727:     foreach my $prefix (@prefixes) {
16728:         next if ($prefix eq 'spares');
16729:         foreach my $type (@{$types{$prefix}}) {
16730:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16731:             if ($type eq 'version') {
16732:                 my $value = $env{'form.'.$prefix.'_'.$type};
16733:                 my $okvalue;
16734:                 if ($value ne '') {
16735:                     if (grep(/^\Q$value\E$/,@lcversions)) {
16736:                         $okvalue = $value;
16737:                     }
16738:                 }
16739:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16740:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16741:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
16742:                             if ($inuse == 0) {
16743:                                 $changes{$prefix}{$type} = 1;
16744:                             } else {
16745:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
16746:                                     $changes{$prefix}{$type} = 1;
16747:                                 }
16748:                                 if ($okvalue ne '') {
16749:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16750:                                 } 
16751:                             }
16752:                         } else {
16753:                             if (($inuse == 1) && ($okvalue ne '')) {
16754:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16755:                                 $changes{$prefix}{$type} = 1;
16756:                             }
16757:                         }
16758:                     } else {
16759:                         if (($inuse == 1) && ($okvalue ne '')) {
16760:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16761:                             $changes{$prefix}{$type} = 1;
16762:                         }
16763:                     }
16764:                 } else {
16765:                     if (($inuse == 1) && ($okvalue ne '')) {
16766:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16767:                         $changes{$prefix}{$type} = 1;
16768:                     }
16769:                 }
16770:             } else {
16771:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16772:                 my @okvals;
16773:                 foreach my $val (@vals) {
16774:                     if ($val =~ /:/) {
16775:                         my @items = split(/:/,$val);
16776:                         foreach my $item (@items) {
16777:                             if (ref($by_location{$item}) eq 'ARRAY') {
16778:                                 push(@okvals,$item);
16779:                             }
16780:                         }
16781:                     } else {
16782:                         if (ref($by_location{$val}) eq 'ARRAY') {
16783:                             push(@okvals,$val);
16784:                         }
16785:                     }
16786:                 }
16787:                 @okvals = sort(@okvals);
16788:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16789:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16790:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16791:                             if ($inuse == 0) {
16792:                                 $changes{$prefix}{$type} = 1; 
16793:                             } else {
16794:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16795:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
16796:                                 if (@changed > 0) {
16797:                                     $changes{$prefix}{$type} = 1;
16798:                                 }
16799:                             }
16800:                         } else {
16801:                             if ($inuse == 1) {
16802:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16803:                                 $changes{$prefix}{$type} = 1;
16804:                             }
16805:                         } 
16806:                     } else {
16807:                         if ($inuse == 1) {
16808:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16809:                             $changes{$prefix}{$type} = 1;
16810:                         }
16811:                     }
16812:                 } else {
16813:                     if ($inuse == 1) {
16814:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16815:                         $changes{$prefix}{$type} = 1;
16816:                     }
16817:                 }
16818:             }
16819:         }
16820:     }
16821: 
16822:     my @alldoms = &Apache::lonnet::all_domains();
16823:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16824:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
16825:     my $savespares;
16826: 
16827:     foreach my $lonhost (sort(keys(%servers))) {
16828:         my $serverhomeID =
16829:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
16830:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
16831:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
16832:         my %spareschg;
16833:         foreach my $type (@{$types{'spares'}}) {
16834:             my @okspares;
16835:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
16836:             foreach my $server (@checked) {
16837:                 if (&Apache::lonnet::hostname($server) ne '') {
16838:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
16839:                         unless (grep(/^\Q$server\E$/,@okspares)) {
16840:                             push(@okspares,$server);
16841:                         }
16842:                     }
16843:                 }
16844:             }
16845:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
16846:             my $newspare;
16847:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
16848:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
16849:                     $newspare = $new;
16850:                 }
16851:             }
16852:             my @spares;
16853:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
16854:                 @spares = sort(@okspares,$newspare);
16855:             } else {
16856:                 @spares = sort(@okspares);
16857:             }
16858:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
16859:             if (ref($spareid{$lonhost}) eq 'HASH') {
16860:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
16861:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
16862:                     if (@diffs > 0) {
16863:                         $spareschg{$type} = 1;
16864:                     }
16865:                 }
16866:             }
16867:         }
16868:         if (keys(%spareschg) > 0) {
16869:             $changes{'spares'}{$lonhost} = \%spareschg;
16870:         }
16871:     }
16872:     $defaultshash{'usersessions'}{'offloadnow'} = {};
16873:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
16874:     my @okoffload;
16875:     if (@offloadnow) {
16876:         foreach my $server (@offloadnow) {
16877:             if (&Apache::lonnet::hostname($server) ne '') {
16878:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
16879:                     push(@okoffload,$server);
16880:                 }
16881:             }
16882:         }
16883:         if (@okoffload) {
16884:             foreach my $lonhost (@okoffload) {
16885:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
16886:             }
16887:         }
16888:     }
16889:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
16890:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
16891:             if (ref($changes{'spares'}) eq 'HASH') {
16892:                 if (keys(%{$changes{'spares'}}) > 0) {
16893:                     $savespares = 1;
16894:                 }
16895:             }
16896:         } else {
16897:             $savespares = 1;
16898:         }
16899:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
16900:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
16901:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
16902:                     $changes{'offloadnow'} = 1;
16903:                     last;
16904:                 }
16905:             }
16906:             unless ($changes{'offloadnow'}) {
16907:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
16908:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
16909:                         $changes{'offloadnow'} = 1;
16910:                         last;
16911:                     }
16912:                 }
16913:             }
16914:         } elsif (@okoffload) {
16915:             $changes{'offloadnow'} = 1;
16916:         }
16917:     } elsif (@okoffload) {
16918:         $changes{'offloadnow'} = 1;
16919:     }
16920:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
16921:     if ((keys(%changes) > 0) || ($savespares)) {
16922:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16923:                                                  $dom);
16924:         if ($putresult eq 'ok') {
16925:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16926:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
16927:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
16928:                 }
16929:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
16930:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
16931:                 }
16932:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16933:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
16934:                 }
16935:             }
16936:             my $cachetime = 24*60*60;
16937:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16938:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
16939:             if (ref($lastactref) eq 'HASH') {
16940:                 $lastactref->{'domdefaults'} = 1;
16941:                 $lastactref->{'usersessions'} = 1;
16942:             }
16943:             if (keys(%changes) > 0) {
16944:                 my %lt = &usersession_titles();
16945:                 $resulttext = &mt('Changes made:').'<ul>';
16946:                 foreach my $prefix (@prefixes) {
16947:                     if (ref($changes{$prefix}) eq 'HASH') {
16948:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
16949:                         if ($prefix eq 'spares') {
16950:                             if (ref($changes{$prefix}) eq 'HASH') {
16951:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
16952:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
16953:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
16954:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
16955:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16956:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
16957:                                         foreach my $type (@{$types{$prefix}}) {
16958:                                             if ($changes{$prefix}{$lonhost}{$type}) {
16959:                                                 my $offloadto = &mt('None');
16960:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
16961:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
16962:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
16963:                                                     }
16964:                                                 }
16965:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
16966:                                             }
16967:                                         }
16968:                                     }
16969:                                     $resulttext .= '</li>';
16970:                                 }
16971:                             }
16972:                         } else {
16973:                             foreach my $type (@{$types{$prefix}}) {
16974:                                 if (defined($changes{$prefix}{$type})) {
16975:                                     my ($newvalue,$notinuse);
16976:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16977:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
16978:                                             if ($type eq 'version') {
16979:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
16980:                                             } else {
16981:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16982:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
16983:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
16984:                                                     }
16985:                                                 } else {
16986:                                                     $notinuse = 1;
16987:                                                 }
16988:                                             }
16989:                                         }
16990:                                     }
16991:                                     if ($newvalue eq '') {
16992:                                         if ($type eq 'version') {
16993:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
16994:                                         } elsif ($notinuse) {
16995:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
16996:                                         } else {
16997:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
16998:                                         }
16999:                                     } else {
17000:                                         if ($type eq 'version') {
17001:                                             $newvalue .= ' '.&mt('(or later)');
17002:                                         }
17003:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
17004:                                     }
17005:                                 }
17006:                             }
17007:                         }
17008:                         $resulttext .= '</ul>';
17009:                     }
17010:                 }
17011:                 if ($changes{'offloadnow'}) {
17012:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
17013:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
17014:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
17015:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
17016:                                 $resulttext .= '<li>'.$lonhost.'</li>';
17017:                             }
17018:                             $resulttext .= '</ul>';
17019:                         } else {
17020:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
17021:                         }
17022:                     } else {
17023:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
17024:                     }
17025:                 }
17026:                 $resulttext .= '</ul>';
17027:             } else {
17028:                 $resulttext = $nochgmsg;
17029:             }
17030:         } else {
17031:             $resulttext = '<span class="LC_error">'.
17032:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17033:         }
17034:     } else {
17035:         $resulttext = $nochgmsg;
17036:     }
17037:     return $resulttext;
17038: }
17039: 
17040: sub modify_ssl {
17041:     my ($dom,$lastactref,%domconfig) = @_;
17042:     my (%by_ip,%by_location,@intdoms,@instdoms);
17043:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17044:     my @locations = sort(keys(%by_location));
17045:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17046:     my (%defaultshash,%changes);
17047:     my $action = 'ssl';
17048:     my @prefixes = ('connto','connfrom','replication');
17049:     foreach my $prefix (@prefixes) {
17050:         $defaultshash{$action}{$prefix} = {};
17051:     }
17052:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17053:     my $resulttext;
17054:     my %iphost = &Apache::lonnet::get_iphost();
17055:     my @reptypes = ('certreq','nocertreq');
17056:     my @connecttypes = ('dom','intdom','other');
17057:     my %types = (
17058:                   connto      => \@connecttypes,
17059:                   connfrom    => \@connecttypes,
17060:                   replication => \@reptypes,
17061:                 );
17062:     foreach my $prefix (sort(keys(%types))) {
17063:         foreach my $type (@{$types{$prefix}}) {
17064:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
17065:                 my $value = 'yes';
17066:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
17067:                     $value = $env{'form.'.$prefix.'_'.$type};
17068:                 }
17069:                 if (ref($domconfig{$action}) eq 'HASH') {
17070:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
17071:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
17072:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
17073:                                 $changes{$prefix}{$type} = 1;
17074:                             }
17075:                             $defaultshash{$action}{$prefix}{$type} = $value;
17076:                         } else {
17077:                             $defaultshash{$action}{$prefix}{$type} = $value;
17078:                             $changes{$prefix}{$type} = 1;
17079:                         }
17080:                     } else {
17081:                         $defaultshash{$action}{$prefix}{$type} = $value;
17082:                         $changes{$prefix}{$type} = 1;
17083:                     }
17084:                 } else {
17085:                     $defaultshash{$action}{$prefix}{$type} = $value;
17086:                     $changes{$prefix}{$type} = 1;
17087:                 }
17088:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
17089:                     delete($changes{$prefix}{$type});
17090:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
17091:                     delete($changes{$prefix}{$type});
17092:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
17093:                     delete($changes{$prefix}{$type});
17094:                 }
17095:             } elsif ($prefix eq 'replication') {
17096:                 if (@locations > 0) {
17097:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
17098:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
17099:                     my @okvals;
17100:                     foreach my $val (@vals) {
17101:                         if ($val =~ /:/) {
17102:                             my @items = split(/:/,$val);
17103:                             foreach my $item (@items) {
17104:                                 if (ref($by_location{$item}) eq 'ARRAY') {
17105:                                     push(@okvals,$item);
17106:                                 }
17107:                             }
17108:                         } else {
17109:                             if (ref($by_location{$val}) eq 'ARRAY') {
17110:                                 push(@okvals,$val);
17111:                             }
17112:                         }
17113:                     }
17114:                     @okvals = sort(@okvals);
17115:                     if (ref($domconfig{$action}) eq 'HASH') {
17116:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
17117:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
17118:                                 if ($inuse == 0) {
17119:                                     $changes{$prefix}{$type} = 1;
17120:                                 } else {
17121:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
17122:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
17123:                                     if (@changed > 0) {
17124:                                         $changes{$prefix}{$type} = 1;
17125:                                     }
17126:                                 }
17127:                             } else {
17128:                                 if ($inuse == 1) {
17129:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
17130:                                     $changes{$prefix}{$type} = 1;
17131:                                 }
17132:                             }
17133:                         } else {
17134:                             if ($inuse == 1) {
17135:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
17136:                                 $changes{$prefix}{$type} = 1;
17137:                             }
17138:                         }
17139:                     } else {
17140:                         if ($inuse == 1) {
17141:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
17142:                             $changes{$prefix}{$type} = 1;
17143:                         }
17144:                     }
17145:                 }
17146:             }
17147:         }
17148:     }
17149:     if (keys(%changes)) {
17150:         foreach my $prefix (keys(%changes)) {
17151:             if (ref($changes{$prefix}) eq 'HASH') {
17152:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
17153:                     delete($changes{$prefix});
17154:                 }
17155:             } else {
17156:                 delete($changes{$prefix});
17157:             }
17158:         }
17159:     }
17160:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
17161:     if (keys(%changes) > 0) {
17162:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17163:                                                  $dom);
17164:         if ($putresult eq 'ok') {
17165:             if (ref($defaultshash{$action}) eq 'HASH') {
17166:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
17167:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
17168:                 }
17169:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
17170:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
17171:                 }
17172:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
17173:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
17174:                 }
17175:             }
17176:             my $cachetime = 24*60*60;
17177:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17178:             if (ref($lastactref) eq 'HASH') {
17179:                 $lastactref->{'domdefaults'} = 1;
17180:             }
17181:             if (keys(%changes) > 0) {
17182:                 my %titles = &ssl_titles();
17183:                 $resulttext = &mt('Changes made:').'<ul>';
17184:                 foreach my $prefix (@prefixes) {
17185:                     if (ref($changes{$prefix}) eq 'HASH') {
17186:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
17187:                         foreach my $type (@{$types{$prefix}}) {
17188:                             if (defined($changes{$prefix}{$type})) {
17189:                                 my ($newvalue,$notinuse);
17190:                                 if (ref($defaultshash{$action}) eq 'HASH') {
17191:                                     if (ref($defaultshash{$action}{$prefix})) {
17192:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
17193:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
17194:                                         } else {
17195:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
17196:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
17197:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
17198:                                                 }
17199:                                             } else {
17200:                                                 $notinuse = 1;
17201:                                             }
17202:                                         }
17203:                                     }
17204:                                     if ($notinuse) {
17205:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
17206:                                     } elsif ($newvalue eq '') {
17207:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
17208:                                     } else {
17209:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
17210:                                     }
17211:                                 }
17212:                             }
17213:                         }
17214:                         $resulttext .= '</ul>';
17215:                     }
17216:                 }
17217:             } else {
17218:                 $resulttext = $nochgmsg;
17219:             }
17220:         } else {
17221:             $resulttext = '<span class="LC_error">'.
17222:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17223:         }
17224:     } else {
17225:         $resulttext = $nochgmsg;
17226:     }
17227:     return $resulttext;
17228: }
17229: 
17230: sub modify_trust {
17231:     my ($dom,$lastactref,%domconfig) = @_;
17232:     my (%by_ip,%by_location,@intdoms,@instdoms);
17233:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17234:     my @locations = sort(keys(%by_location));
17235:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
17236:     my @types = ('exc','inc');
17237:     my (%defaultshash,%changes);
17238:     foreach my $prefix (@prefixes) {
17239:         $defaultshash{'trust'}{$prefix} = {};
17240:     }
17241:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17242:     my $resulttext;
17243:     foreach my $prefix (@prefixes) {
17244:         foreach my $type (@types) {
17245:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
17246:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
17247:             my @okvals;
17248:             foreach my $val (@vals) {
17249:                 if ($val =~ /:/) {
17250:                     my @items = split(/:/,$val);
17251:                     foreach my $item (@items) {
17252:                         if (ref($by_location{$item}) eq 'ARRAY') {
17253:                             push(@okvals,$item);
17254:                         }
17255:                     }
17256:                 } else {
17257:                     if (ref($by_location{$val}) eq 'ARRAY') {
17258:                         push(@okvals,$val);
17259:                     }
17260:                 }
17261:             }
17262:             @okvals = sort(@okvals);
17263:             if (ref($domconfig{'trust'}) eq 'HASH') {
17264:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
17265:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
17266:                         if ($inuse == 0) {
17267:                             $changes{$prefix}{$type} = 1;
17268:                         } else {
17269:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17270:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
17271:                             if (@changed > 0) {
17272:                                 $changes{$prefix}{$type} = 1;
17273:                             }
17274:                         }
17275:                     } else {
17276:                         if ($inuse == 1) {
17277:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17278:                             $changes{$prefix}{$type} = 1;
17279:                         }
17280:                     }
17281:                 } else {
17282:                     if ($inuse == 1) {
17283:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17284:                         $changes{$prefix}{$type} = 1;
17285:                     }
17286:                 }
17287:             } else {
17288:                 if ($inuse == 1) {
17289:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
17290:                     $changes{$prefix}{$type} = 1;
17291:                 }
17292:             }
17293:         }
17294:     }
17295:     my $nochgmsg = &mt('No changes made to trust settings.');
17296:     if (keys(%changes) > 0) {
17297:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17298:                                                  $dom);
17299:         if ($putresult eq 'ok') {
17300:             if (ref($defaultshash{'trust'}) eq 'HASH') {
17301:                 foreach my $prefix (@prefixes) {
17302:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
17303:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
17304:                     }
17305:                 }
17306:             }
17307:             my $cachetime = 24*60*60;
17308:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17309:             if (ref($lastactref) eq 'HASH') {
17310:                 $lastactref->{'domdefaults'} = 1;
17311:             }
17312:             if (keys(%changes) > 0) {
17313:                 my %lt = &trust_titles();
17314:                 $resulttext = &mt('Changes made:').'<ul>';
17315:                 foreach my $prefix (@prefixes) {
17316:                     if (ref($changes{$prefix}) eq 'HASH') {
17317:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
17318:                         foreach my $type (@types) {
17319:                             if (defined($changes{$prefix}{$type})) {
17320:                                 my ($newvalue,$notinuse);
17321:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
17322:                                     if (ref($defaultshash{'trust'}{$prefix})) {
17323:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
17324:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
17325:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
17326:                                             }
17327:                                         } else {
17328:                                             $notinuse = 1;
17329:                                         }
17330:                                     }
17331:                                 }
17332:                                 if ($notinuse) {
17333:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
17334:                                 } elsif ($newvalue eq '') {
17335:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
17336:                                 } else {
17337:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
17338:                                 }
17339:                             }
17340:                         }
17341:                         $resulttext .= '</ul>';
17342:                     }
17343:                 }
17344:                 $resulttext .= '</ul>';
17345:             } else {
17346:                 $resulttext = $nochgmsg;
17347:             }
17348:         } else {
17349:             $resulttext = '<span class="LC_error">'.
17350:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17351:         }
17352:     } else {
17353:         $resulttext = $nochgmsg;
17354:     }
17355:     return $resulttext;
17356: }
17357: 
17358: sub modify_loadbalancing {
17359:     my ($dom,%domconfig) = @_;
17360:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
17361:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
17362:     my ($othertitle,$usertypes,$types) =
17363:         &Apache::loncommon::sorted_inst_types($dom);
17364:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17365:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
17366:     my @sparestypes = ('primary','default');
17367:     my %typetitles = &sparestype_titles();
17368:     my $resulttext;
17369:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
17370:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
17371:         %existing = %{$domconfig{'loadbalancing'}};
17372:     }
17373:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
17374:                               \%currtargets,\%currrules,\%currcookies);
17375:     my ($saveloadbalancing,%defaultshash,%changes);
17376:     my ($alltypes,$othertypes,$titles) =
17377:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
17378:     my %ruletitles = &offloadtype_text();
17379:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
17380:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
17381:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
17382:         if ($balancer eq '') {
17383:             next;
17384:         }
17385:         if (!exists($servers{$balancer})) {
17386:             if (exists($currbalancer{$balancer})) {
17387:                 push(@{$changes{'delete'}},$balancer);
17388:             }
17389:             next;
17390:         }
17391:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
17392:             push(@{$changes{'delete'}},$balancer);
17393:             next;
17394:         }
17395:         if (!exists($currbalancer{$balancer})) {
17396:             push(@{$changes{'add'}},$balancer);
17397:         }
17398:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
17399:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
17400:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
17401:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
17402:             $saveloadbalancing = 1;
17403:         }
17404:         foreach my $sparetype (@sparestypes) {
17405:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
17406:             my @offloadto;
17407:             foreach my $target (@targets) {
17408:                 if (($servers{$target}) && ($target ne $balancer)) {
17409:                     if ($sparetype eq 'default') {
17410:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
17411:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
17412:                         }
17413:                     }
17414:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
17415:                         push(@offloadto,$target);
17416:                     }
17417:                 }
17418:             }
17419:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
17420:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
17421:                     push(@offloadto,$balancer);
17422:                 }
17423:             }
17424:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
17425:         }
17426:         if ($env{'form.loadbalancing_cookie_'.$i}) {
17427:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
17428:             if (exists($currbalancer{$balancer})) { 
17429:                 unless ($currcookies{$balancer}) {
17430:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
17431:                 }
17432:             }
17433:         } elsif (exists($currbalancer{$balancer})) {
17434:             if ($currcookies{$balancer}) {
17435:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
17436:             }
17437:         }
17438:         if (ref($currtargets{$balancer}) eq 'HASH') {
17439:             foreach my $sparetype (@sparestypes) {
17440:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
17441:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
17442:                     if (@targetdiffs > 0) {
17443:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17444:                     }
17445:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17446:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17447:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17448:                     }
17449:                 }
17450:             }
17451:         } else {
17452:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
17453:                 foreach my $sparetype (@sparestypes) {
17454:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17455:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17456:                             $changes{'curr'}{$balancer}{'targets'} = 1;
17457:                         }
17458:                     }
17459:                 }
17460:             }
17461:         }
17462:         my $ishomedom;
17463:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
17464:             $ishomedom = 1;
17465:         }
17466:         if (ref($alltypes) eq 'ARRAY') {
17467:             foreach my $type (@{$alltypes}) {
17468:                 my $rule;
17469:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
17470:                          (!$ishomedom)) {
17471:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
17472:                 }
17473:                 if ($rule eq 'specific') {
17474:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
17475:                     if (exists($servers{$specifiedhost})) {
17476:                         $rule = $specifiedhost;
17477:                     }
17478:                 }
17479:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
17480:                 if (ref($currrules{$balancer}) eq 'HASH') {
17481:                     if ($rule ne $currrules{$balancer}{$type}) {
17482:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17483:                     }
17484:                 } elsif ($rule ne '') {
17485:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17486:                 }
17487:             }
17488:         }
17489:     }
17490:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
17491:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
17492:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
17493:             $defaultshash{'loadbalancing'} = {};
17494:         }
17495:         my $putresult = &Apache::lonnet::put_dom('configuration',
17496:                                                  \%defaultshash,$dom);
17497:         if ($putresult eq 'ok') {
17498:             if (keys(%changes) > 0) {
17499:                 my %toupdate;
17500:                 if (ref($changes{'delete'}) eq 'ARRAY') {
17501:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
17502:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
17503:                         $toupdate{$balancer} = 1;
17504:                     }
17505:                 }
17506:                 if (ref($changes{'add'}) eq 'ARRAY') {
17507:                     foreach my $balancer (sort(@{$changes{'add'}})) {
17508:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
17509:                         $toupdate{$balancer} = 1;
17510:                     }
17511:                 }
17512:                 if (ref($changes{'curr'}) eq 'HASH') {
17513:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
17514:                         $toupdate{$balancer} = 1;
17515:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
17516:                             if ($changes{'curr'}{$balancer}{'targets'}) {
17517:                                 my %offloadstr;
17518:                                 foreach my $sparetype (@sparestypes) {
17519:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17520:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17521:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17522:                                         }
17523:                                     }
17524:                                 }
17525:                                 if (keys(%offloadstr) == 0) {
17526:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
17527:                                 } else {
17528:                                     my $showoffload;
17529:                                     foreach my $sparetype (@sparestypes) {
17530:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
17531:                                         if (defined($offloadstr{$sparetype})) {
17532:                                             $showoffload .= $offloadstr{$sparetype};
17533:                                         } else {
17534:                                             $showoffload .= &mt('None');
17535:                                         }
17536:                                         $showoffload .= ('&nbsp;'x3);
17537:                                     }
17538:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
17539:                                 }
17540:                             }
17541:                         }
17542:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
17543:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
17544:                                 foreach my $type (@{$alltypes}) {
17545:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
17546:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17547:                                         my $balancetext;
17548:                                         if ($rule eq '') {
17549:                                             $balancetext =  $ruletitles{'default'};
17550:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
17551:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17552:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17553:                                                 foreach my $sparetype (@sparestypes) {
17554:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17555:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17556:                                                     }
17557:                                                 }
17558:                                                 foreach my $item (@{$alltypes}) {
17559:                                                     next if ($item =~  /^_LC_ipchange/);
17560:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
17561:                                                     if ($hasrule eq 'homeserver') {
17562:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
17563:                                                     } else {
17564:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
17565:                                                             if ($servers{$hasrule}) {
17566:                                                                 $toupdate{$hasrule} = 1;
17567:                                                             }
17568:                                                         }
17569:                                                     }
17570:                                                 }
17571:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
17572:                                                     $balancetext =  $ruletitles{$rule};
17573:                                                 } else {
17574:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17575:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
17576:                                                     if ($receiver) {
17577:                                                         $toupdate{$receiver};
17578:                                                     }
17579:                                                 }
17580:                                             } else {
17581:                                                 $balancetext =  $ruletitles{$rule};
17582:                                             }
17583:                                         } else {
17584:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
17585:                                         }
17586:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
17587:                                     }
17588:                                 }
17589:                             }
17590:                         }
17591:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
17592:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
17593:                                                       $balancer).'</li>'; 
17594:                         }
17595:                         if (keys(%toupdate)) {
17596:                             my %thismachine;
17597:                             my $updatedhere;
17598:                             my $cachetime = 60*60*24;
17599:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
17600:                             foreach my $lonhost (keys(%toupdate)) {
17601:                                 if ($thismachine{$lonhost}) {
17602:                                     unless ($updatedhere) {
17603:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
17604:                                                                       $defaultshash{'loadbalancing'},
17605:                                                                       $cachetime);
17606:                                         $updatedhere = 1;
17607:                                     }
17608:                                 } else {
17609:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
17610:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
17611:                                 }
17612:                             }
17613:                         }
17614:                     }
17615:                 }
17616:                 if ($resulttext ne '') {
17617:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
17618:                 } else {
17619:                     $resulttext = $nochgmsg;
17620:                 }
17621:             } else {
17622:                 $resulttext = $nochgmsg;
17623:             }
17624:         } else {
17625:             $resulttext = '<span class="LC_error">'.
17626:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17627:         }
17628:     } else {
17629:         $resulttext = $nochgmsg;
17630:     }
17631:     return $resulttext;
17632: }
17633: 
17634: sub recurse_check {
17635:     my ($chkcats,$categories,$depth,$name) = @_;
17636:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
17637:         my $chg = 0;
17638:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
17639:             my $category = $chkcats->[$depth]{$name}[$j];
17640:             my $item;
17641:             if ($category eq '') {
17642:                 $chg ++;
17643:             } else {
17644:                 my $deeper = $depth + 1;
17645:                 $item = &escape($category).':'.&escape($name).':'.$depth;
17646:                 if ($chg) {
17647:                     $categories->{$item} -= $chg;
17648:                 }
17649:                 &recurse_check($chkcats,$categories,$deeper,$category);
17650:                 $deeper --;
17651:             }
17652:         }
17653:     }
17654:     return;
17655: }
17656: 
17657: sub recurse_cat_deletes {
17658:     my ($item,$coursecategories,$deletions) = @_;
17659:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
17660:     my $subdepth = $depth + 1;
17661:     if (ref($coursecategories) eq 'HASH') {
17662:         foreach my $subitem (keys(%{$coursecategories})) {
17663:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
17664:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
17665:                 delete($coursecategories->{$subitem});
17666:                 $deletions->{$subitem} = 1;
17667:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
17668:             }
17669:         }
17670:     }
17671:     return;
17672: }
17673: 
17674: sub active_dc_picker {
17675:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
17676:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17677:     my @domcoord = keys(%domcoords);
17678:     if (keys(%currhash)) {
17679:         foreach my $dc (keys(%currhash)) {
17680:             unless (exists($domcoords{$dc})) {
17681:                 push(@domcoord,$dc);
17682:             }
17683:         }
17684:     }
17685:     @domcoord = sort(@domcoord);
17686:     my $numdcs = scalar(@domcoord);
17687:     my $rows = 0;
17688:     my $table;
17689:     if ($numdcs > 1) {
17690:         $table = '<table>';
17691:         for (my $i=0; $i<@domcoord; $i++) {
17692:             my $rem = $i%($numinrow);
17693:             if ($rem == 0) {
17694:                 if ($i > 0) {
17695:                     $table .= '</tr>';
17696:                 }
17697:                 $table .= '<tr>';
17698:                 $rows ++;
17699:             }
17700:             my $check = '';
17701:             if ($inputtype eq 'radio') {
17702:                 if (keys(%currhash) == 0) {
17703:                     if (!$i) {
17704:                         $check = ' checked="checked"';
17705:                     }
17706:                 } elsif (exists($currhash{$domcoord[$i]})) {
17707:                     $check = ' checked="checked"';
17708:                 }
17709:             } else {
17710:                 if (exists($currhash{$domcoord[$i]})) {
17711:                     $check = ' checked="checked"';
17712:                 }
17713:             }
17714:             if ($i == @domcoord - 1) {
17715:                 my $colsleft = $numinrow - $rem;
17716:                 if ($colsleft > 1) {
17717:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
17718:                 } else {
17719:                     $table .= '<td class="LC_left_item">';
17720:                 }
17721:             } else {
17722:                 $table .= '<td class="LC_left_item">';
17723:             }
17724:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
17725:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17726:             $table .= '<span class="LC_nobreak"><label>'.
17727:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
17728:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
17729:             if ($user ne $dcname.':'.$dcdom) {
17730:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17731:             }
17732:             $table .= '</label></span></td>';
17733:         }
17734:         $table .= '</tr></table>';
17735:     } elsif ($numdcs == 1) {
17736:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
17737:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17738:         if ($inputtype eq 'radio') {
17739:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
17740:             if ($user ne $dcname.':'.$dcdom) {
17741:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17742:             }
17743:         } else {
17744:             my $check;
17745:             if (exists($currhash{$domcoord[0]})) {
17746:                 $check = ' checked="checked"';
17747:             }
17748:             $table = '<span class="LC_nobreak"><label>'.
17749:                      '<input type="checkbox" name="'.$name.'" '.
17750:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
17751:             if ($user ne $dcname.':'.$dcdom) {
17752:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17753:             }
17754:             $table .= '</label></span>';
17755:             $rows ++;
17756:         }
17757:     }
17758:     return ($numdcs,$table,$rows);
17759: }
17760: 
17761: sub usersession_titles {
17762:     return &Apache::lonlocal::texthash(
17763:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
17764:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
17765:                spares => 'Servers offloaded to, when busy',
17766:                version => 'LON-CAPA version requirement',
17767:                excludedomain => 'Allow all, but exclude specific domains',
17768:                includedomain => 'Deny all, but include specific domains',
17769:                primary => 'Primary (checked first)',
17770:                default => 'Default',
17771:            );
17772: }
17773: 
17774: sub id_for_thisdom {
17775:     my (%servers) = @_;
17776:     my %altids;
17777:     foreach my $server (keys(%servers)) {
17778:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
17779:         if ($serverhome ne $server) {
17780:             $altids{$serverhome} = $server;
17781:         }
17782:     }
17783:     return %altids;
17784: }
17785: 
17786: sub count_servers {
17787:     my ($currbalancer,%servers) = @_;
17788:     my (@spares,$numspares);
17789:     foreach my $lonhost (sort(keys(%servers))) {
17790:         next if ($currbalancer eq $lonhost);
17791:         push(@spares,$lonhost);
17792:     }
17793:     if ($currbalancer) {
17794:         $numspares = scalar(@spares);
17795:     } else {
17796:         $numspares = scalar(@spares) - 1;
17797:     }
17798:     return ($numspares,@spares);
17799: }
17800: 
17801: sub lonbalance_targets_js {
17802:     my ($dom,$types,$servers,$settings) = @_;
17803:     my $select = &mt('Select');
17804:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
17805:     if (ref($servers) eq 'HASH') {
17806:         $alltargets = join("','",sort(keys(%{$servers})));
17807:         my @homedoms;
17808:         foreach my $server (sort(keys(%{$servers}))) {
17809:             if (&Apache::lonnet::host_domain($server) eq $dom) {
17810:                 push(@homedoms,'1');
17811:             } else {
17812:                 push(@homedoms,'0');
17813:             }
17814:         }
17815:         $allishome = join("','",@homedoms);
17816:     }
17817:     if (ref($types) eq 'ARRAY') {
17818:         if (@{$types} > 0) {
17819:             @alltypes = @{$types};
17820:         }
17821:     }
17822:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
17823:     $allinsttypes = join("','",@alltypes);
17824:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
17825:     if (ref($settings) eq 'HASH') {
17826:         %existing = %{$settings};
17827:     }
17828:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
17829:                               \%currtargets,\%currrules,\%currcookies);
17830:     my $balancers = join("','",sort(keys(%currbalancer)));
17831:     return <<"END";
17832: 
17833: <script type="text/javascript">
17834: // <![CDATA[
17835: 
17836: currBalancers = new Array('$balancers');
17837: 
17838: function toggleTargets(balnum) {
17839:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17840:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
17841:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
17842:     var prevbalancer = prevhostitem.value;
17843:     var baltotal = document.getElementById('loadbalancing_total').value;
17844:     prevhostitem.value = balancer;
17845:     if (prevbalancer != '') {
17846:         var prevIdx = currBalancers.indexOf(prevbalancer);
17847:         if (prevIdx != -1) {
17848:             currBalancers.splice(prevIdx,1);
17849:         }
17850:     }
17851:     if (balancer == '') {
17852:         hideSpares(balnum);
17853:     } else {
17854:         var currIdx = currBalancers.indexOf(balancer);
17855:         if (currIdx == -1) {
17856:             currBalancers.push(balancer);
17857:         }
17858:         var homedoms = new Array('$allishome');
17859:         var ishomedom = homedoms[lonhostitem.selectedIndex];
17860:         showSpares(balancer,ishomedom,balnum);
17861:     }
17862:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
17863:     return;
17864: }
17865: 
17866: function showSpares(balancer,ishomedom,balnum) {
17867:     var alltargets = new Array('$alltargets');
17868:     var insttypes = new Array('$allinsttypes');
17869:     var offloadtypes = new Array('primary','default');
17870: 
17871:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
17872:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
17873:  
17874:     for (var i=0; i<offloadtypes.length; i++) {
17875:         var count = 0;
17876:         for (var j=0; j<alltargets.length; j++) {
17877:             if (alltargets[j] != balancer) {
17878:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
17879:                 item.value = alltargets[j];
17880:                 item.style.textAlign='left';
17881:                 item.style.textFace='normal';
17882:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
17883:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
17884:                     item.disabled = '';
17885:                 } else {
17886:                     item.disabled = 'disabled';
17887:                     item.checked = false;
17888:                 }
17889:                 count ++;
17890:             }
17891:         }
17892:     }
17893:     for (var k=0; k<insttypes.length; k++) {
17894:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
17895:             if (ishomedom == 1) {
17896:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17897:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17898:             } else {
17899:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17900:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17901:             }
17902:         } else {
17903:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17904:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17905:         }
17906:         if ((insttypes[k] != '_LC_external') && 
17907:             ((insttypes[k] != '_LC_internetdom') ||
17908:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
17909:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
17910:             item.options.length = 0;
17911:             item.options[0] = new Option("","",true,true);
17912:             var idx = 0;
17913:             for (var m=0; m<alltargets.length; m++) {
17914:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
17915:                     idx ++;
17916:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17917:                 }
17918:             }
17919:         }
17920:     }
17921:     return;
17922: }
17923: 
17924: function hideSpares(balnum) {
17925:     var alltargets = new Array('$alltargets');
17926:     var insttypes = new Array('$allinsttypes');
17927:     var offloadtypes = new Array('primary','default');
17928: 
17929:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
17930:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
17931: 
17932:     var total = alltargets.length - 1;
17933:     for (var i=0; i<offloadtypes; i++) {
17934:         for (var j=0; j<total; j++) {
17935:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
17936:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
17937:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
17938:         }
17939:     }
17940:     for (var k=0; k<insttypes.length; k++) {
17941:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17942:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17943:         if (insttypes[k] != '_LC_external') {
17944:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
17945:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
17946:         }
17947:     }
17948:     return;
17949: }
17950: 
17951: function checkOffloads(item,balnum,type) {
17952:     var alltargets = new Array('$alltargets');
17953:     var offloadtypes = new Array('primary','default');
17954:     if (item.checked) {
17955:         var total = alltargets.length - 1;
17956:         var other;
17957:         if (type == offloadtypes[0]) {
17958:             other = offloadtypes[1];
17959:         } else {
17960:             other = offloadtypes[0];
17961:         }
17962:         for (var i=0; i<total; i++) {
17963:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
17964:             if (server == item.value) {
17965:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
17966:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
17967:                 }
17968:             }
17969:         }
17970:     }
17971:     return;
17972: }
17973: 
17974: function singleServerToggle(balnum,type) {
17975:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
17976:     if (offloadtoSelIdx == 0) {
17977:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
17978:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17979: 
17980:     } else {
17981:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
17982:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17983:     }
17984:     return;
17985: }
17986: 
17987: function balanceruleChange(formname,balnum,type) {
17988:     if (type == '_LC_external') {
17989:         return;
17990:     }
17991:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
17992:     for (var i=0; i<typesRules.length; i++) {
17993:         if (formname.elements[typesRules[i]].checked) {
17994:             if (formname.elements[typesRules[i]].value != 'specific') {
17995:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
17996:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17997:             } else {
17998:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17999:             }
18000:         }
18001:     }
18002:     return;
18003: }
18004: 
18005: function balancerDeleteChange(balnum) {
18006:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
18007:     var baltotal = document.getElementById('loadbalancing_total').value;
18008:     var addtarget;
18009:     var removetarget;
18010:     var action = 'delete';
18011:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
18012:         var lonhost = hostitem.value;
18013:         var currIdx = currBalancers.indexOf(lonhost);
18014:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
18015:             if (currIdx != -1) {
18016:                 currBalancers.splice(currIdx,1);
18017:             }
18018:             addtarget = lonhost;
18019:         } else {
18020:             if (currIdx == -1) {
18021:                 currBalancers.push(lonhost);
18022:             }
18023:             removetarget = lonhost;
18024:             action = 'undelete';
18025:         }
18026:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
18027:     }
18028:     return;
18029: }
18030: 
18031: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
18032:     if (baltotal > 1) {
18033:         var offloadtypes = new Array('primary','default');
18034:         var alltargets = new Array('$alltargets');
18035:         var insttypes = new Array('$allinsttypes');
18036:         for (var i=0; i<baltotal; i++) {
18037:             if (i != balnum) {
18038:                 for (var j=0; j<offloadtypes.length; j++) {
18039:                     var total = alltargets.length - 1;
18040:                     for (var k=0; k<total; k++) {
18041:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
18042:                         var server = serveritem.value;
18043:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
18044:                             if (server == addtarget) {
18045:                                 serveritem.disabled = '';
18046:                             }
18047:                         }
18048:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
18049:                             if (server == removetarget) {
18050:                                 serveritem.disabled = 'disabled';
18051:                                 serveritem.checked = false;
18052:                             }
18053:                         }
18054:                     }
18055:                 }
18056:                 for (var j=0; j<insttypes.length; j++) {
18057:                     if (insttypes[j] != '_LC_external') {
18058:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
18059:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
18060:                             var currSel = singleserver.selectedIndex;
18061:                             var currVal = singleserver.options[currSel].value;
18062:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
18063:                                 var numoptions = singleserver.options.length;
18064:                                 var needsnew = 1;
18065:                                 for (var k=0; k<numoptions; k++) {
18066:                                     if (singleserver.options[k] == addtarget) {
18067:                                         needsnew = 0;
18068:                                         break;
18069:                                     }
18070:                                 }
18071:                                 if (needsnew == 1) {
18072:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
18073:                                 }
18074:                             }
18075:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
18076:                                 singleserver.options.length = 0;
18077:                                 if ((currVal) && (currVal != removetarget)) {
18078:                                     singleserver.options[0] = new Option("","",false,false);
18079:                                 } else {
18080:                                     singleserver.options[0] = new Option("","",true,true);
18081:                                 }
18082:                                 var idx = 0;
18083:                                 for (var m=0; m<alltargets.length; m++) {
18084:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
18085:                                         idx ++;
18086:                                         if (currVal == alltargets[m]) {
18087:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
18088:                                         } else {
18089:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
18090:                                         }
18091:                                     }
18092:                                 }
18093:                             }
18094:                         }
18095:                     }
18096:                 }
18097:             }
18098:         }
18099:     }
18100:     return;
18101: }
18102: 
18103: // ]]>
18104: </script>
18105: 
18106: END
18107: }
18108: 
18109: sub new_spares_js {
18110:     my @sparestypes = ('primary','default');
18111:     my $types = join("','",@sparestypes);
18112:     my $select = &mt('Select');
18113:     return <<"END";
18114: 
18115: <script type="text/javascript">
18116: // <![CDATA[
18117: 
18118: function updateNewSpares(formname,lonhost) {
18119:     var types = new Array('$types');
18120:     var include = new Array();
18121:     var exclude = new Array();
18122:     for (var i=0; i<types.length; i++) {
18123:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
18124:         for (var j=0; j<spareboxes.length; j++) {
18125:             if (formname.elements[spareboxes[j]].checked) {
18126:                 exclude.push(formname.elements[spareboxes[j]].value);
18127:             } else {
18128:                 include.push(formname.elements[spareboxes[j]].value);
18129:             }
18130:         }
18131:     }
18132:     for (var i=0; i<types.length; i++) {
18133:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
18134:         var selIdx = newSpare.selectedIndex;
18135:         var currnew = newSpare.options[selIdx].value;
18136:         var okSpares = new Array();
18137:         for (var j=0; j<newSpare.options.length; j++) {
18138:             var possible = newSpare.options[j].value;
18139:             if (possible != '') {
18140:                 if (exclude.indexOf(possible) == -1) {
18141:                     okSpares.push(possible);
18142:                 } else {
18143:                     if (currnew == possible) {
18144:                         selIdx = 0;
18145:                     }
18146:                 }
18147:             }
18148:         }
18149:         for (var k=0; k<include.length; k++) {
18150:             if (okSpares.indexOf(include[k]) == -1) {
18151:                 okSpares.push(include[k]);
18152:             }
18153:         }
18154:         okSpares.sort();
18155:         newSpare.options.length = 0;
18156:         if (selIdx == 0) {
18157:             newSpare.options[0] = new Option("$select","",true,true);
18158:         } else {
18159:             newSpare.options[0] = new Option("$select","",false,false);
18160:         }
18161:         for (var m=0; m<okSpares.length; m++) {
18162:             var idx = m+1;
18163:             var selThis = 0;
18164:             if (selIdx != 0) {
18165:                 if (okSpares[m] == currnew) {
18166:                     selThis = 1;
18167:                 }
18168:             }
18169:             if (selThis == 1) {
18170:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
18171:             } else {
18172:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
18173:             }
18174:         }
18175:     }
18176:     return;
18177: }
18178: 
18179: function checkNewSpares(lonhost,type) {
18180:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
18181:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
18182:     if (chosen != '') { 
18183:         var othertype;
18184:         var othernewSpare;
18185:         if (type == 'primary') {
18186:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
18187:         }
18188:         if (type == 'default') {
18189:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
18190:         }
18191:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
18192:             othernewSpare.selectedIndex = 0;
18193:         }
18194:     }
18195:     return;
18196: }
18197: 
18198: // ]]>
18199: </script>
18200: 
18201: END
18202: 
18203: }
18204: 
18205: sub common_domprefs_js {
18206:     return <<"END";
18207: 
18208: <script type="text/javascript">
18209: // <![CDATA[
18210: 
18211: function getIndicesByName(formname,item) {
18212:     var group = new Array();
18213:     for (var i=0;i<formname.elements.length;i++) {
18214:         if (formname.elements[i].name == item) {
18215:             group.push(formname.elements[i].id);
18216:         }
18217:     }
18218:     return group;
18219: }
18220: 
18221: // ]]>
18222: </script>
18223: 
18224: END
18225: 
18226: }
18227: 
18228: sub recaptcha_js {
18229:     my %lt = &captcha_phrases();
18230:     return <<"END";
18231: 
18232: <script type="text/javascript">
18233: // <![CDATA[
18234: 
18235: function updateCaptcha(caller,context) {
18236:     var privitem;
18237:     var pubitem;
18238:     var privtext;
18239:     var pubtext;
18240:     var versionitem;
18241:     var versiontext;
18242:     if (document.getElementById(context+'_recaptchapub')) {
18243:         pubitem = document.getElementById(context+'_recaptchapub');
18244:     } else {
18245:         return;
18246:     }
18247:     if (document.getElementById(context+'_recaptchapriv')) {
18248:         privitem = document.getElementById(context+'_recaptchapriv');
18249:     } else {
18250:         return;
18251:     }
18252:     if (document.getElementById(context+'_recaptchapubtxt')) {
18253:         pubtext = document.getElementById(context+'_recaptchapubtxt');
18254:     } else {
18255:         return;
18256:     }
18257:     if (document.getElementById(context+'_recaptchaprivtxt')) {
18258:         privtext = document.getElementById(context+'_recaptchaprivtxt');
18259:     } else {
18260:         return;
18261:     }
18262:     if (document.getElementById(context+'_recaptchaversion')) {
18263:         versionitem = document.getElementById(context+'_recaptchaversion');
18264:     } else {
18265:         return;
18266:     }
18267:     if (document.getElementById(context+'_recaptchavertxt')) {
18268:         versiontext = document.getElementById(context+'_recaptchavertxt');
18269:     } else {
18270:         return;
18271:     }
18272:     if (caller.checked) {
18273:         if (caller.value == 'recaptcha') {
18274:             pubitem.type = 'text';
18275:             privitem.type = 'text';
18276:             pubitem.size = '40';
18277:             privitem.size = '40';
18278:             pubtext.innerHTML = "$lt{'pub'}";
18279:             privtext.innerHTML = "$lt{'priv'}";
18280:             versionitem.type = 'text';
18281:             versionitem.size = '3';
18282:             versiontext.innerHTML = "$lt{'ver'}";
18283:         } else {
18284:             pubitem.type = 'hidden';
18285:             privitem.type = 'hidden';
18286:             versionitem.type = 'hidden';
18287:             pubtext.innerHTML = '';
18288:             privtext.innerHTML = '';
18289:             versiontext.innerHTML = '';
18290:         }
18291:     }
18292:     return;
18293: }
18294: 
18295: // ]]>
18296: </script>
18297: 
18298: END
18299: 
18300: }
18301: 
18302: sub toggle_display_js {
18303:     return <<"END";
18304: 
18305: <script type="text/javascript">
18306: // <![CDATA[
18307: 
18308: function toggleDisplay(domForm,caller) {
18309:     if (document.getElementById(caller)) {
18310:         var divitem = document.getElementById(caller);
18311:         var optionsElement = domForm.coursecredits;
18312:         var checkval = 1;
18313:         var dispval = 'block';
18314:         var selfcreateRegExp = /^cancreate_emailverified/;
18315:         if (caller == 'emailoptions') {
18316:             optionsElement = domForm.cancreate_email; 
18317:         }
18318:         if (caller == 'studentsubmission') {
18319:             optionsElement = domForm.postsubmit;
18320:         }
18321:         if (caller == 'cloneinstcode') {
18322:             optionsElement = domForm.canclone;
18323:             checkval = 'instcode';
18324:         }
18325:         if (selfcreateRegExp.test(caller)) {
18326:             optionsElement = domForm.elements[caller];
18327:             checkval = 'other';
18328:             dispval = 'inline'
18329:         }
18330:         if (optionsElement.length) {
18331:             var currval;
18332:             for (var i=0; i<optionsElement.length; i++) {
18333:                 if (optionsElement[i].checked) {
18334:                    currval = optionsElement[i].value;
18335:                 }
18336:             }
18337:             if (currval == checkval) {
18338:                 divitem.style.display = dispval;
18339:             } else {
18340:                 divitem.style.display = 'none';
18341:             }
18342:         }
18343:     }
18344:     return;
18345: }
18346: 
18347: // ]]>
18348: </script>
18349: 
18350: END
18351: 
18352: }
18353: 
18354: sub captcha_phrases {
18355:     return &Apache::lonlocal::texthash (
18356:                  priv => 'Private key',
18357:                  pub  => 'Public key',
18358:                  original  => 'original (CAPTCHA)',
18359:                  recaptcha => 'successor (ReCAPTCHA)',
18360:                  notused   => 'unused',
18361:                  ver => 'ReCAPTCHA version (1 or 2)',
18362:     );
18363: }
18364: 
18365: sub devalidate_remote_domconfs {
18366:     my ($dom,$cachekeys) = @_;
18367:     return unless (ref($cachekeys) eq 'HASH');
18368:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
18369:     my %thismachine;
18370:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
18371:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
18372:     if (keys(%servers)) {
18373:         foreach my $server (keys(%servers)) {
18374:             next if ($thismachine{$server});
18375:             my @cached;
18376:             foreach my $name (@posscached) {
18377:                 if ($cachekeys->{$name}) {
18378:                     push(@cached,&escape($name).':'.&escape($dom));
18379:                 }
18380:             }
18381:             if (@cached) {
18382:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
18383:             }
18384:         }
18385:     }
18386:     return;
18387: }
18388: 
18389: 1;

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