File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.354: download - view: text, annotated - select for diffs
Mon Apr 22 01:55:17 2019 UTC (5 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain Configuration for Passwords for internally authenticated users.
  - Options for "Forgot Password" utility: link lifetime, case sensitivity,
    information required, e-mail types, custom text.
  - Options for encryption of Stored Passwords (moved from "Defaults" menu).
  - Rules for LON-CAPA Passwords (length, characters, expiration).
  - Option to allow Course Owner to change student's password (with conditions).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.354 2019/04/22 01:55:17 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','passwords'],$dom);
  223:     my %encconfig =
  224:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti'],$dom);
  225:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  226:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  227:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  228:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  229:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  230:                     foreach my $item ('key','secret') {
  231:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  232:                     }
  233:                 }
  234:             }
  235:         }
  236:     }
  237:     if (ref($domconfig{'lti'}) eq 'HASH') {
  238:         if (ref($encconfig{'lti'}) eq 'HASH') {
  239:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  240:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  241:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  242:                     foreach my $item ('key','secret') {
  243:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  244:                     }
  245:                 }
  246:             }
  247:         }
  248:     }
  249:     my @prefs_order = ('rolecolors','login','defaults','passwords','quotas','autoenroll',
  250:                        'autoupdate','autocreate','directorysrch','contacts',
  251:                        'usercreation','selfcreation','usermodification','scantron',
  252:                        'requestcourses','requestauthor','coursecategories',
  253:                        'serverstatuses','helpsettings','coursedefaults',
  254:                        'ltitools','selfenrollment','usersessions','ssl','trust','lti');
  255:     my %existing;
  256:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  257:         %existing = %{$domconfig{'loadbalancing'}};
  258:     }
  259:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  260:         push(@prefs_order,'loadbalancing');
  261:     }
  262:     my %prefs = (
  263:         'rolecolors' =>
  264:                    { text => 'Default color schemes',
  265:                      help => 'Domain_Configuration_Color_Schemes',
  266:                      header => [{col1 => 'Student Settings',
  267:                                  col2 => '',},
  268:                                 {col1 => 'Coordinator Settings',
  269:                                  col2 => '',},
  270:                                 {col1 => 'Author Settings',
  271:                                  col2 => '',},
  272:                                 {col1 => 'Administrator Settings',
  273:                                  col2 => '',}],
  274:                       print => \&print_rolecolors,
  275:                       modify => \&modify_rolecolors,
  276:                     },
  277:         'login' =>
  278:                     { text => 'Log-in page options',
  279:                       help => 'Domain_Configuration_Login_Page',
  280:                       header => [{col1 => 'Log-in Page Items',
  281:                                   col2 => '',},
  282:                                  {col1 => 'Log-in Help',
  283:                                   col2 => 'Value'},
  284:                                  {col1 => 'Custom HTML in document head',
  285:                                   col2 => 'Value'}],
  286:                       print => \&print_login,
  287:                       modify => \&modify_login,
  288:                     },
  289:         'defaults' => 
  290:                     { text => 'Default authentication/language/timezone/portal/types',
  291:                       help => 'Domain_Configuration_LangTZAuth',
  292:                       header => [{col1 => 'Setting',
  293:                                   col2 => 'Value'},
  294:                                  {col1 => 'Institutional user types',
  295:                                   col2 => 'Name displayed'}],
  296:                       print => \&print_defaults,
  297:                       modify => \&modify_defaults,
  298:                     },
  299:         'passwords' =>
  300:                     { text => 'Passwords (Internal authentication)',
  301:                       help => 'Domain_Configuration_Passwords',
  302:                       header => [{col1 => 'Resetting Forgotten Password',
  303:                                   col2 => 'Settings'},
  304:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  305:                                   col2 => 'Settings'},
  306:                                  {col1 => 'Rules for LON-CAPA Passwords',
  307:                                   col2 => 'Settings'},
  308:                                  {col1 => 'Course Owner Changing Student Passwords',
  309:                                   col2 => 'Settings'}],
  310:                       print => \&print_passwords,
  311:                       modify => \&modify_passwords,
  312:                     },
  313:         'quotas' => 
  314:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  315:                       help => 'Domain_Configuration_Quotas',
  316:                       header => [{col1 => 'User affiliation',
  317:                                   col2 => 'Available tools',
  318:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  319:                       print => \&print_quotas,
  320:                       modify => \&modify_quotas,
  321:                     },
  322:         'autoenroll' =>
  323:                    { text => 'Auto-enrollment settings',
  324:                      help => 'Domain_Configuration_Auto_Enrollment',
  325:                      header => [{col1 => 'Configuration setting',
  326:                                  col2 => 'Value(s)'}],
  327:                      print => \&print_autoenroll,
  328:                      modify => \&modify_autoenroll,
  329:                    },
  330:         'autoupdate' => 
  331:                    { text => 'Auto-update settings',
  332:                      help => 'Domain_Configuration_Auto_Updates',
  333:                      header => [{col1 => 'Setting',
  334:                                  col2 => 'Value',},
  335:                                 {col1 => 'Setting',
  336:                                  col2 => 'Affiliation'},
  337:                                 {col1 => 'User population',
  338:                                  col2 => 'Updatable user data'}],
  339:                      print => \&print_autoupdate,
  340:                      modify => \&modify_autoupdate,
  341:                   },
  342:         'autocreate' => 
  343:                   { text => 'Auto-course creation settings',
  344:                      help => 'Domain_Configuration_Auto_Creation',
  345:                      header => [{col1 => 'Configuration Setting',
  346:                                  col2 => 'Value',}],
  347:                      print => \&print_autocreate,
  348:                      modify => \&modify_autocreate,
  349:                   },
  350:         'directorysrch' => 
  351:                   { text => 'Directory searches',
  352:                     help => 'Domain_Configuration_InstDirectory_Search',
  353:                     header => [{col1 => 'Institutional Directory Setting',
  354:                                 col2 => 'Value',},
  355:                                {col1 => 'LON-CAPA Directory Setting',
  356:                                 col2 => 'Value',}],
  357:                     print => \&print_directorysrch,
  358:                     modify => \&modify_directorysrch,
  359:                   },
  360:         'contacts' =>
  361:                   { text => 'E-mail addresses and helpform',
  362:                     help => 'Domain_Configuration_Contact_Info',
  363:                     header => [{col1 => 'Default e-mail addresses',
  364:                                 col2 => 'Value',},
  365:                                {col1 => 'Recipient(s) for notifications',
  366:                                 col2 => 'Value',},
  367:                                {col1 => 'Nightly status check e-mail',
  368:                                 col2 => 'Settings',},
  369:                                {col1 => 'Ask helpdesk form settings',
  370:                                 col2 => 'Value',},],
  371:                     print => \&print_contacts,
  372:                     modify => \&modify_contacts,
  373:                   },
  374:         'usercreation' => 
  375:                   { text => 'User creation',
  376:                     help => 'Domain_Configuration_User_Creation',
  377:                     header => [{col1 => 'Format rule type',
  378:                                 col2 => 'Format rules in force'},
  379:                                {col1 => 'User account creation',
  380:                                 col2 => 'Usernames which may be created',},
  381:                                {col1 => 'Context',
  382:                                 col2 => 'Assignable authentication types'}],
  383:                     print => \&print_usercreation,
  384:                     modify => \&modify_usercreation,
  385:                   },
  386:         'selfcreation' => 
  387:                   { text => 'Users self-creating accounts',
  388:                     help => 'Domain_Configuration_Self_Creation', 
  389:                     header => [{col1 => 'Self-creation with institutional username',
  390:                                 col2 => 'Enabled?'},
  391:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  392:                                 col2 => 'Information user can enter'},
  393:                                {col1 => 'Self-creation with e-mail verification',
  394:                                 col2 => 'Settings'}],
  395:                     print => \&print_selfcreation,
  396:                     modify => \&modify_selfcreation,
  397:                   },
  398:         'usermodification' =>
  399:                   { text => 'User modification',
  400:                     help => 'Domain_Configuration_User_Modification',
  401:                     header => [{col1 => 'Target user has role',
  402:                                 col2 => 'User information updatable in author context'},
  403:                                {col1 => 'Target user has role',
  404:                                 col2 => 'User information updatable in course context'}],
  405:                     print => \&print_usermodification,
  406:                     modify => \&modify_usermodification,
  407:                   },
  408:         'scantron' =>
  409:                   { text => 'Bubblesheet format',
  410:                     help => 'Domain_Configuration_Scantron_Format',
  411:                     header => [ {col1 => 'Bubblesheet format file',
  412:                                  col2 => ''},
  413:                                 {col1 => 'Bubblesheet data upload formats',
  414:                                  col2 => 'Settings'}],
  415:                     print => \&print_scantron,
  416:                     modify => \&modify_scantron,
  417:                   },
  418:         'requestcourses' => 
  419:                  {text => 'Request creation of courses',
  420:                   help => 'Domain_Configuration_Request_Courses',
  421:                   header => [{col1 => 'User affiliation',
  422:                               col2 => 'Availability/Processing of requests',},
  423:                              {col1 => 'Setting',
  424:                               col2 => 'Value'},
  425:                              {col1 => 'Available textbooks',
  426:                               col2 => ''},
  427:                              {col1 => 'Available templates',
  428:                               col2 => ''},
  429:                              {col1 => 'Validation (not official courses)',
  430:                               col2 => 'Value'},],
  431:                   print => \&print_quotas,
  432:                   modify => \&modify_quotas,
  433:                  },
  434:         'requestauthor' =>
  435:                  {text => 'Request Authoring Space',
  436:                   help => 'Domain_Configuration_Request_Author',
  437:                   header => [{col1 => 'User affiliation',
  438:                               col2 => 'Availability/Processing of requests',},
  439:                              {col1 => 'Setting',
  440:                               col2 => 'Value'}],
  441:                   print => \&print_quotas,
  442:                   modify => \&modify_quotas,
  443:                  },
  444:         'coursecategories' =>
  445:                   { text => 'Cataloging of courses/communities',
  446:                     help => 'Domain_Configuration_Cataloging_Courses',
  447:                     header => [{col1 => 'Catalog type/availability',
  448:                                 col2 => '',},
  449:                                {col1 => 'Category settings for standard catalog',
  450:                                 col2 => '',},
  451:                                {col1 => 'Categories',
  452:                                 col2 => '',
  453:                                }],
  454:                     print => \&print_coursecategories,
  455:                     modify => \&modify_coursecategories,
  456:                   },
  457:         'serverstatuses' =>
  458:                  {text   => 'Access to server status pages',
  459:                   help   => 'Domain_Configuration_Server_Status',
  460:                   header => [{col1 => 'Status Page',
  461:                               col2 => 'Other named users',
  462:                               col3 => 'Specific IPs',
  463:                             }],
  464:                   print => \&print_serverstatuses,
  465:                   modify => \&modify_serverstatuses,
  466:                  },
  467:         'helpsettings' =>
  468:                  {text   => 'Support settings',
  469:                   help   => 'Domain_Configuration_Help_Settings',
  470:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  471:                               col2 => 'Value'},
  472:                              {col1 => 'Helpdesk Roles',
  473:                               col2 => 'Settings'},],
  474:                   print  => \&print_helpsettings,
  475:                   modify => \&modify_helpsettings,
  476:                  },
  477:         'coursedefaults' => 
  478:                  {text => 'Course/Community defaults',
  479:                   help => 'Domain_Configuration_Course_Defaults',
  480:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  481:                               col2 => 'Value',},
  482:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  483:                               col2 => 'Value',},],
  484:                   print => \&print_coursedefaults,
  485:                   modify => \&modify_coursedefaults,
  486:                  },
  487:         'selfenrollment' => 
  488:                  {text   => 'Self-enrollment in Course/Community',
  489:                   help   => 'Domain_Configuration_Selfenrollment',
  490:                   header => [{col1 => 'Configuration Rights',
  491:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  492:                              {col1 => 'Defaults',
  493:                               col2 => 'Value'},
  494:                              {col1 => 'Self-enrollment validation (optional)',
  495:                               col2 => 'Value'},],
  496:                   print => \&print_selfenrollment,
  497:                   modify => \&modify_selfenrollment,
  498:                  },
  499:         'privacy' => 
  500:                  {text   => 'User Privacy',
  501:                   help   => 'Domain_Configuration_User_Privacy',
  502:                   header => [{col1 => 'Setting',
  503:                               col2 => 'Value',}],
  504:                   print => \&print_privacy,
  505:                   modify => \&modify_privacy,
  506:                  },
  507:         'usersessions' =>
  508:                  {text  => 'User session hosting/offloading',
  509:                   help  => 'Domain_Configuration_User_Sessions',
  510:                   header => [{col1 => 'Domain server',
  511:                               col2 => 'Servers to offload sessions to when busy'},
  512:                              {col1 => 'Hosting of users from other domains',
  513:                               col2 => 'Rules'},
  514:                              {col1 => "Hosting domain's own users elsewhere",
  515:                               col2 => 'Rules'}],
  516:                   print => \&print_usersessions,
  517:                   modify => \&modify_usersessions,
  518:                  },
  519:         'loadbalancing' =>
  520:                  {text  => 'Dedicated Load Balancer(s)',
  521:                   help  => 'Domain_Configuration_Load_Balancing',
  522:                   header => [{col1 => 'Balancers',
  523:                               col2 => 'Default destinations',
  524:                               col3 => 'User affiliation',
  525:                               col4 => 'Overrides'},
  526:                             ],
  527:                   print => \&print_loadbalancing,
  528:                   modify => \&modify_loadbalancing,
  529:                  },
  530:         'ltitools' => 
  531:                  {text => 'External Tools (LTI)',
  532:                   help => 'Domain_Configuration_LTI_Tools',
  533:                   header => [{col1 => 'Setting',
  534:                               col2 => 'Value',}],
  535:                   print => \&print_ltitools,
  536:                   modify => \&modify_ltitools,
  537:                  },
  538:         'ssl' =>
  539:                  {text  => 'LON-CAPA Network (SSL)',
  540:                   help  => 'Domain_Configuration_Network_SSL',
  541:                   header => [{col1 => 'Server',
  542:                               col2 => 'Certificate Status'},
  543:                              {col1 => 'Connections to other servers',
  544:                               col2 => 'Rules'},
  545:                              {col1 => 'Connections from other servers',
  546:                               col2 => 'Rules'},
  547:                              {col1 => "Replicating domain's published content",
  548:                               col2 => 'Rules'}],
  549:                   print => \&print_ssl,
  550:                   modify => \&modify_ssl,
  551:                  },
  552:         'trust' =>
  553:                  {text   => 'Trust Settings',
  554:                   help   => 'Domain_Configuration_Trust',
  555:                   header => [{col1 => "Access to this domain's content by others",
  556:                               col2 => 'Rules'},
  557:                              {col1 => "Access to other domain's content by this domain",
  558:                               col2 => 'Rules'},
  559:                              {col1 => "Enrollment in this domain's courses by others",
  560:                               col2 => 'Rules',},
  561:                              {col1 => "Co-author roles in this domain for others",
  562:                               col2 => 'Rules',},
  563:                              {col1 => "Co-author roles for this domain's users elsewhere",
  564:                               col2 => 'Rules',},
  565:                              {col1 => "Domain roles in this domain assignable to others",
  566:                               col2 => 'Rules'},
  567:                              {col1 => "Course catalog for this domain displayed elsewhere",
  568:                               col2 => 'Rules'},
  569:                              {col1 => "Requests for creation of courses in this domain by others",
  570:                               col2 => 'Rules'},
  571:                              {col1 => "Users in other domains can send messages to this domain",
  572:                               col2 => 'Rules'},],
  573:                   print => \&print_trust,
  574:                   modify => \&modify_trust,
  575:                  },
  576:           'lti' =>
  577:                  {text => 'LTI Provider',
  578:                   help => 'Domain_Configuration_LTI_Provider',
  579:                   header => [{col1 => 'Setting',
  580:                               col2 => 'Value',}],
  581:                   print => \&print_lti,
  582:                   modify => \&modify_lti,
  583:                  },
  584:     );
  585:     if (keys(%servers) > 1) {
  586:         $prefs{'login'}  = { text   => 'Log-in page options',
  587:                              help   => 'Domain_Configuration_Login_Page',
  588:                             header => [{col1 => 'Log-in Service',
  589:                                         col2 => 'Server Setting',},
  590:                                        {col1 => 'Log-in Page Items',
  591:                                         col2 => ''},
  592:                                        {col1 => 'Log-in Help',
  593:                                         col2 => 'Value'},
  594:                                        {col1 => 'Custom HTML in document head',
  595:                                         col2 => 'Value'}],
  596:                             print => \&print_login,
  597:                             modify => \&modify_login,
  598:                            };
  599:     }
  600: 
  601:     my @roles = ('student','coordinator','author','admin');
  602:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  603:     &Apache::lonhtmlcommon::add_breadcrumb
  604:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  605:       text=>"Settings to display/modify"});
  606:     my $confname = $dom.'-domainconfig';
  607: 
  608:     if ($phase eq 'process') {
  609:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  610:                                                               \%prefs,\%domconfig,$confname,\@roles);
  611:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  612:             $r->rflush();
  613:             &devalidate_remote_domconfs($dom,$result);
  614:         }
  615:     } elsif ($phase eq 'display') {
  616:         my $js = &recaptcha_js().
  617:                  &toggle_display_js();
  618:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  619:             my ($othertitle,$usertypes,$types) =
  620:                 &Apache::loncommon::sorted_inst_types($dom);
  621:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  622:                                           $domconfig{'loadbalancing'}).
  623:                    &new_spares_js().
  624:                    &common_domprefs_js().
  625:                    &Apache::loncommon::javascript_array_indexof();
  626:         }
  627:         if (grep(/^requestcourses$/,@actions)) {
  628:             my $javascript_validations;
  629:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  630:             $js .= <<END;
  631: <script type="text/javascript">
  632: $javascript_validations
  633: </script>
  634: $coursebrowserjs
  635: END
  636:         }
  637:         if (grep(/^selfcreation$/,@actions)) {
  638:             $js .= &selfcreate_javascript();
  639:         }
  640:         if (grep(/^contacts$/,@actions)) {
  641:             $js .= &contacts_javascript();
  642:         }
  643:         if (grep(/^scantron$/,@actions)) {
  644:             $js .= &scantron_javascript();
  645:         }
  646:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  647:     } else {
  648: # check if domconfig user exists for the domain.
  649:         my $servadm = $r->dir_config('lonAdmEMail');
  650:         my ($configuserok,$author_ok,$switchserver) =
  651:             &config_check($dom,$confname,$servadm);
  652:         unless ($configuserok eq 'ok') {
  653:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  654:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  655:                           $confname).
  656:                       '<br />'
  657:             );
  658:             if ($switchserver) {
  659:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  660:                           '<br />'.
  661:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  662:                           '<br />'.
  663:                           &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).
  664:                           '<br />'.
  665:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  666:                 );
  667:             } else {
  668:                 $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.').
  669:                           '<br />'.
  670:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  671:                 );
  672:             }
  673:             $r->print(&Apache::loncommon::end_page());
  674:             return OK;
  675:         }
  676:         if (keys(%domconfig) == 0) {
  677:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  678:             my @ids=&Apache::lonnet::current_machine_ids();
  679:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  680:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  681:                 my @loginimages = ('img','logo','domlogo','login');
  682:                 my $custom_img_count = 0;
  683:                 foreach my $img (@loginimages) {
  684:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  685:                         $custom_img_count ++;
  686:                     }
  687:                 }
  688:                 foreach my $role (@roles) {
  689:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  690:                         $custom_img_count ++;
  691:                     }
  692:                 }
  693:                 if ($custom_img_count > 0) {
  694:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  695:                     my $switch_server = &check_switchserver($dom,$confname);
  696:                     $r->print(
  697:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  698:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  699:     &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 />'.
  700:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  701:                     if ($switch_server) {
  702:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  703:                     }
  704:                     $r->print(&Apache::loncommon::end_page());
  705:                     return OK;
  706:                 }
  707:             }
  708:         }
  709:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  710:     }
  711:     return OK;
  712: }
  713: 
  714: sub process_changes {
  715:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  716:     my %domconfig;
  717:     if (ref($values) eq 'HASH') {
  718:         %domconfig = %{$values};
  719:     }
  720:     my $output;
  721:     if ($action eq 'login') {
  722:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  723:     } elsif ($action eq 'rolecolors') {
  724:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  725:                                      $lastactref,%domconfig);
  726:     } elsif ($action eq 'quotas') {
  727:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  728:     } elsif ($action eq 'autoenroll') {
  729:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  730:     } elsif ($action eq 'autoupdate') {
  731:         $output = &modify_autoupdate($dom,%domconfig);
  732:     } elsif ($action eq 'autocreate') {
  733:         $output = &modify_autocreate($dom,%domconfig);
  734:     } elsif ($action eq 'directorysrch') {
  735:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  736:     } elsif ($action eq 'usercreation') {
  737:         $output = &modify_usercreation($dom,%domconfig);
  738:     } elsif ($action eq 'selfcreation') {
  739:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  740:     } elsif ($action eq 'usermodification') {
  741:         $output = &modify_usermodification($dom,%domconfig);
  742:     } elsif ($action eq 'contacts') {
  743:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  744:     } elsif ($action eq 'defaults') {
  745:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  746:     } elsif ($action eq 'scantron') {
  747:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  748:     } elsif ($action eq 'coursecategories') {
  749:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  750:     } elsif ($action eq 'serverstatuses') {
  751:         $output = &modify_serverstatuses($dom,%domconfig);
  752:     } elsif ($action eq 'requestcourses') {
  753:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  754:     } elsif ($action eq 'requestauthor') {
  755:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  756:     } elsif ($action eq 'helpsettings') {
  757:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  758:     } elsif ($action eq 'coursedefaults') {
  759:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  760:     } elsif ($action eq 'selfenrollment') {
  761:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  762:     } elsif ($action eq 'usersessions') {
  763:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  764:     } elsif ($action eq 'loadbalancing') {
  765:         $output = &modify_loadbalancing($dom,%domconfig);
  766:     } elsif ($action eq 'ltitools') {
  767:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  768:     } elsif ($action eq 'ssl') {
  769:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  770:     } elsif ($action eq 'trust') {
  771:         $output = &modify_trust($dom,$lastactref,%domconfig);
  772:     } elsif ($action eq 'lti') {
  773:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  774:     } elsif ($action eq 'passwords') {
  775:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  776:     }
  777:     return $output;
  778: }
  779: 
  780: sub print_config_box {
  781:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  782:     my $rowtotal = 0;
  783:     my $output;
  784:     if ($action eq 'coursecategories') {
  785:         $output = &coursecategories_javascript($settings);
  786:     } elsif ($action eq 'defaults') {
  787:         $output = &defaults_javascript($settings); 
  788:     } elsif ($action eq 'passwords') {
  789:         $output = &passwords_javascript();
  790:     } elsif ($action eq 'helpsettings') {
  791:         my (%privs,%levelscurrent);
  792:         my %full=();
  793:         my %levels=(
  794:                      course => {},
  795:                      domain => {},
  796:                      system => {},
  797:                    );
  798:         my $context = 'domain';
  799:         my $crstype = 'Course';
  800:         my $formname = 'display';
  801:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  802:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  803:         $output =
  804:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  805:                                                       \@templateroles);
  806:     } elsif ($action eq 'ltitools') {
  807:         $output .= &ltitools_javascript($settings);
  808:     } elsif ($action eq 'lti') {
  809:         $output .= &lti_javascript($settings);
  810:     }
  811:     $output .=
  812:          '<table class="LC_nested_outer">
  813:           <tr>
  814:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  815:            &mt($item->{text}).'&nbsp;'.
  816:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  817:           '</tr>';
  818:     $rowtotal ++;
  819:     my $numheaders = 1;
  820:     if (ref($item->{'header'}) eq 'ARRAY') {
  821:         $numheaders = scalar(@{$item->{'header'}});
  822:     }
  823:     if ($numheaders > 1) {
  824:         my $colspan = '';
  825:         my $rightcolspan = '';
  826:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  827:             ($action eq 'directorysrch') ||
  828:             (($action eq 'login') && ($numheaders < 4))) {
  829:             $colspan = ' colspan="2"';
  830:         }
  831:         if ($action eq 'usersessions') {
  832:             $rightcolspan = ' colspan="3"'; 
  833:         }
  834:         $output .= '
  835:           <tr>
  836:            <td>
  837:             <table class="LC_nested">
  838:              <tr class="LC_info_row">
  839:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  840:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  841:              </tr>';
  842:         $rowtotal ++;
  843:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  844:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  845:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  846:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  847:             ($action eq 'contacts')) {
  848:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  849:         } elsif ($action eq 'passwords') {
  850:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  851:         } elsif ($action eq 'coursecategories') {
  852:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  853:         } elsif ($action eq 'scantron') {
  854:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  855:         } elsif ($action eq 'login') {
  856:             if ($numheaders == 4) {
  857:                 $colspan = ' colspan="2"';
  858:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  859:             } else {
  860:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  861:             }
  862:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  863:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  864:         } elsif ($action eq 'rolecolors') {
  865:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  866:         }
  867:         $output .= '
  868:            </table>
  869:           </td>
  870:          </tr>
  871:          <tr>
  872:            <td>
  873:             <table class="LC_nested">
  874:              <tr class="LC_info_row">
  875:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  876:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  877:              </tr>';
  878:             $rowtotal ++;
  879:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  880:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  881:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  882:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'passwords')) {
  883:             if ($action eq 'coursecategories') {
  884:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  885:                 $colspan = ' colspan="2"';
  886:             } elsif ($action eq 'trust') {
  887:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  888:             } elsif ($action eq 'passwords') {
  889:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  890:             } else {
  891:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  892:             }
  893:             if ($action eq 'trust') {
  894:                 $output .= '
  895:             </table>
  896:           </td>
  897:          </tr>';
  898:                 my @trusthdrs = qw(2 3 4 5 6 7);
  899:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  900:                 for (my $i=0; $i<@trusthdrs; $i++) {
  901:                     $output .= '
  902:          <tr>
  903:            <td>
  904:             <table class="LC_nested">
  905:              <tr class="LC_info_row">
  906:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  907:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  908:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  909:             </table>
  910:           </td>
  911:          </tr>';
  912:                 }
  913:                 $output .= '
  914:          <tr>
  915:            <td>
  916:             <table class="LC_nested">
  917:              <tr class="LC_info_row">
  918:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  919:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  920:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  921:             } else {
  922:                 $output .= '
  923:            </table>
  924:           </td>
  925:          </tr>
  926:          <tr>
  927:            <td>
  928:             <table class="LC_nested">
  929:              <tr class="LC_info_row">
  930:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  931:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  932:              </tr>'."\n";
  933:                 if ($action eq 'coursecategories') {
  934:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  935:                 } elsif (($action eq 'contacts') || ($action eq 'passwords')) {
  936:                     if ($action eq 'passwords') {
  937:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  938:                     } else {
  939:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  940:                     }
  941:                     $output .= '
  942:              </tr>
  943:             </table>
  944:            </td>
  945:           </tr>
  946:           <tr>
  947:            <td>
  948:             <table class="LC_nested">
  949:              <tr class="LC_info_row">
  950:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  951:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
  952:                     if ($action eq 'passwords') {
  953:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
  954:                     } else {
  955:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  956:                     }
  957:                     $output .= '
  958:             </table>
  959:           </td>
  960:          </tr>
  961:          <tr>';
  962:                 } else {
  963:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  964:                 }
  965:             }
  966:             $rowtotal ++;
  967:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  968:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  969:                  ($action eq 'helpsettings')) {
  970:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  971:         } elsif ($action eq 'scantron') {
  972:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  973:         } elsif ($action eq 'ssl') {
  974:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  975:             </table>
  976:           </td>
  977:          </tr>
  978:          <tr>
  979:            <td>
  980:             <table class="LC_nested">
  981:              <tr class="LC_info_row">
  982:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  983:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  984:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  985:             </table>
  986:           </td>
  987:          </tr>
  988:          <tr>
  989:            <td>
  990:             <table class="LC_nested">
  991:              <tr class="LC_info_row">
  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></tr>'.
  994:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  995:         } elsif ($action eq 'login') {
  996:             if ($numheaders == 4) {
  997:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  998:            </table>
  999:           </td>
 1000:          </tr>
 1001:          <tr>
 1002:            <td>
 1003:             <table class="LC_nested">
 1004:              <tr class="LC_info_row">
 1005:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1006:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1007:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1008:                 $rowtotal ++;
 1009:             } else {
 1010:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1011:             }
 1012:             $output .= '
 1013:            </table>
 1014:           </td>
 1015:          </tr>
 1016:          <tr>
 1017:            <td>
 1018:             <table class="LC_nested">
 1019:              <tr class="LC_info_row">';
 1020:             if ($numheaders == 4) {
 1021:                 $output .= '
 1022:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1023:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1024:              </tr>';
 1025:             } else {
 1026:                 $output .= '
 1027:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1028:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1029:              </tr>';
 1030:             }
 1031:             $rowtotal ++;
 1032:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
 1033:         } elsif ($action eq 'requestcourses') {
 1034:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1035:             $rowtotal ++;
 1036:             $output .= &print_studentcode($settings,\$rowtotal).'
 1037:            </table>
 1038:           </td>
 1039:          </tr>
 1040:          <tr>
 1041:            <td>
 1042:             <table class="LC_nested">
 1043:              <tr class="LC_info_row">
 1044:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1045:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1046:                        &textbookcourses_javascript($settings).
 1047:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1048:             </table>
 1049:            </td>
 1050:           </tr>
 1051:          <tr>
 1052:            <td>
 1053:             <table class="LC_nested">
 1054:              <tr class="LC_info_row">
 1055:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1056:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1057:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1058:             </table>
 1059:            </td>
 1060:           </tr>
 1061:           <tr>
 1062:            <td>
 1063:             <table class="LC_nested">
 1064:              <tr class="LC_info_row">
 1065:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1066:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1067:              </tr>'.
 1068:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1069:         } elsif ($action eq 'requestauthor') {
 1070:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1071:             $rowtotal ++;
 1072:         } elsif ($action eq 'rolecolors') {
 1073:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1074:            </table>
 1075:           </td>
 1076:          </tr>
 1077:          <tr>
 1078:            <td>
 1079:             <table class="LC_nested">
 1080:              <tr class="LC_info_row">
 1081:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1082:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1083:               <td class="LC_right_item" style="vertical-align: top">'.
 1084:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1085:              </tr>'.
 1086:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1087:            </table>
 1088:           </td>
 1089:          </tr>
 1090:          <tr>
 1091:            <td>
 1092:             <table class="LC_nested">
 1093:              <tr class="LC_info_row">
 1094:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1095:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1096:              </tr>'.
 1097:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1098:             $rowtotal += 2;
 1099:         }
 1100:     } else {
 1101:         $output .= '
 1102:           <tr>
 1103:            <td>
 1104:             <table class="LC_nested">
 1105:              <tr class="LC_info_row">';
 1106:         if ($action eq 'login') {
 1107:             $output .= '  
 1108:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1109:         } elsif ($action eq 'serverstatuses') {
 1110:             $output .= '
 1111:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1112:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1113: 
 1114:         } else {
 1115:             $output .= '
 1116:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1117:         }
 1118:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1119:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1120:                        &mt($item->{'header'}->[0]->{'col2'});
 1121:             if ($action eq 'serverstatuses') {
 1122:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1123:             } 
 1124:         } else {
 1125:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1126:                        &mt($item->{'header'}->[0]->{'col2'});
 1127:         }
 1128:         $output .= '</td>';
 1129:         if ($item->{'header'}->[0]->{'col3'}) {
 1130:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1131:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1132:                             &mt($item->{'header'}->[0]->{'col3'});
 1133:             } else {
 1134:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1135:                            &mt($item->{'header'}->[0]->{'col3'});
 1136:             }
 1137:             if ($action eq 'serverstatuses') {
 1138:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1139:             }
 1140:             $output .= '</td>';
 1141:         }
 1142:         if ($item->{'header'}->[0]->{'col4'}) {
 1143:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1144:                        &mt($item->{'header'}->[0]->{'col4'});
 1145:         }
 1146:         $output .= '</tr>';
 1147:         $rowtotal ++;
 1148:         if ($action eq 'quotas') {
 1149:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1150:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1151:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1152:                  ($action eq 'ltitools') || ($action eq 'lti')) {
 1153:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1154:         }
 1155:     }
 1156:     $output .= '
 1157:    </table>
 1158:   </td>
 1159:  </tr>
 1160: </table><br />';
 1161:     return ($output,$rowtotal);
 1162: }
 1163: 
 1164: sub print_login {
 1165:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1166:     my ($css_class,$datatable);
 1167:     my %choices = &login_choices();
 1168: 
 1169:     if ($caller eq 'service') {
 1170:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1171:         my $choice = $choices{'disallowlogin'};
 1172:         $css_class = ' class="LC_odd_row"';
 1173:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1174:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1175:                       '<th>'.$choices{'server'}.'</th>'.
 1176:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1177:                       '<th>'.$choices{'custompath'}.'</th>'.
 1178:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1179:         my %disallowed;
 1180:         if (ref($settings) eq 'HASH') {
 1181:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1182:                %disallowed = %{$settings->{'loginvia'}};
 1183:             }
 1184:         }
 1185:         foreach my $lonhost (sort(keys(%servers))) {
 1186:             my $direct = 'selected="selected"';
 1187:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1188:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1189:                     $direct = '';
 1190:                 }
 1191:             }
 1192:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1193:                           '<td><select name="'.$lonhost.'_server">'.
 1194:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1195:                           '</option>';
 1196:             foreach my $hostid (sort(keys(%servers))) {
 1197:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1198:                 my $selected = '';
 1199:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1200:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1201:                         $selected = 'selected="selected"';
 1202:                     }
 1203:                 }
 1204:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1205:                               $servers{$hostid}.'</option>';
 1206:             }
 1207:             $datatable .= '</select></td>'.
 1208:                           '<td><select name="'.$lonhost.'_serverpath">';
 1209:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1210:                 my $pathname = $path;
 1211:                 if ($path eq 'custom') {
 1212:                     $pathname = &mt('Custom Path').' ->';
 1213:                 }
 1214:                 my $selected = '';
 1215:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1216:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1217:                         $selected = 'selected="selected"';
 1218:                     }
 1219:                 } elsif ($path eq '') {
 1220:                     $selected = 'selected="selected"';
 1221:                 }
 1222:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1223:             }
 1224:             $datatable .= '</select></td>';
 1225:             my ($custom,$exempt);
 1226:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1227:                 $custom = $disallowed{$lonhost}{'custompath'};
 1228:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1229:             }
 1230:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1231:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1232:                           '</tr>';
 1233:         }
 1234:         $datatable .= '</table></td></tr>';
 1235:         return $datatable;
 1236:     } elsif ($caller eq 'page') {
 1237:         my %defaultchecked = ( 
 1238:                                'coursecatalog' => 'on',
 1239:                                'helpdesk'      => 'on',
 1240:                                'adminmail'     => 'off',
 1241:                                'newuser'       => 'off',
 1242:                              );
 1243:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1244:         my (%checkedon,%checkedoff);
 1245:         foreach my $item (@toggles) {
 1246:             if ($defaultchecked{$item} eq 'on') { 
 1247:                 $checkedon{$item} = ' checked="checked" ';
 1248:                 $checkedoff{$item} = ' ';
 1249:             } elsif ($defaultchecked{$item} eq 'off') {
 1250:                 $checkedoff{$item} = ' checked="checked" ';
 1251:                 $checkedon{$item} = ' ';
 1252:             }
 1253:         }
 1254:         my @images = ('img','logo','domlogo','login');
 1255:         my @logintext = ('textcol','bgcol');
 1256:         my @bgs = ('pgbg','mainbg','sidebg');
 1257:         my @links = ('link','alink','vlink');
 1258:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1259:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1260:         my (%is_custom,%designs);
 1261:         my %defaults = (
 1262:                        font => $defaultdesign{'login.font'},
 1263:                        );
 1264:         foreach my $item (@images) {
 1265:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1266:             $defaults{'showlogo'}{$item} = 1;
 1267:         }
 1268:         foreach my $item (@bgs) {
 1269:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1270:         }
 1271:         foreach my $item (@logintext) {
 1272:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1273:         }
 1274:         foreach my $item (@links) {
 1275:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1276:         }
 1277:         if (ref($settings) eq 'HASH') {
 1278:             foreach my $item (@toggles) {
 1279:                 if ($settings->{$item} eq '1') {
 1280:                     $checkedon{$item} =  ' checked="checked" ';
 1281:                     $checkedoff{$item} = ' ';
 1282:                 } elsif ($settings->{$item} eq '0') {
 1283:                     $checkedoff{$item} =  ' checked="checked" ';
 1284:                     $checkedon{$item} = ' ';
 1285:                 }
 1286:             }
 1287:             foreach my $item (@images) {
 1288:                 if (defined($settings->{$item})) {
 1289:                     $designs{$item} = $settings->{$item};
 1290:                     $is_custom{$item} = 1;
 1291:                 }
 1292:                 if (defined($settings->{'showlogo'}{$item})) {
 1293:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1294:                 }
 1295:             }
 1296:             foreach my $item (@logintext) {
 1297:                 if ($settings->{$item} ne '') {
 1298:                     $designs{'logintext'}{$item} = $settings->{$item};
 1299:                     $is_custom{$item} = 1;
 1300:                 }
 1301:             }
 1302:             if ($settings->{'font'} ne '') {
 1303:                 $designs{'font'} = $settings->{'font'};
 1304:                 $is_custom{'font'} = 1;
 1305:             }
 1306:             foreach my $item (@bgs) {
 1307:                 if ($settings->{$item} ne '') {
 1308:                     $designs{'bgs'}{$item} = $settings->{$item};
 1309:                     $is_custom{$item} = 1;
 1310:                 }
 1311:             }
 1312:             foreach my $item (@links) {
 1313:                 if ($settings->{$item} ne '') {
 1314:                     $designs{'links'}{$item} = $settings->{$item};
 1315:                     $is_custom{$item} = 1;
 1316:                 }
 1317:             }
 1318:         } else {
 1319:             if ($designhash{$dom.'.login.font'} ne '') {
 1320:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1321:                 $is_custom{'font'} = 1;
 1322:             }
 1323:             foreach my $item (@images) {
 1324:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1325:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1326:                     $is_custom{$item} = 1;
 1327:                 }
 1328:             }
 1329:             foreach my $item (@bgs) {
 1330:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1331:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1332:                     $is_custom{$item} = 1;
 1333:                 }
 1334:             }
 1335:             foreach my $item (@links) {
 1336:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1337:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1338:                     $is_custom{$item} = 1;
 1339:                 }
 1340:             }
 1341:         }
 1342:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1343:                                                       logo => 'Institution Logo',
 1344:                                                       domlogo => 'Domain Logo',
 1345:                                                       login => 'Login box');
 1346:         my $itemcount = 1;
 1347:         foreach my $item (@toggles) {
 1348:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1349:             $datatable .=  
 1350:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1351:                 '</td><td>'.
 1352:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1353:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1354:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1355:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1356:                 '</tr>';
 1357:             $itemcount ++;
 1358:         }
 1359:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1360:         $datatable .= '</tr></table></td></tr>';
 1361:     } elsif ($caller eq 'help') {
 1362:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1363:         my $switchserver = &check_switchserver($dom,$confname);
 1364:         my $itemcount = 1;
 1365:         $defaulturl = '/adm/loginproblems.html';
 1366:         $defaulttype = 'default';
 1367:         %lt = &Apache::lonlocal::texthash (
 1368:                      del     => 'Delete?',
 1369:                      rep     => 'Replace:',
 1370:                      upl     => 'Upload:',
 1371:                      default => 'Default',
 1372:                      custom  => 'Custom',
 1373:                                              );
 1374:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1375:         my @currlangs;
 1376:         if (ref($settings) eq 'HASH') {
 1377:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1378:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1379:                     next if ($settings->{'helpurl'}{$key} eq '');
 1380:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1381:                     $type{$key} = 'custom';
 1382:                     unless ($key eq 'nolang') {
 1383:                         push(@currlangs,$key);
 1384:                     }
 1385:                 }
 1386:             } elsif ($settings->{'helpurl'} ne '') {
 1387:                 $type{'nolang'} = 'custom';
 1388:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1389:             }
 1390:         }
 1391:         foreach my $lang ('nolang',sort(@currlangs)) {
 1392:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1393:             $datatable .= '<tr'.$css_class.'>';
 1394:             if ($url{$lang} eq '') {
 1395:                 $url{$lang} = $defaulturl;
 1396:             }
 1397:             if ($type{$lang} eq '') {
 1398:                 $type{$lang} = $defaulttype;
 1399:             }
 1400:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1401:             if ($lang eq 'nolang') {
 1402:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1403:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1404:             } else {
 1405:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1406:                                   $langchoices{$lang},
 1407:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1408:             }
 1409:             $datatable .= '</span></td>'."\n".
 1410:                           '<td class="LC_left_item">';
 1411:             if ($type{$lang} eq 'custom') {
 1412:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1413:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1414:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1415:             } else {
 1416:                 $datatable .= $lt{'upl'};
 1417:             }
 1418:             $datatable .='<br />';
 1419:             if ($switchserver) {
 1420:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1421:             } else {
 1422:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1423:             }
 1424:             $datatable .= '</td></tr>';
 1425:             $itemcount ++;
 1426:         }
 1427:         my @addlangs;
 1428:         foreach my $lang (sort(keys(%langchoices))) {
 1429:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1430:             push(@addlangs,$lang);
 1431:         }
 1432:         if (@addlangs > 0) {
 1433:             my %toadd;
 1434:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1435:             $toadd{''} = &mt('Select');
 1436:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1437:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1438:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1439:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1440:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1441:             if ($switchserver) {
 1442:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1443:             } else {
 1444:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1445:             }
 1446:             $datatable .= '</td></tr>';
 1447:             $itemcount ++;
 1448:         }
 1449:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1450:     } elsif ($caller eq 'headtag') {
 1451:         my %domservers = &Apache::lonnet::get_servers($dom);
 1452:         my $choice = $choices{'headtag'};
 1453:         $css_class = ' class="LC_odd_row"';
 1454:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1455:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1456:                       '<th>'.$choices{'current'}.'</th>'.
 1457:                       '<th>'.$choices{'action'}.'</th>'.
 1458:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1459:         my (%currurls,%currexempt);
 1460:         if (ref($settings) eq 'HASH') {
 1461:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1462:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1463:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1464:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1465:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1466:                     }
 1467:                 }
 1468:             }
 1469:         }
 1470:         my %lt = &Apache::lonlocal::texthash(
 1471:                                                del  => 'Delete?',
 1472:                                                rep  => 'Replace:',
 1473:                                                upl  => 'Upload:',
 1474:                                                curr => 'View contents',
 1475:                                                none => 'None',
 1476:         );
 1477:         my $switchserver = &check_switchserver($dom,$confname);
 1478:         foreach my $lonhost (sort(keys(%domservers))) {
 1479:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1480:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1481:             if ($currurls{$lonhost}) {
 1482:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1483:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1484:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1485:                               '">'.$lt{'curr'}.'</a></td>'.
 1486:                               '<td><span class="LC_nobreak"><label>'.
 1487:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1488:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1489:             } else {
 1490:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1491:             }
 1492:             $datatable .='<br />';
 1493:             if ($switchserver) {
 1494:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1495:             } else {
 1496:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1497:             }
 1498:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1499:         }
 1500:         $datatable .= '</table></td></tr>';
 1501:     }
 1502:     return $datatable;
 1503: }
 1504: 
 1505: sub login_choices {
 1506:     my %choices =
 1507:         &Apache::lonlocal::texthash (
 1508:             coursecatalog => 'Display Course/Community Catalog link?',
 1509:             adminmail     => "Display Administrator's E-mail Address?",
 1510:             helpdesk      => 'Display "Contact Helpdesk" link',
 1511:             disallowlogin => "Login page requests redirected",
 1512:             hostid        => "Server",
 1513:             server        => "Redirect to:",
 1514:             serverpath    => "Path",
 1515:             custompath    => "Custom", 
 1516:             exempt        => "Exempt IP(s)",
 1517:             directlogin   => "No redirect",
 1518:             newuser       => "Link to create a user account",
 1519:             img           => "Header",
 1520:             logo          => "Main Logo",
 1521:             domlogo       => "Domain Logo",
 1522:             login         => "Log-in Header", 
 1523:             textcol       => "Text color",
 1524:             bgcol         => "Box color",
 1525:             bgs           => "Background colors",
 1526:             links         => "Link colors",
 1527:             font          => "Font color",
 1528:             pgbg          => "Header",
 1529:             mainbg        => "Page",
 1530:             sidebg        => "Login box",
 1531:             link          => "Link",
 1532:             alink         => "Active link",
 1533:             vlink         => "Visited link",
 1534:             headtag       => "Custom markup",
 1535:             action        => "Action",
 1536:             current       => "Current",
 1537:         );
 1538:     return %choices;
 1539: }
 1540: 
 1541: sub print_rolecolors {
 1542:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1543:     my %choices = &color_font_choices();
 1544:     my @bgs = ('pgbg','tabbg','sidebg');
 1545:     my @links = ('link','alink','vlink');
 1546:     my @images = ('img');
 1547:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1548:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1549:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1550:     my (%is_custom,%designs);
 1551:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1552:     if (ref($settings) eq 'HASH') {
 1553:         if (ref($settings->{$role}) eq 'HASH') {
 1554:             if ($settings->{$role}->{'img'} ne '') {
 1555:                 $designs{'img'} = $settings->{$role}->{'img'};
 1556:                 $is_custom{'img'} = 1;
 1557:             }
 1558:             if ($settings->{$role}->{'font'} ne '') {
 1559:                 $designs{'font'} = $settings->{$role}->{'font'};
 1560:                 $is_custom{'font'} = 1;
 1561:             }
 1562:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1563:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1564:                 $is_custom{'fontmenu'} = 1;
 1565:             }
 1566:             foreach my $item (@bgs) {
 1567:                 if ($settings->{$role}->{$item} ne '') {
 1568:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1569:                     $is_custom{$item} = 1;
 1570:                 }
 1571:             }
 1572:             foreach my $item (@links) {
 1573:                 if ($settings->{$role}->{$item} ne '') {
 1574:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1575:                     $is_custom{$item} = 1;
 1576:                 }
 1577:             }
 1578:         }
 1579:     } else {
 1580:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1581:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1582:             $is_custom{'img'} = 1;
 1583:         }
 1584:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1585:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1586:             $is_custom{'fontmenu'} = 1; 
 1587:         }
 1588:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1589:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1590:             $is_custom{'font'} = 1;
 1591:         }
 1592:         foreach my $item (@bgs) {
 1593:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1594:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1595:                 $is_custom{$item} = 1;
 1596:             
 1597:             }
 1598:         }
 1599:         foreach my $item (@links) {
 1600:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1601:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1602:                 $is_custom{$item} = 1;
 1603:             }
 1604:         }
 1605:     }
 1606:     my $itemcount = 1;
 1607:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1608:     $datatable .= '</tr></table></td></tr>';
 1609:     return $datatable;
 1610: }
 1611: 
 1612: sub role_defaults {
 1613:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1614:     my %defaults;
 1615:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1616:         return %defaults;
 1617:     }
 1618:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1619:     if ($role eq 'login') {
 1620:         %defaults = (
 1621:                        font => $defaultdesign{$role.'.font'},
 1622:                     );
 1623:         if (ref($logintext) eq 'ARRAY') {
 1624:             foreach my $item (@{$logintext}) {
 1625:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1626:             }
 1627:         }
 1628:         foreach my $item (@{$images}) {
 1629:             $defaults{'showlogo'}{$item} = 1;
 1630:         }
 1631:     } else {
 1632:         %defaults = (
 1633:                        img => $defaultdesign{$role.'.img'},
 1634:                        font => $defaultdesign{$role.'.font'},
 1635:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1636:                     );
 1637:     }
 1638:     foreach my $item (@{$bgs}) {
 1639:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1640:     }
 1641:     foreach my $item (@{$links}) {
 1642:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1643:     }
 1644:     foreach my $item (@{$images}) {
 1645:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1646:     }
 1647:     return %defaults;
 1648: }
 1649: 
 1650: sub display_color_options {
 1651:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1652:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1653:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1654:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1655:     my $datatable = '<tr'.$css_class.'>'.
 1656:         '<td>'.$choices->{'font'}.'</td>';
 1657:     if (!$is_custom->{'font'}) {
 1658:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1659:     } else {
 1660:         $datatable .= '<td>&nbsp;</td>';
 1661:     }
 1662:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1663: 
 1664:     $datatable .= '<td><span class="LC_nobreak">'.
 1665:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1666:                   ' value="'.$current_color.'" />&nbsp;'.
 1667:                   '&nbsp;</span></td></tr>';
 1668:     unless ($role eq 'login') { 
 1669:         $datatable .= '<tr'.$css_class.'>'.
 1670:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1671:         if (!$is_custom->{'fontmenu'}) {
 1672:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1673:         } else {
 1674:             $datatable .= '<td>&nbsp;</td>';
 1675:         }
 1676: 	$current_color = $designs->{'fontmenu'} ?
 1677: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1678:         $datatable .= '<td><span class="LC_nobreak">'.
 1679:                       '<input class="colorchooser" type="text" size="10" name="'
 1680: 		      .$role.'_fontmenu"'.
 1681:                       ' value="'.$current_color.'" />&nbsp;'.
 1682:                       '&nbsp;</span></td></tr>';
 1683:     }
 1684:     my $switchserver = &check_switchserver($dom,$confname);
 1685:     foreach my $img (@{$images}) {
 1686: 	$itemcount ++;
 1687:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1688:         $datatable .= '<tr'.$css_class.'>'.
 1689:                       '<td>'.$choices->{$img};
 1690:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1691:         if ($role eq 'login') {
 1692:             if ($img eq 'login') {
 1693:                 $login_hdr_pick =
 1694:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1695:                 $logincolors =
 1696:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1697:                                        $designs,$defaults);
 1698:             } elsif ($img ne 'domlogo') {
 1699:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1700:             }
 1701:         }
 1702:         $datatable .= '</td>';
 1703:         if ($designs->{$img} ne '') {
 1704:             $imgfile = $designs->{$img};
 1705: 	    $img_import = ($imgfile =~ m{^/adm/});
 1706:         } else {
 1707:             $imgfile = $defaults->{$img};
 1708:         }
 1709:         if ($imgfile) {
 1710:             my ($showfile,$fullsize);
 1711:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1712:                 my $urldir = $1;
 1713:                 my $filename = $2;
 1714:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1715:                 if (@info) {
 1716:                     my $thumbfile = 'tn-'.$filename;
 1717:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1718:                     if (@thumb) {
 1719:                         $showfile = $urldir.'/'.$thumbfile;
 1720:                     } else {
 1721:                         $showfile = $imgfile;
 1722:                     }
 1723:                 } else {
 1724:                     $showfile = '';
 1725:                 }
 1726:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1727:                 $showfile = $imgfile;
 1728:                 my $imgdir = $1;
 1729:                 my $filename = $2;
 1730:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1731:                     $showfile = "/$imgdir/tn-".$filename;
 1732:                 } else {
 1733:                     my $input = $londocroot.$imgfile;
 1734:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1735:                     if (!-e $output) {
 1736:                         my ($width,$height) = &thumb_dimensions();
 1737:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1738:                         if ($fullwidth ne '' && $fullheight ne '') {
 1739:                             if ($fullwidth > $width && $fullheight > $height) { 
 1740:                                 my $size = $width.'x'.$height;
 1741:                                 my @args = ('convert','-sample',$size,$input,$output);
 1742:                                 system({$args[0]} @args);
 1743:                                 $showfile = "/$imgdir/tn-".$filename;
 1744:                             }
 1745:                         }
 1746:                     }
 1747:                 }
 1748:             }
 1749:             if ($showfile) {
 1750:                 if ($showfile =~ m{^/(adm|res)/}) {
 1751:                     if ($showfile =~ m{^/res/}) {
 1752:                         my $local_showfile =
 1753:                             &Apache::lonnet::filelocation('',$showfile);
 1754:                         &Apache::lonnet::repcopy($local_showfile);
 1755:                     }
 1756:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1757:                 }
 1758:                 if ($imgfile) {
 1759:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1760:                         if ($imgfile =~ m{^/res/}) {
 1761:                             my $local_imgfile =
 1762:                                 &Apache::lonnet::filelocation('',$imgfile);
 1763:                             &Apache::lonnet::repcopy($local_imgfile);
 1764:                         }
 1765:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1766:                     } else {
 1767:                         $fullsize = $imgfile;
 1768:                     }
 1769:                 }
 1770:                 $datatable .= '<td>';
 1771:                 if ($img eq 'login') {
 1772:                     $datatable .= $login_hdr_pick;
 1773:                 } 
 1774:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1775:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1776:             } else {
 1777:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1778:                               &mt('Upload:').'<br />';
 1779:             }
 1780:         } else {
 1781:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1782:                           &mt('Upload:').'<br />';
 1783:         }
 1784:         if ($switchserver) {
 1785:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1786:         } else {
 1787:             if ($img ne 'login') { # suppress file selection for Log-in header
 1788:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1789:             }
 1790:         }
 1791:         $datatable .= '</td></tr>';
 1792:     }
 1793:     $itemcount ++;
 1794:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1795:     $datatable .= '<tr'.$css_class.'>'.
 1796:                   '<td>'.$choices->{'bgs'}.'</td>';
 1797:     my $bgs_def;
 1798:     foreach my $item (@{$bgs}) {
 1799:         if (!$is_custom->{$item}) {
 1800:             $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>';
 1801:         }
 1802:     }
 1803:     if ($bgs_def) {
 1804:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1805:     } else {
 1806:         $datatable .= '<td>&nbsp;</td>';
 1807:     }
 1808:     $datatable .= '<td class="LC_right_item">'.
 1809:                   '<table border="0"><tr>';
 1810: 
 1811:     foreach my $item (@{$bgs}) {
 1812:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1813: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1814:         if ($designs->{'bgs'}{$item}) {
 1815:             $datatable .= '&nbsp;';
 1816:         }
 1817:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1818:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1819:     }
 1820:     $datatable .= '</tr></table></td></tr>';
 1821:     $itemcount ++;
 1822:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1823:     $datatable .= '<tr'.$css_class.'>'.
 1824:                   '<td>'.$choices->{'links'}.'</td>';
 1825:     my $links_def;
 1826:     foreach my $item (@{$links}) {
 1827:         if (!$is_custom->{$item}) {
 1828:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1829:         }
 1830:     }
 1831:     if ($links_def) {
 1832:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1833:     } else {
 1834:         $datatable .= '<td>&nbsp;</td>';
 1835:     }
 1836:     $datatable .= '<td class="LC_right_item">'.
 1837:                   '<table border="0"><tr>';
 1838:     foreach my $item (@{$links}) {
 1839: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1840:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1841:         if ($designs->{'links'}{$item}) {
 1842:             $datatable.='&nbsp;';
 1843:         }
 1844:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1845:                       '" /></td>';
 1846:     }
 1847:     $$rowtotal += $itemcount;
 1848:     return $datatable;
 1849: }
 1850: 
 1851: sub logo_display_options {
 1852:     my ($img,$defaults,$designs) = @_;
 1853:     my $checkedon;
 1854:     if (ref($defaults) eq 'HASH') {
 1855:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1856:             if ($defaults->{'showlogo'}{$img}) {
 1857:                 $checkedon = 'checked="checked" ';     
 1858:             }
 1859:         } 
 1860:     }
 1861:     if (ref($designs) eq 'HASH') {
 1862:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1863:             if (defined($designs->{'showlogo'}{$img})) {
 1864:                 if ($designs->{'showlogo'}{$img} == 0) {
 1865:                     $checkedon = '';
 1866:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1867:                     $checkedon = 'checked="checked" ';
 1868:                 }
 1869:             }
 1870:         }
 1871:     }
 1872:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1873:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1874:            &mt('show').'</label>'."\n";
 1875: }
 1876: 
 1877: sub login_header_options  {
 1878:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1879:     my $output = '';
 1880:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1881:         $output .= &mt('Text default(s):').'<br />';
 1882:         if (!$is_custom->{'textcol'}) {
 1883:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1884:                        '&nbsp;&nbsp;&nbsp;';
 1885:         }
 1886:         if (!$is_custom->{'bgcol'}) {
 1887:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1888:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1889:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1890:         }
 1891:         $output .= '<br />';
 1892:     }
 1893:     $output .='<br />';
 1894:     return $output;
 1895: }
 1896: 
 1897: sub login_text_colors {
 1898:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1899:     my $color_menu = '<table border="0"><tr>';
 1900:     foreach my $item (@{$logintext}) {
 1901:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1902:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1903:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1904:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1905:     }
 1906:     $color_menu .= '</tr></table><br />';
 1907:     return $color_menu;
 1908: }
 1909: 
 1910: sub image_changes {
 1911:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1912:     my $output;
 1913:     if ($img eq 'login') {
 1914:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1915:     } elsif (!$is_custom) {
 1916:         if ($img ne 'domlogo') {
 1917:             $output = &mt('Default image:').'<br />';
 1918:         } else {
 1919:             $output = &mt('Default in use:').'<br />';
 1920:         }
 1921:     }
 1922:     if ($img ne 'login') {
 1923:         if ($img_import) {
 1924:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1925:         }
 1926:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1927:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1928:         if ($is_custom) {
 1929:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1930:                        '<input type="checkbox" name="'.
 1931:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1932:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1933:         } else {
 1934:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1935:         }
 1936:     }
 1937:     return $output;
 1938: }
 1939: 
 1940: sub print_quotas {
 1941:     my ($dom,$settings,$rowtotal,$action) = @_;
 1942:     my $context;
 1943:     if ($action eq 'quotas') {
 1944:         $context = 'tools';
 1945:     } else {
 1946:         $context = $action;
 1947:     }
 1948:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1949:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1950:     my $typecount = 0;
 1951:     my ($css_class,%titles);
 1952:     if ($context eq 'requestcourses') {
 1953:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 1954:         @options =('norequest','approval','validate','autolimit');
 1955:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1956:         %titles = &courserequest_titles();
 1957:     } elsif ($context eq 'requestauthor') {
 1958:         @usertools = ('author');
 1959:         @options = ('norequest','approval','automatic');
 1960:         %titles = &authorrequest_titles();
 1961:     } else {
 1962:         @usertools = ('aboutme','blog','webdav','portfolio');
 1963:         %titles = &tool_titles();
 1964:     }
 1965:     if (ref($types) eq 'ARRAY') {
 1966:         foreach my $type (@{$types}) {
 1967:             my ($currdefquota,$currauthorquota);
 1968:             unless (($context eq 'requestcourses') ||
 1969:                     ($context eq 'requestauthor')) {
 1970:                 if (ref($settings) eq 'HASH') {
 1971:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1972:                         $currdefquota = $settings->{defaultquota}->{$type};
 1973:                     } else {
 1974:                         $currdefquota = $settings->{$type};
 1975:                     }
 1976:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1977:                         $currauthorquota = $settings->{authorquota}->{$type};
 1978:                     }
 1979:                 }
 1980:             }
 1981:             if (defined($usertypes->{$type})) {
 1982:                 $typecount ++;
 1983:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1984:                 $datatable .= '<tr'.$css_class.'>'.
 1985:                               '<td>'.$usertypes->{$type}.'</td>'.
 1986:                               '<td class="LC_left_item">';
 1987:                 if ($context eq 'requestcourses') {
 1988:                     $datatable .= '<table><tr>';
 1989:                 }
 1990:                 my %cell;  
 1991:                 foreach my $item (@usertools) {
 1992:                     if ($context eq 'requestcourses') {
 1993:                         my ($curroption,$currlimit);
 1994:                         if (ref($settings) eq 'HASH') {
 1995:                             if (ref($settings->{$item}) eq 'HASH') {
 1996:                                 $curroption = $settings->{$item}->{$type};
 1997:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1998:                                     $currlimit = $1; 
 1999:                                 }
 2000:                             }
 2001:                         }
 2002:                         if (!$curroption) {
 2003:                             $curroption = 'norequest';
 2004:                         }
 2005:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2006:                         foreach my $option (@options) {
 2007:                             my $val = $option;
 2008:                             if ($option eq 'norequest') {
 2009:                                 $val = 0;  
 2010:                             }
 2011:                             if ($option eq 'validate') {
 2012:                                 my $canvalidate = 0;
 2013:                                 if (ref($validations{$item}) eq 'HASH') { 
 2014:                                     if ($validations{$item}{$type}) {
 2015:                                         $canvalidate = 1;
 2016:                                     }
 2017:                                 }
 2018:                                 next if (!$canvalidate);
 2019:                             }
 2020:                             my $checked = '';
 2021:                             if ($option eq $curroption) {
 2022:                                 $checked = ' checked="checked"';
 2023:                             } elsif ($option eq 'autolimit') {
 2024:                                 if ($curroption =~ /^autolimit/) {
 2025:                                     $checked = ' checked="checked"';
 2026:                                 }                       
 2027:                             } 
 2028:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2029:                                   '<input type="radio" name="crsreq_'.$item.
 2030:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2031:                                   $titles{$option}.'</label>';
 2032:                             if ($option eq 'autolimit') {
 2033:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2034:                                                 $item.'_limit_'.$type.'" size="1" '.
 2035:                                                 'value="'.$currlimit.'" />';
 2036:                             }
 2037:                             $cell{$item} .= '</span> ';
 2038:                             if ($option eq 'autolimit') {
 2039:                                 $cell{$item} .= $titles{'unlimited'};
 2040:                             }
 2041:                         }
 2042:                     } elsif ($context eq 'requestauthor') {
 2043:                         my $curroption;
 2044:                         if (ref($settings) eq 'HASH') {
 2045:                             $curroption = $settings->{$type};
 2046:                         }
 2047:                         if (!$curroption) {
 2048:                             $curroption = 'norequest';
 2049:                         }
 2050:                         foreach my $option (@options) {
 2051:                             my $val = $option;
 2052:                             if ($option eq 'norequest') {
 2053:                                 $val = 0;
 2054:                             }
 2055:                             my $checked = '';
 2056:                             if ($option eq $curroption) {
 2057:                                 $checked = ' checked="checked"';
 2058:                             }
 2059:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2060:                                   '<input type="radio" name="authorreq_'.$type.
 2061:                                   '" value="'.$val.'"'.$checked.' />'.
 2062:                                   $titles{$option}.'</label></span>&nbsp; ';
 2063:                         }
 2064:                     } else {
 2065:                         my $checked = 'checked="checked" ';
 2066:                         if (ref($settings) eq 'HASH') {
 2067:                             if (ref($settings->{$item}) eq 'HASH') {
 2068:                                 if ($settings->{$item}->{$type} == 0) {
 2069:                                     $checked = '';
 2070:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2071:                                     $checked =  'checked="checked" ';
 2072:                                 }
 2073:                             }
 2074:                         }
 2075:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2076:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2077:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2078:                                       '</label></span>&nbsp; ';
 2079:                     }
 2080:                 }
 2081:                 if ($context eq 'requestcourses') {
 2082:                     $datatable .= '</tr><tr>';
 2083:                     foreach my $item (@usertools) {
 2084:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2085:                     }
 2086:                     $datatable .= '</tr></table>';
 2087:                 }
 2088:                 $datatable .= '</td>';
 2089:                 unless (($context eq 'requestcourses') ||
 2090:                         ($context eq 'requestauthor')) {
 2091:                     $datatable .= 
 2092:                               '<td class="LC_right_item">'.
 2093:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2094:                               '<input type="text" name="quota_'.$type.
 2095:                               '" value="'.$currdefquota.
 2096:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2097:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2098:                               '<input type="text" name="authorquota_'.$type.
 2099:                               '" value="'.$currauthorquota.
 2100:                               '" size="5" /></span></td>';
 2101:                 }
 2102:                 $datatable .= '</tr>';
 2103:             }
 2104:         }
 2105:     }
 2106:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2107:         $defaultquota = '20';
 2108:         $authorquota = '500';
 2109:         if (ref($settings) eq 'HASH') {
 2110:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2111:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2112:             } elsif (defined($settings->{'default'})) {
 2113:                 $defaultquota = $settings->{'default'};
 2114:             }
 2115:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2116:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2117:             }
 2118:         }
 2119:     }
 2120:     $typecount ++;
 2121:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2122:     $datatable .= '<tr'.$css_class.'>'.
 2123:                   '<td>'.$othertitle.'</td>'.
 2124:                   '<td class="LC_left_item">';
 2125:     if ($context eq 'requestcourses') {
 2126:         $datatable .= '<table><tr>';
 2127:     }
 2128:     my %defcell;
 2129:     foreach my $item (@usertools) {
 2130:         if ($context eq 'requestcourses') {
 2131:             my ($curroption,$currlimit);
 2132:             if (ref($settings) eq 'HASH') {
 2133:                 if (ref($settings->{$item}) eq 'HASH') {
 2134:                     $curroption = $settings->{$item}->{'default'};
 2135:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2136:                         $currlimit = $1;
 2137:                     }
 2138:                 }
 2139:             }
 2140:             if (!$curroption) {
 2141:                 $curroption = 'norequest';
 2142:             }
 2143:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2144:             foreach my $option (@options) {
 2145:                 my $val = $option;
 2146:                 if ($option eq 'norequest') {
 2147:                     $val = 0;
 2148:                 }
 2149:                 if ($option eq 'validate') {
 2150:                     my $canvalidate = 0;
 2151:                     if (ref($validations{$item}) eq 'HASH') {
 2152:                         if ($validations{$item}{'default'}) {
 2153:                             $canvalidate = 1;
 2154:                         }
 2155:                     }
 2156:                     next if (!$canvalidate);
 2157:                 }
 2158:                 my $checked = '';
 2159:                 if ($option eq $curroption) {
 2160:                     $checked = ' checked="checked"';
 2161:                 } elsif ($option eq 'autolimit') {
 2162:                     if ($curroption =~ /^autolimit/) {
 2163:                         $checked = ' checked="checked"';
 2164:                     }
 2165:                 }
 2166:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2167:                                   '<input type="radio" name="crsreq_'.$item.
 2168:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2169:                                   $titles{$option}.'</label>';
 2170:                 if ($option eq 'autolimit') {
 2171:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2172:                                        $item.'_limit_default" size="1" '.
 2173:                                        'value="'.$currlimit.'" />';
 2174:                 }
 2175:                 $defcell{$item} .= '</span> ';
 2176:                 if ($option eq 'autolimit') {
 2177:                     $defcell{$item} .= $titles{'unlimited'};
 2178:                 }
 2179:             }
 2180:         } elsif ($context eq 'requestauthor') {
 2181:             my $curroption;
 2182:             if (ref($settings) eq 'HASH') {
 2183:                 $curroption = $settings->{'default'};
 2184:             }
 2185:             if (!$curroption) {
 2186:                 $curroption = 'norequest';
 2187:             }
 2188:             foreach my $option (@options) {
 2189:                 my $val = $option;
 2190:                 if ($option eq 'norequest') {
 2191:                     $val = 0;
 2192:                 }
 2193:                 my $checked = '';
 2194:                 if ($option eq $curroption) {
 2195:                     $checked = ' checked="checked"';
 2196:                 }
 2197:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2198:                               '<input type="radio" name="authorreq_default"'.
 2199:                               ' value="'.$val.'"'.$checked.' />'.
 2200:                               $titles{$option}.'</label></span>&nbsp; ';
 2201:             }
 2202:         } else {
 2203:             my $checked = 'checked="checked" ';
 2204:             if (ref($settings) eq 'HASH') {
 2205:                 if (ref($settings->{$item}) eq 'HASH') {
 2206:                     if ($settings->{$item}->{'default'} == 0) {
 2207:                         $checked = '';
 2208:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2209:                         $checked = 'checked="checked" ';
 2210:                     }
 2211:                 }
 2212:             }
 2213:             $datatable .= '<span class="LC_nobreak"><label>'.
 2214:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2215:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2216:                           '</label></span>&nbsp; ';
 2217:         }
 2218:     }
 2219:     if ($context eq 'requestcourses') {
 2220:         $datatable .= '</tr><tr>';
 2221:         foreach my $item (@usertools) {
 2222:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2223:         }
 2224:         $datatable .= '</tr></table>';
 2225:     }
 2226:     $datatable .= '</td>';
 2227:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2228:         $datatable .= '<td class="LC_right_item">'.
 2229:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2230:                       '<input type="text" name="defaultquota" value="'.
 2231:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2232:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2233:                       '<input type="text" name="authorquota" value="'.
 2234:                       $authorquota.'" size="5" /></span></td>';
 2235:     }
 2236:     $datatable .= '</tr>';
 2237:     $typecount ++;
 2238:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2239:     $datatable .= '<tr'.$css_class.'>'.
 2240:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2241:     if ($context eq 'requestcourses') {
 2242:         $datatable .= &mt('(overrides affiliation, if set)').
 2243:                       '</td>'.
 2244:                       '<td class="LC_left_item">'.
 2245:                       '<table><tr>';
 2246:     } else {
 2247:         $datatable .= &mt('(overrides affiliation, if checked)').
 2248:                       '</td>'.
 2249:                       '<td class="LC_left_item" colspan="2">'.
 2250:                       '<br />';
 2251:     }
 2252:     my %advcell;
 2253:     foreach my $item (@usertools) {
 2254:         if ($context eq 'requestcourses') {
 2255:             my ($curroption,$currlimit);
 2256:             if (ref($settings) eq 'HASH') {
 2257:                 if (ref($settings->{$item}) eq 'HASH') {
 2258:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2259:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2260:                         $currlimit = $1;
 2261:                     }
 2262:                 }
 2263:             }
 2264:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2265:             my $checked = '';
 2266:             if ($curroption eq '') {
 2267:                 $checked = ' checked="checked"';
 2268:             }
 2269:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2270:                                '<input type="radio" name="crsreq_'.$item.
 2271:                                '__LC_adv" value=""'.$checked.' />'.
 2272:                                &mt('No override set').'</label></span>&nbsp; ';
 2273:             foreach my $option (@options) {
 2274:                 my $val = $option;
 2275:                 if ($option eq 'norequest') {
 2276:                     $val = 0;
 2277:                 }
 2278:                 if ($option eq 'validate') {
 2279:                     my $canvalidate = 0;
 2280:                     if (ref($validations{$item}) eq 'HASH') {
 2281:                         if ($validations{$item}{'_LC_adv'}) {
 2282:                             $canvalidate = 1;
 2283:                         }
 2284:                     }
 2285:                     next if (!$canvalidate);
 2286:                 }
 2287:                 my $checked = '';
 2288:                 if ($val eq $curroption) {
 2289:                     $checked = ' checked="checked"';
 2290:                 } elsif ($option eq 'autolimit') {
 2291:                     if ($curroption =~ /^autolimit/) {
 2292:                         $checked = ' checked="checked"';
 2293:                     }
 2294:                 }
 2295:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2296:                                   '<input type="radio" name="crsreq_'.$item.
 2297:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2298:                                   $titles{$option}.'</label>';
 2299:                 if ($option eq 'autolimit') {
 2300:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2301:                                        $item.'_limit__LC_adv" size="1" '.
 2302:                                        'value="'.$currlimit.'" />';
 2303:                 }
 2304:                 $advcell{$item} .= '</span> ';
 2305:                 if ($option eq 'autolimit') {
 2306:                     $advcell{$item} .= $titles{'unlimited'};
 2307:                 }
 2308:             }
 2309:         } elsif ($context eq 'requestauthor') {
 2310:             my $curroption;
 2311:             if (ref($settings) eq 'HASH') {
 2312:                 $curroption = $settings->{'_LC_adv'};
 2313:             }
 2314:             my $checked = '';
 2315:             if ($curroption eq '') {
 2316:                 $checked = ' checked="checked"';
 2317:             }
 2318:             $datatable .= '<span class="LC_nobreak"><label>'.
 2319:                           '<input type="radio" name="authorreq__LC_adv"'.
 2320:                           ' value=""'.$checked.' />'.
 2321:                           &mt('No override set').'</label></span>&nbsp; ';
 2322:             foreach my $option (@options) {
 2323:                 my $val = $option;
 2324:                 if ($option eq 'norequest') {
 2325:                     $val = 0;
 2326:                 }
 2327:                 my $checked = '';
 2328:                 if ($val eq $curroption) {
 2329:                     $checked = ' checked="checked"';
 2330:                 }
 2331:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2332:                               '<input type="radio" name="authorreq__LC_adv"'.
 2333:                               ' value="'.$val.'"'.$checked.' />'.
 2334:                               $titles{$option}.'</label></span>&nbsp; ';
 2335:             }
 2336:         } else {
 2337:             my $checked = 'checked="checked" ';
 2338:             if (ref($settings) eq 'HASH') {
 2339:                 if (ref($settings->{$item}) eq 'HASH') {
 2340:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2341:                         $checked = '';
 2342:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2343:                         $checked = 'checked="checked" ';
 2344:                     }
 2345:                 }
 2346:             }
 2347:             $datatable .= '<span class="LC_nobreak"><label>'.
 2348:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2349:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2350:                           '</label></span>&nbsp; ';
 2351:         }
 2352:     }
 2353:     if ($context eq 'requestcourses') {
 2354:         $datatable .= '</tr><tr>';
 2355:         foreach my $item (@usertools) {
 2356:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2357:         }
 2358:         $datatable .= '</tr></table>';
 2359:     }
 2360:     $datatable .= '</td></tr>';
 2361:     $$rowtotal += $typecount;
 2362:     return $datatable;
 2363: }
 2364: 
 2365: sub print_requestmail {
 2366:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2367:     my ($now,$datatable,%currapp);
 2368:     $now = time;
 2369:     if (ref($settings) eq 'HASH') {
 2370:         if (ref($settings->{'notify'}) eq 'HASH') {
 2371:             if ($settings->{'notify'}{'approval'} ne '') {
 2372:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2373:             }
 2374:         }
 2375:     }
 2376:     my $numinrow = 2;
 2377:     my $css_class;
 2378:     if ($$rowtotal%2) {
 2379:         $css_class = 'LC_odd_row';
 2380:     }
 2381:     if ($customcss) {
 2382:         $css_class .= " $customcss";
 2383:     }
 2384:     $css_class =~ s/^\s+//;
 2385:     if ($css_class) {
 2386:         $css_class = ' class="'.$css_class.'"';
 2387:     }
 2388:     if ($rowstyle) {
 2389:         $css_class .= ' style="'.$rowstyle.'"';
 2390:     }
 2391:     my $text;
 2392:     if ($action eq 'requestcourses') {
 2393:         $text = &mt('Receive notification of course requests requiring approval');
 2394:     } elsif ($action eq 'requestauthor') {
 2395:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2396:     } else {
 2397:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2398:     }
 2399:     $datatable = '<tr'.$css_class.'>'.
 2400:                  ' <td>'.$text.'</td>'.
 2401:                  ' <td class="LC_left_item">';
 2402:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2403:                                                  $action.'notifyapproval',%currapp);
 2404:     if ($numdc > 0) {
 2405:         $datatable .= $table;
 2406:     } else {
 2407:         $datatable .= &mt('There are no active Domain Coordinators');
 2408:     }
 2409:     $datatable .='</td></tr>';
 2410:     return $datatable;
 2411: }
 2412: 
 2413: sub print_studentcode {
 2414:     my ($settings,$rowtotal) = @_;
 2415:     my $rownum = 0; 
 2416:     my ($output,%current);
 2417:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2418:     if (ref($settings) eq 'HASH') {
 2419:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2420:             foreach my $type (@crstypes) {
 2421:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2422:             }
 2423:         }
 2424:     }
 2425:     $output .= '<tr>'.
 2426:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2427:                '<td class="LC_left_item">';
 2428:     foreach my $type (@crstypes) {
 2429:         my $check = ' ';
 2430:         if ($current{$type}) {
 2431:             $check = ' checked="checked" ';
 2432:         }
 2433:         $output .= '<span class="LC_nobreak"><label>'.
 2434:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2435:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2436:     }
 2437:     $output .= '</td></tr>';
 2438:     $$rowtotal ++;
 2439:     return $output;
 2440: }
 2441: 
 2442: sub print_textbookcourses {
 2443:     my ($dom,$type,$settings,$rowtotal) = @_;
 2444:     my $rownum = 0;
 2445:     my $css_class;
 2446:     my $itemcount = 1;
 2447:     my $maxnum = 0;
 2448:     my $bookshash;
 2449:     if (ref($settings) eq 'HASH') {
 2450:         $bookshash = $settings->{$type};
 2451:     }
 2452:     my %ordered;
 2453:     if (ref($bookshash) eq 'HASH') {
 2454:         foreach my $item (keys(%{$bookshash})) {
 2455:             if (ref($bookshash->{$item}) eq 'HASH') {
 2456:                 my $num = $bookshash->{$item}{'order'};
 2457:                 $ordered{$num} = $item;
 2458:             }
 2459:         }
 2460:     }
 2461:     my $confname = $dom.'-domainconfig';
 2462:     my $switchserver = &check_switchserver($dom,$confname);
 2463:     my $maxnum = scalar(keys(%ordered));
 2464:     my $datatable;
 2465:     if (keys(%ordered)) {
 2466:         my @items = sort { $a <=> $b } keys(%ordered);
 2467:         for (my $i=0; $i<@items; $i++) {
 2468:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2469:             my $key = $ordered{$items[$i]};
 2470:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2471:             my $coursetitle = $coursehash{'description'};
 2472:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2473:             if (ref($bookshash->{$key}) eq 'HASH') {
 2474:                 $subject = $bookshash->{$key}->{'subject'};
 2475:                 $title = $bookshash->{$key}->{'title'};
 2476:                 if ($type eq 'textbooks') {
 2477:                     $publisher = $bookshash->{$key}->{'publisher'};
 2478:                     $author = $bookshash->{$key}->{'author'};
 2479:                     $image = $bookshash->{$key}->{'image'};
 2480:                     if ($image ne '') {
 2481:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2482:                         my $imagethumb = "$path/tn-".$imagefile;
 2483:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2484:                     }
 2485:                 }
 2486:             }
 2487:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2488:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2489:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2490:             for (my $k=0; $k<=$maxnum; $k++) {
 2491:                 my $vpos = $k+1;
 2492:                 my $selstr;
 2493:                 if ($k == $i) {
 2494:                     $selstr = ' selected="selected" ';
 2495:                 }
 2496:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2497:             }
 2498:             $datatable .= '</select>'.('&nbsp;'x2).
 2499:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2500:                 &mt('Delete?').'</label></span></td>'.
 2501:                 '<td colspan="2">'.
 2502:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2503:                 ('&nbsp;'x2).
 2504:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2505:             if ($type eq 'textbooks') {
 2506:                 $datatable .= ('&nbsp;'x2).
 2507:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2508:                               ('&nbsp;'x2).
 2509:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2510:                               ('&nbsp;'x2).
 2511:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2512:                 if ($image) {
 2513:                     $datatable .= $imgsrc.
 2514:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2515:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2516:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2517:                 }
 2518:                 if ($switchserver) {
 2519:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2520:                 } else {
 2521:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2522:                 }
 2523:             }
 2524:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2525:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2526:                           $coursetitle.'</span></td></tr>'."\n";
 2527:             $itemcount ++;
 2528:         }
 2529:     }
 2530:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2531:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2532:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2533:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2534:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2535:     for (my $k=0; $k<$maxnum+1; $k++) {
 2536:         my $vpos = $k+1;
 2537:         my $selstr;
 2538:         if ($k == $maxnum) {
 2539:             $selstr = ' selected="selected" ';
 2540:         }
 2541:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2542:     }
 2543:     $datatable .= '</select>&nbsp;'."\n".
 2544:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2545:                   '<td colspan="2">'.
 2546:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2547:                   ('&nbsp;'x2).
 2548:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2549:                   ('&nbsp;'x2);
 2550:     if ($type eq 'textbooks') {
 2551:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2552:                       ('&nbsp;'x2).
 2553:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2554:                       ('&nbsp;'x2).
 2555:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2556:         if ($switchserver) {
 2557:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2558:         } else {
 2559:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2560:         }
 2561:         $datatable .= '</span>'."\n";
 2562:     }
 2563:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2564:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2565:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2566:                   &Apache::loncommon::selectcourse_link
 2567:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2568:                   '</span></td>'."\n".
 2569:                   '</tr>'."\n";
 2570:     $itemcount ++;
 2571:     return $datatable;
 2572: }
 2573: 
 2574: sub textbookcourses_javascript {
 2575:     my ($settings) = @_;
 2576:     return unless(ref($settings) eq 'HASH');
 2577:     my (%ordered,%total,%jstext);
 2578:     foreach my $type ('textbooks','templates') {
 2579:         $total{$type} = 0;
 2580:         if (ref($settings->{$type}) eq 'HASH') {
 2581:             foreach my $item (keys(%{$settings->{$type}})) {
 2582:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2583:                     my $num = $settings->{$type}->{$item}{'order'};
 2584:                     $ordered{$type}{$num} = $item;
 2585:                 }
 2586:             }
 2587:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2588:         }
 2589:         my @jsarray = ();
 2590:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2591:             push(@jsarray,$ordered{$type}{$item});
 2592:         }
 2593:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2594:     }
 2595:     return <<"ENDSCRIPT";
 2596: <script type="text/javascript">
 2597: // <![CDATA[
 2598: function reorderBooks(form,item,caller) {
 2599:     var changedVal;
 2600: $jstext{'textbooks'};
 2601: $jstext{'templates'};
 2602:     var newpos;
 2603:     var maxh;
 2604:     if (caller == 'textbooks') {  
 2605:         newpos = 'textbooks_addbook_pos';
 2606:         maxh = 1 + $total{'textbooks'};
 2607:     } else {
 2608:         newpos = 'templates_addbook_pos';
 2609:         maxh = 1 + $total{'templates'};
 2610:     }
 2611:     var current = new Array;
 2612:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2613:     if (item == newpos) {
 2614:         changedVal = newitemVal;
 2615:     } else {
 2616:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2617:         current[newitemVal] = newpos;
 2618:     }
 2619:     if (caller == 'textbooks') {
 2620:         for (var i=0; i<textbooks.length; i++) {
 2621:             var elementName = 'textbooks_'+textbooks[i];
 2622:             if (elementName != item) {
 2623:                 if (form.elements[elementName]) {
 2624:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2625:                     current[currVal] = elementName;
 2626:                 }
 2627:             }
 2628:         }
 2629:     }
 2630:     if (caller == 'templates') {
 2631:         for (var i=0; i<templates.length; i++) {
 2632:             var elementName = 'templates_'+templates[i];
 2633:             if (elementName != item) {
 2634:                 if (form.elements[elementName]) {
 2635:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2636:                     current[currVal] = elementName;
 2637:                 }
 2638:             }
 2639:         }
 2640:     }
 2641:     var oldVal;
 2642:     for (var j=0; j<maxh; j++) {
 2643:         if (current[j] == undefined) {
 2644:             oldVal = j;
 2645:         }
 2646:     }
 2647:     if (oldVal < changedVal) {
 2648:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2649:            var elementName = current[k];
 2650:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2651:         }
 2652:     } else {
 2653:         for (var k=changedVal; k<oldVal; k++) {
 2654:             var elementName = current[k];
 2655:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2656:         }
 2657:     }
 2658:     return;
 2659: }
 2660: 
 2661: // ]]>
 2662: </script>
 2663: 
 2664: ENDSCRIPT
 2665: }
 2666: 
 2667: sub ltitools_javascript {
 2668:     my ($settings) = @_;
 2669:     my $togglejs = &ltitools_toggle_js();
 2670:     unless (ref($settings) eq 'HASH') {
 2671:         return $togglejs;
 2672:     }
 2673:     my (%ordered,$total,%jstext);
 2674:     $total = 0;
 2675:     foreach my $item (keys(%{$settings})) {
 2676:         if (ref($settings->{$item}) eq 'HASH') {
 2677:             my $num = $settings->{$item}{'order'};
 2678:             $ordered{$num} = $item;
 2679:         }
 2680:     }
 2681:     $total = scalar(keys(%{$settings}));
 2682:     my @jsarray = ();
 2683:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2684:         push(@jsarray,$ordered{$item});
 2685:     }
 2686:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2687:     return <<"ENDSCRIPT";
 2688: <script type="text/javascript">
 2689: // <![CDATA[
 2690: function reorderLTITools(form,item) {
 2691:     var changedVal;
 2692: $jstext
 2693:     var newpos = 'ltitools_add_pos';
 2694:     var maxh = 1 + $total;
 2695:     var current = new Array;
 2696:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2697:     if (item == newpos) {
 2698:         changedVal = newitemVal;
 2699:     } else {
 2700:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2701:         current[newitemVal] = newpos;
 2702:     }
 2703:     for (var i=0; i<ltitools.length; i++) {
 2704:         var elementName = 'ltitools_'+ltitools[i];
 2705:         if (elementName != item) {
 2706:             if (form.elements[elementName]) {
 2707:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2708:                 current[currVal] = elementName;
 2709:             }
 2710:         }
 2711:     }
 2712:     var oldVal;
 2713:     for (var j=0; j<maxh; j++) {
 2714:         if (current[j] == undefined) {
 2715:             oldVal = j;
 2716:         }
 2717:     }
 2718:     if (oldVal < changedVal) {
 2719:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2720:            var elementName = current[k];
 2721:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2722:         }
 2723:     } else {
 2724:         for (var k=changedVal; k<oldVal; k++) {
 2725:             var elementName = current[k];
 2726:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2727:         }
 2728:     }
 2729:     return;
 2730: }
 2731: 
 2732: // ]]>
 2733: </script>
 2734: 
 2735: $togglejs
 2736: 
 2737: ENDSCRIPT
 2738: }
 2739: 
 2740: sub ltitools_toggle_js {
 2741:     return <<"ENDSCRIPT";
 2742: <script type="text/javascript">
 2743: // <![CDATA[
 2744: 
 2745: function toggleLTITools(form,setting,item) {
 2746:     var radioname = '';
 2747:     var divid = '';
 2748:     if ((setting == 'passback') || (setting == 'roster')) {
 2749:         radioname = 'ltitools_'+setting+'_'+item;
 2750:         divid = 'ltitools_'+setting+'time_'+item;
 2751:         var num = form.elements[radioname].length;
 2752:         if (num) {
 2753:             var setvis = '';
 2754:             for (var i=0; i<num; i++) {
 2755:                 if (form.elements[radioname][i].checked) {
 2756:                     if (form.elements[radioname][i].value == '1') {
 2757:                         if (document.getElementById(divid)) {
 2758:                             document.getElementById(divid).style.display = 'inline-block';
 2759:                         }
 2760:                         setvis = 1;
 2761:                     }
 2762:                     break;
 2763:                 }
 2764:             }
 2765:         }
 2766:         if (!setvis) {
 2767:             if (document.getElementById(divid)) {
 2768:                 document.getElementById(divid).style.display = 'none';
 2769:             }
 2770:         }
 2771:     }
 2772:     if (setting == 'user') {
 2773:         divid = 'ltitools_'+setting+'_div_'+item;
 2774:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2775:         if (document.getElementById(divid)) {
 2776:             if (document.getElementById(checkid)) {
 2777:                 if (document.getElementById(checkid).checked) {
 2778:                     document.getElementById(divid).style.display = 'inline-block';
 2779:                 } else {
 2780:                     document.getElementById(divid).style.display = 'none';
 2781:                 }
 2782:             }
 2783:         }
 2784:     }
 2785:     return;
 2786: }
 2787: // ]]>
 2788: </script>
 2789: 
 2790: ENDSCRIPT
 2791: }
 2792: 
 2793: sub lti_javascript {
 2794:     my ($settings) = @_;
 2795:     my $togglejs = &lti_toggle_js();
 2796:     unless (ref($settings) eq 'HASH') {
 2797:         return $togglejs;
 2798:     }
 2799:     my (%ordered,$total,%jstext);
 2800:     $total = 0;
 2801:     foreach my $item (keys(%{$settings})) {
 2802:         if (ref($settings->{$item}) eq 'HASH') {
 2803:             my $num = $settings->{$item}{'order'};
 2804:             $ordered{$num} = $item;
 2805:         }
 2806:     }
 2807:     $total = scalar(keys(%{$settings}));
 2808:     my @jsarray = ();
 2809:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2810:         push(@jsarray,$ordered{$item});
 2811:     }
 2812:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 2813:     return <<"ENDSCRIPT";
 2814: <script type="text/javascript">
 2815: // <![CDATA[
 2816: function reorderLTI(form,item) {
 2817:     var changedVal;
 2818: $jstext
 2819:     var newpos = 'lti_pos_add';
 2820:     var maxh = 1 + $total;
 2821:     var current = new Array;
 2822:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2823:     if (item == newpos) {
 2824:         changedVal = newitemVal;
 2825:     } else {
 2826:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2827:         current[newitemVal] = newpos;
 2828:     }
 2829:     for (var i=0; i<lti.length; i++) {
 2830:         var elementName = 'lti_pos_'+lti[i];
 2831:         if (elementName != item) {
 2832:             if (form.elements[elementName]) {
 2833:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2834:                 current[currVal] = elementName;
 2835:             }
 2836:         }
 2837:     }
 2838:     var oldVal;
 2839:     for (var j=0; j<maxh; j++) {
 2840:         if (current[j] == undefined) {
 2841:             oldVal = j;
 2842:         }
 2843:     }
 2844:     if (oldVal < changedVal) {
 2845:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2846:            var elementName = current[k];
 2847:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2848:         }
 2849:     } else {
 2850:         for (var k=changedVal; k<oldVal; k++) {
 2851:             var elementName = current[k];
 2852:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2853:         }
 2854:     }
 2855:     return;
 2856: }
 2857: // ]]>
 2858: </script>
 2859: 
 2860: $togglejs
 2861: 
 2862: ENDSCRIPT
 2863: }
 2864: 
 2865: sub lti_toggle_js {
 2866:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 2867:                             localauth => 'Local auth argument',
 2868:                             krb       => 'Kerberos domain',
 2869:                          );
 2870:     return <<"ENDSCRIPT";
 2871: <script type="text/javascript">
 2872: // <![CDATA[
 2873: 
 2874: function toggleLTI(form,setting,item) {
 2875:     if (setting == 'requser') {
 2876:         var fieldsets = document.getElementsByClassName('ltioption_'+item);
 2877:         if (fieldsets.length) {
 2878:             var radioname = 'lti_'+setting+'_'+item;
 2879:             var num = form.elements[radioname].length;
 2880:             if (num) {
 2881:                 var setvis = '';
 2882:                 for (var i=0; i<num; i++) {
 2883:                     if (form.elements[radioname][i].checked) {
 2884:                         if (form.elements[radioname][i].value == '1') {
 2885:                            setvis = 1;
 2886:                            break;
 2887:                         }
 2888:                     }
 2889:                 }
 2890:                 for (var j=0; j<fieldsets.length; j++) {
 2891:                     if (setvis) {
 2892:                         fieldsets[j].style.display = 'block';
 2893:                     } else {
 2894:                         fieldsets[j].style.display = 'none';
 2895:                     }
 2896:                 }
 2897:             }
 2898:         }
 2899:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback')) {
 2900:         var radioname = '';
 2901:         var divid = '';
 2902:         if (setting == 'user') {
 2903:             radioname = 'lti_mapuser_'+item;
 2904:             divid = 'lti_userfield_'+item;
 2905:         } else if (setting == 'crs') {
 2906:             radioname = 'lti_mapcrs_'+item;
 2907:             divid = 'lti_crsfield_'+item;
 2908:         } else {
 2909:             radioname = 'lti_passback_'+item;
 2910:             divid =  'lti_passback_'+item;
 2911:         }
 2912:         var num = form.elements[radioname].length;
 2913:         if (num) {
 2914:             var setvis = '';
 2915:             for (var i=0; i<num; i++) {
 2916:                if (form.elements[radioname][i].checked) {
 2917:                    if (setting == 'passback') {
 2918:                        if (form.elements[radioname][i].value == '1') {
 2919:                            if (document.getElementById(divid)) {
 2920:                                document.getElementById(divid).style.display = 'inline-block';
 2921:                            }
 2922:                            setvis = 1;
 2923:                            break;
 2924:                        }
 2925:                    } else {
 2926:                        if (form.elements[radioname][i].value == 'other') {
 2927:                            if (document.getElementById(divid)) {
 2928:                                document.getElementById(divid).style.display = 'inline-block';
 2929:                            }
 2930:                            setvis = 1;
 2931:                            break;
 2932:                        }
 2933:                    }
 2934:                } 
 2935:             }
 2936:             if (!setvis) {
 2937:                 if (document.getElementById(divid)) {
 2938:                     document.getElementById(divid).style.display = 'none';
 2939:                 }
 2940:             }
 2941:         }
 2942:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 2943:         var numsec = form.elements['lti_crssec_'+item].length;
 2944:         if (numsec) {
 2945:             var setvis = '';
 2946:             for (var i=0; i<numsec; i++) {
 2947:                 if (form.elements['lti_crssec_'+item][i].checked) {
 2948:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 2949:                         if (document.getElementById('lti_crssecfield_'+item)) {
 2950:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 2951:                             setvis = 1;
 2952:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 2953:                             if (numsrcsec) {
 2954:                                 var setsrcvis = '';
 2955:                                 for (var j=0; j<numsrcsec; j++) {
 2956:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 2957:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 2958:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 2959:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 2960:                                                 setsrcvis = 1;
 2961:                                             }
 2962:                                         }
 2963:                                     }
 2964:                                 }
 2965:                                 if (!setsrcvis) {
 2966:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 2967:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2968:                                     }
 2969:                                 }
 2970:                             }
 2971:                         }
 2972:                     }
 2973:                 }
 2974:             }
 2975:             if (!setvis) {
 2976:                 if (document.getElementById('lti_crssecfield_'+item)) {
 2977:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 2978:                 }
 2979:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 2980:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2981:                 }
 2982:             }
 2983:         }
 2984:     } else if (setting == 'lcauth') {
 2985:         var numauth = form.elements['lti_lcauth_'+item].length;
 2986:         if (numauth) {
 2987:             for (var i=0; i<numauth; i++) {
 2988:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 2989:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 2990:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 2991:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 2992:                         } else {
 2993:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 2994:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 2995:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 2996:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 2997:                                 } else {
 2998:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 2999:                                 }
 3000:                             }
 3001:                         }
 3002:                     }
 3003:                 }
 3004:             }
 3005:         }
 3006:     } else if (setting == 'lcmenu') {
 3007:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3008:         var divid = 'lti_menufield_'+item;
 3009:         var setvis = '';
 3010:         for (var i=0; i<menus.length; i++) {
 3011:             var radioname = menus[i];  
 3012:             var num = form.elements[radioname].length;
 3013:             if (num) {
 3014:                 for (var j=0; j<num; j++) {
 3015:                     if (form.elements[radioname][j].checked) {
 3016:                         if (form.elements[radioname][j].value == '1') {
 3017:                             if (document.getElementById(divid)) {
 3018:                                 document.getElementById(divid).style.display = 'inline-block';
 3019:                             }
 3020:                             setvis = 1;
 3021:                             break;
 3022:                         }
 3023:                     }
 3024:                 }
 3025:             }
 3026:             if (setvis == 1) {
 3027:                 break;
 3028:             }
 3029:         }
 3030:         if (!setvis) {
 3031:             if (document.getElementById(divid)) {
 3032:                 document.getElementById(divid).style.display = 'none';
 3033:             }
 3034:         }
 3035:     }
 3036:     return;
 3037: }
 3038: // ]]>
 3039: </script>
 3040: 
 3041: ENDSCRIPT
 3042: }
 3043: 
 3044: sub print_autoenroll {
 3045:     my ($dom,$settings,$rowtotal) = @_;
 3046:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3047:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3048:     if (ref($settings) eq 'HASH') {
 3049:         if (exists($settings->{'run'})) {
 3050:             if ($settings->{'run'} eq '0') {
 3051:                 $runoff = ' checked="checked" ';
 3052:                 $runon = ' ';
 3053:             } else {
 3054:                 $runon = ' checked="checked" ';
 3055:                 $runoff = ' ';
 3056:             }
 3057:         } else {
 3058:             if ($autorun) {
 3059:                 $runon = ' checked="checked" ';
 3060:                 $runoff = ' ';
 3061:             } else {
 3062:                 $runoff = ' checked="checked" ';
 3063:                 $runon = ' ';
 3064:             }
 3065:         }
 3066:         if (exists($settings->{'co-owners'})) {
 3067:             if ($settings->{'co-owners'} eq '0') {
 3068:                 $coownersoff = ' checked="checked" ';
 3069:                 $coownerson = ' ';
 3070:             } else {
 3071:                 $coownerson = ' checked="checked" ';
 3072:                 $coownersoff = ' ';
 3073:             }
 3074:         } else {
 3075:             $coownersoff = ' checked="checked" ';
 3076:             $coownerson = ' ';
 3077:         }
 3078:         if (exists($settings->{'sender_domain'})) {
 3079:             $defdom = $settings->{'sender_domain'};
 3080:         }
 3081:         if (exists($settings->{'autofailsafe'})) {
 3082:             $failsafe = $settings->{'autofailsafe'};
 3083:         }
 3084:     } else {
 3085:         if ($autorun) {
 3086:             $runon = ' checked="checked" ';
 3087:             $runoff = ' ';
 3088:         } else {
 3089:             $runoff = ' checked="checked" ';
 3090:             $runon = ' ';
 3091:         }
 3092:     }
 3093:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3094:     my $notif_sender;
 3095:     if (ref($settings) eq 'HASH') {
 3096:         $notif_sender = $settings->{'sender_uname'};
 3097:     }
 3098:     my $datatable='<tr class="LC_odd_row">'.
 3099:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3100:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3101:                   '<input type="radio" name="autoenroll_run"'.
 3102:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3103:                   '<label><input type="radio" name="autoenroll_run"'.
 3104:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3105:                   '</tr><tr>'.
 3106:                   '<td>'.&mt('Notification messages - sender').
 3107:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3108:                   &mt('username').':&nbsp;'.
 3109:                   '<input type="text" name="sender_uname" value="'.
 3110:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3111:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3112:                   '<tr class="LC_odd_row">'.
 3113:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3114:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3115:                   '<input type="radio" name="autoassign_coowners"'.
 3116:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3117:                   '<label><input type="radio" name="autoassign_coowners"'.
 3118:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3119:                   '</tr><tr>'.
 3120:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3121:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3122:                   '<input type="text" name="autoenroll_failsafe"'.
 3123:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3124:     $$rowtotal += 4;
 3125:     return $datatable;
 3126: }
 3127: 
 3128: sub print_autoupdate {
 3129:     my ($position,$dom,$settings,$rowtotal) = @_;
 3130:     my $datatable;
 3131:     if ($position eq 'top') {
 3132:         my $updateon = ' ';
 3133:         my $updateoff = ' checked="checked" ';
 3134:         my $classlistson = ' ';
 3135:         my $classlistsoff = ' checked="checked" ';
 3136:         if (ref($settings) eq 'HASH') {
 3137:             if ($settings->{'run'} eq '1') {
 3138:                 $updateon = $updateoff;
 3139:                 $updateoff = ' ';
 3140:             }
 3141:             if ($settings->{'classlists'} eq '1') {
 3142:                 $classlistson = $classlistsoff;
 3143:                 $classlistsoff = ' ';
 3144:             }
 3145:         }
 3146:         my %title = (
 3147:                    run => 'Auto-update active?',
 3148:                    classlists => 'Update information in classlists?',
 3149:                     );
 3150:         $datatable = '<tr class="LC_odd_row">'. 
 3151:                   '<td>'.&mt($title{'run'}).'</td>'.
 3152:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3153:                   '<input type="radio" name="autoupdate_run"'.
 3154:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3155:                   '<label><input type="radio" name="autoupdate_run"'.
 3156:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3157:                   '</tr><tr>'.
 3158:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 3159:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3160:                   '<label><input type="radio" name="classlists"'.
 3161:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3162:                   '<label><input type="radio" name="classlists"'.
 3163:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3164:                   '</tr>';
 3165:         $$rowtotal += 2;
 3166:     } elsif ($position eq 'middle') {
 3167:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3168:         my $numinrow = 3;
 3169:         my $locknamesettings;
 3170:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3171:                                      $dom,$numinrow,$othertitle,
 3172:                                     'lockablenames',$rowtotal);
 3173:         $$rowtotal ++;
 3174:     } else {
 3175:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3176:         my @fields = ('lastname','firstname','middlename','generation',
 3177:                       'permanentemail','id');
 3178:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3179:         my $numrows = 0;
 3180:         if (ref($types) eq 'ARRAY') {
 3181:             if (@{$types} > 0) {
 3182:                 $datatable = 
 3183:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3184:                                          \@fields,$types,\$numrows);
 3185:                     $$rowtotal += @{$types}; 
 3186:             }
 3187:         }
 3188:         $datatable .= 
 3189:             &usertype_update_row($settings,{'default' => $othertitle},
 3190:                                  \%fieldtitles,\@fields,['default'],
 3191:                                  \$numrows);
 3192:         $$rowtotal ++;     
 3193:     }
 3194:     return $datatable;
 3195: }
 3196: 
 3197: sub print_autocreate {
 3198:     my ($dom,$settings,$rowtotal) = @_;
 3199:     my (%createon,%createoff,%currhash);
 3200:     my @types = ('xml','req');
 3201:     if (ref($settings) eq 'HASH') {
 3202:         foreach my $item (@types) {
 3203:             $createoff{$item} = ' checked="checked" ';
 3204:             $createon{$item} = ' ';
 3205:             if (exists($settings->{$item})) {
 3206:                 if ($settings->{$item}) {
 3207:                     $createon{$item} = ' checked="checked" ';
 3208:                     $createoff{$item} = ' ';
 3209:                 }
 3210:             }
 3211:         }
 3212:         if ($settings->{'xmldc'} ne '') {
 3213:             $currhash{$settings->{'xmldc'}} = 1;
 3214:         }
 3215:     } else {
 3216:         foreach my $item (@types) {
 3217:             $createoff{$item} = ' checked="checked" ';
 3218:             $createon{$item} = ' ';
 3219:         }
 3220:     }
 3221:     $$rowtotal += 2;
 3222:     my $numinrow = 2;
 3223:     my $datatable='<tr class="LC_odd_row">'.
 3224:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3225:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3226:                   '<input type="radio" name="autocreate_xml"'.
 3227:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3228:                   '<label><input type="radio" name="autocreate_xml"'.
 3229:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3230:                   '</td></tr><tr>'.
 3231:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3232:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3233:                   '<input type="radio" name="autocreate_req"'.
 3234:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3235:                   '<label><input type="radio" name="autocreate_req"'.
 3236:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3237:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3238:                                                    'autocreate_xmldc',%currhash);
 3239:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3240:     if ($numdc > 1) {
 3241:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3242:                       '</td><td class="LC_left_item">';
 3243:     } else {
 3244:         $datatable .= &mt('Course creation processed as:').
 3245:                       '</td><td class="LC_right_item">';
 3246:     }
 3247:     $datatable .= $dctable.'</td></tr>';
 3248:     $$rowtotal += $rows;
 3249:     return $datatable;
 3250: }
 3251: 
 3252: sub print_directorysrch {
 3253:     my ($position,$dom,$settings,$rowtotal) = @_;
 3254:     my $datatable;
 3255:     if ($position eq 'top') {
 3256:         my $instsrchon = ' ';
 3257:         my $instsrchoff = ' checked="checked" ';
 3258:         my ($exacton,$containson,$beginson);
 3259:         my $instlocalon = ' ';
 3260:         my $instlocaloff = ' checked="checked" ';
 3261:         if (ref($settings) eq 'HASH') {
 3262:             if ($settings->{'available'} eq '1') {
 3263:                 $instsrchon = $instsrchoff;
 3264:                 $instsrchoff = ' ';
 3265:             }
 3266:             if ($settings->{'localonly'} eq '1') {
 3267:                 $instlocalon = $instlocaloff;
 3268:                 $instlocaloff = ' ';
 3269:             }
 3270:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3271:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3272:                     if ($type eq 'exact') {
 3273:                         $exacton = ' checked="checked" ';
 3274:                     } elsif ($type eq 'contains') {
 3275:                         $containson = ' checked="checked" ';
 3276:                     } elsif ($type eq 'begins') {
 3277:                         $beginson = ' checked="checked" ';
 3278:                     }
 3279:                 }
 3280:             } else {
 3281:                 if ($settings->{'searchtypes'} eq 'exact') {
 3282:                     $exacton = ' checked="checked" ';
 3283:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3284:                     $containson = ' checked="checked" ';
 3285:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3286:                     $exacton = ' checked="checked" ';
 3287:                     $containson = ' checked="checked" ';
 3288:                 }
 3289:             }
 3290:         }
 3291:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3292:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3293: 
 3294:         my $numinrow = 4;
 3295:         my $cansrchrow = 0;
 3296:         $datatable='<tr class="LC_odd_row">'.
 3297:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3298:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3299:                    '<input type="radio" name="dirsrch_available"'.
 3300:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3301:                    '<label><input type="radio" name="dirsrch_available"'.
 3302:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3303:                    '</tr><tr>'.
 3304:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3305:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3306:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3307:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3308:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3309:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3310:                    '</tr>';
 3311:         $$rowtotal += 2;
 3312:         if (ref($usertypes) eq 'HASH') {
 3313:             if (keys(%{$usertypes}) > 0) {
 3314:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3315:                                              $numinrow,$othertitle,'cansearch',
 3316:                                              $rowtotal);
 3317:                 $cansrchrow = 1;
 3318:             }
 3319:         }
 3320:         if ($cansrchrow) {
 3321:             $$rowtotal ++;
 3322:             $datatable .= '<tr>';
 3323:         } else {
 3324:             $datatable .= '<tr class="LC_odd_row">';
 3325:         }
 3326:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3327:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3328:         foreach my $title (@{$titleorder}) {
 3329:             if (defined($searchtitles->{$title})) {
 3330:                 my $check = ' ';
 3331:                 if (ref($settings) eq 'HASH') {
 3332:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3333:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3334:                             $check = ' checked="checked" ';
 3335:                         }
 3336:                     }
 3337:                 }
 3338:                 $datatable .= '<td class="LC_left_item">'.
 3339:                               '<span class="LC_nobreak"><label>'.
 3340:                               '<input type="checkbox" name="searchby" '.
 3341:                               'value="'.$title.'"'.$check.'/>'.
 3342:                               $searchtitles->{$title}.'</label></span></td>';
 3343:             }
 3344:         }
 3345:         $datatable .= '</tr></table></td></tr>';
 3346:         $$rowtotal ++;
 3347:         if ($cansrchrow) {
 3348:             $datatable .= '<tr class="LC_odd_row">';
 3349:         } else {
 3350:             $datatable .= '<tr>';
 3351:         }
 3352:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3353:                       '<td class="LC_left_item" colspan="2">'.
 3354:                       '<span class="LC_nobreak"><label>'.
 3355:                       '<input type="checkbox" name="searchtypes" '.
 3356:                       $exacton.' value="exact" />'.&mt('Exact match').
 3357:                       '</label>&nbsp;'.
 3358:                       '<label><input type="checkbox" name="searchtypes" '.
 3359:                       $beginson.' value="begins" />'.&mt('Begins with').
 3360:                       '</label>&nbsp;'.
 3361:                       '<label><input type="checkbox" name="searchtypes" '.
 3362:                       $containson.' value="contains" />'.&mt('Contains').
 3363:                       '</label></span></td></tr>';
 3364:         $$rowtotal ++;
 3365:     } else {
 3366:         my $domsrchon = ' checked="checked" ';
 3367:         my $domsrchoff = ' ';
 3368:         my $domlocalon = ' ';
 3369:         my $domlocaloff = ' checked="checked" ';
 3370:         if (ref($settings) eq 'HASH') {
 3371:             if ($settings->{'lclocalonly'} eq '1') {
 3372:                 $domlocalon = $domlocaloff;
 3373:                 $domlocaloff = ' ';
 3374:             }
 3375:             if ($settings->{'lcavailable'} eq '0') {
 3376:                 $domsrchoff = $domsrchon;
 3377:                 $domsrchon = ' ';
 3378:             }
 3379:         }
 3380:         $datatable='<tr class="LC_odd_row">'.
 3381:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3382:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3383:                       '<input type="radio" name="dirsrch_domavailable"'.
 3384:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3385:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3386:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3387:                       '</tr><tr>'.
 3388:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3389:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3390:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3391:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3392:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3393:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3394:                       '</tr>';
 3395:         $$rowtotal += 2;
 3396:     }
 3397:     return $datatable;
 3398: }
 3399: 
 3400: sub print_contacts {
 3401:     my ($position,$dom,$settings,$rowtotal) = @_;
 3402:     my $datatable;
 3403:     my @contacts = ('adminemail','supportemail');
 3404:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3405:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3406:     if ($position eq 'top') {
 3407:         if (ref($settings) eq 'HASH') {
 3408:             foreach my $item (@contacts) {
 3409:                 if (exists($settings->{$item})) {
 3410:                     $to{$item} = $settings->{$item};
 3411:                 }
 3412:             }
 3413:         }
 3414:     } elsif ($position eq 'middle') {
 3415:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3416:                      'updatesmail','idconflictsmail','hostipmail');
 3417:         foreach my $type (@mailings) {
 3418:             $otheremails{$type} = '';
 3419:         }
 3420:     } elsif ($position eq 'lower') {
 3421:         if (ref($settings) eq 'HASH') {
 3422:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3423:                 %lonstatus = %{$settings->{'lonstatus'}};
 3424:             }
 3425:         }
 3426:     } else {
 3427:         @mailings = ('helpdeskmail','otherdomsmail');
 3428:         foreach my $type (@mailings) {
 3429:             $otheremails{$type} = '';
 3430:         }
 3431:         $bccemails{'helpdeskmail'} = '';
 3432:         $bccemails{'otherdomsmail'} = '';
 3433:         $includestr{'helpdeskmail'} = '';
 3434:         $includestr{'otherdomsmail'} = '';
 3435:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3436:     }
 3437:     if (ref($settings) eq 'HASH') {
 3438:         unless (($position eq 'top') || ($position eq 'lower')) {
 3439:             foreach my $type (@mailings) {
 3440:                 if (exists($settings->{$type})) {
 3441:                     if (ref($settings->{$type}) eq 'HASH') {
 3442:                         foreach my $item (@contacts) {
 3443:                             if ($settings->{$type}{$item}) {
 3444:                                 $checked{$type}{$item} = ' checked="checked" ';
 3445:                             }
 3446:                         }
 3447:                         $otheremails{$type} = $settings->{$type}{'others'};
 3448:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3449:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3450:                             if ($settings->{$type}{'include'} ne '') {
 3451:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3452:                                 $includestr{$type} = &unescape($includestr{$type});
 3453:                             }
 3454:                         }
 3455:                     }
 3456:                 } elsif ($type eq 'lonstatusmail') {
 3457:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3458:                 }
 3459:             }
 3460:         }
 3461:         if ($position eq 'bottom') {
 3462:             foreach my $type (@mailings) {
 3463:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3464:                 if ($settings->{$type}{'include'} ne '') {
 3465:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3466:                     $includestr{$type} = &unescape($includestr{$type});
 3467:                 }
 3468:             }
 3469:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3470:                 if (ref($fields) eq 'ARRAY') {
 3471:                     foreach my $field (@{$fields}) {
 3472:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3473:                     }
 3474:                 }
 3475:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3476:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3477:                 } else {
 3478:                     $maxsize = '1.0';
 3479:                 }
 3480:             } else {
 3481:                 if (ref($fields) eq 'ARRAY') {
 3482:                     foreach my $field (@{$fields}) {
 3483:                         $currfield{$field} = 'yes';
 3484:                     }
 3485:                 }
 3486:                 $maxsize = '1.0';
 3487:             }
 3488:         }
 3489:     } else {
 3490:         if ($position eq 'top') {
 3491:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3492:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3493:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3494:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3495:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3496:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3497:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3498:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3499:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3500:         } elsif ($position eq 'bottom') {
 3501:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3502:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3503:             if (ref($fields) eq 'ARRAY') {
 3504:                 foreach my $field (@{$fields}) {
 3505:                     $currfield{$field} = 'yes';
 3506:                 }
 3507:             }
 3508:             $maxsize = '1.0';
 3509:         }
 3510:     }
 3511:     my ($titles,$short_titles) = &contact_titles();
 3512:     my $rownum = 0;
 3513:     my $css_class;
 3514:     if ($position eq 'top') {
 3515:         foreach my $item (@contacts) {
 3516:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3517:             $datatable .= '<tr'.$css_class.'>'. 
 3518:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3519:                           '</span></td><td class="LC_right_item">'.
 3520:                           '<input type="text" name="'.$item.'" value="'.
 3521:                           $to{$item}.'" /></td></tr>';
 3522:             $rownum ++;
 3523:         }
 3524:     } elsif ($position eq 'bottom') {
 3525:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3526:         $datatable .= '<tr'.$css_class.'>'.
 3527:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3528:                       &mt('(e-mail, subject, and description always shown)').
 3529:                       '</td><td class="LC_left_item">';
 3530:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3531:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3532:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3533:             foreach my $field (@{$fields}) {
 3534:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3535:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3536:                     $datatable .= ' '.&mt('(logged-in users)');
 3537:                 }
 3538:                 $datatable .='</td><td>';
 3539:                 my $clickaction;
 3540:                 if ($field eq 'screenshot') {
 3541:                     $clickaction = ' onclick="screenshotSize(this);"';
 3542:                 }
 3543:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3544:                     foreach my $option (@{$possoptions->{$field}}) {
 3545:                         my $checked;
 3546:                         if ($currfield{$field} eq $option) {
 3547:                             $checked = ' checked="checked"';
 3548:                         }
 3549:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3550:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3551:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3552:                                       '</label></span>'.('&nbsp;'x2);
 3553:                     }
 3554:                 }
 3555:                 if ($field eq 'screenshot') {
 3556:                     my $display;
 3557:                     if ($currfield{$field} eq 'no') {
 3558:                         $display = ' style="display:none"';
 3559:                     }
 3560:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3561:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3562:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3563:                 }
 3564:                 $datatable .= '</td></tr>';
 3565:             }
 3566:             $datatable .= '</table>';
 3567:         }
 3568:         $datatable .= '</td></tr>'."\n";
 3569:         $rownum ++;
 3570:     }
 3571:     unless (($position eq 'top') || ($position eq 'lower')) {
 3572:         foreach my $type (@mailings) {
 3573:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3574:             $datatable .= '<tr'.$css_class.'>'.
 3575:                           '<td><span class="LC_nobreak">'.
 3576:                           $titles->{$type}.': </span></td>'.
 3577:                           '<td class="LC_left_item">';
 3578:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3579:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3580:             }
 3581:             $datatable .= '<span class="LC_nobreak">';
 3582:             foreach my $item (@contacts) {
 3583:                 $datatable .= '<label>'.
 3584:                               '<input type="checkbox" name="'.$type.'"'.
 3585:                               $checked{$type}{$item}.
 3586:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3587:                               '</label>&nbsp;';
 3588:             }
 3589:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3590:                           '<input type="text" name="'.$type.'_others" '.
 3591:                           'value="'.$otheremails{$type}.'"  />';
 3592:             my %locchecked;
 3593:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3594:                 foreach my $loc ('s','b') {
 3595:                     if ($includeloc{$type} eq $loc) {
 3596:                         $locchecked{$loc} = ' checked="checked"';
 3597:                         last;
 3598:                     }
 3599:                 }
 3600:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3601:                               '<input type="text" name="'.$type.'_bcc" '.
 3602:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3603:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3604:                               &mt('Text automatically added to e-mail:').' '.
 3605:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3606:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3607:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3608:                               ('&nbsp;'x2).
 3609:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3610:                               '</span></fieldset>';
 3611:             }
 3612:             $datatable .= '</td></tr>'."\n";
 3613:             $rownum ++;
 3614:         }
 3615:     }
 3616:     if ($position eq 'middle') {
 3617:         my %choices;
 3618:         my $corelink = &core_link_msu();
 3619:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3620:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3621:                                         $corelink);
 3622:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3623:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3624:         my %defaultchecked = ('reporterrors'  => 'on',
 3625:                               'reportupdates' => 'on',
 3626:                               'reportstatus'  => 'on');
 3627:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3628:                                                    \%choices,$rownum);
 3629:         $datatable .= $reports;
 3630:     } elsif ($position eq 'lower') {
 3631:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3632:         my ($threshold,$sysmail,%excluded,%weights);
 3633:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3634:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3635:             $threshold = $lonstatus{'threshold'};
 3636:         } else {
 3637:             $threshold = $defaults->{'threshold'};
 3638:         }
 3639:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3640:             $sysmail = $lonstatus{'sysmail'};
 3641:         } else {
 3642:             $sysmail = $defaults->{'sysmail'};
 3643:         }
 3644:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3645:             foreach my $type ('E','W','N','U') {
 3646:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3647:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3648:                 } else {
 3649:                     $weights{$type} = $defaults->{$type};
 3650:                 }
 3651:             }
 3652:         } else {
 3653:             foreach my $type ('E','W','N','U') {
 3654:                 $weights{$type} = $defaults->{$type};
 3655:             }
 3656:         }
 3657:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3658:             if (@{$lonstatus{'excluded'}} > 0) {
 3659:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3660:             }
 3661:         }
 3662:         $datatable .= '<tr'.$css_class.'>'.
 3663:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3664:                       $titles->{'errorthreshold'}.
 3665:                       '</span></td><td class="LC_left_item">'.
 3666:                       '<input type="text" name="errorthreshold" value="'.
 3667:                       $threshold.'" size="5" /></td></tr>';
 3668:         $rownum ++;
 3669:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3670:         $datatable .= '<tr'.$css_class.'>'.
 3671:                       '<td class="LC_left_item">'.
 3672:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3673:                       '</span></td><td class="LC_left_item"><table><tr>';
 3674:         foreach my $type ('E','W','N','U') {
 3675:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3676:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3677:                           $weights{$type}.'" size="5" /></td>';
 3678:         }
 3679:         $datatable .= '</tr></table></tr>';
 3680:         $rownum ++;
 3681:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3682:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3683:                       $titles->{'errorexcluded'}.'</td>'.
 3684:                       '<td class="LC_left_item"><table>';
 3685:         my $numinrow = 4;
 3686:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3687:         for (my $i=0; $i<@ids; $i++) {
 3688:             my $rem = $i%($numinrow);
 3689:             if ($rem == 0) {
 3690:                 if ($i > 0) {
 3691:                     $datatable .= '</tr>';
 3692:                 }
 3693:                 $datatable .= '<tr>';
 3694:             }
 3695:             my $check;
 3696:             if ($excluded{$ids[$i]}) {
 3697:                 $check = ' checked="checked" ';
 3698:             }
 3699:             $datatable .= '<td class="LC_left_item">'.
 3700:                           '<span class="LC_nobreak"><label>'.
 3701:                           '<input type="checkbox" name="errorexcluded" '.
 3702:                           'value="'.$ids[$i].'"'.$check.' />'.
 3703:                           $ids[$i].'</label></span></td>';
 3704:         }
 3705:         my $colsleft = $numinrow - @ids%($numinrow);
 3706:         if ($colsleft > 1 ) {
 3707:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3708:                           '&nbsp;</td>';
 3709:         } elsif ($colsleft == 1) {
 3710:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3711:         }
 3712:         $datatable .= '</tr></table></td></tr>';
 3713:         $rownum ++;
 3714:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3715:         $datatable .= '<tr'.$css_class.'>'.
 3716:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3717:                       $titles->{'errorsysmail'}.
 3718:                       '</span></td><td class="LC_left_item">'.
 3719:                       '<input type="text" name="errorsysmail" value="'.
 3720:                       $sysmail.'" size="5" /></td></tr>';
 3721:         $rownum ++;
 3722:     } elsif ($position eq 'bottom') {
 3723:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3724:         my (@posstypes,%usertypeshash);
 3725:         if (ref($types) eq 'ARRAY') {
 3726:             @posstypes = @{$types};
 3727:         }
 3728:         if (@posstypes) {
 3729:             if (ref($usertypes) eq 'HASH') {
 3730:                 %usertypeshash = %{$usertypes};
 3731:             }
 3732:             my @overridden;
 3733:             my $numinrow = 4;
 3734:             if (ref($settings) eq 'HASH') {
 3735:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3736:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3737:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3738:                             push(@overridden,$key);
 3739:                             foreach my $item (@contacts) {
 3740:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3741:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3742:                                 }
 3743:                             }
 3744:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3745:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3746:                             $includeloc{'override_'.$key} = '';
 3747:                             $includestr{'override_'.$key} = '';
 3748:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3749:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3750:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3751:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3752:                             }
 3753:                         }
 3754:                     }
 3755:                 }
 3756:             }
 3757:             my $customclass = 'LC_helpdesk_override';
 3758:             my $optionsprefix = 'LC_options_helpdesk_';
 3759: 
 3760:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3761:  
 3762:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3763:                                          $numinrow,$othertitle,'overrides',
 3764:                                          \$rownum,$onclicktypes,$customclass);
 3765:             $rownum ++;
 3766:             $usertypeshash{'default'} = $othertitle;
 3767:             foreach my $status (@posstypes) {
 3768:                 my $css_class;
 3769:                 if ($rownum%2) {
 3770:                     $css_class = 'LC_odd_row ';
 3771:                 }
 3772:                 $css_class .= $customclass;
 3773:                 my $rowid = $optionsprefix.$status;
 3774:                 my $hidden = 1;
 3775:                 my $currstyle = 'display:none';
 3776:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3777:                     $currstyle = 'display:table-row';
 3778:                     $hidden = 0;
 3779:                 }
 3780:                 my $key = 'override_'.$status;
 3781:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3782:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3783:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3784:                                                   \@contacts,$short_titles);
 3785:                 unless ($hidden) {
 3786:                     $rownum ++;
 3787:                 }
 3788:             }
 3789:         }
 3790:     }
 3791:     $$rowtotal += $rownum;
 3792:     return $datatable;
 3793: }
 3794: 
 3795: sub core_link_msu {
 3796:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3797:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3798: }
 3799: 
 3800: sub overridden_helpdesk {
 3801:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3802:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3803:     my $class = 'LC_left_item';
 3804:     if ($css_class) {
 3805:         $css_class = ' class="'.$css_class.'"';
 3806:     }
 3807:     if ($rowid) {
 3808:         $rowid = ' id="'.$rowid.'"';
 3809:     }
 3810:     if ($rowstyle) {
 3811:         $rowstyle = ' style="'.$rowstyle.'"';
 3812:     }
 3813:     my ($output,$description);
 3814:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3815:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3816:               "<td>$description</td>\n".
 3817:               '<td class="'.$class.'" colspan="2">'.
 3818:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3819:               '<span class="LC_nobreak">';
 3820:     if (ref($contacts) eq 'ARRAY') {
 3821:         foreach my $item (@{$contacts}) {
 3822:             my $check;
 3823:             if (ref($checked) eq 'HASH') {
 3824:                $check = $checked->{$item};
 3825:             }
 3826:             my $title;
 3827:             if (ref($short_titles) eq 'HASH') {
 3828:                 $title = $short_titles->{$item}; 
 3829:             }
 3830:             $output .= '<label>'.
 3831:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3832:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3833:         }
 3834:     }
 3835:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3836:                '<input type="text" name="override_'.$type.'_others" '.
 3837:                'value="'.$otheremails.'"  />';
 3838:     my %locchecked;
 3839:     foreach my $loc ('s','b') {
 3840:         if ($includeloc eq $loc) {
 3841:             $locchecked{$loc} = ' checked="checked"';
 3842:             last;
 3843:         }
 3844:     }
 3845:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3846:                '<input type="text" name="override_'.$type.'_bcc" '.
 3847:                'value="'.$bccemails.'"  /></fieldset>'.
 3848:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3849:                &mt('Text automatically added to e-mail:').' '.
 3850:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3851:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3852:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3853:                ('&nbsp;'x2).
 3854:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3855:                '</span></fieldset>'.
 3856:                '</td></tr>'."\n";
 3857:     return $output;
 3858: }
 3859: 
 3860: sub contacts_javascript {
 3861:     return <<"ENDSCRIPT";
 3862: 
 3863: <script type="text/javascript">
 3864: // <![CDATA[
 3865: 
 3866: function screenshotSize(field) {
 3867:     if (document.getElementById('help_screenshotsize')) {
 3868:         if (field.value == 'no') {
 3869:             document.getElementById('help_screenshotsize').style.display="none";
 3870:         } else {
 3871:             document.getElementById('help_screenshotsize').style.display="";
 3872:         }
 3873:     }
 3874:     return;
 3875: }
 3876: 
 3877: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3878:     if (form.elements[checkbox].length != undefined) {
 3879:         var count = 0;
 3880:         if (docount) {
 3881:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3882:                 if (form.elements[checkbox][i].checked) {
 3883:                     count ++;
 3884:                 }
 3885:             }
 3886:         }
 3887:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3888:             var type = form.elements[checkbox][i].value;
 3889:             if (document.getElementById(prefix+type)) {
 3890:                 if (form.elements[checkbox][i].checked) {
 3891:                     document.getElementById(prefix+type).style.display = 'table-row';
 3892:                     if (count % 2 == 1) {
 3893:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3894:                     } else {
 3895:                         document.getElementById(prefix+type).className = target;
 3896:                     }
 3897:                     count ++;
 3898:                 } else {
 3899:                     document.getElementById(prefix+type).style.display = 'none';
 3900:                 }
 3901:             }
 3902:         }
 3903:     }
 3904:     return;
 3905: }
 3906: 
 3907: 
 3908: // ]]>
 3909: </script>
 3910: 
 3911: ENDSCRIPT
 3912: }
 3913: 
 3914: sub print_helpsettings {
 3915:     my ($position,$dom,$settings,$rowtotal) = @_;
 3916:     my $confname = $dom.'-domainconfig';
 3917:     my $formname = 'display';
 3918:     my ($datatable,$itemcount);
 3919:     if ($position eq 'top') {
 3920:         $itemcount = 1;
 3921:         my (%choices,%defaultchecked,@toggles);
 3922:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3923:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3924:                                      &mt('LON-CAPA bug tracker'),600,500));
 3925:         %defaultchecked = ('submitbugs' => 'on');
 3926:         @toggles = ('submitbugs');
 3927:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3928:                                                      \%choices,$itemcount);
 3929:         $$rowtotal ++;
 3930:     } else {
 3931:         my $css_class;
 3932:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3933:         my (%customroles,%ordered,%current);
 3934:         if (ref($settings) eq 'HASH') {
 3935:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3936:                 %current = %{$settings->{'adhoc'}};
 3937:             }
 3938:         }
 3939:         my $count = 0;
 3940:         foreach my $key (sort(keys(%existing))) {
 3941:             if ($key=~/^rolesdef\_(\w+)$/) {
 3942:                 my $rolename = $1;
 3943:                 my (%privs,$order);
 3944:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3945:                 $customroles{$rolename} = \%privs;
 3946:                 if (ref($current{$rolename}) eq 'HASH') {
 3947:                     $order = $current{$rolename}{'order'};
 3948:                 }
 3949:                 if ($order eq '') {
 3950:                     $order = $count;
 3951:                 }
 3952:                 $ordered{$order} = $rolename;
 3953:                 $count++;
 3954:             }
 3955:         }
 3956:         my $maxnum = scalar(keys(%ordered));
 3957:         my @roles_by_num = ();
 3958:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3959:             push(@roles_by_num,$item);
 3960:         }
 3961:         my $context = 'domprefs';
 3962:         my $crstype = 'Course';
 3963:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3964:         my @accesstypes = ('all','dh','da','none');
 3965:         my ($numstatustypes,@jsarray);
 3966:         if (ref($types) eq 'ARRAY') {
 3967:             if (@{$types} > 0) {
 3968:                 $numstatustypes = scalar(@{$types});
 3969:                 push(@accesstypes,'status');
 3970:                 @jsarray = ('bystatus');
 3971:             }
 3972:         }
 3973:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3974:         if (keys(%domhelpdesk)) {
 3975:             push(@accesstypes,('inc','exc'));
 3976:             push(@jsarray,('notinc','notexc'));
 3977:         }
 3978:         my $hiddenstr = join("','",@jsarray);
 3979:         my $context = 'domprefs';
 3980:         my $crstype = 'Course';
 3981:         my $prefix = 'helproles_';
 3982:         my $add_class = 'LC_hidden';
 3983:         foreach my $num (@roles_by_num) {
 3984:             my $role = $ordered{$num};
 3985:             my ($desc,$access,@statuses);
 3986:             if (ref($current{$role}) eq 'HASH') {
 3987:                 $desc = $current{$role}{'desc'};
 3988:                 $access = $current{$role}{'access'};
 3989:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3990:                     @statuses = @{$current{$role}{'insttypes'}};
 3991:                 }
 3992:             }
 3993:             if ($desc eq '') {
 3994:                 $desc = $role;
 3995:             }
 3996:             my $identifier = 'custhelp'.$num;
 3997:             my %full=();
 3998:             my %levels= (
 3999:                          course => {},
 4000:                          domain => {},
 4001:                          system => {},
 4002:                         );
 4003:             my %levelscurrent=(
 4004:                                course => {},
 4005:                                domain => {},
 4006:                                system => {},
 4007:                               );
 4008:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4009:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4010:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4011:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4012:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4013:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4014:             for (my $k=0; $k<=$maxnum; $k++) {
 4015:                 my $vpos = $k+1;
 4016:                 my $selstr;
 4017:                 if ($k == $num) {
 4018:                     $selstr = ' selected="selected" ';
 4019:                 }
 4020:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4021:             }
 4022:             $datatable .= '</select>'.('&nbsp;'x2).
 4023:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4024:                           '</td>'.
 4025:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4026:                           &mt('Name shown to users:').
 4027:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4028:                           '</fieldset>'.
 4029:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4030:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4031:                           '<fieldset>'.
 4032:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4033:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4034:                                                                    \%levelscurrent,$identifier,
 4035:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4036:                           '</fieldset></td>';
 4037:             $itemcount ++;
 4038:         }
 4039:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4040:         my $newcust = 'custhelp'.$count;
 4041:         my (%privs,%levelscurrent);
 4042:         my %full=();
 4043:         my %levels= (
 4044:                      course => {},
 4045:                      domain => {},
 4046:                      system => {},
 4047:                     );
 4048:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4049:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4050:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4051:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4052:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4053:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4054:         for (my $k=0; $k<$maxnum+1; $k++) {
 4055:             my $vpos = $k+1;
 4056:             my $selstr;
 4057:             if ($k == $maxnum) {
 4058:                 $selstr = ' selected="selected" ';
 4059:             }
 4060:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4061:         }
 4062:         $datatable .= '</select>&nbsp;'."\n".
 4063:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4064:                       '</label></span></td>'.
 4065:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4066:                       '<span class="LC_nobreak">'.
 4067:                       &mt('Internal name:').
 4068:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4069:                       '</span>'.('&nbsp;'x4).
 4070:                       '<span class="LC_nobreak">'.
 4071:                       &mt('Name shown to users:').
 4072:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4073:                       '</span></fieldset>'.
 4074:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4075:                                              $usertypes,$types,\%domhelpdesk).
 4076:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4077:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4078:                                                                 \@templateroles,$newcust).
 4079:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4080:                                                                \%levelscurrent,$newcust).
 4081:                       '</fieldset>'.
 4082:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4083:                       '</td></tr>';
 4084:         $count ++;
 4085:         $$rowtotal += $count;
 4086:     }
 4087:     return $datatable;
 4088: }
 4089: 
 4090: sub adhocbutton {
 4091:     my ($prefix,$num,$field,$visibility) = @_;
 4092:     my %lt = &Apache::lonlocal::texthash(
 4093:                                           show => 'Show details',
 4094:                                           hide => 'Hide details',
 4095:                                         );
 4096:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4097:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4098:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4099:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4100: }
 4101: 
 4102: sub helpsettings_javascript {
 4103:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4104:     return unless(ref($roles_by_num) eq 'ARRAY');
 4105:     my %html_js_lt = &Apache::lonlocal::texthash(
 4106:                                           show => 'Show details',
 4107:                                           hide => 'Hide details',
 4108:                                         );
 4109:     &html_escape(\%html_js_lt);
 4110:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4111:     return <<"ENDSCRIPT";
 4112: <script type="text/javascript">
 4113: // <![CDATA[
 4114: 
 4115: function reorderHelpRoles(form,item) {
 4116:     var changedVal;
 4117: $jstext
 4118:     var newpos = 'helproles_${total}_pos';
 4119:     var maxh = 1 + $total;
 4120:     var current = new Array();
 4121:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4122:     if (item == newpos) {
 4123:         changedVal = newitemVal;
 4124:     } else {
 4125:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4126:         current[newitemVal] = newpos;
 4127:     }
 4128:     for (var i=0; i<helproles.length; i++) {
 4129:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4130:         if (elementName != item) {
 4131:             if (form.elements[elementName]) {
 4132:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4133:                 current[currVal] = elementName;
 4134:             }
 4135:         }
 4136:     }
 4137:     var oldVal;
 4138:     for (var j=0; j<maxh; j++) {
 4139:         if (current[j] == undefined) {
 4140:             oldVal = j;
 4141:         }
 4142:     }
 4143:     if (oldVal < changedVal) {
 4144:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4145:            var elementName = current[k];
 4146:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4147:         }
 4148:     } else {
 4149:         for (var k=changedVal; k<oldVal; k++) {
 4150:             var elementName = current[k];
 4151:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4152:         }
 4153:     }
 4154:     return;
 4155: }
 4156: 
 4157: function helpdeskAccess(num) {
 4158:     var curraccess = null;
 4159:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4160:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4161:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4162:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4163:             }
 4164:         }
 4165:     }
 4166:     var shown = Array();
 4167:     var hidden = Array();
 4168:     if (curraccess == 'none') {
 4169:         hidden = Array('$hiddenstr');
 4170:     } else {
 4171:         if (curraccess == 'status') {
 4172:             shown = Array('bystatus');
 4173:             hidden = Array('notinc','notexc');
 4174:         } else {
 4175:             if (curraccess == 'exc') {
 4176:                 shown = Array('notexc');
 4177:                 hidden = Array('notinc','bystatus');
 4178:             }
 4179:             if (curraccess == 'inc') {
 4180:                 shown = Array('notinc');
 4181:                 hidden = Array('notexc','bystatus');
 4182:             }
 4183:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4184:                 hidden = Array('notinc','notexc','bystatus');
 4185:             }
 4186:         }
 4187:     }
 4188:     if (hidden.length > 0) {
 4189:         for (var i=0; i<hidden.length; i++) {
 4190:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4191:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4192:             }
 4193:         }
 4194:     }
 4195:     if (shown.length > 0) {
 4196:         for (var i=0; i<shown.length; i++) {
 4197:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4198:                 if (shown[i] == 'privs') {
 4199:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4200:                 } else {
 4201:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4202:                 }
 4203:             }
 4204:         }
 4205:     }
 4206:     return;
 4207: }
 4208: 
 4209: function toggleHelpdeskItem(num,field) {
 4210:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4211:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4212:             document.getElementById('helproles_'+num+'_'+field).className =
 4213:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4214:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4215:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4216:             }
 4217:         } else {
 4218:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4219:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4220:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4221:             }
 4222:         }
 4223:     }
 4224:     return;
 4225: }
 4226: 
 4227: // ]]>
 4228: </script>
 4229: 
 4230: ENDSCRIPT
 4231: }
 4232: 
 4233: sub helpdeskroles_access {
 4234:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4235:         $usertypes,$types,$domhelpdesk) = @_;
 4236:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4237:     my %lt = &Apache::lonlocal::texthash(
 4238:                     'rou'    => 'Role usage',
 4239:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4240:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4241:                     'dh'     => 'All with domain helpdesk role',
 4242:                     'da'     => 'All with domain helpdesk assistant role',
 4243:                     'none'   => 'None',
 4244:                     'status' => 'Determined based on institutional status',
 4245:                     'inc'    => 'Include all, but exclude specific personnel',
 4246:                     'exc'    => 'Exclude all, but include specific personnel',
 4247:                   );
 4248:     my %usecheck = (
 4249:                      all => ' checked="checked"',
 4250:                    );
 4251:     my %displaydiv = (
 4252:                       status => 'none',
 4253:                       inc    => 'none',
 4254:                       exc    => 'none',
 4255:                       priv   => 'block',
 4256:                      );
 4257:     my $output;
 4258:     if (ref($current) eq 'HASH') {
 4259:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4260:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4261:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4262:                 delete($usecheck{'all'});
 4263:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4264:                     my $access = $1;
 4265:                     $displaydiv{$access} = 'inline';
 4266:                 } elsif ($current->{access} eq 'none') {
 4267:                     $displaydiv{'priv'} = 'none';
 4268:                 }
 4269:             }
 4270:         }
 4271:     }
 4272:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4273:               '<p>'.$lt{'whi'}.'</p>';
 4274:     foreach my $access (@{$accesstypes}) {
 4275:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4276:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4277:                    $lt{$access}.'</label>';
 4278:         if ($access eq 'status') {
 4279:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4280:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4281:                                                                  $othertitle,$usertypes,$types).
 4282:                        '</div>';
 4283:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4284:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4285:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4286:                        '</div>';
 4287:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4288:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4289:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4290:                        '</div>';
 4291:         }
 4292:         $output .= '</p>';
 4293:     }
 4294:     $output .= '</fieldset>';
 4295:     return $output;
 4296: }
 4297: 
 4298: sub radiobutton_prefs {
 4299:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4300:         $additional,$align) = @_;
 4301:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4302:                    (ref($choices) eq 'HASH'));
 4303: 
 4304:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4305: 
 4306:     foreach my $item (@{$toggles}) {
 4307:         if ($defaultchecked->{$item} eq 'on') {
 4308:             $checkedon{$item} = ' checked="checked" ';
 4309:             $checkedoff{$item} = ' ';
 4310:         } elsif ($defaultchecked->{$item} eq 'off') {
 4311:             $checkedoff{$item} = ' checked="checked" ';
 4312:             $checkedon{$item} = ' ';
 4313:         }
 4314:     }
 4315:     if (ref($settings) eq 'HASH') {
 4316:         foreach my $item (@{$toggles}) {
 4317:             if ($settings->{$item} eq '1') {
 4318:                 $checkedon{$item} =  ' checked="checked" ';
 4319:                 $checkedoff{$item} = ' ';
 4320:             } elsif ($settings->{$item} eq '0') {
 4321:                 $checkedoff{$item} =  ' checked="checked" ';
 4322:                 $checkedon{$item} = ' ';
 4323:             }
 4324:         }
 4325:     }
 4326:     if ($onclick) {
 4327:         $onclick = ' onclick="'.$onclick.'"';
 4328:     }
 4329:     foreach my $item (@{$toggles}) {
 4330:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4331:         $datatable .=
 4332:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4333:             '<span class="LC_nobreak">'.$choices->{$item}.
 4334:             '</span></td>';
 4335:         if ($align eq 'left') {
 4336:             $datatable .= '<td class="LC_left_item">';
 4337:         } else {
 4338:             $datatable .= '<td class="LC_right_item">';
 4339:         }
 4340:         $datatable .=
 4341:             '<span class="LC_nobreak">'.
 4342:             '<label><input type="radio" name="'.
 4343:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4344:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4345:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4346:             '</span>'.$additional.
 4347:             '</td>'.
 4348:             '</tr>';
 4349:         $itemcount ++;
 4350:     }
 4351:     return ($datatable,$itemcount);
 4352: }
 4353: 
 4354: sub print_ltitools {
 4355:     my ($dom,$settings,$rowtotal) = @_;
 4356:     my $rownum = 0;
 4357:     my $css_class;
 4358:     my $itemcount = 1;
 4359:     my $maxnum = 0;
 4360:     my %ordered;
 4361:     if (ref($settings) eq 'HASH') {
 4362:         foreach my $item (keys(%{$settings})) {
 4363:             if (ref($settings->{$item}) eq 'HASH') {
 4364:                 my $num = $settings->{$item}{'order'};
 4365:                 $ordered{$num} = $item;
 4366:             }
 4367:         }
 4368:     }
 4369:     my $confname = $dom.'-domainconfig';
 4370:     my $switchserver = &check_switchserver($dom,$confname);
 4371:     my $maxnum = scalar(keys(%ordered));
 4372:     my $datatable;
 4373:     my %lt = &ltitools_names();
 4374:     my @courseroles = ('cc','in','ta','ep','st');
 4375:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4376:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4377:     if (keys(%ordered)) {
 4378:         my @items = sort { $a <=> $b } keys(%ordered);
 4379:         for (my $i=0; $i<@items; $i++) {
 4380:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4381:             my $item = $ordered{$items[$i]};
 4382:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4383:             if (ref($settings->{$item}) eq 'HASH') {
 4384:                 $title = $settings->{$item}->{'title'};
 4385:                 $url = $settings->{$item}->{'url'};
 4386:                 $key = $settings->{$item}->{'key'};
 4387:                 $secret = $settings->{$item}->{'secret'};
 4388:                 $lifetime = $settings->{$item}->{'lifetime'};
 4389:                 my $image = $settings->{$item}->{'image'};
 4390:                 if ($image ne '') {
 4391:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4392:                 }
 4393:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4394:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4395:                 } else {
 4396:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4397:                 }
 4398:             }
 4399:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4400:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4401:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4402:             for (my $k=0; $k<=$maxnum; $k++) {
 4403:                 my $vpos = $k+1;
 4404:                 my $selstr;
 4405:                 if ($k == $i) {
 4406:                     $selstr = ' selected="selected" ';
 4407:                 }
 4408:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4409:             }
 4410:             $datatable .= '</select>'.('&nbsp;'x2).
 4411:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4412:                 &mt('Delete?').'</label></span></td>'.
 4413:                 '<td colspan="2">'.
 4414:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4415:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4416:                 ('&nbsp;'x2).
 4417:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4418:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4419:                 ('&nbsp;'x2).
 4420:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4421:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4422:                 ('&nbsp;'x2).
 4423:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4424:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4425:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4426:                 '<br /><br />'.
 4427:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4428:                 ' value="'.$url.'" /></span>'.
 4429:                 ('&nbsp;'x2).
 4430:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4431:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4432:                 ('&nbsp;'x2).
 4433:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4434:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4435:                 ('&nbsp;'x2).
 4436:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4437:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4438:                 '<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>'.
 4439:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4440:                 '</fieldset>'.
 4441:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4442:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4443:             my %currdisp;
 4444:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4445:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4446:                     $currdisp{'window'} = ' checked="checked"';
 4447:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4448:                     $currdisp{'tab'} = ' checked="checked"';
 4449:                 } else {
 4450:                     $currdisp{'iframe'} = ' checked="checked"';
 4451:                 }
 4452:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4453:                     $currdisp{'width'} = $1;
 4454:                 }
 4455:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4456:                      $currdisp{'height'} = $1;
 4457:                 }
 4458:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4459:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4460:             } else {
 4461:                 $currdisp{'iframe'} = ' checked="checked"';
 4462:             }
 4463:             foreach my $disp ('iframe','tab','window') {
 4464:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4465:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4466:             }
 4467:             $datatable .= ('&nbsp;'x4);
 4468:             foreach my $dimen ('width','height') {
 4469:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4470:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4471:                               ('&nbsp;'x2);
 4472:             }
 4473:             $datatable .= '</span><br />'.
 4474:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4475:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4476:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4477:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4478:                           '</textarea></div><div style=""></div><br />';
 4479:             my %units = (
 4480:                           'passback' => 'days',
 4481:                           'roster'   => 'seconds',
 4482:                         );
 4483:             foreach my $extra ('passback','roster') {
 4484:                 my $validsty = 'none';
 4485:                 my $currvalid;
 4486:                 my $checkedon = '';
 4487:                 my $checkedoff = ' checked="checked"';
 4488:                 if ($settings->{$item}->{$extra}) {
 4489:                     $checkedon = $checkedoff;
 4490:                     $checkedoff = '';
 4491:                     $validsty = 'inline-block';
 4492:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4493:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4494:                     }
 4495:                 }
 4496:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4497:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4498:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4499:                               &mt('No').'</label>'.('&nbsp;'x2).
 4500:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4501:                               &mt('Yes').'</label></span></div>'.
 4502:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4503:                               '<span class="LC_nobreak">'.
 4504:                               &mt("at least [_1] $units{$extra} after launch",
 4505:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4506:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4507:             }
 4508:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4509:             if ($imgsrc) {
 4510:                 $datatable .= $imgsrc.
 4511:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4512:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4513:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4514:             } else {
 4515:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4516:             }
 4517:             if ($switchserver) {
 4518:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4519:             } else {
 4520:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4521:             }
 4522:             $datatable .= '</span></fieldset>';
 4523:             my (%checkedfields,%rolemaps,$userincdom);
 4524:             if (ref($settings->{$item}) eq 'HASH') {
 4525:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4526:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4527:                 }
 4528:                 $userincdom = $settings->{$item}->{'incdom'};
 4529:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4530:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4531:                     $checkedfields{'roles'} = 1;
 4532:                 }
 4533:             }
 4534:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4535:                           '<span class="LC_nobreak">';
 4536:             my $userfieldstyle = 'display:none;';
 4537:             my $seluserdom = '';
 4538:             my $unseluserdom = ' selected="selected"';
 4539:             foreach my $field (@fields) {
 4540:                 my ($checked,$onclick,$id,$spacer);
 4541:                 if ($checkedfields{$field}) {
 4542:                     $checked = ' checked="checked"';
 4543:                 }
 4544:                 if ($field eq 'user') {
 4545:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4546:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4547:                     if ($checked) {
 4548:                         $userfieldstyle = 'display:inline-block';
 4549:                         if ($userincdom) {
 4550:                             $seluserdom = $unseluserdom;
 4551:                             $unseluserdom = '';
 4552:                         }
 4553:                     }
 4554:                 } else {
 4555:                     $spacer = ('&nbsp;' x2);
 4556:                 }
 4557:                 $datatable .= '<label>'.
 4558:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4559:                               $lt{$field}.'</label>'.$spacer;
 4560:             }
 4561:             $datatable .= '</span>';
 4562:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4563:                           '<span class="LC_nobreak"> : '.
 4564:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4565:                           '<option value="">'.&mt('Select').'</option>'.
 4566:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4567:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4568:                           '</select></span></div>';
 4569:             $datatable .= '</fieldset>'.
 4570:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4571:             foreach my $role (@courseroles) {
 4572:                 my ($selected,$selectnone);
 4573:                 if (!$rolemaps{$role}) {
 4574:                     $selectnone = ' selected="selected"';
 4575:                 }
 4576:                 $datatable .= '<td style="text-align: center">'. 
 4577:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4578:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4579:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4580:                 foreach my $ltirole (@ltiroles) {
 4581:                     unless ($selectnone) {
 4582:                         if ($rolemaps{$role} eq $ltirole) {
 4583:                             $selected = ' selected="selected"';
 4584:                         } else {
 4585:                             $selected = '';
 4586:                         }
 4587:                     }
 4588:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4589:                 }
 4590:                 $datatable .= '</select></td>';
 4591:             }
 4592:             $datatable .= '</tr></table></fieldset>';
 4593:             my %courseconfig;
 4594:             if (ref($settings->{$item}) eq 'HASH') {
 4595:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4596:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4597:                 }
 4598:             }
 4599:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4600:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4601:                 my $checked;
 4602:                 if ($courseconfig{$item}) {
 4603:                     $checked = ' checked="checked"';
 4604:                 }
 4605:                 $datatable .= '<label>'.
 4606:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4607:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4608:             }
 4609:             $datatable .= '</span></fieldset>'.
 4610:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4611:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4612:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4613:                 my %custom = %{$settings->{$item}->{'custom'}};
 4614:                 if (keys(%custom) > 0) {
 4615:                     foreach my $key (sort(keys(%custom))) {
 4616:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4617:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4618:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4619:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4620:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4621:                     }
 4622:                 }
 4623:             }
 4624:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4625:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4626:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4627:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4628:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4629:             $itemcount ++;
 4630:         }
 4631:     }
 4632:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4633:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4634:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4635:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4636:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4637:     for (my $k=0; $k<$maxnum+1; $k++) {
 4638:         my $vpos = $k+1;
 4639:         my $selstr;
 4640:         if ($k == $maxnum) {
 4641:             $selstr = ' selected="selected" ';
 4642:         }
 4643:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4644:     }
 4645:     $datatable .= '</select>&nbsp;'."\n".
 4646:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4647:                   '<td colspan="2">'.
 4648:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4649:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4650:                   ('&nbsp;'x2).
 4651:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4652:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4653:                   ('&nbsp;'x2).
 4654:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4655:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4656:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4657:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4658:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4659:                   '<br />'.
 4660:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4661:                   ('&nbsp;'x2).
 4662:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4663:                   ('&nbsp;'x2).
 4664:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4665:                   ('&nbsp;'x2).
 4666:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4667:                   '<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".
 4668:                   '</fieldset>'.
 4669:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4670:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4671:     my %defaultdisp;
 4672:     $defaultdisp{'iframe'} = ' checked="checked"';
 4673:     foreach my $disp ('iframe','tab','window') {
 4674:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4675:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4676:     }
 4677:     $datatable .= ('&nbsp;'x4);
 4678:     foreach my $dimen ('width','height') {
 4679:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4680:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4681:                       ('&nbsp;'x2);
 4682:     }
 4683:     $datatable .= '</span><br />'.
 4684:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4685:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4686:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4687:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4688:                   '</div><div style=""></div><br />';
 4689:     my %units = (
 4690:                   'passback' => 'days',
 4691:                   'roster'   => 'seconds',
 4692:                 );
 4693:     my %defaulttimes = (
 4694:                      'passback' => '7',
 4695:                      'roster'   => '300',
 4696:                    );
 4697:     foreach my $extra ('passback','roster') {
 4698:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4699:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4700:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4701:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4702:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4703:                       &mt('Yes').'</label></span></div>'.
 4704:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4705:                       '<span class="LC_nobreak">'.
 4706:                       &mt("at least [_1] $units{$extra} after launch",
 4707:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4708:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4709:     }
 4710:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4711:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4712:     if ($switchserver) {
 4713:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4714:     } else {
 4715:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4716:     }
 4717:     $datatable .= '</span></fieldset>'.
 4718:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4719:                   '<span class="LC_nobreak">';
 4720:     foreach my $field (@fields) {
 4721:         my ($id,$onclick,$spacer);
 4722:         if ($field eq 'user') {
 4723:             $id = ' id="ltitools_user_field_add"';
 4724:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4725:         } else {
 4726:             $spacer = ('&nbsp;' x2);
 4727:         }
 4728:         $datatable .= '<label>'.
 4729:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4730:                       $lt{$field}.'</label>'.$spacer;
 4731:     }
 4732:     $datatable .= '</span>'.
 4733:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4734:                   '<span class="LC_nobreak"> : '.
 4735:                   '<select name="ltitools_userincdom_add">'.
 4736:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4737:                   '<option value="0">'.&mt('username').'</option>'.
 4738:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4739:                   '</select></span></div></fieldset>';
 4740:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4741:     foreach my $role (@courseroles) {
 4742:         my ($checked,$checkednone);
 4743:         $datatable .= '<td style="text-align: center">'.
 4744:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4745:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4746:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4747:         foreach my $ltirole (@ltiroles) {
 4748:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4749:         }
 4750:         $datatable .= '</select></td>';
 4751:     }
 4752:     $datatable .= '</tr></table></fieldset>'.
 4753:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4754:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4755:         $datatable .= '<label>'.
 4756:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4757:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4758:     }
 4759:     $datatable .= '</span></fieldset>'.
 4760:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4761:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4762:                   '<tr><td><span class="LC_nobreak">'.
 4763:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4764:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4765:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4766:                   '</table></fieldset>'."\n".
 4767:                   '</td>'."\n".
 4768:                   '</tr>'."\n";
 4769:     $itemcount ++;
 4770:     return $datatable;
 4771: }
 4772: 
 4773: sub ltitools_names {
 4774:     my %lt = &Apache::lonlocal::texthash(
 4775:                                           'title'          => 'Title',
 4776:                                           'version'        => 'Version',
 4777:                                           'msgtype'        => 'Message Type',
 4778:                                           'sigmethod'      => 'Signature Method',
 4779:                                           'url'            => 'URL',
 4780:                                           'key'            => 'Key',
 4781:                                           'lifetime'       => 'Nonce lifetime (s)',
 4782:                                           'secret'         => 'Secret',
 4783:                                           'icon'           => 'Icon',   
 4784:                                           'user'           => 'User',
 4785:                                           'fullname'       => 'Full Name',
 4786:                                           'firstname'      => 'First Name',
 4787:                                           'lastname'       => 'Last Name',
 4788:                                           'email'          => 'E-mail',
 4789:                                           'roles'          => 'Role',
 4790:                                           'window'         => 'Window',
 4791:                                           'tab'            => 'Tab',
 4792:                                           'iframe'         => 'iFrame',
 4793:                                           'height'         => 'Height',
 4794:                                           'width'          => 'Width',
 4795:                                           'linktext'       => 'Default Link Text',
 4796:                                           'explanation'    => 'Default Explanation',
 4797:                                           'passback'       => 'Tool can return grades:',
 4798:                                           'roster'         => 'Tool can retrieve roster:',
 4799:                                           'crstarget'      => 'Display target',
 4800:                                           'crslabel'       => 'Course label',
 4801:                                           'crstitle'       => 'Course title', 
 4802:                                           'crslinktext'    => 'Link Text',
 4803:                                           'crsexplanation' => 'Explanation',
 4804:                                           'crsappend'      => 'Provider URL',
 4805:                                         );
 4806:     return %lt;
 4807: }
 4808: 
 4809: sub print_lti {
 4810:     my ($dom,$settings,$rowtotal) = @_;
 4811:     my $itemcount = 1;
 4812:     my $maxnum = 0;
 4813:     my $css_class;
 4814:     my %ordered;
 4815:     if (ref($settings) eq 'HASH') {
 4816:         foreach my $item (keys(%{$settings})) {
 4817:             if (ref($settings->{$item}) eq 'HASH') {
 4818:                 my $num = $settings->{$item}{'order'};
 4819:                 $ordered{$num} = $item;
 4820:             }
 4821:         }
 4822:     }
 4823:     my $maxnum = scalar(keys(%ordered));
 4824:     my $datatable;
 4825:     my %lt = &lti_names();
 4826:     if (keys(%ordered)) {
 4827:         my @items = sort { $a <=> $b } keys(%ordered);
 4828:         for (my $i=0; $i<@items; $i++) {
 4829:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4830:             my $item = $ordered{$items[$i]};
 4831:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 4832:             if (ref($settings->{$item}) eq 'HASH') {
 4833:                 $key = $settings->{$item}->{'key'};
 4834:                 $secret = $settings->{$item}->{'secret'};
 4835:                 $lifetime = $settings->{$item}->{'lifetime'};
 4836:                 $consumer = $settings->{$item}->{'consumer'};
 4837:                 $requser = $settings->{$item}->{'requser'};
 4838:                 $current = $settings->{$item};
 4839:             }
 4840:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 4841:             my %checkedrequser = (
 4842:                                    yes => ' checked="checked"',
 4843:                                    no  => '',
 4844:                                  );
 4845:             if (!$requser) {
 4846:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 4847:                 $checkedrequser{'yes'} = '';
 4848:             }
 4849:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 4850:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4851:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 4852:             for (my $k=0; $k<=$maxnum; $k++) {
 4853:                 my $vpos = $k+1;
 4854:                 my $selstr;
 4855:                 if ($k == $i) {
 4856:                     $selstr = ' selected="selected" ';
 4857:                 }
 4858:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4859:             }
 4860:             $datatable .= '</select>'.('&nbsp;'x2).
 4861:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 4862:                 &mt('Delete?').'</label></span></td>'.
 4863:                 '<td colspan="2">'.
 4864:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4865:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 4866:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 4867:                 ('&nbsp;'x2).
 4868:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 4869:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4870:                 ('&nbsp;'x2).
 4871:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 4872:                 'value="'.$lifetime.'" size="3" /></span>'.
 4873:                 ('&nbsp;'x2).
 4874:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4875:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 4876:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 4877:                 '<br /><br />'.
 4878:                 '<span class="LC_nobreak">'.$lt{'key'}.
 4879:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 4880:                 ('&nbsp;'x2).
 4881:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4882:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 4883:                 '<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>'.
 4884:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 4885:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 4886:             $itemcount ++;
 4887:         }
 4888:     }
 4889:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4890:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 4891:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4892:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 4893:                   '<select name="lti_pos_add"'.$chgstr.'>';
 4894:     for (my $k=0; $k<$maxnum+1; $k++) {
 4895:         my $vpos = $k+1;
 4896:         my $selstr;
 4897:         if ($k == $maxnum) {
 4898:             $selstr = ' selected="selected" ';
 4899:         }
 4900:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4901:     }
 4902:     $datatable .= '</select>&nbsp;'."\n".
 4903:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4904:                   '<td colspan="2">'.
 4905:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4906:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 4907:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 4908:                   ('&nbsp;'x2).
 4909:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 4910:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4911:                   ('&nbsp;'x2).
 4912:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 4913:                   ('&nbsp;'x2).
 4914:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4915:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 4916:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 4917:                   '<br /><br />'.
 4918:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 4919:                   ('&nbsp;'x2).
 4920:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 4921:                   '<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".
 4922:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 4923:                   '</td>'."\n".
 4924:                   '</tr>'."\n";
 4925:     $$rowtotal ++;
 4926:     return $datatable;;
 4927: }
 4928: 
 4929: sub lti_names {
 4930:     my %lt = &Apache::lonlocal::texthash(
 4931:                                           'version'   => 'LTI Version',
 4932:                                           'url'       => 'URL',
 4933:                                           'key'       => 'Key',
 4934:                                           'lifetime'  => 'Nonce lifetime (s)',
 4935:                                           'consumer'  => 'Consumer',
 4936:                                           'secret'    => 'Secret',
 4937:                                           'requser'   => "User's identity sent",
 4938:                                           'email'     => 'Email address',
 4939:                                           'sourcedid' => 'User ID',
 4940:                                           'other'     => 'Other',
 4941:                                           'passback'  => 'Can return grades to Consumer:',
 4942:                                           'roster'    => 'Can retrieve roster from Consumer:',
 4943:                                           'topmenu'   => 'Display LON-CAPA page header',
 4944:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 4945:                                         );
 4946:     return %lt;
 4947: }
 4948: 
 4949: sub lti_options {
 4950:     my ($num,$current,$itemcount,%lt) = @_;
 4951:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield);
 4952:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 4953:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 4954:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 4955:     $checked{'mapcrstype'} = {};
 4956:     $checked{'makeuser'} = {};
 4957:     $checked{'selfenroll'} = {};
 4958:     $checked{'crssec'} = {};
 4959:     $checked{'crssecsrc'} = {};
 4960:     $checked{'lcauth'} = {};
 4961:     $checked{'menuitem'} = {};
 4962:     if ($num eq 'add') {
 4963:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 4964:     }
 4965:     my $userfieldsty = 'none';
 4966:     my $crsfieldsty = 'none';
 4967:     my $crssecfieldsty = 'none';
 4968:     my $secsrcfieldsty = 'none';
 4969:     my $passbacksty = 'none';
 4970:     my $optionsty = 'block';
 4971:     my $lcauthparm;
 4972:     my $lcauthparmstyle = 'display:none';
 4973:     my $lcauthparmtext;
 4974:     my $menusty;
 4975:     my $numinrow = 4;
 4976:     my %menutitles = &ltimenu_titles();
 4977: 
 4978:     if (ref($current) eq 'HASH') {
 4979:         if (!$current->{'requser'}) {
 4980:             $optionsty = 'none';
 4981:         }
 4982:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 4983:             $checked{'mapuser'}{'sourcedid'} = '';
 4984:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 4985:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 4986:             } else {
 4987:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 4988:                 $userfield = $current->{'mapuser'};
 4989:                 $userfieldsty = 'inline-block';
 4990:             }
 4991:         }
 4992:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 4993:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 4994:             if ($current->{'mapcrs'} eq 'context_id') {
 4995:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 4996:             } else {
 4997:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 4998:                 $cidfield = $current->{'mapcrs'};
 4999:                 $crsfieldsty = 'inline-block';
 5000:             }
 5001:         }
 5002:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 5003:             foreach my $type (@{$current->{'mapcrstype'}}) {
 5004:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 5005:             }
 5006:         }
 5007:         if ($current->{'makecrs'}) {
 5008:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 5009:         }
 5010:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 5011:             foreach my $role (@{$current->{'makeuser'}}) {
 5012:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 5013:             }
 5014:         }
 5015:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 5016:             $checked{'lcauth'}{$1} = ' checked="checked"';
 5017:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 5018:                 $lcauthparm = $current->{'lcauthparm'};
 5019:                 $lcauthparmstyle = 'display:table-row'; 
 5020:                 if ($current->{'lcauth'} eq 'localauth') {
 5021:                     $lcauthparmtext = &mt('Local auth argument');
 5022:                 } else {
 5023:                     $lcauthparmtext = &mt('Kerberos domain');
 5024:                 }
 5025:             }
 5026:         }
 5027:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 5028:             foreach my $role (@{$current->{'selfenroll'}}) {
 5029:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5030:             }
 5031:         }
 5032:         if (ref($current->{'maproles'}) eq 'HASH') {
 5033:             %rolemaps = %{$current->{'maproles'}};
 5034:         }
 5035:         if ($current->{'section'} ne '') {
 5036:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 5037:             $crssecfieldsty = 'inline-block';
 5038:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5039:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5040:             } else {
 5041:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5042:                 $crssecsrc = $current->{'section'};
 5043:                 $secsrcfieldsty = 'inline-block';
 5044:             }
 5045:         } else {
 5046:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5047:         }
 5048:         if ($current->{'topmenu'}) {
 5049:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5050:         } else {
 5051:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5052:         }
 5053:         if ($current->{'inlinemenu'}) {
 5054:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5055:         } else {
 5056:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5057:         }
 5058:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5059:             $menusty = 'inline-block';
 5060:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5061:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5062:                     if (exists($menutitles{$item})) {
 5063:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5064:                     }
 5065:                 }
 5066:             }
 5067:         } else {
 5068:             $menusty = 'none';
 5069:         }
 5070:     } else {
 5071:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5072:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5073:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5074:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 5075:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5076:         $menusty = 'inline-block'; 
 5077:     }
 5078:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 5079:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5080:                                official   => 'Official',
 5081:                                unofficial => 'Unofficial',
 5082:                                community  => 'Community',
 5083:                                textbook   => 'Textbook',
 5084:                                placement  => 'Placement Test',
 5085:                                lti        => 'LTI Provider',
 5086:     );
 5087:     my @authtypes = ('internal','krb4','krb5','localauth');
 5088:     my %shortauth = (
 5089:                      internal => 'int',
 5090:                      krb4 => 'krb4',
 5091:                      krb5 => 'krb5',
 5092:                      localauth  => 'loc'
 5093:                     );
 5094:     my %authnames = &authtype_names();
 5095:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5096:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5097:     my @courseroles = ('cc','in','ta','ep','st');
 5098:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5099:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5100:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5101:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5102:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5103:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5104:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5105:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5106:     foreach my $option ('sourcedid','email','other') {
 5107:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5108:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5109:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5110:     }
 5111:     $output .= '</span></div>'.
 5112:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5113:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5114:                'value="'.$userfield.'" /></div></fieldset>'. 
 5115:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5116:     foreach my $ltirole (@lticourseroles) {
 5117:         my ($selected,$selectnone);
 5118:         if ($rolemaps{$ltirole} eq '') {
 5119:             $selectnone = ' selected="selected"';
 5120:         }
 5121:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5122:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5123:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5124:         foreach my $role (@courseroles) {
 5125:             unless ($selectnone) {
 5126:                 if ($rolemaps{$ltirole} eq $role) {
 5127:                     $selected = ' selected="selected"';
 5128:                 } else {
 5129:                     $selected = '';
 5130:                 }
 5131:             }
 5132:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5133:                        &Apache::lonnet::plaintext($role,'Course').
 5134:                        '</option>';
 5135:         }
 5136:         $output .= '</select></td>';
 5137:     }
 5138:     $output .= '</tr></table></fieldset>'.
 5139:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5140:     foreach my $ltirole (@ltiroles) {
 5141:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5142:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 5143:     }
 5144:     $output .= '</fieldset>'.
 5145:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5146:                '<table>'.
 5147:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5148:                '</table>'.
 5149:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5150:                '<td class="LC_left_item">';
 5151:     foreach my $auth ('lti',@authtypes) {
 5152:         my $authtext;
 5153:         if ($auth eq 'lti') {
 5154:             $authtext = &mt('None');
 5155:         } else {
 5156:             $authtext = $authnames{$shortauth{$auth}};
 5157:         }
 5158:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5159:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5160:                    $authtext.'</label></span> &nbsp;';
 5161:     }
 5162:     $output .= '</td></tr>'.
 5163:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5164:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5165:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5166:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5167:                '</table></fieldset>'.
 5168:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5169:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5170:                &mt('Unique course identifier').':&nbsp;';
 5171:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5172:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5173:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5174:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5175:     }
 5176:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5177:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5178:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5179:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5180:     foreach my $type (@coursetypes) {
 5181:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5182:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5183:                    ('&nbsp;'x2);
 5184:     }
 5185:     $output .= '</span></fieldset>'.
 5186:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5187:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5188:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5189:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5190:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5191:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5192:                '</fieldset>'.
 5193:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5194:     foreach my $lticrsrole (@lticourseroles) {
 5195:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5196:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5197:     }
 5198:     $output .= '</fieldset>'.
 5199:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 5200:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5201:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5202:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5203:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5204:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5205:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5206:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5207:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5208:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5209:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5210:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5211:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5212:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5213:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5214:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5215:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5216:     foreach my $extra ('roster','passback') {
 5217:         my $checkedon = '';
 5218:         my $checkedoff = ' checked="checked"';
 5219:         if ($extra eq 'passback') {
 5220:             $pb1p1chk = ' checked="checked"';
 5221:             $pb1p0chk = '';
 5222:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 5223:         } else {
 5224:             $onclickpb = ''; 
 5225:         }
 5226:         if (ref($current) eq 'HASH') {
 5227:             if (($current->{$extra})) {
 5228:                 $checkedon = $checkedoff;
 5229:                 $checkedoff = '';
 5230:                 if ($extra eq 'passback') {
 5231:                     $passbacksty = 'inline-block';
 5232:                 }
 5233:                 if ($current->{'passbackformat'} eq '1.0') {
 5234:                     $pb1p0chk =  ' checked="checked"';
 5235:                     $pb1p1chk = '';
 5236:                 }
 5237:             }
 5238:         }
 5239:         $output .= $lt{$extra}.'&nbsp;'.
 5240:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 5241:                    &mt('No').'</label>'.('&nbsp;'x2).
 5242:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 5243:                    &mt('Yes').'</label><br />';
 5244:     }
 5245:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 5246:                '<span class="LC_nobreak">'.&mt('Grade format').
 5247:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 5248:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 5249:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 5250:                &mt('Outcomes Extension (1.0)').'</label></span></div></fieldset>'.
 5251:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 5252:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 5253:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 5254:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5255:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 5256:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 5257:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5258:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 5259:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 5260:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5261:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 5262:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 5263:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 5264:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 5265:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 5266:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 5267:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 5268:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 5269:                    ('&nbsp;'x2);
 5270:     }
 5271:     $output .= '</span></div></fieldset>';
 5272: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 5273: #
 5274: #    $output .= '</fieldset>'.
 5275: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 5276:     return $output;
 5277: }
 5278: 
 5279: sub ltimenu_titles {
 5280:     return &Apache::lonlocal::texthash(
 5281:                                         fullname    => 'Full name',
 5282:                                         coursetitle => 'Course title',
 5283:                                         role        => 'Role',
 5284:                                         logout      => 'Logout',
 5285:                                         grades      => 'Grades',
 5286:     );
 5287: }
 5288: 
 5289: sub print_coursedefaults {
 5290:     my ($position,$dom,$settings,$rowtotal) = @_;
 5291:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 5292:     my $itemcount = 1;
 5293:     my %choices =  &Apache::lonlocal::texthash (
 5294:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 5295:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 5296:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 5297:         coursecredits        => 'Credits can be specified for courses',
 5298:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 5299:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 5300:         texengine            => 'Default method to display mathematics',
 5301:         postsubmit           => 'Disable submit button/keypress following student submission',
 5302:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 5303:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 5304:     );
 5305:     my %staticdefaults = (
 5306:                            anonsurvey_threshold => 10,
 5307:                            uploadquota          => 500,
 5308:                            postsubmit           => 60,
 5309:                            mysqltables          => 172800,
 5310:                          );
 5311:     if ($position eq 'top') {
 5312:         %defaultchecked = (
 5313:                             'canuse_pdfforms' => 'off',
 5314:                             'uselcmath'       => 'on',
 5315:                             'usejsme'         => 'on',
 5316:                             'canclone'        => 'none',
 5317:                           );
 5318:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 5319:         my $deftex = $Apache::lonnet::deftex;
 5320:         if (ref($settings) eq 'HASH') {
 5321:             if ($settings->{'texengine'}) {
 5322:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 5323:                     $deftex = $settings->{'texengine'};
 5324:                 }
 5325:             }
 5326:         }
 5327:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5328:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 5329:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 5330:                        '</span></td><td class="LC_right_item">'.
 5331:                        '<select name="texengine">'."\n";
 5332:         my %texoptions = (
 5333:                             MathJax  => 'MathJax',
 5334:                             mimetex  => &mt('Convert to Images'),
 5335:                             tth      => &mt('TeX to HTML'),
 5336:                          );
 5337:         foreach my $renderer ('MathJax','mimetex','tth') {
 5338:             my $selected = '';
 5339:             if ($renderer eq $deftex) {
 5340:                 $selected = ' selected="selected"';
 5341:             }
 5342:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 5343:         }
 5344:         $mathdisp .= '</select></td></tr>'."\n";
 5345:         $itemcount ++;
 5346:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 5347:                                                      \%choices,$itemcount);
 5348:         $datatable = $mathdisp.$datatable;
 5349:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5350:         $datatable .=
 5351:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5352:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 5353:             '</span></td><td class="LC_left_item">';
 5354:         my $currcanclone = 'none';
 5355:         my $onclick;
 5356:         my @cloneoptions = ('none','domain');
 5357:         my %clonetitles = (
 5358:                              none     => 'No additional course requesters',
 5359:                              domain   => "Any course requester in course's domain",
 5360:                              instcode => 'Course requests for official courses ...',
 5361:                           );
 5362:         my (%codedefaults,@code_order,@posscodes);
 5363:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 5364:                                                     \@code_order) eq 'ok') {
 5365:             if (@code_order > 0) {
 5366:                 push(@cloneoptions,'instcode');
 5367:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 5368:             }
 5369:         }
 5370:         if (ref($settings) eq 'HASH') {
 5371:             if ($settings->{'canclone'}) {
 5372:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 5373:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 5374:                         if (@code_order > 0) {
 5375:                             $currcanclone = 'instcode';
 5376:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 5377:                         }
 5378:                     }
 5379:                 } elsif ($settings->{'canclone'} eq 'domain') {
 5380:                     $currcanclone = $settings->{'canclone'};
 5381:                 }
 5382:             }
 5383:         }
 5384:         foreach my $option (@cloneoptions) {
 5385:             my ($checked,$additional);
 5386:             if ($currcanclone eq $option) {
 5387:                 $checked = ' checked="checked"';
 5388:             }
 5389:             if ($option eq 'instcode') {
 5390:                 if (@code_order) {
 5391:                     my $show = 'none';
 5392:                     if ($checked) {
 5393:                         $show = 'block';
 5394:                     }
 5395:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 5396:                                   &mt('Institutional codes for new and cloned course have identical:').
 5397:                                   '<br />';
 5398:                     foreach my $item (@code_order) {
 5399:                         my $codechk;
 5400:                         if ($checked) {
 5401:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 5402:                                 $codechk = ' checked="checked"';
 5403:                             }
 5404:                         }
 5405:                         $additional .= '<label>'.
 5406:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 5407:                                        $item.'</label>';
 5408:                     }
 5409:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 5410:                 }
 5411:             }
 5412:             $datatable .=
 5413:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 5414:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 5415:                 '</label>&nbsp;'.$additional.'</span><br />';
 5416:         }
 5417:         $datatable .= '</td>'.
 5418:                       '</tr>';
 5419:         $itemcount ++;
 5420:     } else {
 5421:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5422:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 5423:         my $currusecredits = 0;
 5424:         my $postsubmitclient = 1;
 5425:         my @types = ('official','unofficial','community','textbook','placement');
 5426:         if (ref($settings) eq 'HASH') {
 5427:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 5428:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 5429:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 5430:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 5431:                 }
 5432:             }
 5433:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 5434:                 foreach my $type (@types) {
 5435:                     next if ($type eq 'community');
 5436:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 5437:                     if ($defcredits{$type} ne '') {
 5438:                         $currusecredits = 1;
 5439:                     }
 5440:                 }
 5441:             }
 5442:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 5443:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 5444:                     $postsubmitclient = 0;
 5445:                     foreach my $type (@types) {
 5446:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5447:                     }
 5448:                 } else {
 5449:                     foreach my $type (@types) {
 5450:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 5451:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 5452:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 5453:                             } else {
 5454:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5455:                             }
 5456:                         } else {
 5457:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5458:                         }
 5459:                     }
 5460:                 }
 5461:             } else {
 5462:                 foreach my $type (@types) {
 5463:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5464:                 }
 5465:             }
 5466:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 5467:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 5468:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 5469:                 }
 5470:             } else {
 5471:                 foreach my $type (@types) {
 5472:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 5473:                 }
 5474:             }
 5475:         } else {
 5476:             foreach my $type (@types) {
 5477:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5478:             }
 5479:         }
 5480:         if (!$currdefresponder) {
 5481:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 5482:         } elsif ($currdefresponder < 1) {
 5483:             $currdefresponder = 1;
 5484:         }
 5485:         foreach my $type (@types) {
 5486:             if ($curruploadquota{$type} eq '') {
 5487:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 5488:             }
 5489:         }
 5490:         $datatable .=
 5491:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5492:                 $choices{'anonsurvey_threshold'}.
 5493:                 '</span></td>'.
 5494:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 5495:                 '<input type="text" name="anonsurvey_threshold"'.
 5496:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 5497:                 '</td></tr>'."\n";
 5498:         $itemcount ++;
 5499:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5500:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5501:                       $choices{'uploadquota'}.
 5502:                       '</span></td>'.
 5503:                       '<td style="text-align: right" class="LC_right_item">'.
 5504:                       '<table><tr>';
 5505:         foreach my $type (@types) {
 5506:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5507:                            '<input type="text" name="uploadquota_'.$type.'"'.
 5508:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 5509:         }
 5510:         $datatable .= '</tr></table></td></tr>'."\n";
 5511:         $itemcount ++;
 5512:         my $onclick = "toggleDisplay(this.form,'credits');";
 5513:         my $display = 'none';
 5514:         if ($currusecredits) {
 5515:             $display = 'block';
 5516:         }
 5517:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 5518:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 5519:         foreach my $type (@types) {
 5520:             next if ($type eq 'community');
 5521:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5522:                            '<input type="text" name="'.$type.'_credits"'.
 5523:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 5524:         }
 5525:         $additional .= '</tr></table></div>'."\n";
 5526:         %defaultchecked = ('coursecredits' => 'off');
 5527:         @toggles = ('coursecredits');
 5528:         my $current = {
 5529:                         'coursecredits' => $currusecredits,
 5530:                       };
 5531:         (my $table,$itemcount) =
 5532:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5533:                                \%choices,$itemcount,$onclick,$additional,'left');
 5534:         $datatable .= $table;
 5535:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 5536:         my $display = 'none';
 5537:         if ($postsubmitclient) {
 5538:             $display = 'block';
 5539:         }
 5540:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 5541:                       &mt('Number of seconds submit is disabled').'<br />'.
 5542:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 5543:                       '<table><tr>';
 5544:         foreach my $type (@types) {
 5545:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5546:                            '<input type="text" name="'.$type.'_timeout" value="'.
 5547:                            $deftimeout{$type}.'" size="5" /></td>';
 5548:         }
 5549:         $additional .= '</tr></table></div>'."\n";
 5550:         %defaultchecked = ('postsubmit' => 'on');
 5551:         @toggles = ('postsubmit');
 5552:         $current = {
 5553:                        'postsubmit' => $postsubmitclient,
 5554:                    };
 5555:         ($table,$itemcount) =
 5556:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5557:                                \%choices,$itemcount,$onclick,$additional,'left');
 5558:         $datatable .= $table;
 5559:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5560:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5561:                       $choices{'mysqltables'}.
 5562:                       '</span></td>'.
 5563:                       '<td style="text-align: right" class="LC_right_item">'.
 5564:                       '<table><tr>';
 5565:         foreach my $type (@types) {
 5566:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5567:                            '<input type="text" name="mysqltables_'.$type.'"'.
 5568:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 5569:         }
 5570:         $datatable .= '</tr></table></td></tr>'."\n";
 5571:         $itemcount ++;
 5572: 
 5573:     }
 5574:     $$rowtotal += $itemcount;
 5575:     return $datatable;
 5576: }
 5577: 
 5578: sub print_selfenrollment {
 5579:     my ($position,$dom,$settings,$rowtotal) = @_;
 5580:     my ($css_class,$datatable);
 5581:     my $itemcount = 1;
 5582:     my @types = ('official','unofficial','community','textbook','placement');
 5583:     if (($position eq 'top') || ($position eq 'middle')) {
 5584:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 5585:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 5586:         my @rows;
 5587:         my $key;
 5588:         if ($position eq 'top') {
 5589:             $key = 'admin'; 
 5590:             if (ref($rowsref) eq 'ARRAY') {
 5591:                 @rows = @{$rowsref};
 5592:             }
 5593:         } elsif ($position eq 'middle') {
 5594:             $key = 'default';
 5595:             @rows = ('types','registered','approval','limit');
 5596:         }
 5597:         foreach my $row (@rows) {
 5598:             if (defined($titlesref->{$row})) {
 5599:                 $itemcount ++;
 5600:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5601:                 $datatable .= '<tr'.$css_class.'>'.
 5602:                               '<td>'.$titlesref->{$row}.'</td>'.
 5603:                               '<td class="LC_left_item">'.
 5604:                               '<table><tr>';
 5605:                 my (%current,%currentcap);
 5606:                 if (ref($settings) eq 'HASH') {
 5607:                     if (ref($settings->{$key}) eq 'HASH') {
 5608:                         foreach my $type (@types) {
 5609:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5610:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 5611:                             }
 5612:                             if (($row eq 'limit') && ($key eq 'default')) {
 5613:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5614:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 5615:                                 }
 5616:                             }
 5617:                         }
 5618:                     }
 5619:                 }
 5620:                 my %roles = (
 5621:                              '0' => &Apache::lonnet::plaintext('dc'),
 5622:                             ); 
 5623:             
 5624:                 foreach my $type (@types) {
 5625:                     unless (($row eq 'registered') && ($key eq 'default')) {
 5626:                         $datatable .= '<th>'.&mt($type).'</th>';
 5627:                     }
 5628:                 }
 5629:                 unless (($row eq 'registered') && ($key eq 'default')) {
 5630:                     $datatable .= '</tr><tr>';
 5631:                 }
 5632:                 foreach my $type (@types) {
 5633:                     if ($type eq 'community') {
 5634:                         $roles{'1'} = &mt('Community personnel');
 5635:                     } else {
 5636:                         $roles{'1'} = &mt('Course personnel');
 5637:                     }
 5638:                     $datatable .= '<td style="vertical-align: top">';
 5639:                     if ($position eq 'top') {
 5640:                         my %checked;
 5641:                         if ($current{$type} eq '0') {
 5642:                             $checked{'0'} = ' checked="checked"';
 5643:                         } else {
 5644:                             $checked{'1'} = ' checked="checked"';
 5645:                         }
 5646:                         foreach my $role ('1','0') {
 5647:                             $datatable .= '<span class="LC_nobreak"><label>'.
 5648:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 5649:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 5650:                                           $roles{$role}.'</label></span> ';
 5651:                         }
 5652:                     } else {
 5653:                         if ($row eq 'types') {
 5654:                             my %checked;
 5655:                             if ($current{$type} =~ /^(all|dom)$/) {
 5656:                                 $checked{$1} = ' checked="checked"';
 5657:                             } else {
 5658:                                 $checked{''} = ' checked="checked"';
 5659:                             }
 5660:                             foreach my $val ('','dom','all') {
 5661:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5662:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5663:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5664:                             }
 5665:                         } elsif ($row eq 'registered') {
 5666:                             my %checked;
 5667:                             if ($current{$type} eq '1') {
 5668:                                 $checked{'1'} = ' checked="checked"';
 5669:                             } else {
 5670:                                 $checked{'0'} = ' checked="checked"';
 5671:                             }
 5672:                             foreach my $val ('0','1') {
 5673:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5674:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5675:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5676:                             }
 5677:                         } elsif ($row eq 'approval') {
 5678:                             my %checked;
 5679:                             if ($current{$type} =~ /^([12])$/) {
 5680:                                 $checked{$1} = ' checked="checked"';
 5681:                             } else {
 5682:                                 $checked{'0'} = ' checked="checked"';
 5683:                             }
 5684:                             for my $val (0..2) {
 5685:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5686:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5687:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5688:                             }
 5689:                         } elsif ($row eq 'limit') {
 5690:                             my %checked;
 5691:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 5692:                                 $checked{$1} = ' checked="checked"';
 5693:                             } else {
 5694:                                 $checked{'none'} = ' checked="checked"';
 5695:                             }
 5696:                             my $cap;
 5697:                             if ($currentcap{$type} =~ /^\d+$/) {
 5698:                                 $cap = $currentcap{$type};
 5699:                             }
 5700:                             foreach my $val ('none','allstudents','selfenrolled') {
 5701:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5702:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5703:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5704:                             }
 5705:                             $datatable .= '<br />'.
 5706:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 5707:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 5708:                                           '</span>'; 
 5709:                         }
 5710:                     }
 5711:                     $datatable .= '</td>';
 5712:                 }
 5713:                 $datatable .= '</tr>';
 5714:             }
 5715:             $datatable .= '</table></td></tr>';
 5716:         }
 5717:     } elsif ($position eq 'bottom') {
 5718:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 5719:     }
 5720:     $$rowtotal += $itemcount;
 5721:     return $datatable;
 5722: }
 5723: 
 5724: sub print_validation_rows {
 5725:     my ($caller,$dom,$settings,$rowtotal) = @_;
 5726:     my ($itemsref,$namesref,$fieldsref);
 5727:     if ($caller eq 'selfenroll') { 
 5728:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 5729:     } elsif ($caller eq 'requestcourses') {
 5730:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 5731:     }
 5732:     my %currvalidation;
 5733:     if (ref($settings) eq 'HASH') {
 5734:         if (ref($settings->{'validation'}) eq 'HASH') {
 5735:             %currvalidation = %{$settings->{'validation'}};
 5736:         }
 5737:     }
 5738:     my $datatable;
 5739:     my $itemcount = 0;
 5740:     foreach my $item (@{$itemsref}) {
 5741:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5742:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5743:                       $namesref->{$item}.
 5744:                       '</span></td>'.
 5745:                       '<td class="LC_left_item">';
 5746:         if (($item eq 'url') || ($item eq 'button')) {
 5747:             $datatable .= '<span class="LC_nobreak">'.
 5748:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 5749:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 5750:         } elsif ($item eq 'fields') {
 5751:             my @currfields;
 5752:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 5753:                 @currfields = @{$currvalidation{$item}};
 5754:             }
 5755:             foreach my $field (@{$fieldsref}) {
 5756:                 my $check = '';
 5757:                 if (grep(/^\Q$field\E$/,@currfields)) {
 5758:                     $check = ' checked="checked"';
 5759:                 }
 5760:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5761:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 5762:                               ' value="'.$field.'"'.$check.' />'.$field.
 5763:                               '</label></span> ';
 5764:             }
 5765:         } elsif ($item eq 'markup') {
 5766:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 5767:                            $currvalidation{$item}.
 5768:                               '</textarea>';
 5769:         }
 5770:         $datatable .= '</td></tr>'."\n";
 5771:         if (ref($rowtotal)) {
 5772:             $itemcount ++;
 5773:         }
 5774:     }
 5775:     if ($caller eq 'requestcourses') {
 5776:         my %currhash;
 5777:         if (ref($settings) eq 'HASH') {
 5778:             if (ref($settings->{'validation'}) eq 'HASH') {
 5779:                 if ($settings->{'validation'}{'dc'} ne '') {
 5780:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 5781:                 }
 5782:             }
 5783:         }
 5784:         my $numinrow = 2;
 5785:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 5786:                                                        'validationdc',%currhash);
 5787:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5788:         $datatable .= '<tr'.$css_class.'><td>';
 5789:         if ($numdc > 1) {
 5790:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 5791:         } else {
 5792:             $datatable .=  &mt('Course creation processed as: ');
 5793:         }
 5794:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 5795:         $itemcount ++;
 5796:     }
 5797:     if (ref($rowtotal)) {
 5798:         $$rowtotal += $itemcount;
 5799:     }
 5800:     return $datatable;
 5801: }
 5802: 
 5803: sub print_passwords {
 5804:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 5805:     my ($datatable,$css_class);
 5806:     my $itemcount = 0;
 5807:     my %titles = &Apache::lonlocal::texthash (
 5808:         captcha        => '"Forgot Password" CAPTCHA validation',
 5809:         link           => 'Reset link expiration (hours)',
 5810:         case           => 'Case-sensitive usernames/e-mail',
 5811:         prelink        => 'Information required (form 1)',
 5812:         postlink       => 'Information required (form 2)',
 5813:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 5814:         customtext     => 'Domain specific text (HTML)',
 5815:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 5816:         intauth_check  => 'Check bcrypt cost if authenticated',
 5817:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 5818:         permanent      => 'Permanent e-mail address',
 5819:         critical       => 'Critical notification address',
 5820:         notify         => 'Notification address',
 5821:         min            => 'Minimum password length',
 5822:         max            => 'Maximum password length',
 5823:         chars          => 'Required characters',
 5824:         expire         => 'Password expiration (days)',
 5825:     );
 5826:     if ($position eq 'top') {
 5827:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5828:         my $shownlinklife = 2;
 5829:         my $prelink = 'both';
 5830:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 5831:         if (ref($settings) eq 'HASH') {
 5832:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 5833:                 $shownlinklife = $settings->{resetlink};
 5834:             }
 5835:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 5836:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 5837:             }
 5838:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 5839:                 $prelink = $settings->{resetprelink};
 5840:             }
 5841:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 5842:                 %postlink = %{$settings->{resetpostlink}};
 5843:             }
 5844:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 5845:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 5846:             }
 5847:             if ($settings->{resetremove}) {
 5848:                 $nostdtext = 1;
 5849:             }
 5850:             if ($settings->{resetcustom}) {
 5851:                 $customurl = $settings->{resetcustom};
 5852:             }
 5853:         } else {
 5854:             if (ref($types) eq 'ARRAY') {
 5855:                 foreach my $item (@{$types}) {
 5856:                     $casesens{$item} = 1;
 5857:                     $postlink{$item} = ['username','email'];
 5858:                 }
 5859:             }
 5860:             $casesens{'default'} = 1;
 5861:             $postlink{'default'} = ['username','email'];
 5862:             $prelink = 'both';
 5863:             %emailsrc = (
 5864:                           permanent => 1,
 5865:                           critical  => 1,
 5866:                           notify    => 1,
 5867:             );
 5868:         }
 5869:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 5870:         $itemcount ++;
 5871:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5872:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 5873:                       '<td class="LC_left_item">'.
 5874:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 5875:                       'name="passwords_link" size="3" /></td></tr>';
 5876:         $itemcount ++;
 5877:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5878:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 5879:                       '<td class="LC_left_item">';
 5880:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5881:             foreach my $item (@{$types}) {
 5882:                 my $checkedcase;
 5883:                 if ($casesens{$item}) {
 5884:                     $checkedcase = ' checked="checked"';
 5885:                 }
 5886:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5887:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 5888:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 5889:                               '<span>&nbsp;&nbsp; ';
 5890:             }
 5891:         }
 5892:         my $checkedcase;
 5893:         if ($casesens{'default'}) {
 5894:             $checkedcase = ' checked="checked"';
 5895:         }
 5896:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 5897:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 5898:                       $othertitle.'</label></span></td>';
 5899:         $itemcount ++;
 5900:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5901:         my %checkedpre = (
 5902:                              both => ' checked="checked"',
 5903:                              either => '',
 5904:                          );
 5905:         if ($prelink eq 'either') {
 5906:             $checkedpre{either} = ' checked="checked"';
 5907:             $checkedpre{both} = '';
 5908:         }
 5909:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 5910:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5911:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 5912:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 5913:                       '<span class="LC_nobreak"><label>'.
 5914:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 5915:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 5916:         $itemcount ++;
 5917:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5918:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 5919:                       '<td class="LC_left_item">';
 5920:         my %postlinked;
 5921:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5922:             foreach my $item (@{$types}) {
 5923:                 undef(%postlinked);
 5924:                 $datatable .= '<fieldset style="display: inline-block;">'.
 5925:                               '<legend>'.$usertypes->{$item}.'</legend>';
 5926:                 if (ref($postlink{$item}) eq 'ARRAY') {
 5927:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 5928:                 }
 5929:                 foreach my $field ('email','username') {
 5930:                     my $checked;
 5931:                     if ($postlinked{$field}) {
 5932:                         $checked = ' checked="checked"';
 5933:                     }
 5934:                     $datatable .= '<span class="LC_nobreak"><label>'.
 5935:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 5936:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 5937:                                   '<span>&nbsp;&nbsp; ';
 5938:                 }
 5939:                 $datatable .= '</fieldset>';
 5940:             }
 5941:         }
 5942:         if (ref($postlink{'default'}) eq 'ARRAY') {
 5943:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 5944:         }
 5945:         $datatable .= '<fieldset style="display: inline-block;">'.
 5946:                       '<legend>'.$othertitle.'</legend>';
 5947:         foreach my $field ('email','username') {
 5948:             my $checked;
 5949:             if ($postlinked{$field}) {
 5950:                 $checked = ' checked="checked"';
 5951:             }
 5952:             $datatable .= '<span class="LC_nobreak"><label>'.
 5953:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 5954:                           $field.'"'.$checked.' />'.$field.'</label>'.
 5955:                           '<span>&nbsp;&nbsp; ';
 5956:         }
 5957:         $datatable .= '</fieldset></td></tr>';
 5958:         $itemcount ++;
 5959:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5960:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 5961:                       '<td class="LC_left_item">';
 5962:         foreach my $type ('permanent','critical','notify') {
 5963:             my $checkedemail;
 5964:             if ($emailsrc{$type}) {
 5965:                 $checkedemail = ' checked="checked"';
 5966:             }
 5967:             $datatable .= '<span class="LC_nobreak"><label>'.
 5968:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 5969:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 5970:                           '<span>&nbsp;&nbsp; ';
 5971:         }
 5972:         $datatable .= '</td></tr>';
 5973:         $itemcount ++;
 5974:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5975:         my $switchserver = &check_switchserver($dom,$confname);
 5976:         my ($showstd,$noshowstd);
 5977:         if ($nostdtext) {
 5978:             $noshowstd = ' checked="checked"';
 5979:         } else {
 5980:             $showstd = ' checked="checked"';
 5981:         }
 5982:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 5983:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5984:                       &mt('Retain standard text:').
 5985:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 5986:                       &mt('Yes').'</label>'.'&nbsp;'.
 5987:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 5988:                       &mt('No').'</label></span><br />'.
 5989:                       '<span class="LC_fontsize_small">'.
 5990:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 5991:                       &mt('Include custom text:');
 5992:         if ($customurl) {
 5993:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('Custom text file'),600,500,
 5994:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5995:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 5996:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 5997:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 5998:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 5999:         }
 6000:         if ($switchserver) {
 6001:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 6002:         } else {
 6003:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6004:                          '<input type="file" name="passwords_customfile" /></span>';
 6005:         }
 6006:         $datatable .= '</td></tr>';
 6007:     } elsif ($position eq 'middle') {
 6008:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 6009:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6010:         my %defaults;
 6011:         if (ref($domconf{'defaults'}) eq 'HASH') {
 6012:             %defaults = %{$domconf{'defaults'}};
 6013:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6014:                 $defaults{'intauth_cost'} = 10;
 6015:             }
 6016:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6017:                 $defaults{'intauth_check'} = 0;
 6018:             }
 6019:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6020:                 $defaults{'intauth_switch'} = 0;
 6021:             }
 6022:         } else {
 6023:             %defaults = (
 6024:                           'intauth_cost'   => 10,
 6025:                           'intauth_check'  => 0,
 6026:                           'intauth_switch' => 0,
 6027:                         );
 6028:         }
 6029:         foreach my $item (@items) {
 6030:             if ($itemcount%2) {
 6031:                 $css_class = '';
 6032:             } else {
 6033:                 $css_class = ' class="LC_odd_row" ';
 6034:             }
 6035:             $datatable .= '<tr'.$css_class.'>'.
 6036:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 6037:                           '</span></td><td class="LC_left_item" colspan="3">';
 6038:             if ($item eq 'intauth_switch') {
 6039:                 my @options = (0,1,2);
 6040:                 my %optiondesc = &Apache::lonlocal::texthash (
 6041:                                    0 => 'No',
 6042:                                    1 => 'Yes',
 6043:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6044:                                  );
 6045:                 $datatable .= '<table width="100%">';
 6046:                 foreach my $option (@options) {
 6047:                     my $checked = ' ';
 6048:                     if ($defaults{$item} eq $option) {
 6049:                         $checked = ' checked="checked"';
 6050:                     }
 6051:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6052:                                   '<label><input type="radio" name="'.$item.
 6053:                                   '" value="'.$option.'"'.$checked.' />'.
 6054:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6055:                 }
 6056:                 $datatable .= '</table>';
 6057:             } elsif ($item eq 'intauth_check') {
 6058:                 my @options = (0,1,2);
 6059:                 my %optiondesc = &Apache::lonlocal::texthash (
 6060:                                    0 => 'No',
 6061:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6062:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6063:                                  );
 6064:                 $datatable .= '<table width="100%">';
 6065:                 foreach my $option (@options) {
 6066:                     my $checked = ' ';
 6067:                     my $onclick;
 6068:                     if ($defaults{$item} eq $option) {
 6069:                         $checked = ' checked="checked"';
 6070:                     }
 6071:                     if ($option == 2) {
 6072:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6073:                     }
 6074:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6075:                                   '<label><input type="radio" name="'.$item.
 6076:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6077:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6078:                 }
 6079:                 $datatable .= '</table>';
 6080:             } else {
 6081:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6082:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 6083:             }
 6084:             $datatable .= '</td></tr>';
 6085:             $itemcount ++;
 6086:         }
 6087:     } elsif ($position eq 'lower') {
 6088:         my ($min,$max,%chars,$expire);
 6089:         if (ref($settings) eq 'HASH') {
 6090:             if ($settings->{min}) {
 6091:                 $min = $settings->{min};
 6092:             }
 6093:             if ($settings->{max}) {
 6094:                 $max = $settings->{max};
 6095:             }
 6096:             if (ref($settings->{chars}) eq 'ARRAY') {
 6097:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 6098:             }
 6099:             if ($settings->{expire}) {
 6100:                 $expire = $settings->{expire};
 6101:             }
 6102:         } else {
 6103:             $min = '7';
 6104:         }
 6105:         my %rulenames = &Apache::lonlocal::texthash(
 6106:                                                      uc => 'At least one upper case letter',
 6107:                                                      lc => 'At least one lower case letter',
 6108:                                                      num => 'At least one number',
 6109:                                                      spec => 'At least one non-alphanumeric',
 6110:                                                    );
 6111:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6112:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 6113:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6114:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" />'.
 6115:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no minimum)').'</span>'.
 6116:                       '</span></td></tr>';
 6117:         $itemcount ++;
 6118:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6119:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 6120:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6121:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" />'.
 6122:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 6123:                       '</span></td></tr>';
 6124:         $itemcount ++;
 6125:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6126:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 6127:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 6128:                       '</span></td>';
 6129:         my $numinrow = 2;
 6130:         my @possrules = ('uc','lc','num','spec');
 6131:         $datatable .= '<td class="LC_left_item"><table>';
 6132:         for (my $i=0; $i<@possrules; $i++) {
 6133:             my ($rem,$checked);
 6134:             if ($chars{$possrules[$i]}) {
 6135:                 $checked = ' checked="checked"';
 6136:             }
 6137:             $rem = $i%($numinrow);
 6138:             if ($rem == 0) {
 6139:                 if ($i > 0) {
 6140:                     $datatable .= '</tr>';
 6141:                 }
 6142:                 $datatable .= '<tr>';
 6143:             }
 6144:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 6145:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 6146:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 6147:         }
 6148:         my $rem = @possrules%($numinrow);
 6149:         my $colsleft = $numinrow - $rem;
 6150:         if ($colsleft > 1 ) {
 6151:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6152:                           '&nbsp;</td>';
 6153:         } elsif ($colsleft == 1) {
 6154:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6155:         }
 6156:         $datatable .='</table></td></tr>';
 6157:         $itemcount ++;
 6158:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6159:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 6160:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6161:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" />'.
 6162:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 6163:                       '</span></td></tr>';
 6164:     } else {
 6165:         my $checkedon;
 6166:         my $checkedoff = ' checked="checked"';
 6167:         if (ref($settings) eq 'HASH') {
 6168:             if ($settings->{crsownerchg}) {
 6169:                 $checkedon = $checkedoff;
 6170:                 $checkedoff = '';
 6171:             }
 6172:         }
 6173:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6174:         $datatable .= '<tr '.$css_class.'>'.
 6175:                       '<td>'.
 6176:                       &mt('Requirements').'<ul>'.
 6177:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 6178:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 6179:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 6180:                       '</ul>'.
 6181:                       '</td>'.
 6182:                       '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 6183:                       '<label><input type="radio" name="passwords_crsowner" value="1"'.$checkedon.' />'.&mt('Yes').'</label></span> &nbsp;&nbsp;'.
 6184:                       '<span class="LC_nobreak"><label><input type="radio" name="passwords_crsowner" value="0"'.$checkedoff.' />'.
 6185:                       &mt('No').'</label></span>'.
 6186:                       '</td></tr>';
 6187: 
 6188:     }
 6189:     return $datatable;
 6190: }
 6191: 
 6192: sub print_usersessions {
 6193:     my ($position,$dom,$settings,$rowtotal) = @_;
 6194:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 6195:     my (%by_ip,%by_location,@intdoms,@instdoms);
 6196:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6197: 
 6198:     my @alldoms = &Apache::lonnet::all_domains();
 6199:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 6200:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6201:     my %altids = &id_for_thisdom(%servers);
 6202:     if ($position eq 'top') {
 6203:         if (keys(%serverhomes) > 1) {
 6204:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 6205:             my $curroffloadnow;
 6206:             if (ref($settings) eq 'HASH') {
 6207:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 6208:                     $curroffloadnow = $settings->{'offloadnow'};
 6209:                 }
 6210:             }
 6211:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 6212:         } else {
 6213:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 6214:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 6215:                           '</td></tr>';
 6216:         }
 6217:     } else {
 6218:         my %titles = &usersession_titles();
 6219:         my ($prefix,@types);
 6220:         if ($position eq 'bottom') {
 6221:             $prefix = 'remote';
 6222:             @types = ('version','excludedomain','includedomain');
 6223:         } else {
 6224:             $prefix = 'hosted';
 6225:             @types = ('excludedomain','includedomain');
 6226:         }
 6227:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6228:     }
 6229:     $$rowtotal += $itemcount;
 6230:     return $datatable;
 6231: }
 6232: 
 6233: sub rules_by_location {
 6234:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 6235:     my ($datatable,$itemcount,$css_class);
 6236:     if (keys(%{$by_location}) == 0) {
 6237:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6238:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 6239:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 6240:                      '</td></tr>';
 6241:         $itemcount = 1;
 6242:     } else {
 6243:         $itemcount = 0;
 6244:         my $numinrow = 5;
 6245:         my (%current,%checkedon,%checkedoff);
 6246:         my @locations = sort(keys(%{$by_location}));
 6247:         foreach my $type (@{$types}) {
 6248:             $checkedon{$type} = '';
 6249:             $checkedoff{$type} = ' checked="checked"';
 6250:         }
 6251:         if (ref($settings) eq 'HASH') {
 6252:             if (ref($settings->{$prefix}) eq 'HASH') {
 6253:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 6254:                     $current{$key} = $settings->{$prefix}{$key};
 6255:                     if ($key eq 'version') {
 6256:                         if ($current{$key} ne '') {
 6257:                             $checkedon{$key} = ' checked="checked"';
 6258:                             $checkedoff{$key} = '';
 6259:                         }
 6260:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 6261:                         $checkedon{$key} = ' checked="checked"';
 6262:                         $checkedoff{$key} = '';
 6263:                     }
 6264:                 }
 6265:             }
 6266:         }
 6267:         foreach my $type (@{$types}) {
 6268:             next if ($type ne 'version' && !@locations);
 6269:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6270:             $datatable .= '<tr'.$css_class.'>
 6271:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 6272:                            <span class="LC_nobreak">&nbsp;
 6273:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 6274:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 6275:             if ($type eq 'version') {
 6276:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 6277:                 my $selector = '<select name="'.$prefix.'_version">';
 6278:                 foreach my $version (@lcversions) {
 6279:                     my $selected = '';
 6280:                     if ($current{'version'} eq $version) {
 6281:                         $selected = ' selected="selected"';
 6282:                     }
 6283:                     $selector .= ' <option value="'.$version.'"'.
 6284:                                  $selected.'>'.$version.'</option>';
 6285:                 }
 6286:                 $selector .= '</select> ';
 6287:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 6288:             } else {
 6289:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 6290:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 6291:                              ' />'.('&nbsp;'x2).
 6292:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 6293:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 6294:                              "\n".
 6295:                              '</div><div><table>';
 6296:                 my $rem;
 6297:                 for (my $i=0; $i<@locations; $i++) {
 6298:                     my ($showloc,$value,$checkedtype);
 6299:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 6300:                         my $ip = $by_location->{$locations[$i]}->[0];
 6301:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6302:                             $value = join(':',@{$by_ip->{$ip}});
 6303:                             $showloc = join(', ',@{$by_ip->{$ip}});
 6304:                             if (ref($current{$type}) eq 'ARRAY') {
 6305:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 6306:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 6307:                                         $checkedtype = ' checked="checked"';
 6308:                                         last;
 6309:                                     }
 6310:                                 }
 6311:                             }
 6312:                         }
 6313:                     }
 6314:                     $rem = $i%($numinrow);
 6315:                     if ($rem == 0) {
 6316:                         if ($i > 0) {
 6317:                             $datatable .= '</tr>';
 6318:                         }
 6319:                         $datatable .= '<tr>';
 6320:                     }
 6321:                     $datatable .= '<td class="LC_left_item">'.
 6322:                                   '<span class="LC_nobreak"><label>'.
 6323:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 6324:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 6325:                                   '</label></span></td>';
 6326:                 }
 6327:                 $rem = @locations%($numinrow);
 6328:                 my $colsleft = $numinrow - $rem;
 6329:                 if ($colsleft > 1 ) {
 6330:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6331:                                   '&nbsp;</td>';
 6332:                 } elsif ($colsleft == 1) {
 6333:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6334:                 }
 6335:                 $datatable .= '</tr></table>';
 6336:             }
 6337:             $datatable .= '</td></tr>';
 6338:             $itemcount ++;
 6339:         }
 6340:     }
 6341:     return ($datatable,$itemcount);
 6342: }
 6343: 
 6344: sub print_ssl {
 6345:     my ($position,$dom,$settings,$rowtotal) = @_;
 6346:     my ($css_class,$datatable);
 6347:     my $itemcount = 1;
 6348:     if ($position eq 'top') {
 6349:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6350:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6351:         my $same_institution;
 6352:         if ($intdom ne '') {
 6353:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 6354:             if (ref($internet_names) eq 'ARRAY') {
 6355:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 6356:                     $same_institution = 1;
 6357:                 }
 6358:             }
 6359:         }
 6360:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6361:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 6362:         if ($same_institution) {
 6363:             my %domservers = &Apache::lonnet::get_servers($dom);
 6364:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 6365:         } else {
 6366:             $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.");
 6367:         }
 6368:         $datatable .= '</td></tr>';
 6369:         $itemcount ++;
 6370:     } else {
 6371:         my %titles = &ssl_titles();
 6372:         my (%by_ip,%by_location,@intdoms,@instdoms);
 6373:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6374:         my @alldoms = &Apache::lonnet::all_domains();
 6375:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 6376:         my @domservers = &Apache::lonnet::get_servers($dom);
 6377:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6378:         my %altids = &id_for_thisdom(%servers);
 6379:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 6380:             my $legacy;
 6381:             unless (ref($settings) eq 'HASH') {
 6382:                 my $name;
 6383:                 if ($position eq 'connto') {
 6384:                     $name = 'loncAllowInsecure';
 6385:                 } else {
 6386:                     $name = 'londAllowInsecure';
 6387:                 }
 6388:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 6389:                 my @ids=&Apache::lonnet::current_machine_ids();
 6390:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 6391:                     my %what = (
 6392:                                    $name => 1,
 6393:                                );
 6394:                     my ($result,$returnhash) =
 6395:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 6396:                     if ($result eq 'ok') {
 6397:                         if (ref($returnhash) eq 'HASH') {
 6398:                             $legacy = $returnhash->{$name};
 6399:                         }
 6400:                     }
 6401:                 } else {
 6402:                     $legacy = $Apache::lonnet::perlvar{$name};
 6403:                 }
 6404:             }
 6405:             foreach my $type ('dom','intdom','other') {
 6406:                 my %checked;
 6407:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6408:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 6409:                               '<td class="LC_right_item">';
 6410:                 my $skip; 
 6411:                 if ($type eq 'dom') {
 6412:                     unless (keys(%servers) > 1) {
 6413:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 6414:                         $skip = 1;
 6415:                     }
 6416:                 }
 6417:                 if ($type eq 'intdom') {
 6418:                     unless (@instdoms > 1) {
 6419:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 6420:                         $skip = 1;
 6421:                     } 
 6422:                 } elsif ($type eq 'other') {
 6423:                     if (keys(%by_location) == 0) {
 6424:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 6425:                         $skip = 1;
 6426:                     }
 6427:                 }
 6428:                 unless ($skip) {
 6429:                     $checked{'yes'} = ' checked="checked"'; 
 6430:                     if (ref($settings) eq 'HASH') {
 6431:                         if (ref($settings->{$position}) eq 'HASH') {
 6432:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 6433:                                 $checked{$1} = $checked{'yes'};
 6434:                                 delete($checked{'yes'}); 
 6435:                             }
 6436:                         }
 6437:                     } else {
 6438:                         if ($legacy == 0) {
 6439:                             $checked{'req'} = $checked{'yes'};
 6440:                             delete($checked{'yes'});    
 6441:                         }
 6442:                     }
 6443:                     foreach my $option ('no','yes','req') {
 6444:                         $datatable .= '<span class="LC_nobreak"><label>'.
 6445:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 6446:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 6447:                                       '</label></span>'.('&nbsp;'x2);
 6448:                     }
 6449:                 }
 6450:                 $datatable .= '</td></tr>';
 6451:                 $itemcount ++; 
 6452:             }
 6453:         } else {
 6454:             my $prefix = 'replication';
 6455:             my @types = ('certreq','nocertreq');
 6456:             if (keys(%by_location) == 0) {
 6457:                 $datatable .= '<tr'.$css_class.'><td>'.
 6458:                               &mt('Nothing to set here, as there are no other institutions').
 6459:                               '</td></tr>';
 6460:                 $itemcount ++;
 6461:             } else {
 6462:                 ($datatable,$itemcount) = 
 6463:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6464:             }
 6465:         }
 6466:     }
 6467:     $$rowtotal += $itemcount;
 6468:     return $datatable;
 6469: }
 6470: 
 6471: sub ssl_titles {
 6472:     return &Apache::lonlocal::texthash (
 6473:                dom           => 'LON-CAPA servers/VMs from same domain',
 6474:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 6475:                other         => 'External LON-CAPA servers/VMs',
 6476:                connto        => 'Connections to other servers',
 6477:                connfrom      => 'Connections from other servers',
 6478:                replication   => 'Replicating content to other institutions',
 6479:                certreq       => 'Client certificate required, but specific domains exempt',
 6480:                nocertreq     => 'No client certificate required, except for specific domains',
 6481:                no            => 'SSL not used',
 6482:                yes           => 'SSL Optional (used if available)',
 6483:                req           => 'SSL Required',
 6484:     );
 6485: }
 6486: 
 6487: sub print_trust {
 6488:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 6489:     my ($css_class,$datatable,%checked,%choices);
 6490:     my (%by_ip,%by_location,@intdoms,@instdoms);
 6491:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6492:     my $itemcount = 1;
 6493:     my %titles = &trust_titles();
 6494:     my @types = ('exc','inc');
 6495:     if ($prefix eq 'top') {
 6496:         $prefix = 'content';
 6497:     } elsif ($prefix eq 'bottom') {
 6498:         $prefix = 'msg';
 6499:     }
 6500:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6501:     $$rowtotal += $itemcount;
 6502:     return $datatable;
 6503: }
 6504: 
 6505: sub trust_titles {
 6506:     return &Apache::lonlocal::texthash(
 6507:                content  => "Access to this domain's content by others",
 6508:                shared   => "Access to other domain's content by this domain",
 6509:                enroll   => "Enrollment in this domain's courses by others", 
 6510:                othcoau  => "Co-author roles in this domain for others",
 6511:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 6512:                domroles => "Domain roles in this domain assignable to others",
 6513:                catalog  => "Course Catalog for this domain displayed elsewhere",
 6514:                reqcrs   => "Requests for creation of courses in this domain by others",
 6515:                msg      => "Users in other domains can send messages to this domain",
 6516:                exc      => "Allow all, but exclude specific domains",
 6517:                inc      => "Deny all, but include specific domains",
 6518:            );
 6519: } 
 6520: 
 6521: sub build_location_hashes {
 6522:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 6523:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 6524:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 6525:     my %iphost = &Apache::lonnet::get_iphost();
 6526:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 6527:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 6528:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 6529:         foreach my $id (@{$iphost{$primary_ip}}) {
 6530:             my $intdom = &Apache::lonnet::internet_dom($id);
 6531:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 6532:                 push(@{$intdoms},$intdom);
 6533:             }
 6534:         }
 6535:     }
 6536:     foreach my $ip (keys(%iphost)) {
 6537:         if (ref($iphost{$ip}) eq 'ARRAY') {
 6538:             foreach my $id (@{$iphost{$ip}}) {
 6539:                 my $location = &Apache::lonnet::internet_dom($id);
 6540:                 if ($location) {
 6541:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 6542:                         my $dom = &Apache::lonnet::host_domain($id);
 6543:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 6544:                             push(@{$instdoms},$dom);
 6545:                         }
 6546:                         next;
 6547:                     }
 6548:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6549:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 6550:                             push(@{$by_ip->{$ip}},$location);
 6551:                         }
 6552:                     } else {
 6553:                         $by_ip->{$ip} = [$location];
 6554:                     }
 6555:                 }
 6556:             }
 6557:         }
 6558:     }
 6559:     foreach my $ip (sort(keys(%{$by_ip}))) {
 6560:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6561:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 6562:             my $first = $by_ip->{$ip}->[0];
 6563:             if (ref($by_location->{$first}) eq 'ARRAY') {
 6564:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 6565:                     push(@{$by_location->{$first}},$ip);
 6566:                 }
 6567:             } else {
 6568:                 $by_location->{$first} = [$ip];
 6569:             }
 6570:         }
 6571:     }
 6572:     return;
 6573: }
 6574: 
 6575: sub current_offloads_to {
 6576:     my ($dom,$settings,$servers) = @_;
 6577:     my (%spareid,%otherdomconfigs);
 6578:     if (ref($servers) eq 'HASH') {
 6579:         foreach my $lonhost (sort(keys(%{$servers}))) {
 6580:             my $gotspares;
 6581:             if (ref($settings) eq 'HASH') {
 6582:                 if (ref($settings->{'spares'}) eq 'HASH') {
 6583:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 6584:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 6585:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 6586:                         $gotspares = 1;
 6587:                     }
 6588:                 }
 6589:             }
 6590:             unless ($gotspares) {
 6591:                 my $gotspares;
 6592:                 my $serverhomeID =
 6593:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 6594:                 my $serverhomedom =
 6595:                     &Apache::lonnet::host_domain($serverhomeID);
 6596:                 if ($serverhomedom ne $dom) {
 6597:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 6598:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6599:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6600:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6601:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6602:                                 $gotspares = 1;
 6603:                             }
 6604:                         }
 6605:                     } else {
 6606:                         $otherdomconfigs{$serverhomedom} =
 6607:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 6608:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 6609:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6610:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6611:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 6612:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6613:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6614:                                         $gotspares = 1;
 6615:                                     }
 6616:                                 }
 6617:                             }
 6618:                         }
 6619:                     }
 6620:                 }
 6621:             }
 6622:             unless ($gotspares) {
 6623:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6624:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6625:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6626:                } else {
 6627:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 6628:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 6629:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6630:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6631:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6632:                     } else {
 6633:                         my %what = (
 6634:                              spareid => 1,
 6635:                         );
 6636:                         my ($result,$returnhash) = 
 6637:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 6638:                         if ($result eq 'ok') { 
 6639:                             if (ref($returnhash) eq 'HASH') {
 6640:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 6641:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 6642:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 6643:                                 }
 6644:                             }
 6645:                         }
 6646:                     }
 6647:                 }
 6648:             }
 6649:         }
 6650:     }
 6651:     return %spareid;
 6652: }
 6653: 
 6654: sub spares_row {
 6655:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 6656:     my $css_class;
 6657:     my $numinrow = 4;
 6658:     my $itemcount = 1;
 6659:     my $datatable;
 6660:     my %typetitles = &sparestype_titles();
 6661:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6662:         foreach my $server (sort(keys(%{$servers}))) {
 6663:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 6664:             my ($othercontrol,$serverdom);
 6665:             if ($serverhome ne $server) {
 6666:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 6667:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6668:             } else {
 6669:                 $serverdom = &Apache::lonnet::host_domain($server);
 6670:                 if ($serverdom ne $dom) {
 6671:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6672:                 }
 6673:             }
 6674:             next unless (ref($spareid->{$server}) eq 'HASH');
 6675:             my $checkednow;
 6676:             if (ref($curroffloadnow) eq 'HASH') {
 6677:                 if ($curroffloadnow->{$server}) {
 6678:                     $checkednow = ' checked="checked"';
 6679:                 }
 6680:             }
 6681:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6682:             $datatable .= '<tr'.$css_class.'>
 6683:                            <td rowspan="2">
 6684:                             <span class="LC_nobreak">'.
 6685:                           &mt('[_1] when busy, offloads to:'
 6686:                               ,'<b>'.$server.'</b>').'</span><br />'.
 6687:                           '<span class="LC_nobreak">'."\n".
 6688:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 6689:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 6690:                           "\n";
 6691:             my (%current,%canselect);
 6692:             my @choices = 
 6693:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 6694:             foreach my $type ('primary','default') {
 6695:                 if (ref($spareid->{$server}) eq 'HASH') {
 6696:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 6697:                         my @spares = @{$spareid->{$server}{$type}};
 6698:                         if (@spares > 0) {
 6699:                             if ($othercontrol) {
 6700:                                 $current{$type} = join(', ',@spares);
 6701:                             } else {
 6702:                                 $current{$type} .= '<table>';
 6703:                                 my $numspares = scalar(@spares);
 6704:                                 for (my $i=0;  $i<@spares; $i++) {
 6705:                                     my $rem = $i%($numinrow);
 6706:                                     if ($rem == 0) {
 6707:                                         if ($i > 0) {
 6708:                                             $current{$type} .= '</tr>';
 6709:                                         }
 6710:                                         $current{$type} .= '<tr>';
 6711:                                     }
 6712:                                     $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;'.
 6713:                                                        $spareid->{$server}{$type}[$i].
 6714:                                                        '</label></td>'."\n";
 6715:                                 }
 6716:                                 my $rem = @spares%($numinrow);
 6717:                                 my $colsleft = $numinrow - $rem;
 6718:                                 if ($colsleft > 1 ) {
 6719:                                     $current{$type} .= '<td colspan="'.$colsleft.
 6720:                                                        '" class="LC_left_item">'.
 6721:                                                        '&nbsp;</td>';
 6722:                                 } elsif ($colsleft == 1) {
 6723:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 6724:                                 }
 6725:                                 $current{$type} .= '</tr></table>';
 6726:                             }
 6727:                         }
 6728:                     }
 6729:                     if ($current{$type} eq '') {
 6730:                         $current{$type} = &mt('None specified');
 6731:                     }
 6732:                     if ($othercontrol) {
 6733:                         if ($type eq 'primary') {
 6734:                             $canselect{$type} = $othercontrol;
 6735:                         }
 6736:                     } else {
 6737:                         $canselect{$type} = 
 6738:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 6739:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 6740:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 6741:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 6742:                         if (@choices > 0) {
 6743:                             foreach my $lonhost (@choices) {
 6744:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 6745:                             }
 6746:                         }
 6747:                         $canselect{$type} .= '</select>'."\n";
 6748:                     }
 6749:                 } else {
 6750:                     $current{$type} = &mt('Could not be determined');
 6751:                     if ($type eq 'primary') {
 6752:                         $canselect{$type} =  $othercontrol;
 6753:                     }
 6754:                 }
 6755:                 if ($type eq 'default') {
 6756:                     $datatable .= '<tr'.$css_class.'>';
 6757:                 }
 6758:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 6759:                               '<td>'.$current{$type}.'</td>'."\n".
 6760:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 6761:             }
 6762:             $itemcount ++;
 6763:         }
 6764:     }
 6765:     $$rowtotal += $itemcount;
 6766:     return $datatable;
 6767: }
 6768: 
 6769: sub possible_newspares {
 6770:     my ($server,$currspares,$serverhomes,$altids) = @_;
 6771:     my $serverhostname = &Apache::lonnet::hostname($server);
 6772:     my %excluded;
 6773:     if ($serverhostname ne '') {
 6774:         %excluded = (
 6775:                        $serverhostname => 1,
 6776:                     );
 6777:     }
 6778:     if (ref($currspares) eq 'HASH') {
 6779:         foreach my $type (keys(%{$currspares})) {
 6780:             if (ref($currspares->{$type}) eq 'ARRAY') {
 6781:                 if (@{$currspares->{$type}} > 0) {
 6782:                     foreach my $curr (@{$currspares->{$type}}) {
 6783:                         my $hostname = &Apache::lonnet::hostname($curr);
 6784:                         $excluded{$hostname} = 1;
 6785:                     }
 6786:                 }
 6787:             }
 6788:         }
 6789:     }
 6790:     my @choices;
 6791:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6792:         if (keys(%{$serverhomes}) > 1) {
 6793:             foreach my $name (sort(keys(%{$serverhomes}))) {
 6794:                 unless ($excluded{$name}) {
 6795:                     if (exists($altids->{$serverhomes->{$name}})) {
 6796:                         push(@choices,$altids->{$serverhomes->{$name}});
 6797:                     } else {
 6798:                         push(@choices,$serverhomes->{$name});
 6799:                     }
 6800:                 }
 6801:             }
 6802:         }
 6803:     }
 6804:     return sort(@choices);
 6805: }
 6806: 
 6807: sub print_loadbalancing {
 6808:     my ($dom,$settings,$rowtotal) = @_;
 6809:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6810:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6811:     my $numinrow = 1;
 6812:     my $datatable;
 6813:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6814:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 6815:     if (ref($settings) eq 'HASH') {
 6816:         %existing = %{$settings};
 6817:     }
 6818:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 6819:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 6820:                                   \%currtargets,\%currrules,\%currcookies);
 6821:     } else {
 6822:         return;
 6823:     }
 6824:     my ($othertitle,$usertypes,$types) =
 6825:         &Apache::loncommon::sorted_inst_types($dom);
 6826:     my $rownum = 8;
 6827:     if (ref($types) eq 'ARRAY') {
 6828:         $rownum += scalar(@{$types});
 6829:     }
 6830:     my @css_class = ('LC_odd_row','LC_even_row');
 6831:     my $balnum = 0;
 6832:     my $islast;
 6833:     my (@toshow,$disabledtext);
 6834:     if (keys(%currbalancer) > 0) {
 6835:         @toshow = sort(keys(%currbalancer));
 6836:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 6837:             push(@toshow,'');
 6838:         }
 6839:     } else {
 6840:         @toshow = ('');
 6841:         $disabledtext = &mt('No existing load balancer');
 6842:     }
 6843:     foreach my $lonhost (@toshow) {
 6844:         if ($balnum == scalar(@toshow)-1) {
 6845:             $islast = 1;
 6846:         } else {
 6847:             $islast = 0;
 6848:         }
 6849:         my $cssidx = $balnum%2;
 6850:         my $targets_div_style = 'display: none';
 6851:         my $disabled_div_style = 'display: block';
 6852:         my $homedom_div_style = 'display: none';
 6853:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 6854:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6855:                       '<p>';
 6856:         if ($lonhost eq '') {
 6857:             $datatable .= '<span class="LC_nobreak">';
 6858:             if (keys(%currbalancer) > 0) {
 6859:                 $datatable .= &mt('Add balancer:');
 6860:             } else {
 6861:                 $datatable .= &mt('Enable balancer:');
 6862:             }
 6863:             $datatable .= '&nbsp;'.
 6864:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 6865:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 6866:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 6867:                           '<option value="" selected="selected">'.&mt('None').
 6868:                           '</option>'."\n";
 6869:             foreach my $server (sort(keys(%servers))) {
 6870:                 next if ($currbalancer{$server});
 6871:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 6872:             }
 6873:             $datatable .=
 6874:                 '</select>'."\n".
 6875:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 6876:         } else {
 6877:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 6878:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 6879:                            &mt('Stop balancing').'</label>'.
 6880:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 6881:             $targets_div_style = 'display: block';
 6882:             $disabled_div_style = 'display: none';
 6883:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 6884:                 $homedom_div_style = 'display: block';
 6885:             }
 6886:         }
 6887:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 6888:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 6889:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 6890:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 6891:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 6892:         my @sparestypes = ('primary','default');
 6893:         my %typetitles = &sparestype_titles();
 6894:         my %hostherechecked = (
 6895:                                   no => ' checked="checked"',
 6896:                               );
 6897:         my %balcookiechecked = (
 6898:                                   no => ' checked="checked"', 
 6899:                                );
 6900:         foreach my $sparetype (@sparestypes) {
 6901:             my $targettable;
 6902:             for (my $i=0; $i<$numspares; $i++) {
 6903:                 my $checked;
 6904:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 6905:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6906:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6907:                             $checked = ' checked="checked"';
 6908:                         }
 6909:                     }
 6910:                 }
 6911:                 my ($chkboxval,$disabled);
 6912:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 6913:                     $chkboxval = $spares[$i];
 6914:                 }
 6915:                 if (exists($currbalancer{$spares[$i]})) {
 6916:                     $disabled = ' disabled="disabled"';
 6917:                 }
 6918:                 $targettable .=
 6919:                     '<td><span class="LC_nobreak"><label>'.
 6920:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 6921:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 6922:                     '</span></label></span></td>';
 6923:                 my $rem = $i%($numinrow);
 6924:                 if ($rem == 0) {
 6925:                     if (($i > 0) && ($i < $numspares-1)) {
 6926:                         $targettable .= '</tr>';
 6927:                     }
 6928:                     if ($i < $numspares-1) {
 6929:                         $targettable .= '<tr>';
 6930:                     }
 6931:                 }
 6932:             }
 6933:             if ($targettable ne '') {
 6934:                 my $rem = $numspares%($numinrow);
 6935:                 my $colsleft = $numinrow - $rem;
 6936:                 if ($colsleft > 1 ) {
 6937:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6938:                                     '&nbsp;</td>';
 6939:                 } elsif ($colsleft == 1) {
 6940:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 6941:                 }
 6942:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 6943:                                '<table><tr>'.$targettable.'</tr></table><br />';
 6944:             }
 6945:             $hostherechecked{$sparetype} = '';
 6946:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 6947:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6948:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6949:                         $hostherechecked{$sparetype} = ' checked="checked"';
 6950:                         $hostherechecked{'no'} = '';
 6951:                     }
 6952:                 }
 6953:             }
 6954:         }
 6955:         if ($currcookies{$lonhost}) {
 6956:             %balcookiechecked = (
 6957:                                     yes => ' checked="checked"',
 6958:                                 );
 6959:         }
 6960:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 6961:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 6962:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 6963:         foreach my $sparetype (@sparestypes) {
 6964:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 6965:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 6966:                           '</i></label><br />';
 6967:         }
 6968:         $datatable .= &mt('Use balancer cookie').'<br />'.
 6969:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 6970:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 6971:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 6972:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 6973:                       '</div></td></tr>'.
 6974:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 6975:                                            $othertitle,$usertypes,$types,\%servers,
 6976:                                            \%currbalancer,$lonhost,
 6977:                                            $targets_div_style,$homedom_div_style,
 6978:                                            $css_class[$cssidx],$balnum,$islast);
 6979:         $$rowtotal += $rownum;
 6980:         $balnum ++;
 6981:     }
 6982:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 6983:     return $datatable;
 6984: }
 6985: 
 6986: sub get_loadbalancers_config {
 6987:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 6988:     return unless ((ref($servers) eq 'HASH') &&
 6989:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 6990:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 6991:                    (ref($currcookies) eq 'HASH'));
 6992:     if (keys(%{$existing}) > 0) {
 6993:         my $oldlonhost;
 6994:         foreach my $key (sort(keys(%{$existing}))) {
 6995:             if ($key eq 'lonhost') {
 6996:                 $oldlonhost = $existing->{'lonhost'};
 6997:                 $currbalancer->{$oldlonhost} = 1;
 6998:             } elsif ($key eq 'targets') {
 6999:                 if ($oldlonhost) {
 7000:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 7001:                 }
 7002:             } elsif ($key eq 'rules') {
 7003:                 if ($oldlonhost) {
 7004:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 7005:                 }
 7006:             } elsif (ref($existing->{$key}) eq 'HASH') {
 7007:                 $currbalancer->{$key} = 1;
 7008:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 7009:                 $currrules->{$key} = $existing->{$key}{'rules'};
 7010:                 if ($existing->{$key}{'cookie'}) {
 7011:                     $currcookies->{$key} = 1;
 7012:                 }
 7013:             }
 7014:         }
 7015:     } else {
 7016:         my ($balancerref,$targetsref) =
 7017:                 &Apache::lonnet::get_lonbalancer_config($servers);
 7018:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 7019:             foreach my $server (sort(keys(%{$balancerref}))) {
 7020:                 $currbalancer->{$server} = 1;
 7021:                 $currtargets->{$server} = $targetsref->{$server};
 7022:             }
 7023:         }
 7024:     }
 7025:     return;
 7026: }
 7027: 
 7028: sub loadbalancing_rules {
 7029:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 7030:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 7031:         $css_class,$balnum,$islast) = @_;
 7032:     my $output;
 7033:     my $num = 0;
 7034:     my ($alltypes,$othertypes,$titles) =
 7035:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 7036:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 7037:         foreach my $type (@{$alltypes}) {
 7038:             $num ++;
 7039:             my $current;
 7040:             if (ref($currrules) eq 'HASH') {
 7041:                 $current = $currrules->{$type};
 7042:             }
 7043:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 7044:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 7045:                     $current = '';
 7046:                 }
 7047:             }
 7048:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 7049:                                              $servers,$currbalancer,$lonhost,$dom,
 7050:                                              $targets_div_style,$homedom_div_style,
 7051:                                              $css_class,$balnum,$num,$islast);
 7052:         }
 7053:     }
 7054:     return $output;
 7055: }
 7056: 
 7057: sub loadbalancing_titles {
 7058:     my ($dom,$intdom,$usertypes,$types) = @_;
 7059:     my %othertypes = (
 7060:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 7061:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 7062:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 7063:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 7064:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 7065:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 7066:                      );
 7067:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 7068:     my @available;
 7069:     if (ref($types) eq 'ARRAY') {
 7070:         @available = @{$types};
 7071:     }
 7072:     unless (grep(/^default$/,@available)) {
 7073:         push(@available,'default');
 7074:     }
 7075:     unshift(@alltypes,@available);
 7076:     my %titles;
 7077:     foreach my $type (@alltypes) {
 7078:         if ($type =~ /^_LC_/) {
 7079:             $titles{$type} = $othertypes{$type};
 7080:         } elsif ($type eq 'default') {
 7081:             $titles{$type} = &mt('All users from [_1]',$dom);
 7082:             if (ref($types) eq 'ARRAY') {
 7083:                 if (@{$types} > 0) {
 7084:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 7085:                 }
 7086:             }
 7087:         } elsif (ref($usertypes) eq 'HASH') {
 7088:             $titles{$type} = $usertypes->{$type};
 7089:         }
 7090:     }
 7091:     return (\@alltypes,\%othertypes,\%titles);
 7092: }
 7093: 
 7094: sub loadbalance_rule_row {
 7095:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 7096:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 7097:     my @rulenames;
 7098:     my %ruletitles = &offloadtype_text();
 7099:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 7100:         @rulenames = ('balancer','offloadedto','specific');
 7101:     } else {
 7102:         @rulenames = ('default','homeserver');
 7103:         if ($type eq '_LC_external') {
 7104:             push(@rulenames,'externalbalancer');
 7105:         } else {
 7106:             push(@rulenames,'specific');
 7107:         }
 7108:         push(@rulenames,'none');
 7109:     }
 7110:     my $style = $targets_div_style;
 7111:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 7112:         $style = $homedom_div_style;
 7113:     }
 7114:     my $space;
 7115:     if ($islast && $num == 1) {
 7116:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 7117:     }
 7118:     my $output =
 7119:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 7120:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 7121:         '<td valaign="top">'.$space.
 7122:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 7123:     for (my $i=0; $i<@rulenames; $i++) {
 7124:         my $rule = $rulenames[$i];
 7125:         my ($checked,$extra);
 7126:         if ($rulenames[$i] eq 'default') {
 7127:             $rule = '';
 7128:         }
 7129:         if ($rulenames[$i] eq 'specific') {
 7130:             if (ref($servers) eq 'HASH') {
 7131:                 my $default;
 7132:                 if (($current ne '') && (exists($servers->{$current}))) {
 7133:                     $checked = ' checked="checked"';
 7134:                 }
 7135:                 unless ($checked) {
 7136:                     $default = ' selected="selected"';
 7137:                 }
 7138:                 $extra =
 7139:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 7140:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 7141:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 7142:                     '<option value=""'.$default.'></option>'."\n";
 7143:                 foreach my $server (sort(keys(%{$servers}))) {
 7144:                     if (ref($currbalancer) eq 'HASH') {
 7145:                         next if (exists($currbalancer->{$server}));
 7146:                     }
 7147:                     my $selected;
 7148:                     if ($server eq $current) {
 7149:                         $selected = ' selected="selected"';
 7150:                     }
 7151:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 7152:                 }
 7153:                 $extra .= '</select>';
 7154:             }
 7155:         } elsif ($rule eq $current) {
 7156:             $checked = ' checked="checked"';
 7157:         }
 7158:         $output .= '<span class="LC_nobreak"><label>'.
 7159:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 7160:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 7161:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 7162:                    ')"'.$checked.' />&nbsp;';
 7163:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 7164:             $output .= $ruletitles{'particular'};
 7165:         } else {
 7166:             $output .= $ruletitles{$rulenames[$i]};
 7167:         }
 7168:         $output .= '</label>'.$extra.'</span><br />'."\n";
 7169:     }
 7170:     $output .= '</div></td></tr>'."\n";
 7171:     return $output;
 7172: }
 7173: 
 7174: sub offloadtype_text {
 7175:     my %ruletitles = &Apache::lonlocal::texthash (
 7176:            'default'          => 'Offloads to default destinations',
 7177:            'homeserver'       => "Offloads to user's home server",
 7178:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 7179:            'specific'         => 'Offloads to specific server',
 7180:            'none'             => 'No offload',
 7181:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 7182:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 7183:            'particular'       => 'Session hosted (after re-auth) on server:',
 7184:     );
 7185:     return %ruletitles;
 7186: }
 7187: 
 7188: sub sparestype_titles {
 7189:     my %typestitles = &Apache::lonlocal::texthash (
 7190:                           'primary' => 'primary',
 7191:                           'default' => 'default',
 7192:                       );
 7193:     return %typestitles;
 7194: }
 7195: 
 7196: sub contact_titles {
 7197:     my %titles = &Apache::lonlocal::texthash (
 7198:                    'supportemail'    => 'Support E-mail address',
 7199:                    'adminemail'      => 'Default Server Admin E-mail address',
 7200:                    'errormail'       => 'Error reports to be e-mailed to',
 7201:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 7202:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 7203:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 7204:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 7205:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 7206:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 7207:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 7208:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 7209:                    'errorthreshold'  => 'Error/warning threshold for status e-mail',
 7210:                    'errorsysmail'    => 'Error threshold for e-mail to core group',
 7211:                    'errorweights'    => 'Weights used to compute error count',
 7212:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 7213:                  );
 7214:     my %short_titles = &Apache::lonlocal::texthash (
 7215:                            adminemail   => 'Admin E-mail address',
 7216:                            supportemail => 'Support E-mail',
 7217:                        );   
 7218:     return (\%titles,\%short_titles);
 7219: }
 7220: 
 7221: sub helpform_fields {
 7222:     my %titles =  &Apache::lonlocal::texthash (
 7223:                        'username'   => 'Name',
 7224:                        'user'       => 'Username/domain',
 7225:                        'phone'      => 'Phone',
 7226:                        'cc'         => 'Cc e-mail',
 7227:                        'course'     => 'Course Details',
 7228:                        'section'    => 'Sections',
 7229:                        'screenshot' => 'File upload',
 7230:     );
 7231:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 7232:     my %possoptions = (
 7233:                         username     => ['yes','no','req'],
 7234:                         phone        => ['yes','no','req'],
 7235:                         user         => ['yes','no'],
 7236:                         cc           => ['yes','no'],
 7237:                         course       => ['yes','no'],
 7238:                         section      => ['yes','no'],
 7239:                         screenshot   => ['yes','no'],
 7240:                       );
 7241:     my %fieldoptions = &Apache::lonlocal::texthash (
 7242:                          'yes'  => 'Optional',
 7243:                          'req'  => 'Required',
 7244:                          'no'   => "Not shown",
 7245:     );
 7246:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 7247: }
 7248: 
 7249: sub tool_titles {
 7250:     my %titles = &Apache::lonlocal::texthash (
 7251:                      aboutme    => 'Personal web page',
 7252:                      blog       => 'Blog',
 7253:                      webdav     => 'WebDAV',
 7254:                      portfolio  => 'Portfolio',
 7255:                      official   => 'Official courses (with institutional codes)',
 7256:                      unofficial => 'Unofficial courses',
 7257:                      community  => 'Communities',
 7258:                      textbook   => 'Textbook courses',
 7259:                      placement  => 'Placement tests',
 7260:                  );
 7261:     return %titles;
 7262: }
 7263: 
 7264: sub courserequest_titles {
 7265:     my %titles = &Apache::lonlocal::texthash (
 7266:                                    official   => 'Official',
 7267:                                    unofficial => 'Unofficial',
 7268:                                    community  => 'Communities',
 7269:                                    textbook   => 'Textbook',
 7270:                                    placement  => 'Placement tests',
 7271:                                    lti        => 'LTI Provider',
 7272:                                    norequest  => 'Not allowed',
 7273:                                    approval   => 'Approval by DC',
 7274:                                    validate   => 'With validation',
 7275:                                    autolimit  => 'Numerical limit',
 7276:                                    unlimited  => '(blank for unlimited)',
 7277:                  );
 7278:     return %titles;
 7279: }
 7280: 
 7281: sub authorrequest_titles {
 7282:     my %titles = &Apache::lonlocal::texthash (
 7283:                                    norequest  => 'Not allowed',
 7284:                                    approval   => 'Approval by Dom. Coord.',
 7285:                                    automatic  => 'Automatic approval',
 7286:                  );
 7287:     return %titles;
 7288: }
 7289: 
 7290: sub courserequest_conditions {
 7291:     my %conditions = &Apache::lonlocal::texthash (
 7292:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 7293:        validate   => '(Processing of request subject to institutional validation).',
 7294:                  );
 7295:     return %conditions;
 7296: }
 7297: 
 7298: 
 7299: sub print_usercreation {
 7300:     my ($position,$dom,$settings,$rowtotal) = @_;
 7301:     my $numinrow = 4;
 7302:     my $datatable;
 7303:     if ($position eq 'top') {
 7304:         $$rowtotal ++;
 7305:         my $rowcount = 0;
 7306:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 7307:         if (ref($rules) eq 'HASH') {
 7308:             if (keys(%{$rules}) > 0) {
 7309:                 $datatable .= &user_formats_row('username',$settings,$rules,
 7310:                                                 $ruleorder,$numinrow,$rowcount);
 7311:                 $$rowtotal ++;
 7312:                 $rowcount ++;
 7313:             }
 7314:         }
 7315:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 7316:         if (ref($idrules) eq 'HASH') {
 7317:             if (keys(%{$idrules}) > 0) {
 7318:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 7319:                                                 $idruleorder,$numinrow,$rowcount);
 7320:                 $$rowtotal ++;
 7321:                 $rowcount ++;
 7322:             }
 7323:         }
 7324:         if ($rowcount == 0) {
 7325:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 7326:             $$rowtotal ++;
 7327:             $rowcount ++;
 7328:         }
 7329:     } elsif ($position eq 'middle') {
 7330:         my @creators = ('author','course','requestcrs');
 7331:         my ($rules,$ruleorder) =
 7332:             &Apache::lonnet::inst_userrules($dom,'username');
 7333:         my %lt = &usercreation_types();
 7334:         my %checked;
 7335:         if (ref($settings) eq 'HASH') {
 7336:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 7337:                 foreach my $item (@creators) {
 7338:                     $checked{$item} = $settings->{'cancreate'}{$item};
 7339:                 }
 7340:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 7341:                 foreach my $item (@creators) {
 7342:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 7343:                         $checked{$item} = 'none';
 7344:                     }
 7345:                 }
 7346:             }
 7347:         }
 7348:         my $rownum = 0;
 7349:         foreach my $item (@creators) {
 7350:             $rownum ++;
 7351:             if ($checked{$item} eq '') {
 7352:                 $checked{$item} = 'any';
 7353:             }
 7354:             my $css_class;
 7355:             if ($rownum%2) {
 7356:                 $css_class = '';
 7357:             } else {
 7358:                 $css_class = ' class="LC_odd_row" ';
 7359:             }
 7360:             $datatable .= '<tr'.$css_class.'>'.
 7361:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 7362:                          '</span></td><td style="text-align: right">';
 7363:             my @options = ('any');
 7364:             if (ref($rules) eq 'HASH') {
 7365:                 if (keys(%{$rules}) > 0) {
 7366:                     push(@options,('official','unofficial'));
 7367:                 }
 7368:             }
 7369:             push(@options,'none');
 7370:             foreach my $option (@options) {
 7371:                 my $type = 'radio';
 7372:                 my $check = ' ';
 7373:                 if ($checked{$item} eq $option) {
 7374:                     $check = ' checked="checked" ';
 7375:                 } 
 7376:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7377:                               '<input type="'.$type.'" name="can_createuser_'.
 7378:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 7379:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 7380:             }
 7381:             $datatable .= '</td></tr>';
 7382:         }
 7383:     } else {
 7384:         my @contexts = ('author','course','domain');
 7385:         my @authtypes = ('int','krb4','krb5','loc','lti');
 7386:         my %checked;
 7387:         if (ref($settings) eq 'HASH') {
 7388:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 7389:                 foreach my $item (@contexts) {
 7390:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 7391:                         foreach my $auth (@authtypes) {
 7392:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 7393:                                 $checked{$item}{$auth} = ' checked="checked" ';
 7394:                             }
 7395:                         }
 7396:                     }
 7397:                 }
 7398:             }
 7399:         } else {
 7400:             foreach my $item (@contexts) {
 7401:                 foreach my $auth (@authtypes) {
 7402:                     $checked{$item}{$auth} = ' checked="checked" ';
 7403:                 }
 7404:             }
 7405:         }
 7406:         my %title = &context_names();
 7407:         my %authname = &authtype_names();
 7408:         my $rownum = 0;
 7409:         my $css_class; 
 7410:         foreach my $item (@contexts) {
 7411:             if ($rownum%2) {
 7412:                 $css_class = '';
 7413:             } else {
 7414:                 $css_class = ' class="LC_odd_row" ';
 7415:             }
 7416:             $datatable .=   '<tr'.$css_class.'>'.
 7417:                             '<td>'.$title{$item}.
 7418:                             '</td><td class="LC_left_item">'.
 7419:                             '<span class="LC_nobreak">';
 7420:             foreach my $auth (@authtypes) {
 7421:                 $datatable .= '<label>'. 
 7422:                               '<input type="checkbox" name="'.$item.'_auth" '.
 7423:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 7424:                               $authname{$auth}.'</label>&nbsp;';
 7425:             }
 7426:             $datatable .= '</span></td></tr>';
 7427:             $rownum ++;
 7428:         }
 7429:         $$rowtotal += $rownum;
 7430:     }
 7431:     return $datatable;
 7432: }
 7433: 
 7434: sub print_selfcreation {
 7435:     my ($position,$dom,$settings,$rowtotal) = @_;
 7436:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 7437:         $emaildomain,$datatable);
 7438:     if (ref($settings) eq 'HASH') {
 7439:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 7440:             $createsettings = $settings->{'cancreate'};
 7441:             if (ref($createsettings) eq 'HASH') {
 7442:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 7443:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 7444:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 7445:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 7446:                         @selfcreate = ('email','login','sso');
 7447:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 7448:                         @selfcreate = ($createsettings->{'selfcreate'});
 7449:                     }
 7450:                 }
 7451:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 7452:                     $processing = $createsettings->{'selfcreateprocessing'};
 7453:                 }
 7454:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 7455:                     $emailoptions = $createsettings->{'emailoptions'};
 7456:                 }
 7457:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 7458:                     $emailverified = $createsettings->{'emailverified'};
 7459:                 }
 7460:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 7461:                     $emaildomain = $createsettings->{'emaildomain'};
 7462:                 }
 7463:             }
 7464:         }
 7465:     }
 7466:     my %radiohash;
 7467:     my $numinrow = 4;
 7468:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 7469:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7470:     if ($position eq 'top') {
 7471:         my %choices = &Apache::lonlocal::texthash (
 7472:                                                       cancreate_login      => 'Institutional Login',
 7473:                                                       cancreate_sso        => 'Institutional Single Sign On',
 7474:                                                   );
 7475:         my @toggles = sort(keys(%choices));
 7476:         my %defaultchecked = (
 7477:                                'cancreate_login' => 'off',
 7478:                                'cancreate_sso'   => 'off',
 7479:                              );
 7480:         my ($onclick,$itemcount);
 7481:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7482:                                                      \%choices,$itemcount,$onclick);
 7483:         $$rowtotal += $itemcount;
 7484:         
 7485:         if (ref($usertypes) eq 'HASH') {
 7486:             if (keys(%{$usertypes}) > 0) {
 7487:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 7488:                                              $dom,$numinrow,$othertitle,
 7489:                                              'statustocreate',$rowtotal);
 7490:                 $$rowtotal ++;
 7491:             }
 7492:         }
 7493:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 7494:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7495:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 7496:         my $rem;
 7497:         my $numperrow = 2;
 7498:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 7499:         $datatable .= '<tr'.$css_class.'>'.
 7500:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 7501:                      '<td class="LC_left_item">'."\n".
 7502:                      '<table>'."\n";
 7503:         for (my $i=0; $i<@fields; $i++) {
 7504:             $rem = $i%($numperrow);
 7505:             if ($rem == 0) {
 7506:                 if ($i > 0) {
 7507:                     $datatable .= '</tr>';
 7508:                 }
 7509:                 $datatable .= '<tr>';
 7510:             }
 7511:             my $currval;
 7512:             if (ref($createsettings) eq 'HASH') {
 7513:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 7514:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 7515:                 }
 7516:             }
 7517:             $datatable .= '<td class="LC_left_item">'.
 7518:                           '<span class="LC_nobreak">'.
 7519:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 7520:                           'value="'.$currval.'" size="10" />&nbsp;'.
 7521:                           $fieldtitles{$fields[$i]}.'</span></td>';
 7522:         }
 7523:         my $colsleft = $numperrow - $rem;
 7524:         if ($colsleft > 1 ) {
 7525:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7526:                          '&nbsp;</td>';
 7527:         } elsif ($colsleft == 1) {
 7528:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7529:         }
 7530:         $datatable .= '</tr></table></td></tr>';
 7531:         $$rowtotal ++;
 7532:     } elsif ($position eq 'middle') {
 7533:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 7534:         my @posstypes;
 7535:         if (ref($types) eq 'ARRAY') {
 7536:             @posstypes = @{$types};
 7537:         }
 7538:         unless (grep(/^default$/,@posstypes)) {
 7539:             push(@posstypes,'default');
 7540:         }
 7541:         my %usertypeshash;
 7542:         if (ref($usertypes) eq 'HASH') {
 7543:             %usertypeshash = %{$usertypes};
 7544:         }
 7545:         $usertypeshash{'default'} = $othertitle;
 7546:         foreach my $status (@posstypes) {
 7547:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 7548:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 7549:             $$rowtotal ++;
 7550:         }
 7551:     } else {
 7552:         my %choices = &Apache::lonlocal::texthash (
 7553:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 7554:                                                   );
 7555:         my @toggles = sort(keys(%choices));
 7556:         my %defaultchecked = (
 7557:                                'cancreate_email' => 'off',
 7558:                              );
 7559:         my $customclass = 'LC_selfcreate_email';
 7560:         my $classprefix = 'LC_canmodify_emailusername_';
 7561:         my $optionsprefix = 'LC_options_emailusername_';
 7562:         my $display = 'none';
 7563:         my $rowstyle = 'display:none';
 7564:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 7565:             $display = 'block';
 7566:             $rowstyle = 'display:table-row';
 7567:         }
 7568:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 7569:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7570:                                                      \%choices,$$rowtotal,$onclick);
 7571:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 7572:                                          $rowstyle);
 7573:         $$rowtotal ++;
 7574:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 7575:                                       $rowstyle);
 7576:         $$rowtotal ++;
 7577:         my (@ordered,@posstypes,%usertypeshash);
 7578:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7579:         my ($emailrules,$emailruleorder) =
 7580:             &Apache::lonnet::inst_userrules($dom,'email');
 7581:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7582:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7583:         if (ref($types) eq 'ARRAY') {
 7584:             @posstypes = @{$types};
 7585:         }
 7586:         if (@posstypes) {
 7587:             unless (grep(/^default$/,@posstypes)) {
 7588:                 push(@posstypes,'default');
 7589:             }
 7590:             if (ref($usertypes) eq 'HASH') {
 7591:                 %usertypeshash = %{$usertypes};
 7592:             }
 7593:             my $currassign;
 7594:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 7595:                 $currassign = {
 7596:                                   selfassign => $domdefaults{'inststatusguest'},
 7597:                               };
 7598:                 @ordered = @{$domdefaults{'inststatusguest'}};
 7599:             } else {
 7600:                 $currassign = { selfassign => [] };
 7601:             }
 7602:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 7603:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 7604:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 7605:                                          $numinrow,$othertitle,'selfassign',
 7606:                                          $rowtotal,$onclicktypes,$customclass,
 7607:                                          $rowstyle);
 7608:             $$rowtotal ++;
 7609:             $usertypeshash{'default'} = $othertitle;
 7610:             foreach my $status (@posstypes) {
 7611:                 my $css_class;
 7612:                 if ($$rowtotal%2) {
 7613:                     $css_class = 'LC_odd_row ';
 7614:                 }
 7615:                 $css_class .= $customclass;
 7616:                 my $rowid = $optionsprefix.$status;
 7617:                 my $hidden = 1;
 7618:                 my $currstyle = 'display:none';
 7619:                 if (grep(/^\Q$status\E$/,@ordered)) {
 7620:                     $currstyle = $rowstyle;
 7621:                     $hidden = 0; 
 7622:                 }
 7623:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7624:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 7625:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 7626:                 unless ($hidden) {
 7627:                     $$rowtotal ++;
 7628:                 }
 7629:             }
 7630:         } else {
 7631:             my $css_class;
 7632:             if ($$rowtotal%2) {
 7633:                 $css_class = 'LC_odd_row ';
 7634:             }
 7635:             $css_class .= $customclass;
 7636:             $usertypeshash{'default'} = $othertitle;
 7637:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7638:                                          $emailrules,$emailruleorder,$settings,'default','',
 7639:                                          $othertitle,$css_class,$rowstyle,$intdom);
 7640:             $$rowtotal ++;
 7641:         }
 7642:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7643:         $numinrow = 1;
 7644:         if (@posstypes) {
 7645:             foreach my $status (@posstypes) {
 7646:                 my $rowid = $classprefix.$status;
 7647:                 my $datarowstyle = 'display:none';
 7648:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 7649:                     $datarowstyle = $rowstyle; 
 7650:                 }
 7651:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 7652:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7653:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 7654:                 unless ($datarowstyle eq 'display:none') {
 7655:                     $$rowtotal ++;
 7656:                 }
 7657:             }
 7658:         } else {
 7659:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 7660:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7661:                                                    $infotitles,'',$customclass,$rowstyle);
 7662:         }
 7663:     }
 7664:     return $datatable;
 7665: }
 7666: 
 7667: sub selfcreate_javascript {
 7668:     return <<"ENDSCRIPT";
 7669: 
 7670: <script type="text/javascript">
 7671: // <![CDATA[
 7672: 
 7673: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 7674:     var x = document.getElementsByClassName(target);
 7675:     var insttypes = 0;
 7676:     var insttypeRegExp = new RegExp(prefix);
 7677:     if ((x.length != undefined) && (x.length > 0)) {
 7678:         if (form.elements[radio].length != undefined) {
 7679:             for (var i=0; i<form.elements[radio].length; i++) {
 7680:                 if (form.elements[radio][i].checked) {
 7681:                     if (form.elements[radio][i].value == 1) {
 7682:                         for (var j=0; j<x.length; j++) {
 7683:                             if (x[j].id == 'undefined') {
 7684:                                 x[j].style.display = 'table-row';
 7685:                             } else if (insttypeRegExp.test(x[j].id)) {
 7686:                                 insttypes ++;
 7687:                             } else {
 7688:                                 x[j].style.display = 'table-row';
 7689:                             }
 7690:                         }
 7691:                     } else {
 7692:                         for (var j=0; j<x.length; j++) {
 7693:                             x[j].style.display = 'none';
 7694:                         }
 7695:                     }
 7696:                     break;
 7697:                 }
 7698:             }
 7699:             if (insttypes > 0) {
 7700:                 toggleDataRow(form,checkbox,target,altprefix);
 7701:                 toggleDataRow(form,checkbox,target,prefix,1);
 7702:             }
 7703:         }
 7704:     }
 7705:     return;
 7706: }
 7707: 
 7708: function toggleDataRow(form,checkbox,target,prefix,docount) {
 7709:     if (form.elements[checkbox].length != undefined) {
 7710:         var count = 0;
 7711:         if (docount) {
 7712:             for (var i=0; i<form.elements[checkbox].length; i++) {
 7713:                 if (form.elements[checkbox][i].checked) {
 7714:                     count ++;
 7715:                 }
 7716:             }
 7717:         }
 7718:         for (var i=0; i<form.elements[checkbox].length; i++) {
 7719:             var type = form.elements[checkbox][i].value;
 7720:             if (document.getElementById(prefix+type)) {
 7721:                 if (form.elements[checkbox][i].checked) {
 7722:                     document.getElementById(prefix+type).style.display = 'table-row';
 7723:                     if (count % 2 == 1) {
 7724:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 7725:                     } else {
 7726:                         document.getElementById(prefix+type).className = target;
 7727:                     }
 7728:                     count ++;
 7729:                 } else {
 7730:                     document.getElementById(prefix+type).style.display = 'none';
 7731:                 }
 7732:             }
 7733:         }
 7734:     }
 7735:     return;
 7736: }
 7737: 
 7738: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 7739:     var caller = radio+'_'+status;
 7740:     if (form.elements[caller].length != undefined) {
 7741:         for (var i=0; i<form.elements[caller].length; i++) {
 7742:             if (form.elements[caller][i].checked) {
 7743:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 7744:                     var curr = form.elements[caller][i].value;
 7745:                     if (prefix) {
 7746:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 7747:                     }
 7748:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 7749:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 7750:                     if (curr == 'custom') {
 7751:                         if (prefix) { 
 7752:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 7753:                         }
 7754:                     } else if (curr == 'inst') {
 7755:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 7756:                     } else if (curr == 'noninst') {
 7757:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 7758:                     }
 7759:                     break;
 7760:                 }
 7761:             }
 7762:         }
 7763:     }
 7764: }
 7765: 
 7766: // ]]>
 7767: </script>
 7768: 
 7769: ENDSCRIPT
 7770: }
 7771: 
 7772: sub noninst_users {
 7773:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 7774:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 7775:     my $class = 'LC_left_item';
 7776:     if ($css_class) {
 7777:         $css_class = ' class="'.$css_class.'"'; 
 7778:     }
 7779:     if ($rowid) {
 7780:         $rowid = ' id="'.$rowid.'"';
 7781:     }
 7782:     if ($rowstyle) {
 7783:         $rowstyle = ' style="'.$rowstyle.'"';
 7784:     }
 7785:     my ($output,$description);
 7786:     if ($type eq 'default') {
 7787:         $description = &mt('Requests for: [_1]',$typetitle);
 7788:     } else {
 7789:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 7790:     }
 7791:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 7792:               "<td>$description</td>\n".           
 7793:               '<td class="'.$class.'" colspan="2">'.
 7794:               '<table><tr>';
 7795:     my %headers = &Apache::lonlocal::texthash( 
 7796:               approve  => 'Processing',
 7797:               email    => 'E-mail',
 7798:               username => 'Username',
 7799:     );
 7800:     foreach my $item ('approve','email','username') {
 7801:         $output .= '<th>'.$headers{$item}.'</th>';
 7802:     }
 7803:     $output .= '</tr><tr>';
 7804:     foreach my $item ('approve','email','username') {
 7805:         $output .= '<td style="vertical-align: top">';
 7806:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 7807:         if ($item eq 'approve') {
 7808:             %choices = &Apache::lonlocal::texthash (
 7809:                                                      automatic => 'Automatically approved',
 7810:                                                      approval  => 'Queued for approval',
 7811:                                                    );
 7812:             @options = ('automatic','approval');
 7813:             $hashref = $processing;
 7814:             $defoption = 'automatic';
 7815:             $name = 'cancreate_emailprocess_'.$type;
 7816:         } elsif ($item eq 'email') {
 7817:             %choices = &Apache::lonlocal::texthash (
 7818:                                                      any     => 'Any e-mail',
 7819:                                                      inst    => 'Institutional only',
 7820:                                                      noninst => 'Non-institutional only',
 7821:                                                      custom  => 'Custom restrictions',
 7822:                                                    );
 7823:             @options = ('any','inst','noninst');
 7824:             my $showcustom;
 7825:             if (ref($emailrules) eq 'HASH') {
 7826:                 if (keys(%{$emailrules}) > 0) {
 7827:                     push(@options,'custom');
 7828:                     $showcustom = 'cancreate_emailrule';
 7829:                     if (ref($settings) eq 'HASH') {
 7830:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 7831:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 7832:                                 if (exists($emailrules->{$rule})) {
 7833:                                     $hascustom ++;
 7834:                                 }
 7835:                             }
 7836:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 7837:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 7838:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 7839:                                     if (exists($emailrules->{$rule})) {
 7840:                                         $hascustom ++;
 7841:                                     }
 7842:                                 }
 7843:                             }
 7844:                         }
 7845:                     }
 7846:                 }
 7847:             }
 7848:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 7849:                                                      "'cancreate_emaildomain','$type'".');"';
 7850:             $hashref = $emailoptions;
 7851:             $defoption = 'any';
 7852:             $name = 'cancreate_emailoptions_'.$type;
 7853:         } elsif ($item eq 'username') {
 7854:             %choices = &Apache::lonlocal::texthash (
 7855:                                                      all    => 'Same as e-mail',
 7856:                                                      first  => 'Omit @domain',
 7857:                                                      free   => 'Free to choose',
 7858:                                                    );
 7859:             @options = ('all','first','free');
 7860:             $hashref = $emailverified;
 7861:             $defoption = 'all';
 7862:             $name = 'cancreate_usernameoptions_'.$type;
 7863:         }
 7864:         foreach my $option (@options) {
 7865:             my $checked;
 7866:             if (ref($hashref) eq 'HASH') {
 7867:                 if ($type eq '') {
 7868:                     if (!exists($hashref->{'default'})) {
 7869:                         if ($option eq $defoption) {
 7870:                             $checked = ' checked="checked"';
 7871:                         }
 7872:                     } else {
 7873:                         if ($hashref->{'default'} eq $option) {
 7874:                             $checked = ' checked="checked"';
 7875:                         }
 7876:                     }
 7877:                 } else {
 7878:                     if (!exists($hashref->{$type})) {
 7879:                         if ($option eq $defoption) {
 7880:                             $checked = ' checked="checked"';
 7881:                         }
 7882:                     } else {
 7883:                         if ($hashref->{$type} eq $option) {
 7884:                             $checked = ' checked="checked"';
 7885:                         }
 7886:                     }
 7887:                 }
 7888:             } elsif (($item eq 'email') && ($hascustom)) {
 7889:                 if ($option eq 'custom') {
 7890:                     $checked = ' checked="checked"';
 7891:                 }
 7892:             } elsif ($option eq $defoption) {
 7893:                 $checked = ' checked="checked"';
 7894:             }
 7895:             $output .= '<span class="LC_nobreak"><label>'.
 7896:                        '<input type="radio" name="'.$name.'"'.
 7897:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 7898:                        $choices{$option}.'</label></span><br />';
 7899:             if ($item eq 'email') {
 7900:                 if ($option eq 'custom') {
 7901:                     my $id = 'cancreate_emailrule_'.$type;
 7902:                     my $display = 'none';
 7903:                     if ($checked) {
 7904:                         $display = 'inline';
 7905:                     }
 7906:                     my $numinrow = 2;
 7907:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 7908:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 7909:                                &user_formats_row('email',$settings,$emailrules,
 7910:                                                  $emailruleorder,$numinrow,'',$type);
 7911:                               '</table></fieldset>';
 7912:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 7913:                     my %text = &Apache::lonlocal::texthash (
 7914:                                                              inst    => 'must end:',
 7915:                                                              noninst => 'cannot end:',
 7916:                                                            );
 7917:                     my $value;
 7918:                     if (ref($emaildomain) eq 'HASH') {
 7919:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 7920:                             $value = $emaildomain->{$type}->{$option}; 
 7921:                         }
 7922:                     }
 7923:                     if ($value eq '') {
 7924:                         $value = '@'.$intdom;
 7925:                     }
 7926:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 7927:                     my $display = 'none';
 7928:                     if ($checked) {
 7929:                         $display = 'inline';
 7930:                     }
 7931:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 7932:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 7933:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 7934:                                '</div>';
 7935:                 }
 7936:             }
 7937:         }
 7938:         $output .= '</td>'."\n";
 7939:     }
 7940:     $output .= "</tr></table></td></tr>\n";
 7941:     return $output;
 7942: }
 7943: 
 7944: sub captcha_choice {
 7945:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 7946:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 7947:         $vertext,$currver);
 7948:     my %lt = &captcha_phrases();
 7949:     $keyentry = 'hidden';
 7950:     my $colspan=2;
 7951:     if ($context eq 'cancreate') {
 7952:         $rowname = &mt('CAPTCHA validation');
 7953:     } elsif ($context eq 'login') {
 7954:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 7955:     } elsif ($context eq 'passwords') {
 7956:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 7957:         $colspan=1;
 7958:     }
 7959:     if (ref($settings) eq 'HASH') {
 7960:         if ($settings->{'captcha'}) {
 7961:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 7962:         } else {
 7963:             $checked{'original'} = ' checked="checked"';
 7964:         }
 7965:         if ($settings->{'captcha'} eq 'recaptcha') {
 7966:             $pubtext = $lt{'pub'};
 7967:             $privtext = $lt{'priv'};
 7968:             $keyentry = 'text';
 7969:             $vertext = $lt{'ver'};
 7970:             $currver = $settings->{'recaptchaversion'};
 7971:             if ($currver ne '2') {
 7972:                 $currver = 1;
 7973:             }
 7974:         }
 7975:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 7976:             $currpub = $settings->{'recaptchakeys'}{'public'};
 7977:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 7978:         }
 7979:     } else {
 7980:         $checked{'original'} = ' checked="checked"';
 7981:     }
 7982:     my $css_class;
 7983:     if ($itemcount%2) {
 7984:         $css_class = 'LC_odd_row';
 7985:     }
 7986:     if ($customcss) {
 7987:         $css_class .= " $customcss";
 7988:     }
 7989:     $css_class =~ s/^\s+//;
 7990:     if ($css_class) {
 7991:         $css_class = ' class="'.$css_class.'"';
 7992:     }
 7993:     if ($rowstyle) {
 7994:         $css_class .= ' style="'.$rowstyle.'"';
 7995:     }
 7996:     my $output = '<tr'.$css_class.'>'.
 7997:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 7998:                  '<table><tr><td>'."\n";
 7999:     foreach my $option ('original','recaptcha','notused') {
 8000:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 8001:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 8002:                    $lt{$option}.'</label></span>';
 8003:         unless ($option eq 'notused') {
 8004:             $output .= ('&nbsp;'x2)."\n";
 8005:         }
 8006:     }
 8007: #
 8008: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 8009: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 8010: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 8011: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 8012: #
 8013:     $output .= '</td></tr>'."\n".
 8014:                '<tr><td class="LC_zero_height">'."\n".
 8015:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 8016:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 8017:                $currpub.'" size="40" /></span><br />'."\n".
 8018:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 8019:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 8020:                $currpriv.'" size="40" /></span><br />'.
 8021:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 8022:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 8023:                $currver.'" size="3" /></span><br />'.
 8024:                '</td></tr></table>'."\n".
 8025:                '</td></tr>';
 8026:     return $output;
 8027: }
 8028: 
 8029: sub user_formats_row {
 8030:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 8031:     my $output;
 8032:     my %text = (
 8033:                    'username' => 'new usernames',
 8034:                    'id'       => 'IDs',
 8035:                );
 8036:     unless ($type eq 'email') {
 8037:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 8038:         $output = '<tr '.$css_class.'>'.
 8039:                   '<td><span class="LC_nobreak">'.
 8040:                   &mt("Format rules to check for $text{$type}: ").
 8041:                   '</td><td class="LC_left_item" colspan="2"><table>';
 8042:     }
 8043:     my $rem;
 8044:     if (ref($ruleorder) eq 'ARRAY') {
 8045:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 8046:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 8047:                 my $rem = $i%($numinrow);
 8048:                 if ($rem == 0) {
 8049:                     if ($i > 0) {
 8050:                         $output .= '</tr>';
 8051:                     }
 8052:                     $output .= '<tr>';
 8053:                 }
 8054:                 my $check = ' ';
 8055:                 if (ref($settings) eq 'HASH') {
 8056:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 8057:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 8058:                             $check = ' checked="checked" ';
 8059:                         }
 8060:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 8061:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 8062:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 8063:                                 $check = ' checked="checked" ';
 8064:                             }
 8065:                         }
 8066:                     }
 8067:                 }
 8068:                 my $name = $type.'_rule';
 8069:                 if ($type eq 'email') {
 8070:                     $name .= '_'.$status;
 8071:                 }
 8072:                 $output .= '<td class="LC_left_item">'.
 8073:                            '<span class="LC_nobreak"><label>'.
 8074:                            '<input type="checkbox" name="'.$name.'" '.
 8075:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 8076:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 8077:             }
 8078:         }
 8079:         $rem = @{$ruleorder}%($numinrow);
 8080:     }
 8081:     my $colsleft;
 8082:     if ($rem) {
 8083:         $colsleft = $numinrow - $rem;
 8084:     }
 8085:     if ($colsleft > 1 ) {
 8086:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8087:                    '&nbsp;</td>';
 8088:     } elsif ($colsleft == 1) {
 8089:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8090:     }
 8091:     $output .= '</tr></table>';
 8092:     unless ($type eq 'email') {
 8093:         $output .= '</td></tr>';
 8094:     }
 8095:     return $output;
 8096: }
 8097: 
 8098: sub usercreation_types {
 8099:     my %lt = &Apache::lonlocal::texthash (
 8100:                     author     => 'When adding a co-author',
 8101:                     course     => 'When adding a user to a course',
 8102:                     requestcrs => 'When requesting a course',
 8103:                     any        => 'Any',
 8104:                     official   => 'Institutional only ',
 8105:                     unofficial => 'Non-institutional only',
 8106:                     none       => 'None',
 8107:     );
 8108:     return %lt;
 8109: }
 8110: 
 8111: sub selfcreation_types {
 8112:     my %lt = &Apache::lonlocal::texthash (
 8113:                     selfcreate => 'User creates own account',
 8114:                     any        => 'Any',
 8115:                     official   => 'Institutional only ',
 8116:                     unofficial => 'Non-institutional only',
 8117:                     email      => 'E-mail address',
 8118:                     login      => 'Institutional Login',
 8119:                     sso        => 'SSO',
 8120:              );
 8121: }
 8122: 
 8123: sub authtype_names {
 8124:     my %lt = &Apache::lonlocal::texthash(
 8125:                       int    => 'Internal',
 8126:                       krb4   => 'Kerberos 4',
 8127:                       krb5   => 'Kerberos 5',
 8128:                       loc    => 'Local',
 8129:                       lti    => 'LTI',
 8130:                   );
 8131:     return %lt;
 8132: }
 8133: 
 8134: sub context_names {
 8135:     my %context_title = &Apache::lonlocal::texthash(
 8136:        author => 'Creating users when an Author',
 8137:        course => 'Creating users when in a course',
 8138:        domain => 'Creating users when a Domain Coordinator',
 8139:     );
 8140:     return %context_title;
 8141: }
 8142: 
 8143: sub print_usermodification {
 8144:     my ($position,$dom,$settings,$rowtotal) = @_;
 8145:     my $numinrow = 4;
 8146:     my ($context,$datatable,$rowcount);
 8147:     if ($position eq 'top') {
 8148:         $rowcount = 0;
 8149:         $context = 'author'; 
 8150:         foreach my $role ('ca','aa') {
 8151:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 8152:                                                    $numinrow,$rowcount);
 8153:             $$rowtotal ++;
 8154:             $rowcount ++;
 8155:         }
 8156:     } elsif ($position eq 'bottom') {
 8157:         $context = 'course';
 8158:         $rowcount = 0;
 8159:         foreach my $role ('st','ep','ta','in','cr') {
 8160:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 8161:                                                    $numinrow,$rowcount);
 8162:             $$rowtotal ++;
 8163:             $rowcount ++;
 8164:         }
 8165:     }
 8166:     return $datatable;
 8167: }
 8168: 
 8169: sub print_defaults {
 8170:     my ($position,$dom,$settings,$rowtotal) = @_;
 8171:     my $rownum = 0;
 8172:     my ($datatable,$css_class,$titles);
 8173:     unless ($position eq 'bottom') {
 8174:         $titles = &defaults_titles($dom);
 8175:     }
 8176:     if ($position eq 'top') {
 8177:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 8178:                      'datelocale_def','portal_def');
 8179:         my %defaults;
 8180:         if (ref($settings) eq 'HASH') {
 8181:             %defaults = %{$settings};
 8182:         } else {
 8183:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8184:             foreach my $item (@items) {
 8185:                 $defaults{$item} = $domdefaults{$item};
 8186:             }
 8187:         }
 8188:         foreach my $item (@items) {
 8189:             if ($rownum%2) {
 8190:                 $css_class = '';
 8191:             } else {
 8192:                 $css_class = ' class="LC_odd_row" ';
 8193:             }
 8194:             $datatable .= '<tr'.$css_class.'>'.
 8195:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 8196:                           '</span></td><td class="LC_right_item" colspan="3">';
 8197:             if ($item eq 'auth_def') {
 8198:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 8199:                 my %shortauth = (
 8200:                                  internal => 'int',
 8201:                                  krb4 => 'krb4',
 8202:                                  krb5 => 'krb5',
 8203:                                  localauth  => 'loc',
 8204:                                  lti => 'lti',
 8205:                                 );
 8206:                 my %authnames = &authtype_names();
 8207:                 foreach my $auth (@authtypes) {
 8208:                     my $checked = ' ';
 8209:                     if ($defaults{$item} eq $auth) {
 8210:                         $checked = ' checked="checked" ';
 8211:                     }
 8212:                     $datatable .= '<label><input type="radio" name="'.$item.
 8213:                                   '" value="'.$auth.'"'.$checked.'/>'.
 8214:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 8215:                 }
 8216:             } elsif ($item eq 'timezone_def') {
 8217:                 my $includeempty = 1;
 8218:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 8219:             } elsif ($item eq 'datelocale_def') {
 8220:                 my $includeempty = 1;
 8221:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 8222:             } elsif ($item eq 'lang_def') {
 8223:                 my $includeempty = 1;
 8224:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 8225:             } else {
 8226:                 my $size;
 8227:                 if ($item eq 'portal_def') {
 8228:                     $size = ' size="25"';
 8229:                 }
 8230:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 8231:                               $defaults{$item}.'"'.$size.' />';
 8232:             }
 8233:             $datatable .= '</td></tr>';
 8234:             $rownum ++;
 8235:         }
 8236:     } else {
 8237:         my %defaults;
 8238:         if (ref($settings) eq 'HASH') {
 8239:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 8240:                 my $maxnum = @{$settings->{'inststatusorder'}};
 8241:                 for (my $i=0; $i<$maxnum; $i++) {
 8242:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 8243:                     my $item = $settings->{'inststatusorder'}->[$i];
 8244:                     my $title = $settings->{'inststatustypes'}->{$item};
 8245:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 8246:                     $datatable .= '<tr'.$css_class.'>'.
 8247:                                   '<td><span class="LC_nobreak">'.
 8248:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 8249:                     for (my $k=0; $k<=$maxnum; $k++) {
 8250:                         my $vpos = $k+1;
 8251:                         my $selstr;
 8252:                         if ($k == $i) {
 8253:                             $selstr = ' selected="selected" ';
 8254:                         }
 8255:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8256:                     }
 8257:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 8258:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 8259:                                   &mt('delete').'</span></td>'.
 8260:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 8261:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 8262:                                   '</span></td></tr>';
 8263:                 }
 8264:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 8265:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 8266:                 $datatable .= '<tr '.$css_class.'>'.
 8267:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 8268:                 for (my $k=0; $k<=$maxnum; $k++) {
 8269:                     my $vpos = $k+1;
 8270:                     my $selstr;
 8271:                     if ($k == $maxnum) {
 8272:                         $selstr = ' selected="selected" ';
 8273:                     }
 8274:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8275:                 }
 8276:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 8277:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 8278:                               '&nbsp;'.&mt('(new)').
 8279:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 8280:                               &mt('Name displayed:').
 8281:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 8282:                               '</tr>'."\n";
 8283:                 $rownum ++;
 8284:             }
 8285:         }
 8286:     }
 8287:     $$rowtotal += $rownum;
 8288:     return $datatable;
 8289: }
 8290: 
 8291: sub get_languages_hash {
 8292:     my %langchoices;
 8293:     foreach my $id (&Apache::loncommon::languageids()) {
 8294:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 8295:         if ($code ne '') {
 8296:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 8297:         }
 8298:     }
 8299:     return %langchoices;
 8300: }
 8301: 
 8302: sub defaults_titles {
 8303:     my ($dom) = @_;
 8304:     my %titles = &Apache::lonlocal::texthash (
 8305:                    'auth_def'      => 'Default authentication type',
 8306:                    'auth_arg_def'  => 'Default authentication argument',
 8307:                    'lang_def'      => 'Default language',
 8308:                    'timezone_def'  => 'Default timezone',
 8309:                    'datelocale_def' => 'Default locale for dates',
 8310:                    'portal_def'     => 'Portal/Default URL',
 8311:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 8312:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 8313:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 8314:                  );
 8315:     if ($dom) {
 8316:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 8317:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 8318:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 8319:         $protocol = 'http' if ($protocol ne 'https');
 8320:         if ($uint_dom) {
 8321:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 8322:                                          $uint_dom);
 8323:         }
 8324:     }
 8325:     return (\%titles);
 8326: }
 8327: 
 8328: sub print_scantron {
 8329:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 8330:     if ($position eq 'top') {
 8331:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 8332:     } else {
 8333:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 8334:     }
 8335: }
 8336: 
 8337: sub scantron_javascript {
 8338:     return <<"ENDSCRIPT";
 8339: 
 8340: <script type="text/javascript">
 8341: // <![CDATA[
 8342: 
 8343: function toggleScantron(form) {
 8344:     var csvfieldset = new Array();
 8345:     if (document.getElementById('scantroncsv_cols')) {
 8346:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 8347:     }
 8348:     if (document.getElementById('scantroncsv_options')) {
 8349:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 8350:     }
 8351:     if (csvfieldset.length) {
 8352:         if (document.getElementById('scantronconfcsv')) {
 8353:             var scantroncsv = document.getElementById('scantronconfcsv');
 8354:             if (scantroncsv.checked) {
 8355:                 for (var i=0; i<csvfieldset.length; i++) {
 8356:                     csvfieldset[i].style.display = 'block';
 8357:                 }
 8358:             } else {
 8359:                 for (var i=0; i<csvfieldset.length; i++) {
 8360:                     csvfieldset[i].style.display = 'none';
 8361:                 }
 8362:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 8363:                 if (csvselects.length) {
 8364:                     for (var j=0; j<csvselects.length; j++) {
 8365:                         csvselects[j].selectedIndex = 0;
 8366:                     }
 8367:                 }
 8368:             }
 8369:         }
 8370:     }
 8371:     return;
 8372: }
 8373: // ]]>
 8374: </script>
 8375: 
 8376: ENDSCRIPT
 8377: 
 8378: }
 8379: 
 8380: sub print_scantronformat {
 8381:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 8382:     my $itemcount = 1;
 8383:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 8384:         %confhash);
 8385:     my $switchserver = &check_switchserver($dom,$confname);
 8386:     my %lt = &Apache::lonlocal::texthash (
 8387:                 default => 'Default bubblesheet format file error',
 8388:                 custom  => 'Custom bubblesheet format file error',
 8389:              );
 8390:     my %scantronfiles = (
 8391:         default => 'default.tab',
 8392:         custom => 'custom.tab',
 8393:     );
 8394:     foreach my $key (keys(%scantronfiles)) {
 8395:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 8396:                               .$scantronfiles{$key};
 8397:     }
 8398:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 8399:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 8400:         if (!$switchserver) {
 8401:             my $servadm = $r->dir_config('lonAdmEMail');
 8402:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 8403:             if ($configuserok eq 'ok') {
 8404:                 if ($author_ok eq 'ok') {
 8405:                     my %legacyfile = (
 8406:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 8407:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 8408:                     );
 8409:                     my %md5chk;
 8410:                     foreach my $type (keys(%legacyfile)) {
 8411:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 8412:                         chomp($md5chk{$type});
 8413:                     }
 8414:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 8415:                         foreach my $type (keys(%legacyfile)) {
 8416:                             ($scantronurls{$type},my $error) =
 8417:                                 &legacy_scantronformat($r,$dom,$confname,
 8418:                                                  $type,$legacyfile{$type},
 8419:                                                  $scantronurls{$type},
 8420:                                                  $scantronfiles{$type});
 8421:                             if ($error ne '') {
 8422:                                 $error{$type} = $error;
 8423:                             }
 8424:                         }
 8425:                         if (keys(%error) == 0) {
 8426:                             $is_custom = 1;
 8427:                             $confhash{'scantron'}{'scantronformat'} =
 8428:                                 $scantronurls{'custom'};
 8429:                             my $putresult =
 8430:                                 &Apache::lonnet::put_dom('configuration',
 8431:                                                          \%confhash,$dom);
 8432:                             if ($putresult ne 'ok') {
 8433:                                 $error{'custom'} =
 8434:                                     '<span class="LC_error">'.
 8435:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8436:                             }
 8437:                         }
 8438:                     } else {
 8439:                         ($scantronurls{'default'},my $error) =
 8440:                             &legacy_scantronformat($r,$dom,$confname,
 8441:                                           'default',$legacyfile{'default'},
 8442:                                           $scantronurls{'default'},
 8443:                                           $scantronfiles{'default'});
 8444:                         if ($error eq '') {
 8445:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 8446:                             my $putresult =
 8447:                                 &Apache::lonnet::put_dom('configuration',
 8448:                                                          \%confhash,$dom);
 8449:                             if ($putresult ne 'ok') {
 8450:                                 $error{'default'} =
 8451:                                     '<span class="LC_error">'.
 8452:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8453:                             }
 8454:                         } else {
 8455:                             $error{'default'} = $error;
 8456:                         }
 8457:                     }
 8458:                 }
 8459:             }
 8460:         } else {
 8461:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 8462:         }
 8463:     }
 8464:     if (ref($settings) eq 'HASH') {
 8465:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 8466:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 8467:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 8468:                 $scantronurl = '';
 8469:             } else {
 8470:                 $scantronurl = $settings->{'scantronformat'};
 8471:             }
 8472:             $is_custom = 1;
 8473:         } else {
 8474:             $scantronurl = $scantronurls{'default'};
 8475:         }
 8476:     } else {
 8477:         if ($is_custom) {
 8478:             $scantronurl = $scantronurls{'custom'};
 8479:         } else {
 8480:             $scantronurl = $scantronurls{'default'};
 8481:         }
 8482:     }
 8483:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8484:     $datatable .= '<tr'.$css_class.'>';
 8485:     if (!$is_custom) {
 8486:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 8487:                       '<span class="LC_nobreak">';
 8488:         if ($scantronurl) {
 8489:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 8490:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 8491:         } else {
 8492:             $datatable = &mt('File unavailable for display');
 8493:         }
 8494:         $datatable .= '</span></td>';
 8495:         if (keys(%error) == 0) { 
 8496:             $datatable .= '<td style="vertical-align: bottom">';
 8497:             if (!$switchserver) {
 8498:                 $datatable .= &mt('Upload:').'<br />';
 8499:             }
 8500:         } else {
 8501:             my $errorstr;
 8502:             foreach my $key (sort(keys(%error))) {
 8503:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8504:             }
 8505:             $datatable .= '<td>'.$errorstr;
 8506:         }
 8507:     } else {
 8508:         if (keys(%error) > 0) {
 8509:             my $errorstr;
 8510:             foreach my $key (sort(keys(%error))) {
 8511:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8512:             } 
 8513:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 8514:         } elsif ($scantronurl) {
 8515:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 8516:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8517:             $datatable .= '<td><span class="LC_nobreak">'.
 8518:                           $link.
 8519:                           '<label><input type="checkbox" name="scantronformat_del"'.
 8520:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 8521:                           '<td><span class="LC_nobreak">&nbsp;'.
 8522:                           &mt('Replace:').'</span><br />';
 8523:         }
 8524:     }
 8525:     if (keys(%error) == 0) {
 8526:         if ($switchserver) {
 8527:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 8528:         } else {
 8529:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8530:                          '<input type="file" name="scantronformat" /></span>';
 8531:         }
 8532:     }
 8533:     $datatable .= '</td></tr>';
 8534:     $$rowtotal ++;
 8535:     return $datatable;
 8536: }
 8537: 
 8538: sub legacy_scantronformat {
 8539:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 8540:     my ($url,$error);
 8541:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 8542:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 8543:         (my $result,$url) =
 8544:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 8545:                          '','',$newfile);
 8546:         if ($result ne 'ok') {
 8547:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 8548:         }
 8549:     }
 8550:     return ($url,$error);
 8551: }
 8552: 
 8553: sub print_scantronconfig {
 8554:     my ($dom,$settings,$rowtotal) = @_;
 8555:     my $itemcount = 2;
 8556:     my $is_checked = ' checked="checked"';
 8557:     my %optionson = (
 8558:                      hdr => ' checked="checked"',
 8559:                      pad => ' checked="checked"',
 8560:                      rem => ' checked="checked"',
 8561:                     );
 8562:     my %optionsoff = (
 8563:                       hdr => '',
 8564:                       pad => '',
 8565:                       rem => '',
 8566:                      );
 8567:     my $currcsvsty = 'none';
 8568:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 8569:     my @fields = &scantroncsv_fields();
 8570:     my %titles = &scantronconfig_titles();
 8571:     if (ref($settings) eq 'HASH') {
 8572:         if (ref($settings->{config}) eq 'HASH') {
 8573:             if ($settings->{config}->{dat}) {
 8574:                 $checked{'dat'} = $is_checked;
 8575:             }
 8576:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 8577:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 8578:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 8579:                     if (keys(%csvfields) > 0) {
 8580:                         $checked{'csv'} = $is_checked;
 8581:                         $currcsvsty = 'block';
 8582:                     }
 8583:                 }
 8584:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 8585:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 8586:                     foreach my $option (keys(%optionson)) {
 8587:                         unless ($csvoptions{$option}) {
 8588:                             $optionsoff{$option} = $optionson{$option};
 8589:                             $optionson{$option} = '';
 8590:                         }
 8591:                     }
 8592:                 }
 8593:             }
 8594:         } else {
 8595:             $checked{'dat'} = $is_checked;
 8596:         }
 8597:     } else {
 8598:         $checked{'dat'} = $is_checked;
 8599:     }
 8600:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 8601:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8602:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 8603:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 8604:     foreach my $item ('dat','csv') {
 8605:         my $id;
 8606:         if ($item eq 'csv') {
 8607:             $id = 'id="scantronconfcsv" ';
 8608:         }
 8609:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 8610:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 8611:         if ($item eq 'csv') {
 8612:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 8613:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 8614:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 8615:             foreach my $col (@fields) {
 8616:                 my $selnone;
 8617:                 if ($csvfields{$col} eq '') {
 8618:                     $selnone = ' selected="selected"';
 8619:                 }
 8620:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 8621:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 8622:                               '<option value=""'.$selnone.'></option>';
 8623:                 for (my $i=0; $i<20; $i++) {
 8624:                     my $shown = $i+1;
 8625:                     my $sel;
 8626:                     unless ($selnone) {
 8627:                         if (exists($csvfields{$col})) {
 8628:                             if ($csvfields{$col} == $i) {
 8629:                                 $sel = ' selected="selected"';
 8630:                             }
 8631:                         }
 8632:                     }
 8633:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 8634:                 }
 8635:                 $datatable .= '</select></td></tr>';
 8636:            }
 8637:            $datatable .= '</table></fieldset>'.
 8638:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 8639:                          '<legend>'.&mt('CSV Options').'</legend>';
 8640:            foreach my $option ('hdr','pad','rem') {
 8641:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 8642:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 8643:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 8644:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 8645:            }
 8646:            $datatable .= '</fieldset>';
 8647:            $itemcount ++;
 8648:         }
 8649:     }
 8650:     $datatable .= '</td></tr>';
 8651:     $$rowtotal ++;
 8652:     return $datatable;
 8653: }
 8654: 
 8655: sub scantronconfig_titles {
 8656:     return &Apache::lonlocal::texthash(
 8657:                                           dat => 'Standard format (.dat)',
 8658:                                           csv => 'Comma separated values (.csv)',
 8659:                                           hdr => 'Remove first line in file (contains column titles)',
 8660:                                           pad => 'Prepend 0s to PaperID',
 8661:                                           rem => 'Remove leading spaces (except Question Response columns)',
 8662:                                           CODE => 'CODE',
 8663:                                           ID   => 'Student ID',
 8664:                                           PaperID => 'Paper ID',
 8665:                                           FirstName => 'First Name',
 8666:                                           LastName => 'Last Name',
 8667:                                           FirstQuestion => 'First Question Response',
 8668:                                           Section => 'Section',
 8669:     );
 8670: }
 8671: 
 8672: sub scantroncsv_fields {
 8673:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 8674: }
 8675: 
 8676: sub print_coursecategories {
 8677:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 8678:     my $datatable;
 8679:     if ($position eq 'top') {
 8680:         my (%checked);
 8681:         my @catitems = ('unauth','auth');
 8682:         my @cattypes = ('std','domonly','codesrch','none');
 8683:         $checked{'unauth'} = 'std';
 8684:         $checked{'auth'} = 'std';
 8685:         if (ref($settings) eq 'HASH') {
 8686:             foreach my $type (@cattypes) {
 8687:                 if ($type eq $settings->{'unauth'}) {
 8688:                     $checked{'unauth'} = $type;
 8689:                 }
 8690:                 if ($type eq $settings->{'auth'}) {
 8691:                     $checked{'auth'} = $type;
 8692:                 }
 8693:             }
 8694:         }
 8695:         my %lt = &Apache::lonlocal::texthash (
 8696:                                                unauth   => 'Catalog type for unauthenticated users',
 8697:                                                auth     => 'Catalog type for authenticated users',
 8698:                                                none     => 'No catalog',
 8699:                                                std      => 'Standard catalog',
 8700:                                                domonly  => 'Domain-only catalog',
 8701:                                                codesrch => "Code search form",
 8702:                                              );
 8703:        my $itemcount = 0;
 8704:        foreach my $item (@catitems) {
 8705:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8706:            $datatable .= '<tr '.$css_class.'>'.
 8707:                          '<td>'.$lt{$item}.'</td>'.
 8708:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 8709:            foreach my $type (@cattypes) {
 8710:                my $ischecked;
 8711:                if ($checked{$item} eq $type) {
 8712:                    $ischecked=' checked="checked"';
 8713:                }
 8714:                $datatable .= '<label>'.
 8715:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 8716:                              ' />'.$lt{$type}.'</label>&nbsp;';
 8717:            }
 8718:            $datatable .= '</span></td></tr>';
 8719:            $itemcount ++;
 8720:         }
 8721:         $$rowtotal += $itemcount;
 8722:     } elsif ($position eq 'middle') {
 8723:         my $toggle_cats_crs = ' ';
 8724:         my $toggle_cats_dom = ' checked="checked" ';
 8725:         my $can_cat_crs = ' ';
 8726:         my $can_cat_dom = ' checked="checked" ';
 8727:         my $toggle_catscomm_comm = ' ';
 8728:         my $toggle_catscomm_dom = ' checked="checked" ';
 8729:         my $can_catcomm_comm = ' ';
 8730:         my $can_catcomm_dom = ' checked="checked" ';
 8731:         my $toggle_catsplace_place = ' ';
 8732:         my $toggle_catsplace_dom = ' checked="checked" ';
 8733:         my $can_catplace_place = ' ';
 8734:         my $can_catplace_dom = ' checked="checked" ';
 8735: 
 8736:         if (ref($settings) eq 'HASH') {
 8737:             if ($settings->{'togglecats'} eq 'crs') {
 8738:                 $toggle_cats_crs = $toggle_cats_dom;
 8739:                 $toggle_cats_dom = ' ';
 8740:             }
 8741:             if ($settings->{'categorize'} eq 'crs') {
 8742:                 $can_cat_crs = $can_cat_dom;
 8743:                 $can_cat_dom = ' ';
 8744:             }
 8745:             if ($settings->{'togglecatscomm'} eq 'comm') {
 8746:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 8747:                 $toggle_catscomm_dom = ' ';
 8748:             }
 8749:             if ($settings->{'categorizecomm'} eq 'comm') {
 8750:                 $can_catcomm_comm = $can_catcomm_dom;
 8751:                 $can_catcomm_dom = ' ';
 8752:             }
 8753:             if ($settings->{'togglecatsplace'} eq 'place') {
 8754:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 8755:                 $toggle_catsplace_dom = ' ';
 8756:             }
 8757:             if ($settings->{'categorizeplace'} eq 'place') {
 8758:                 $can_catplace_place = $can_catplace_dom;
 8759:                 $can_catplace_dom = ' ';
 8760:             }
 8761:         }
 8762:         my %title = &Apache::lonlocal::texthash (
 8763:                      togglecats      => 'Show/Hide a course in catalog',
 8764:                      togglecatscomm  => 'Show/Hide a community in catalog',
 8765:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 8766:                      categorize      => 'Assign a category to a course',
 8767:                      categorizecomm  => 'Assign a category to a community',
 8768:                      categorizeplace => 'Assign a category to a placement test',
 8769:                     );
 8770:         my %level = &Apache::lonlocal::texthash (
 8771:                      dom   => 'Set in Domain',
 8772:                      crs   => 'Set in Course',
 8773:                      comm  => 'Set in Community',
 8774:                      place => 'Set in Placement Test',
 8775:                     );
 8776:         $datatable = '<tr class="LC_odd_row">'.
 8777:                   '<td>'.$title{'togglecats'}.'</td>'.
 8778:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8779:                   '<input type="radio" name="togglecats"'.
 8780:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8781:                   '<label><input type="radio" name="togglecats"'.
 8782:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8783:                   '</tr><tr>'.
 8784:                   '<td>'.$title{'categorize'}.'</td>'.
 8785:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8786:                   '<label><input type="radio" name="categorize"'.
 8787:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8788:                   '<label><input type="radio" name="categorize"'.
 8789:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8790:                   '</tr><tr class="LC_odd_row">'.
 8791:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 8792:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8793:                   '<input type="radio" name="togglecatscomm"'.
 8794:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8795:                   '<label><input type="radio" name="togglecatscomm"'.
 8796:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8797:                   '</tr><tr>'.
 8798:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 8799:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8800:                   '<label><input type="radio" name="categorizecomm"'.
 8801:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8802:                   '<label><input type="radio" name="categorizecomm"'.
 8803:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8804:                   '</tr><tr class="LC_odd_row">'.
 8805:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 8806:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8807:                   '<input type="radio" name="togglecatsplace"'.
 8808:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8809:                   '<label><input type="radio" name="togglecatscomm"'.
 8810:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 8811:                   '</tr><tr>'.
 8812:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 8813:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8814:                   '<label><input type="radio" name="categorizeplace"'.
 8815:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8816:                   '<label><input type="radio" name="categorizeplace"'.
 8817:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 8818:                   '</tr>';
 8819:         $$rowtotal += 6;
 8820:     } else {
 8821:         my $css_class;
 8822:         my $itemcount = 1;
 8823:         my $cathash; 
 8824:         if (ref($settings) eq 'HASH') {
 8825:             $cathash = $settings->{'cats'};
 8826:         }
 8827:         if (ref($cathash) eq 'HASH') {
 8828:             my (@cats,@trails,%allitems,%idx,@jsarray);
 8829:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 8830:                                                    \%allitems,\%idx,\@jsarray);
 8831:             my $maxdepth = scalar(@cats);
 8832:             my $colattrib = '';
 8833:             if ($maxdepth > 2) {
 8834:                 $colattrib = ' colspan="2" ';
 8835:             }
 8836:             my @path;
 8837:             if (@cats > 0) {
 8838:                 if (ref($cats[0]) eq 'ARRAY') {
 8839:                     my $numtop = @{$cats[0]};
 8840:                     my $maxnum = $numtop;
 8841:                     my %default_names = (
 8842:                           instcode    => &mt('Official courses'),
 8843:                           communities => &mt('Communities'),
 8844:                           placement   => &mt('Placement Tests'),
 8845:                     );
 8846: 
 8847:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 8848:                         ($cathash->{'instcode::0'} eq '') ||
 8849:                         (!grep(/^communities$/,@{$cats[0]})) || 
 8850:                         ($cathash->{'communities::0'} eq '') ||
 8851:                         (!grep(/^placement$/,@{$cats[0]})) ||
 8852:                         ($cathash->{'placement::0'} eq '')) {
 8853:                         $maxnum ++;
 8854:                     }
 8855:                     my $lastidx;
 8856:                     for (my $i=0; $i<$numtop; $i++) {
 8857:                         my $parent = $cats[0][$i];
 8858:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8859:                         my $item = &escape($parent).'::0';
 8860:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 8861:                         $lastidx = $idx{$item};
 8862:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8863:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 8864:                         for (my $k=0; $k<=$maxnum; $k++) {
 8865:                             my $vpos = $k+1;
 8866:                             my $selstr;
 8867:                             if ($k == $i) {
 8868:                                 $selstr = ' selected="selected" ';
 8869:                             }
 8870:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8871:                         }
 8872:                         $datatable .= '</select></span></td><td>';
 8873:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 8874:                             $datatable .=  '<span class="LC_nobreak">'
 8875:                                            .$default_names{$parent}.'</span>';
 8876:                             if ($parent eq 'instcode') {
 8877:                                 $datatable .= '<br /><span class="LC_nobreak">('
 8878:                                               .&mt('with institutional codes')
 8879:                                               .')</span></td><td'.$colattrib.'>';
 8880:                             } else {
 8881:                                 $datatable .= '<table><tr><td>';
 8882:                             }
 8883:                             $datatable .= '<span class="LC_nobreak">'
 8884:                                           .'<label><input type="radio" name="'
 8885:                                           .$parent.'" value="1" checked="checked" />'
 8886:                                           .&mt('Display').'</label>';
 8887:                             if ($parent eq 'instcode') {
 8888:                                 $datatable .= '&nbsp;';
 8889:                             } else {
 8890:                                 $datatable .= '</span></td></tr><tr><td>'
 8891:                                               .'<span class="LC_nobreak">';
 8892:                             }
 8893:                             $datatable .= '<label><input type="radio" name="'
 8894:                                           .$parent.'" value="0" />'
 8895:                                           .&mt('Do not display').'</label></span>';
 8896:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 8897:                                 $datatable .= '</td></tr></table>';
 8898:                             }
 8899:                             $datatable .= '</td>';
 8900:                         } else {
 8901:                             $datatable .= $parent
 8902:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 8903:                                           .'<input type="checkbox" name="deletecategory" '
 8904:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 8905:                         }
 8906:                         my $depth = 1;
 8907:                         push(@path,$parent);
 8908:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 8909:                         pop(@path);
 8910:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 8911:                         $itemcount ++;
 8912:                     }
 8913:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8914:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 8915:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 8916:                     for (my $k=0; $k<=$maxnum; $k++) {
 8917:                         my $vpos = $k+1;
 8918:                         my $selstr;
 8919:                         if ($k == $numtop) {
 8920:                             $selstr = ' selected="selected" ';
 8921:                         }
 8922:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8923:                     }
 8924:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 8925:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 8926:                                   .'</tr>'."\n";
 8927:                     $itemcount ++;
 8928:                     foreach my $default ('instcode','communities','placement') {
 8929:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 8930:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8931:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 8932:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 8933:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 8934:                             for (my $k=0; $k<=$maxnum; $k++) {
 8935:                                 my $vpos = $k+1;
 8936:                                 my $selstr;
 8937:                                 if ($k == $maxnum) {
 8938:                                     $selstr = ' selected="selected" ';
 8939:                                 }
 8940:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8941:                             }
 8942:                             $datatable .= '</select></span></td>'.
 8943:                                           '<td><span class="LC_nobreak">'.
 8944:                                           $default_names{$default}.'</span>';
 8945:                             if ($default eq 'instcode') {
 8946:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 8947:                                               .&mt('with institutional codes').')</span>';
 8948:                             }
 8949:                             $datatable .= '</td>'
 8950:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 8951:                                           .&mt('Display').'</label>&nbsp;'
 8952:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 8953:                                           .&mt('Do not display').'</label></span></td></tr>';
 8954:                         }
 8955:                     }
 8956:                 }
 8957:             } else {
 8958:                 $datatable .= &initialize_categories($itemcount);
 8959:             }
 8960:         } else {
 8961:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 8962:                           .&initialize_categories($itemcount);
 8963:         }
 8964:         $$rowtotal += $itemcount;
 8965:     }
 8966:     return $datatable;
 8967: }
 8968: 
 8969: sub print_serverstatuses {
 8970:     my ($dom,$settings,$rowtotal) = @_;
 8971:     my $datatable;
 8972:     my @pages = &serverstatus_pages();
 8973:     my (%namedaccess,%machineaccess);
 8974:     foreach my $type (@pages) {
 8975:         $namedaccess{$type} = '';
 8976:         $machineaccess{$type}= '';
 8977:     }
 8978:     if (ref($settings) eq 'HASH') {
 8979:         foreach my $type (@pages) {
 8980:             if (exists($settings->{$type})) {
 8981:                 if (ref($settings->{$type}) eq 'HASH') {
 8982:                     foreach my $key (keys(%{$settings->{$type}})) {
 8983:                         if ($key eq 'namedusers') {
 8984:                             $namedaccess{$type} = $settings->{$type}->{$key};
 8985:                         } elsif ($key eq 'machines') {
 8986:                             $machineaccess{$type} = $settings->{$type}->{$key};
 8987:                         }
 8988:                     }
 8989:                 }
 8990:             }
 8991:         }
 8992:     }
 8993:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8994:     my $rownum = 0;
 8995:     my $css_class;
 8996:     foreach my $type (@pages) {
 8997:         $rownum ++;
 8998:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 8999:         $datatable .= '<tr'.$css_class.'>'.
 9000:                       '<td><span class="LC_nobreak">'.
 9001:                       $titles->{$type}.'</span></td>'.
 9002:                       '<td class="LC_left_item">'.
 9003:                       '<input type="text" name="'.$type.'_namedusers" '.
 9004:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 9005:                       '<td class="LC_right_item">'.
 9006:                       '<span class="LC_nobreak">'.
 9007:                       '<input type="text" name="'.$type.'_machines" '.
 9008:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 9009:                       '</span></td></tr>'."\n";
 9010:     }
 9011:     $$rowtotal += $rownum;
 9012:     return $datatable;
 9013: }
 9014: 
 9015: sub serverstatus_pages {
 9016:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 9017:             'checksums','clusterstatus','certstatus','metadata_keywords',
 9018:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 9019:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 9020: }
 9021: 
 9022: sub defaults_javascript {
 9023:     my ($settings) = @_;
 9024:     return unless (ref($settings) eq 'HASH');
 9025:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9026:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 9027:         if ($maxnum eq '') {
 9028:             $maxnum = 0;
 9029:         }
 9030:         $maxnum ++;
 9031:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 9032:         return <<"ENDSCRIPT";
 9033: <script type="text/javascript">
 9034: // <![CDATA[
 9035: function reorderTypes(form,caller) {
 9036:     var changedVal;
 9037: $jstext 
 9038:     var newpos = 'addinststatus_pos';
 9039:     var current = new Array;
 9040:     var maxh = $maxnum;
 9041:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 9042:     var oldVal;
 9043:     if (caller == newpos) {
 9044:         changedVal = newitemVal;
 9045:     } else {
 9046:         var curritem = 'inststatus_pos_'+caller;
 9047:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 9048:         current[newitemVal] = newpos;
 9049:     }
 9050:     for (var i=0; i<inststatuses.length; i++) {
 9051:         if (inststatuses[i] != caller) {
 9052:             var elementName = 'inststatus_pos_'+inststatuses[i];
 9053:             if (form.elements[elementName]) {
 9054:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 9055:                 current[currVal] = elementName;
 9056:             }
 9057:         }
 9058:     }
 9059:     for (var j=0; j<maxh; j++) {
 9060:         if (current[j] == undefined) {
 9061:             oldVal = j;
 9062:         }
 9063:     }
 9064:     if (oldVal < changedVal) {
 9065:         for (var k=oldVal+1; k<=changedVal ; k++) {
 9066:            var elementName = current[k];
 9067:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 9068:         }
 9069:     } else {
 9070:         for (var k=changedVal; k<oldVal; k++) {
 9071:             var elementName = current[k];
 9072:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 9073:         }
 9074:     }
 9075:     return;
 9076: }
 9077: 
 9078: // ]]>
 9079: </script>
 9080: 
 9081: ENDSCRIPT
 9082:     }
 9083:     return;
 9084: }
 9085: 
 9086: sub passwords_javascript {
 9087:     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.');
 9088:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 9089:     &js_escape(\$intauthcheck);
 9090:     &js_escape(\$intauthcost);
 9091:     my $intauthjs = <<"ENDSCRIPT";
 9092: 
 9093: function warnIntAuth(field) {
 9094:     if (field.name == 'intauth_check') {
 9095:         if (field.value == '2') {
 9096:             alert('$intauthcheck');
 9097:         }
 9098:     }
 9099:     if (field.name == 'intauth_cost') {
 9100:         field.value.replace(/\s/g,'');
 9101:         if (field.value != '') {
 9102:             var regexdigit=/^\\d+\$/;
 9103:             if (!regexdigit.test(field.value)) {
 9104:                 alert('$intauthcost');
 9105:             }
 9106:         }
 9107:     }
 9108:     return;
 9109: }
 9110: 
 9111: ENDSCRIPT
 9112:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 9113: }
 9114: 
 9115: sub coursecategories_javascript {
 9116:     my ($settings) = @_;
 9117:     my ($output,$jstext,$cathash);
 9118:     if (ref($settings) eq 'HASH') {
 9119:         $cathash = $settings->{'cats'};
 9120:     }
 9121:     if (ref($cathash) eq 'HASH') {
 9122:         my (@cats,@jsarray,%idx);
 9123:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 9124:         if (@jsarray > 0) {
 9125:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 9126:             for (my $i=0; $i<@jsarray; $i++) {
 9127:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 9128:                     my $catstr = join('","',@{$jsarray[$i]});
 9129:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 9130:                 }
 9131:             }
 9132:         }
 9133:     } else {
 9134:         $jstext  = '    var categories = Array(1);'."\n".
 9135:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 9136:     }
 9137:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 9138:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 9139:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 9140:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 9141:     &js_escape(\$instcode_reserved);
 9142:     &js_escape(\$communities_reserved);
 9143:     &js_escape(\$placement_reserved);
 9144:     &js_escape(\$choose_again);
 9145:     $output = <<"ENDSCRIPT";
 9146: <script type="text/javascript">
 9147: // <![CDATA[
 9148: function reorderCats(form,parent,item,idx) {
 9149:     var changedVal;
 9150: $jstext
 9151:     var newpos = 'addcategory_pos';
 9152:     if (parent == '') {
 9153:         var has_instcode = 0;
 9154:         var maxtop = categories[idx].length;
 9155:         for (var j=0; j<maxtop; j++) {
 9156:             if (categories[idx][j] == 'instcode::0') {
 9157:                 has_instcode == 1;
 9158:             }
 9159:         }
 9160:         if (has_instcode == 0) {
 9161:             categories[idx][maxtop] = 'instcode_pos';
 9162:         }
 9163:     } else {
 9164:         newpos += '_'+parent;
 9165:     }
 9166:     var maxh = 1 + categories[idx].length;
 9167:     var current = new Array;
 9168:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 9169:     if (item == newpos) {
 9170:         changedVal = newitemVal;
 9171:     } else {
 9172:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 9173:         current[newitemVal] = newpos;
 9174:     }
 9175:     for (var i=0; i<categories[idx].length; i++) {
 9176:         var elementName = categories[idx][i];
 9177:         if (elementName != item) {
 9178:             if (form.elements[elementName]) {
 9179:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 9180:                 current[currVal] = elementName;
 9181:             }
 9182:         }
 9183:     }
 9184:     var oldVal;
 9185:     for (var j=0; j<maxh; j++) {
 9186:         if (current[j] == undefined) {
 9187:             oldVal = j;
 9188:         }
 9189:     }
 9190:     if (oldVal < changedVal) {
 9191:         for (var k=oldVal+1; k<=changedVal ; k++) {
 9192:            var elementName = current[k];
 9193:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 9194:         }
 9195:     } else {
 9196:         for (var k=changedVal; k<oldVal; k++) {
 9197:             var elementName = current[k];
 9198:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 9199:         }
 9200:     }
 9201:     return;
 9202: }
 9203: 
 9204: function categoryCheck(form) {
 9205:     if (form.elements['addcategory_name'].value == 'instcode') {
 9206:         alert('$instcode_reserved\\n$choose_again');
 9207:         return false;
 9208:     }
 9209:     if (form.elements['addcategory_name'].value == 'communities') {
 9210:         alert('$communities_reserved\\n$choose_again');
 9211:         return false;
 9212:     }
 9213:     if (form.elements['addcategory_name'].value == 'placement') {
 9214:         alert('$placement_reserved\\n$choose_again');
 9215:         return false;
 9216:     }
 9217:     return true;
 9218: }
 9219: 
 9220: // ]]>
 9221: </script>
 9222: 
 9223: ENDSCRIPT
 9224:     return $output;
 9225: }
 9226: 
 9227: sub initialize_categories {
 9228:     my ($itemcount) = @_;
 9229:     my ($datatable,$css_class,$chgstr);
 9230:     my %default_names = (
 9231:                       instcode    => 'Official courses (with institutional codes)',
 9232:                       communities => 'Communities',
 9233:                       placement   => 'Placement Tests',
 9234:                         );
 9235:     my %selnum = (
 9236:                    instcode    => '0',
 9237:                    communities => '1',
 9238:                    placement   => '2',
 9239:                  );
 9240:     my %selected;
 9241:     foreach my $default ('instcode','communities','placement') {
 9242:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9243:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 9244:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
 9245:         $selected{$selnum{$default}} = ' selected="selected"';
 9246:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 9247:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
 9248:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
 9249:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
 9250:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
 9251:                      .'<option value="3">4</option></select>&nbsp;'
 9252:                      .$default_names{$default}
 9253:                      .'</span></td><td><span class="LC_nobreak">'
 9254:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 9255:                      .&mt('Display').'</label>&nbsp;<label>'
 9256:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 9257:                  .'</label></span></td></tr>';
 9258:         $itemcount ++;
 9259:     }
 9260:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9261:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 9262:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 9263:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 9264:                   .'<option value="0">1</option>'
 9265:                   .'<option value="1">2</option>'
 9266:                   .'<option value="2">3</option>'
 9267:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
 9268:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 9269:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 9270:                   .'</td></tr>';
 9271:     return $datatable;
 9272: }
 9273: 
 9274: sub build_category_rows {
 9275:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 9276:     my ($text,$name,$item,$chgstr);
 9277:     if (ref($cats) eq 'ARRAY') {
 9278:         my $maxdepth = scalar(@{$cats});
 9279:         if (ref($cats->[$depth]) eq 'HASH') {
 9280:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 9281:                 my $numchildren = @{$cats->[$depth]{$parent}};
 9282:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9283:                 $text .= '<td><table class="LC_data_table">';
 9284:                 my ($idxnum,$parent_name,$parent_item);
 9285:                 my $higher = $depth - 1;
 9286:                 if ($higher == 0) {
 9287:                     $parent_name = &escape($parent).'::'.$higher;
 9288:                 } else {
 9289:                     if (ref($path) eq 'ARRAY') {
 9290:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 9291:                     }
 9292:                 }
 9293:                 $parent_item = 'addcategory_pos_'.$parent_name;
 9294:                 for (my $j=0; $j<=$numchildren; $j++) {
 9295:                     if ($j < $numchildren) {
 9296:                         $name = $cats->[$depth]{$parent}[$j];
 9297:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 9298:                         $idxnum = $idx->{$item};
 9299:                     } else {
 9300:                         $name = $parent_name;
 9301:                         $item = $parent_item;
 9302:                     }
 9303:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 9304:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 9305:                     for (my $i=0; $i<=$numchildren; $i++) {
 9306:                         my $vpos = $i+1;
 9307:                         my $selstr;
 9308:                         if ($j == $i) {
 9309:                             $selstr = ' selected="selected" ';
 9310:                         }
 9311:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 9312:                     }
 9313:                     $text .= '</select>&nbsp;';
 9314:                     if ($j < $numchildren) {
 9315:                         my $deeper = $depth+1;
 9316:                         $text .= $name.'&nbsp;'
 9317:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 9318:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 9319:                         if(ref($path) eq 'ARRAY') {
 9320:                             push(@{$path},$name);
 9321:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 9322:                             pop(@{$path});
 9323:                         }
 9324:                     } else {
 9325:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 9326:                         if ($j == $numchildren) {
 9327:                             $text .= $name;
 9328:                         } else {
 9329:                             $text .= $item;
 9330:                         }
 9331:                         $text .= '" value="" />';
 9332:                     }
 9333:                     $text .= '</td></tr>';
 9334:                 }
 9335:                 $text .= '</table></td>';
 9336:             } else {
 9337:                 my $higher = $depth-1;
 9338:                 if ($higher == 0) {
 9339:                     $name = &escape($parent).'::'.$higher;
 9340:                 } else {
 9341:                     if (ref($path) eq 'ARRAY') {
 9342:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 9343:                     }
 9344:                 }
 9345:                 my $colspan;
 9346:                 if ($parent ne 'instcode') {
 9347:                     $colspan = $maxdepth - $depth - 1;
 9348:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 9349:                 }
 9350:             }
 9351:         }
 9352:     }
 9353:     return $text;
 9354: }
 9355: 
 9356: sub modifiable_userdata_row {
 9357:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 9358:         $rowid,$customcss,$rowstyle) = @_;
 9359:     my ($role,$rolename,$statustype);
 9360:     $role = $item;
 9361:     if ($context eq 'cancreate') {
 9362:         if ($item =~ /^(emailusername)_(.+)$/) {
 9363:             $role = $1;
 9364:             $statustype = $2;
 9365:             if (ref($usertypes) eq 'HASH') {
 9366:                 if ($usertypes->{$statustype}) {
 9367:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 9368:                 } else {
 9369:                     $rolename = &mt('Data provided by user');
 9370:                 }
 9371:             }
 9372:         }
 9373:     } elsif ($context eq 'selfcreate') {
 9374:         if (ref($usertypes) eq 'HASH') {
 9375:             $rolename = $usertypes->{$role};
 9376:         } else {
 9377:             $rolename = $role;
 9378:         }
 9379:     } elsif ($context eq 'lti') {
 9380:         $rolename = &mt('Institutional data used (if available)');
 9381:     } else {
 9382:         if ($role eq 'cr') {
 9383:             $rolename = &mt('Custom role');
 9384:         } else {
 9385:             $rolename = &Apache::lonnet::plaintext($role);
 9386:         }
 9387:     }
 9388:     my (@fields,%fieldtitles);
 9389:     if (ref($fieldsref) eq 'ARRAY') {
 9390:         @fields = @{$fieldsref};
 9391:     } else {
 9392:         @fields = ('lastname','firstname','middlename','generation',
 9393:                    'permanentemail','id');
 9394:     }
 9395:     if ((ref($titlesref) eq 'HASH')) {
 9396:         %fieldtitles = %{$titlesref};
 9397:     } else {
 9398:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9399:     }
 9400:     my $output;
 9401:     my $css_class;
 9402:     if ($rowcount%2) {
 9403:         $css_class = 'LC_odd_row';
 9404:     }
 9405:     if ($customcss) {
 9406:         $css_class .= " $customcss";
 9407:     }
 9408:     $css_class =~ s/^\s+//;
 9409:     if ($css_class) {
 9410:         $css_class = ' class="'.$css_class.'"';
 9411:     }
 9412:     if ($rowstyle) {
 9413:         $css_class .= ' style="'.$rowstyle.'"';
 9414:     }
 9415:     if ($rowid) {
 9416:         $rowid = ' id="'.$rowid.'"';
 9417:     }
 9418:     $output = '<tr '.$css_class.$rowid.'>'.
 9419:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 9420:               '<td class="LC_left_item" colspan="2"><table>';
 9421:     my $rem;
 9422:     my %checks;
 9423:     my %current;
 9424:     if (ref($settings) eq 'HASH') {
 9425:         my $hashref;
 9426:         if ($context eq 'lti') {
 9427:             if (ref($settings) eq 'HASH') {
 9428:                 $hashref = $settings->{'instdata'};
 9429:             }
 9430:         } elsif (ref($settings->{$context}) eq 'HASH') {
 9431:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 9432:                 $hashref = $settings->{'lti_instdata'};
 9433:             }
 9434:             if ($role eq 'emailusername') {
 9435:                 if ($statustype) {
 9436:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 9437:                         $hashref = $settings->{$context}->{$role}->{$statustype};
 9438:                     }
 9439:                 }
 9440:             }
 9441:         }
 9442:         if (ref($hashref) eq 'HASH') { 
 9443:             foreach my $field (@fields) {
 9444:                 if ($hashref->{$field}) {
 9445:                     if ($role eq 'emailusername') {
 9446:                         $checks{$field} = $hashref->{$field};
 9447:                     } else {
 9448:                         $checks{$field} = ' checked="checked" ';
 9449:                     }
 9450:                 }
 9451:             }
 9452:         }
 9453:     }
 9454:  
 9455:     my $total = scalar(@fields);
 9456:     for (my $i=0; $i<$total; $i++) {
 9457:         $rem = $i%($numinrow);
 9458:         if ($rem == 0) {
 9459:             if ($i > 0) {
 9460:                 $output .= '</tr>';
 9461:             }
 9462:             $output .= '<tr>';
 9463:         }
 9464:         my $check = ' ';
 9465:         unless ($role eq 'emailusername') {
 9466:             if (exists($checks{$fields[$i]})) {
 9467:                 $check = $checks{$fields[$i]};
 9468:             } elsif ($context ne 'lti') {
 9469:                 if ($role eq 'st') {
 9470:                     if (ref($settings) ne 'HASH') {
 9471:                         $check = ' checked="checked" '; 
 9472:                     }
 9473:                 }
 9474:             }
 9475:         }
 9476:         $output .= '<td class="LC_left_item">'.
 9477:                    '<span class="LC_nobreak">';
 9478:         my $prefix = 'canmodify';
 9479:         if ($role eq 'emailusername') {
 9480:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 9481:                 $checks{$fields[$i]} = 'omit';
 9482:             }
 9483:             foreach my $option ('required','optional','omit') {
 9484:                 my $checked='';
 9485:                 if ($checks{$fields[$i]} eq $option) {
 9486:                     $checked='checked="checked" ';
 9487:                 }
 9488:                 $output .= '<label>'.
 9489:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 9490:                            &mt($option).'</label>'.('&nbsp;' x2);
 9491:             }
 9492:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 9493:         } else {
 9494:             if ($context eq 'lti') {
 9495:                 $prefix = 'lti';
 9496:             }
 9497:             $output .= '<label>'.
 9498:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
 9499:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 9500:                        '</label>';
 9501:         }
 9502:         $output .= '</span></td>';
 9503:     }
 9504:     $rem = $total%$numinrow;
 9505:     my $colsleft;
 9506:     if ($rem) {
 9507:         $colsleft = $numinrow - $rem;
 9508:     }
 9509:     if ($colsleft > 1) {
 9510:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9511:                    '&nbsp;</td>';
 9512:     } elsif ($colsleft == 1) {
 9513:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9514:     }
 9515:     $output .= '</tr></table></td></tr>';
 9516:     return $output;
 9517: }
 9518: 
 9519: sub insttypes_row {
 9520:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 9521:         $customcss,$rowstyle) = @_;
 9522:     my %lt = &Apache::lonlocal::texthash (
 9523:                       cansearch => 'Users allowed to search',
 9524:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 9525:                       lockablenames => 'User preference to lock name',
 9526:                       selfassign    => 'Self-reportable affiliations',
 9527:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 9528:              );
 9529:     my $showdom;
 9530:     if ($context eq 'cansearch') {
 9531:         $showdom = ' ('.$dom.')';
 9532:     }
 9533:     my $class = 'LC_left_item';
 9534:     if ($context eq 'statustocreate') {
 9535:         $class = 'LC_right_item';
 9536:     }
 9537:     my $css_class;
 9538:     if ($$rowtotal%2) {
 9539:         $css_class = 'LC_odd_row';
 9540:     }
 9541:     if ($customcss) {
 9542:         $css_class .= ' '.$customcss;
 9543:     }
 9544:     $css_class =~ s/^\s+//;
 9545:     if ($css_class) {
 9546:         $css_class = ' class="'.$css_class.'"';
 9547:     }
 9548:     if ($rowstyle) {
 9549:         $css_class .= ' style="'.$rowstyle.'"';
 9550:     }
 9551:     if ($onclick) {
 9552:         $onclick = 'onclick="'.$onclick.'" ';
 9553:     }
 9554:     my $output = '<tr'.$css_class.'>'.
 9555:                  '<td>'.$lt{$context}.$showdom.
 9556:                  '</td><td class="'.$class.'" colspan="2"><table>';
 9557:     my $rem;
 9558:     if (ref($types) eq 'ARRAY') {
 9559:         for (my $i=0; $i<@{$types}; $i++) {
 9560:             if (defined($usertypes->{$types->[$i]})) {
 9561:                 my $rem = $i%($numinrow);
 9562:                 if ($rem == 0) {
 9563:                     if ($i > 0) {
 9564:                         $output .= '</tr>';
 9565:                     }
 9566:                     $output .= '<tr>';
 9567:                 }
 9568:                 my $check = ' ';
 9569:                 if (ref($settings) eq 'HASH') {
 9570:                     if (ref($settings->{$context}) eq 'ARRAY') {
 9571:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 9572:                             $check = ' checked="checked" ';
 9573:                         }
 9574:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 9575:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 9576:                             $check = ' checked="checked" ';
 9577:                         }
 9578:                     } elsif ($context eq 'statustocreate') {
 9579:                         $check = ' checked="checked" ';
 9580:                     }
 9581:                 }
 9582:                 $output .= '<td class="LC_left_item">'.
 9583:                            '<span class="LC_nobreak"><label>'.
 9584:                            '<input type="checkbox" name="'.$context.'" '.
 9585:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 9586:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 9587:             }
 9588:         }
 9589:         $rem = @{$types}%($numinrow);
 9590:     }
 9591:     my $colsleft = $numinrow - $rem;
 9592:     if ($context eq 'overrides') {
 9593:         if ($colsleft > 1) {
 9594:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9595:         } else {
 9596:             $output .= '<td class="LC_left_item">';
 9597:         }
 9598:         $output .= '&nbsp;';  
 9599:     } else {
 9600:         if ($rem == 0) {
 9601:             $output .= '<tr>';
 9602:         }
 9603:         if ($colsleft > 1) {
 9604:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9605:         } else {
 9606:             $output .= '<td class="LC_left_item">';
 9607:         }
 9608:         my $defcheck = ' ';
 9609:         if (ref($settings) eq 'HASH') {  
 9610:             if (ref($settings->{$context}) eq 'ARRAY') {
 9611:                 if (grep(/^default$/,@{$settings->{$context}})) {
 9612:                     $defcheck = ' checked="checked" ';
 9613:                 }
 9614:             } elsif ($context eq 'statustocreate') {
 9615:                 $defcheck = ' checked="checked" ';
 9616:             }
 9617:         }
 9618:         $output .= '<span class="LC_nobreak"><label>'.
 9619:                    '<input type="checkbox" name="'.$context.'" '.
 9620:                    'value="default"'.$defcheck.$onclick.' />'.
 9621:                    $othertitle.'</label></span>';
 9622:     }
 9623:     $output .= '</td></tr></table></td></tr>';
 9624:     return $output;
 9625: }
 9626: 
 9627: sub sorted_searchtitles {
 9628:     my %searchtitles = &Apache::lonlocal::texthash(
 9629:                          'uname' => 'username',
 9630:                          'lastname' => 'last name',
 9631:                          'lastfirst' => 'last name, first name',
 9632:                      );
 9633:     my @titleorder = ('uname','lastname','lastfirst');
 9634:     return (\%searchtitles,\@titleorder);
 9635: }
 9636: 
 9637: sub sorted_searchtypes {
 9638:     my %srchtypes_desc = (
 9639:                            exact    => 'is exact match',
 9640:                            contains => 'contains ..',
 9641:                            begins   => 'begins with ..',
 9642:                          );
 9643:     my @srchtypeorder = ('exact','begins','contains');
 9644:     return (\%srchtypes_desc,\@srchtypeorder);
 9645: }
 9646: 
 9647: sub usertype_update_row {
 9648:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 9649:     my $datatable;
 9650:     my $numinrow = 4;
 9651:     foreach my $type (@{$types}) {
 9652:         if (defined($usertypes->{$type})) {
 9653:             $$rownums ++;
 9654:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 9655:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 9656:                           '</td><td class="LC_left_item"><table>';
 9657:             for (my $i=0; $i<@{$fields}; $i++) {
 9658:                 my $rem = $i%($numinrow);
 9659:                 if ($rem == 0) {
 9660:                     if ($i > 0) {
 9661:                         $datatable .= '</tr>';
 9662:                     }
 9663:                     $datatable .= '<tr>';
 9664:                 }
 9665:                 my $check = ' ';
 9666:                 if (ref($settings) eq 'HASH') {
 9667:                     if (ref($settings->{'fields'}) eq 'HASH') {
 9668:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 9669:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 9670:                                 $check = ' checked="checked" ';
 9671:                             }
 9672:                         }
 9673:                     }
 9674:                 }
 9675: 
 9676:                 if ($i == @{$fields}-1) {
 9677:                     my $colsleft = $numinrow - $rem;
 9678:                     if ($colsleft > 1) {
 9679:                         $datatable .= '<td colspan="'.$colsleft.'">';
 9680:                     } else {
 9681:                         $datatable .= '<td>';
 9682:                     }
 9683:                 } else {
 9684:                     $datatable .= '<td>';
 9685:                 }
 9686:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9687:                               '<input type="checkbox" name="updateable_'.$type.
 9688:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 9689:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 9690:             }
 9691:             $datatable .= '</tr></table></td></tr>';
 9692:         }
 9693:     }
 9694:     return $datatable;
 9695: }
 9696: 
 9697: sub modify_login {
 9698:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9699:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 9700:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 9701:     %title = ( coursecatalog => 'Display course catalog',
 9702:                adminmail => 'Display administrator E-mail address',
 9703:                helpdesk  => 'Display "Contact Helpdesk" link',
 9704:                newuser => 'Link for visitors to create a user account',
 9705:                loginheader => 'Log-in box header');
 9706:     @offon = ('off','on');
 9707:     if (ref($domconfig{login}) eq 'HASH') {
 9708:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 9709:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 9710:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 9711:             }
 9712:         }
 9713:     }
 9714:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 9715:                                            \%domconfig,\%loginhash);
 9716:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9717:     foreach my $item (@toggles) {
 9718:         $loginhash{login}{$item} = $env{'form.'.$item};
 9719:     }
 9720:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 9721:     if (ref($colchanges{'login'}) eq 'HASH') {  
 9722:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 9723:                                          \%loginhash);
 9724:     }
 9725: 
 9726:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9727:     my %domservers = &Apache::lonnet::get_servers($dom);
 9728:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 9729:     if (keys(%servers) > 1) {
 9730:         foreach my $lonhost (keys(%servers)) {
 9731:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 9732:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 9733:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 9734:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 9735:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 9736:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9737:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9738:                         $changes{'loginvia'}{$lonhost} = 1;
 9739:                     } else {
 9740:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 9741:                         $changes{'loginvia'}{$lonhost} = 1;
 9742:                     }
 9743:                 } else {
 9744:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9745:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9746:                         $changes{'loginvia'}{$lonhost} = 1;
 9747:                     }
 9748:                 }
 9749:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 9750:                     foreach my $item (@loginvia_attribs) {
 9751:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 9752:                     }
 9753:                 } else {
 9754:                     foreach my $item (@loginvia_attribs) {
 9755:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9756:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9757:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 9758:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9759:                                 $new = '/';
 9760:                             }
 9761:                         }
 9762:                         if (($item eq 'custompath') && 
 9763:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9764:                             $new = '';
 9765:                         }
 9766:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 9767:                             $changes{'loginvia'}{$lonhost} = 1;
 9768:                         }
 9769:                         if ($item eq 'exempt') {
 9770:                             $new = &check_exempt_addresses($new);
 9771:                         }
 9772:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9773:                     }
 9774:                 }
 9775:             } else {
 9776:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9777:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9778:                     $changes{'loginvia'}{$lonhost} = 1;
 9779:                     foreach my $item (@loginvia_attribs) {
 9780:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9781:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9782:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9783:                                 $new = '/';
 9784:                             }
 9785:                         }
 9786:                         if (($item eq 'custompath') && 
 9787:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9788:                             $new = '';
 9789:                         }
 9790:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9791:                     }
 9792:                 }
 9793:             }
 9794:         }
 9795:     }
 9796: 
 9797:     my $servadm = $r->dir_config('lonAdmEMail');
 9798:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 9799:     if (ref($domconfig{'login'}) eq 'HASH') {
 9800:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 9801:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 9802:                 if ($lang eq 'nolang') {
 9803:                     push(@currlangs,$lang);
 9804:                 } elsif (defined($langchoices{$lang})) {
 9805:                     push(@currlangs,$lang);
 9806:                 } else {
 9807:                     next;
 9808:                 }
 9809:             }
 9810:         }
 9811:     }
 9812:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 9813:     if (@currlangs > 0) {
 9814:         foreach my $lang (@currlangs) {
 9815:             if (grep(/^\Q$lang\E$/,@delurls)) {
 9816:                 $changes{'helpurl'}{$lang} = 1;
 9817:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 9818:                 $changes{'helpurl'}{$lang} = 1;
 9819:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 9820:                 push(@newlangs,$lang);
 9821:             } else {
 9822:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9823:             }
 9824:         }
 9825:     }
 9826:     unless (grep(/^nolang$/,@currlangs)) {
 9827:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 9828:             $changes{'helpurl'}{'nolang'} = 1;
 9829:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 9830:             push(@newlangs,'nolang');
 9831:         }
 9832:     }
 9833:     if ($env{'form.loginhelpurl_add_lang'}) {
 9834:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 9835:             ($env{'form.loginhelpurl_add_file.filename'})) {
 9836:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 9837:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 9838:         }
 9839:     }
 9840:     if ((@newlangs > 0) || ($addedfile)) {
 9841:         my $error;
 9842:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9843:         if ($configuserok eq 'ok') {
 9844:             if ($switchserver) {
 9845:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 9846:             } elsif ($author_ok eq 'ok') {
 9847:                 my @allnew = @newlangs;
 9848:                 if ($addedfile ne '') {
 9849:                     push(@allnew,$addedfile);
 9850:                 }
 9851:                 foreach my $lang (@allnew) {
 9852:                     my $formelem = 'loginhelpurl_'.$lang;
 9853:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 9854:                         $formelem = 'loginhelpurl_add_file';
 9855:                     }
 9856:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9857:                                                                "help/$lang",'','',$newfile{$lang});
 9858:                     if ($result eq 'ok') {
 9859:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 9860:                         $changes{'helpurl'}{$lang} = 1;
 9861:                     } else {
 9862:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 9863:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9864:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 9865:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 9866:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9867:                         }
 9868:                     }
 9869:                 }
 9870:             } else {
 9871:                 $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);
 9872:             }
 9873:         } else {
 9874:             $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);
 9875:         }
 9876:         if ($error) {
 9877:             &Apache::lonnet::logthis($error);
 9878:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9879:         }
 9880:     }
 9881: 
 9882:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 9883:     if (ref($domconfig{'login'}) eq 'HASH') {
 9884:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 9885:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 9886:                 if ($domservers{$lonhost}) {
 9887:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9888:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 9889:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 9890:                     }
 9891:                 }
 9892:             }
 9893:         }
 9894:     }
 9895:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 9896:     foreach my $lonhost (sort(keys(%domservers))) {
 9897:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9898:             $changes{'headtag'}{$lonhost} = 1;
 9899:         } else {
 9900:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 9901:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 9902:             }
 9903:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 9904:                 push(@newhosts,$lonhost);
 9905:             } elsif ($currheadtagurls{$lonhost}) {
 9906:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 9907:                 if ($currexempt{$lonhost}) {
 9908:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 9909:                         $changes{'headtag'}{$lonhost} = 1;
 9910:                     }
 9911:                 } elsif ($possexempt{$lonhost}) {
 9912:                     $changes{'headtag'}{$lonhost} = 1;
 9913:                 }
 9914:                 if ($possexempt{$lonhost}) {
 9915:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9916:                 }
 9917:             }
 9918:         }
 9919:     }
 9920:     if (@newhosts) {
 9921:         my $error;
 9922:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9923:         if ($configuserok eq 'ok') {
 9924:             if ($switchserver) {
 9925:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 9926:             } elsif ($author_ok eq 'ok') {
 9927:                 foreach my $lonhost (@newhosts) {
 9928:                     my $formelem = 'loginheadtag_'.$lonhost;
 9929:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9930:                                                                           "login/headtag/$lonhost",'','',
 9931:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 9932:                     if ($result eq 'ok') {
 9933:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 9934:                         $changes{'headtag'}{$lonhost} = 1;
 9935:                         if ($possexempt{$lonhost}) {
 9936:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9937:                         }
 9938:                     } else {
 9939:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 9940:                                            $newheadtagurls{$lonhost},$result);
 9941:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9942:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 9943:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 9944:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 9945:                         }
 9946:                     }
 9947:                 }
 9948:             } else {
 9949:                 $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);
 9950:             }
 9951:         } else {
 9952:             $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);
 9953:         }
 9954:         if ($error) {
 9955:             &Apache::lonnet::logthis($error);
 9956:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9957:         }
 9958:     }
 9959:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 9960: 
 9961:     my $defaulthelpfile = '/adm/loginproblems.html';
 9962:     my $defaulttext = &mt('Default in use');
 9963: 
 9964:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 9965:                                              $dom);
 9966:     if ($putresult eq 'ok') {
 9967:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9968:         my %defaultchecked = (
 9969:                     'coursecatalog' => 'on',
 9970:                     'helpdesk'      => 'on',
 9971:                     'adminmail'     => 'off',
 9972:                     'newuser'       => 'off',
 9973:         );
 9974:         if (ref($domconfig{'login'}) eq 'HASH') {
 9975:             foreach my $item (@toggles) {
 9976:                 if ($defaultchecked{$item} eq 'on') { 
 9977:                     if (($domconfig{'login'}{$item} eq '0') &&
 9978:                         ($env{'form.'.$item} eq '1')) {
 9979:                         $changes{$item} = 1;
 9980:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9981:                               $domconfig{'login'}{$item} eq '1') &&
 9982:                              ($env{'form.'.$item} eq '0')) {
 9983:                         $changes{$item} = 1;
 9984:                     }
 9985:                 } elsif ($defaultchecked{$item} eq 'off') {
 9986:                     if (($domconfig{'login'}{$item} eq '1') &&
 9987:                         ($env{'form.'.$item} eq '0')) {
 9988:                         $changes{$item} = 1;
 9989:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9990:                               $domconfig{'login'}{$item} eq '0') &&
 9991:                              ($env{'form.'.$item} eq '1')) {
 9992:                         $changes{$item} = 1;
 9993:                     }
 9994:                 }
 9995:             }
 9996:         }
 9997:         if (keys(%changes) > 0 || $colchgtext) {
 9998:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9999:             if (ref($lastactref) eq 'HASH') {
10000:                 $lastactref->{'domainconfig'} = 1;
10001:             }
10002:             $resulttext = &mt('Changes made:').'<ul>';
10003:             foreach my $item (sort(keys(%changes))) {
10004:                 if ($item eq 'loginvia') {
10005:                     if (ref($changes{$item}) eq 'HASH') {
10006:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
10007:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
10008:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
10009:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
10010:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
10011:                                     $protocol = 'http' if ($protocol ne 'https');
10012:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
10013: 
10014:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
10015:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
10016:                                     } else {
10017:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
10018:                                     }
10019:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
10020:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
10021:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
10022:                                     }
10023:                                     $resulttext .= '</li>';
10024:                                 } else {
10025:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
10026:                                 }
10027:                             } else {
10028:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
10029:                             }
10030:                         }
10031:                         $resulttext .= '</ul></li>';
10032:                     }
10033:                 } elsif ($item eq 'helpurl') {
10034:                     if (ref($changes{$item}) eq 'HASH') {
10035:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
10036:                             if (grep(/^\Q$lang\E$/,@delurls)) {
10037:                                 my ($chg,$link);
10038:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
10039:                                 if ($lang eq 'nolang') {
10040:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
10041:                                 } else {
10042:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
10043:                                 }
10044:                                 $resulttext .= '<li>'.$chg.'</li>';
10045:                             } else {
10046:                                 my $chg;
10047:                                 if ($lang eq 'nolang') {
10048:                                     $chg = &mt('custom log-in help file for no preferred language');
10049:                                 } else {
10050:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
10051:                                 }
10052:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
10053:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
10054:                                                       '?inhibitmenu=yes',$chg,600,500).
10055:                                                '</li>';
10056:                             }
10057:                         }
10058:                     }
10059:                 } elsif ($item eq 'headtag') {
10060:                     if (ref($changes{$item}) eq 'HASH') {
10061:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
10062:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
10063:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
10064:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
10065:                                 $resulttext .= '<li><a href="'.
10066:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
10067:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
10068:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
10069:                                 if ($possexempt{$lonhost}) {
10070:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
10071:                                 } else {
10072:                                     $resulttext .= &mt('included for any client IP');
10073:                                 }
10074:                                 $resulttext .= '</li>';
10075:                             }
10076:                         }
10077:                     }
10078:                 } elsif ($item eq 'captcha') {
10079:                     if (ref($loginhash{'login'}) eq 'HASH') {
10080:                         my $chgtxt;
10081:                         if ($loginhash{'login'}{$item} eq 'notused') {
10082:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
10083:                         } else {
10084:                             my %captchas = &captcha_phrases();
10085:                             if ($captchas{$loginhash{'login'}{$item}}) {
10086:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
10087:                             } else {
10088:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
10089:                             }
10090:                         }
10091:                         $resulttext .= '<li>'.$chgtxt.'</li>';
10092:                     }
10093:                 } elsif ($item eq 'recaptchakeys') {
10094:                     if (ref($loginhash{'login'}) eq 'HASH') {
10095:                         my ($privkey,$pubkey);
10096:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
10097:                             $pubkey = $loginhash{'login'}{$item}{'public'};
10098:                             $privkey = $loginhash{'login'}{$item}{'private'};
10099:                         }
10100:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
10101:                         if (!$pubkey) {
10102:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
10103:                         } else {
10104:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
10105:                         }
10106:                         if (!$privkey) {
10107:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
10108:                         } else {
10109:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
10110:                         }
10111:                         $chgtxt .= '</ul>';
10112:                         $resulttext .= '<li>'.$chgtxt.'</li>';
10113:                     }
10114:                 } elsif ($item eq 'recaptchaversion') {
10115:                     if (ref($loginhash{'login'}) eq 'HASH') {
10116:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
10117:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
10118:                                            '</li>';
10119:                         }
10120:                     }
10121:                 } else {
10122:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
10123:                 }
10124:             }
10125:             $resulttext .= $colchgtext.'</ul>';
10126:         } else {
10127:             $resulttext = &mt('No changes made to log-in page settings');
10128:         }
10129:     } else {
10130:         $resulttext = '<span class="LC_error">'.
10131: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10132:     }
10133:     if ($errors) {
10134:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
10135:                        $errors.'</ul>';
10136:     }
10137:     return $resulttext;
10138: }
10139: 
10140: sub check_exempt_addresses {
10141:     my ($iplist) = @_;
10142:     $iplist =~ s/^\s+//;
10143:     $iplist =~ s/\s+$//;
10144:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
10145:     my (@okips,$new);
10146:     foreach my $ip (@poss_ips) {
10147:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
10148:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
10149:                 push(@okips,$ip);
10150:             }
10151:         }
10152:     }
10153:     if (@okips > 0) {
10154:         $new = join(',',@okips);
10155:     } else {
10156:         $new = '';
10157:     }
10158:     return $new;
10159: }
10160: 
10161: sub color_font_choices {
10162:     my %choices =
10163:         &Apache::lonlocal::texthash (
10164:             img => "Header",
10165:             bgs => "Background colors",
10166:             links => "Link colors",
10167:             images => "Images",
10168:             font => "Font color",
10169:             fontmenu => "Font menu",
10170:             pgbg => "Page",
10171:             tabbg => "Header",
10172:             sidebg => "Border",
10173:             link => "Link",
10174:             alink => "Active link",
10175:             vlink => "Visited link",
10176:         );
10177:     return %choices;
10178: }
10179: 
10180: sub modify_rolecolors {
10181:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
10182:     my ($resulttext,%rolehash);
10183:     $rolehash{'rolecolors'} = {};
10184:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
10185:         if ($domconfig{'rolecolors'} eq '') {
10186:             $domconfig{'rolecolors'} = {};
10187:         }
10188:     }
10189:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
10190:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
10191:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
10192:                                              $dom);
10193:     if ($putresult eq 'ok') {
10194:         if (keys(%changes) > 0) {
10195:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10196:             if (ref($lastactref) eq 'HASH') {
10197:                 $lastactref->{'domainconfig'} = 1;
10198:             }
10199:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
10200:                                              $rolehash{'rolecolors'});
10201:         } else {
10202:             $resulttext = &mt('No changes made to default color schemes');
10203:         }
10204:     } else {
10205:         $resulttext = '<span class="LC_error">'.
10206: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10207:     }
10208:     if ($errors) {
10209:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10210:                        $errors.'</ul>';
10211:     }
10212:     return $resulttext;
10213: }
10214: 
10215: sub modify_colors {
10216:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
10217:     my (%changes,%choices);
10218:     my @bgs;
10219:     my @links = ('link','alink','vlink');
10220:     my @logintext;
10221:     my @images;
10222:     my $servadm = $r->dir_config('lonAdmEMail');
10223:     my $errors;
10224:     my %defaults;
10225:     foreach my $role (@{$roles}) {
10226:         if ($role eq 'login') {
10227:             %choices = &login_choices();
10228:             @logintext = ('textcol','bgcol');
10229:         } else {
10230:             %choices = &color_font_choices();
10231:         }
10232:         if ($role eq 'login') {
10233:             @images = ('img','logo','domlogo','login');
10234:             @bgs = ('pgbg','mainbg','sidebg');
10235:         } else {
10236:             @images = ('img');
10237:             @bgs = ('pgbg','tabbg','sidebg');
10238:         }
10239:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
10240:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
10241:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
10242:         }
10243:         if ($role eq 'login') {
10244:             foreach my $item (@logintext) {
10245:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10246:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10247:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10248:                 }
10249:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
10250:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10251:                 }
10252:             }
10253:         } else {
10254:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
10255:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
10256:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
10257:             }
10258:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
10259:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
10260:             }
10261:         }
10262:         foreach my $item (@bgs) {
10263:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10264:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10265:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10266:             }
10267:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
10268:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10269:             }
10270:         }
10271:         foreach my $item (@links) {
10272:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10273:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10274:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10275:             }
10276:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
10277:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10278:             }
10279:         }
10280:         my ($configuserok,$author_ok,$switchserver) = 
10281:             &config_check($dom,$confname,$servadm);
10282:         my ($width,$height) = &thumb_dimensions();
10283:         if (ref($domconfig->{$role}) ne 'HASH') {
10284:             $domconfig->{$role} = {};
10285:         }
10286:         foreach my $img (@images) {
10287:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
10288:                 if (defined($env{'form.login_showlogo_'.$img})) {
10289:                     $confhash->{$role}{'showlogo'}{$img} = 1;
10290:                 } else { 
10291:                     $confhash->{$role}{'showlogo'}{$img} = 0;
10292:                 }
10293:             } 
10294: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
10295: 		 && !defined($domconfig->{$role}{$img})
10296: 		 && !$env{'form.'.$role.'_del_'.$img}
10297: 		 && $env{'form.'.$role.'_import_'.$img}) {
10298: 		# import the old configured image from the .tab setting
10299: 		# if they haven't provided a new one 
10300: 		$domconfig->{$role}{$img} = 
10301: 		    $env{'form.'.$role.'_import_'.$img};
10302: 	    }
10303:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
10304:                 my $error;
10305:                 if ($configuserok eq 'ok') {
10306:                     if ($switchserver) {
10307:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
10308:                     } else {
10309:                         if ($author_ok eq 'ok') {
10310:                             my ($result,$logourl) = 
10311:                                 &publishlogo($r,'upload',$role.'_'.$img,
10312:                                            $dom,$confname,$img,$width,$height);
10313:                             if ($result eq 'ok') {
10314:                                 $confhash->{$role}{$img} = $logourl;
10315:                                 $changes{$role}{'images'}{$img} = 1;
10316:                             } else {
10317:                                 $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);
10318:                             }
10319:                         } else {
10320:                             $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);
10321:                         }
10322:                     }
10323:                 } else {
10324:                     $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);
10325:                 }
10326:                 if ($error) {
10327:                     &Apache::lonnet::logthis($error);
10328:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10329:                 }
10330:             } elsif ($domconfig->{$role}{$img} ne '') {
10331:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
10332:                     my $error;
10333:                     if ($configuserok eq 'ok') {
10334: # is confname an author?
10335:                         if ($switchserver eq '') {
10336:                             if ($author_ok eq 'ok') {
10337:                                 my ($result,$logourl) = 
10338:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
10339:                                             $dom,$confname,$img,$width,$height);
10340:                                 if ($result eq 'ok') {
10341:                                     $confhash->{$role}{$img} = $logourl;
10342: 				    $changes{$role}{'images'}{$img} = 1;
10343:                                 }
10344:                             }
10345:                         }
10346:                     }
10347:                 }
10348:             }
10349:         }
10350:         if (ref($domconfig) eq 'HASH') {
10351:             if (ref($domconfig->{$role}) eq 'HASH') {
10352:                 foreach my $img (@images) {
10353:                     if ($domconfig->{$role}{$img} ne '') {
10354:                         if ($env{'form.'.$role.'_del_'.$img}) {
10355:                             $confhash->{$role}{$img} = '';
10356:                             $changes{$role}{'images'}{$img} = 1;
10357:                         } else {
10358:                             if ($confhash->{$role}{$img} eq '') {
10359:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
10360:                             }
10361:                         }
10362:                     } else {
10363:                         if ($env{'form.'.$role.'_del_'.$img}) {
10364:                             $confhash->{$role}{$img} = '';
10365:                             $changes{$role}{'images'}{$img} = 1;
10366:                         } 
10367:                     }
10368:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
10369:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
10370:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
10371:                                 $domconfig->{$role}{'showlogo'}{$img}) {
10372:                                 $changes{$role}{'showlogo'}{$img} = 1; 
10373:                             }
10374:                         } else {
10375:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10376:                                 $changes{$role}{'showlogo'}{$img} = 1;
10377:                             }
10378:                         }
10379:                     }
10380:                 }
10381:                 if ($domconfig->{$role}{'font'} ne '') {
10382:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
10383:                         $changes{$role}{'font'} = 1;
10384:                     }
10385:                 } else {
10386:                     if ($confhash->{$role}{'font'}) {
10387:                         $changes{$role}{'font'} = 1;
10388:                     }
10389:                 }
10390:                 if ($role ne 'login') {
10391:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
10392:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
10393:                             $changes{$role}{'fontmenu'} = 1;
10394:                         }
10395:                     } else {
10396:                         if ($confhash->{$role}{'fontmenu'}) {
10397:                             $changes{$role}{'fontmenu'} = 1;
10398:                         }
10399:                     }
10400:                 }
10401:                 foreach my $item (@bgs) {
10402:                     if ($domconfig->{$role}{$item} ne '') {
10403:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10404:                             $changes{$role}{'bgs'}{$item} = 1;
10405:                         } 
10406:                     } else {
10407:                         if ($confhash->{$role}{$item}) {
10408:                             $changes{$role}{'bgs'}{$item} = 1;
10409:                         }
10410:                     }
10411:                 }
10412:                 foreach my $item (@links) {
10413:                     if ($domconfig->{$role}{$item} ne '') {
10414:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10415:                             $changes{$role}{'links'}{$item} = 1;
10416:                         }
10417:                     } else {
10418:                         if ($confhash->{$role}{$item}) {
10419:                             $changes{$role}{'links'}{$item} = 1;
10420:                         }
10421:                     }
10422:                 }
10423:                 foreach my $item (@logintext) {
10424:                     if ($domconfig->{$role}{$item} ne '') {
10425:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10426:                             $changes{$role}{'logintext'}{$item} = 1;
10427:                         }
10428:                     } else {
10429:                         if ($confhash->{$role}{$item}) {
10430:                             $changes{$role}{'logintext'}{$item} = 1;
10431:                         }
10432:                     }
10433:                 }
10434:             } else {
10435:                 &default_change_checker($role,\@images,\@links,\@bgs,
10436:                                         \@logintext,$confhash,\%changes); 
10437:             }
10438:         } else {
10439:             &default_change_checker($role,\@images,\@links,\@bgs,
10440:                                     \@logintext,$confhash,\%changes); 
10441:         }
10442:     }
10443:     return ($errors,%changes);
10444: }
10445: 
10446: sub config_check {
10447:     my ($dom,$confname,$servadm) = @_;
10448:     my ($configuserok,$author_ok,$switchserver,%currroles);
10449:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
10450:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
10451:                                                    $confname,$servadm);
10452:     if ($configuserok eq 'ok') {
10453:         $switchserver = &check_switchserver($dom,$confname);
10454:         if ($switchserver eq '') {
10455:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
10456:         }
10457:     }
10458:     return ($configuserok,$author_ok,$switchserver);
10459: }
10460: 
10461: sub default_change_checker {
10462:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
10463:     foreach my $item (@{$links}) {
10464:         if ($confhash->{$role}{$item}) {
10465:             $changes->{$role}{'links'}{$item} = 1;
10466:         }
10467:     }
10468:     foreach my $item (@{$bgs}) {
10469:         if ($confhash->{$role}{$item}) {
10470:             $changes->{$role}{'bgs'}{$item} = 1;
10471:         }
10472:     }
10473:     foreach my $item (@{$logintext}) {
10474:         if ($confhash->{$role}{$item}) {
10475:             $changes->{$role}{'logintext'}{$item} = 1;
10476:         }
10477:     }
10478:     foreach my $img (@{$images}) {
10479:         if ($env{'form.'.$role.'_del_'.$img}) {
10480:             $confhash->{$role}{$img} = '';
10481:             $changes->{$role}{'images'}{$img} = 1;
10482:         }
10483:         if ($role eq 'login') {
10484:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10485:                 $changes->{$role}{'showlogo'}{$img} = 1;
10486:             }
10487:         }
10488:     }
10489:     if ($confhash->{$role}{'font'}) {
10490:         $changes->{$role}{'font'} = 1;
10491:     }
10492: }
10493: 
10494: sub display_colorchgs {
10495:     my ($dom,$changes,$roles,$confhash) = @_;
10496:     my (%choices,$resulttext);
10497:     if (!grep(/^login$/,@{$roles})) {
10498:         $resulttext = &mt('Changes made:').'<br />';
10499:     }
10500:     foreach my $role (@{$roles}) {
10501:         if ($role eq 'login') {
10502:             %choices = &login_choices();
10503:         } else {
10504:             %choices = &color_font_choices();
10505:         }
10506:         if (ref($changes->{$role}) eq 'HASH') {
10507:             if ($role ne 'login') {
10508:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
10509:             }
10510:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
10511:                 if ($role ne 'login') {
10512:                     $resulttext .= '<ul>';
10513:                 }
10514:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
10515:                     if ($role ne 'login') {
10516:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
10517:                     }
10518:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
10519:                         if (($role eq 'login') && ($key eq 'showlogo')) {
10520:                             if ($confhash->{$role}{$key}{$item}) {
10521:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
10522:                             } else {
10523:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
10524:                             }
10525:                         } elsif ($confhash->{$role}{$item} eq '') {
10526:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
10527:                         } else {
10528:                             my $newitem = $confhash->{$role}{$item};
10529:                             if ($key eq 'images') {
10530:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
10531:                             }
10532:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
10533:                         }
10534:                     }
10535:                     if ($role ne 'login') {
10536:                         $resulttext .= '</ul></li>';
10537:                     }
10538:                 } else {
10539:                     if ($confhash->{$role}{$key} eq '') {
10540:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
10541:                     } else {
10542:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
10543:                     }
10544:                 }
10545:                 if ($role ne 'login') {
10546:                     $resulttext .= '</ul>';
10547:                 }
10548:             }
10549:         }
10550:     }
10551:     return $resulttext;
10552: }
10553: 
10554: sub thumb_dimensions {
10555:     return ('200','50');
10556: }
10557: 
10558: sub check_dimensions {
10559:     my ($inputfile) = @_;
10560:     my ($fullwidth,$fullheight);
10561:     if ($inputfile =~ m|^[/\w.\-]+$|) {
10562:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
10563:             my $imageinfo = <PIPE>;
10564:             if (!close(PIPE)) {
10565:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
10566:             }
10567:             chomp($imageinfo);
10568:             my ($fullsize) = 
10569:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
10570:             if ($fullsize) {
10571:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
10572:             }
10573:         }
10574:     }
10575:     return ($fullwidth,$fullheight);
10576: }
10577: 
10578: sub check_configuser {
10579:     my ($uhome,$dom,$confname,$servadm) = @_;
10580:     my ($configuserok,%currroles);
10581:     if ($uhome eq 'no_host') {
10582:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
10583:         my $configpass = &LONCAPA::Enrollment::create_password();
10584:         $configuserok = 
10585:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
10586:                              $configpass,'','','','','',undef,$servadm);
10587:     } else {
10588:         $configuserok = 'ok';
10589:         %currroles = 
10590:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
10591:     }
10592:     return ($configuserok,%currroles);
10593: }
10594: 
10595: sub check_authorstatus {
10596:     my ($dom,$confname,%currroles) = @_;
10597:     my $author_ok;
10598:     if (!$currroles{':'.$dom.':au'}) {
10599:         my $start = time;
10600:         my $end = 0;
10601:         $author_ok = 
10602:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
10603:                                         'au',$end,$start,'','','domconfig');
10604:     } else {
10605:         $author_ok = 'ok';
10606:     }
10607:     return $author_ok;
10608: }
10609: 
10610: sub publishlogo {
10611:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
10612:     my ($output,$fname,$logourl,$madethumb);
10613:     if ($action eq 'upload') {
10614:         $fname=$env{'form.'.$formname.'.filename'};
10615:         chop($env{'form.'.$formname});
10616:     } else {
10617:         ($fname) = ($formname =~ /([^\/]+)$/);
10618:     }
10619:     if ($savefileas ne '') {
10620:         $fname = $savefileas;
10621:     }
10622:     $fname=&Apache::lonnet::clean_filename($fname);
10623: # See if there is anything left
10624:     unless ($fname) { return ('error: no uploaded file'); }
10625:     $fname="$subdir/$fname";
10626:     my $docroot=$r->dir_config('lonDocRoot');
10627:     my $filepath="$docroot/priv";
10628:     my $relpath = "$dom/$confname";
10629:     my ($fnamepath,$file,$fetchthumb);
10630:     $file=$fname;
10631:     if ($fname=~m|/|) {
10632:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
10633:     }
10634:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
10635:     my $count;
10636:     for ($count=5;$count<=$#parts;$count++) {
10637:         $filepath.="/$parts[$count]";
10638:         if ((-e $filepath)!=1) {
10639:             mkdir($filepath,02770);
10640:         }
10641:     }
10642:     # Check for bad extension and disallow upload
10643:     if ($file=~/\.(\w+)$/ &&
10644:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
10645:         $output = 
10646:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
10647:     } elsif ($file=~/\.(\w+)$/ &&
10648:         !defined(&Apache::loncommon::fileembstyle($1))) {
10649:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
10650:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
10651:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
10652:     } elsif (-d "$filepath/$file") {
10653:         $output = &mt('Filename is a directory name - rename the file and re-upload');
10654:     } else {
10655:         my $source = $filepath.'/'.$file;
10656:         my $logfile;
10657:         if (!open($logfile,">>",$source.'.log')) {
10658:             return (&mt('No write permission to Authoring Space'));
10659:         }
10660:         print $logfile
10661: "\n================= Publish ".localtime()." ================\n".
10662: $env{'user.name'}.':'.$env{'user.domain'}."\n";
10663: # Save the file
10664:         if (!open(FH,">",$source)) {
10665:             &Apache::lonnet::logthis('Failed to create '.$source);
10666:             return (&mt('Failed to create file'));
10667:         }
10668:         if ($action eq 'upload') {
10669:             if (!print FH ($env{'form.'.$formname})) {
10670:                 &Apache::lonnet::logthis('Failed to write to '.$source);
10671:                 return (&mt('Failed to write file'));
10672:             }
10673:         } else {
10674:             my $original = &Apache::lonnet::filelocation('',$formname);
10675:             if(!copy($original,$source)) {
10676:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
10677:                 return (&mt('Failed to write file'));
10678:             }
10679:         }
10680:         close(FH);
10681:         chmod(0660, $source); # Permissions to rw-rw---.
10682: 
10683:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
10684:         my $copyfile=$targetdir.'/'.$file;
10685: 
10686:         my @parts=split(/\//,$targetdir);
10687:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
10688:         for (my $count=5;$count<=$#parts;$count++) {
10689:             $path.="/$parts[$count]";
10690:             if (!-e $path) {
10691:                 print $logfile "\nCreating directory ".$path;
10692:                 mkdir($path,02770);
10693:             }
10694:         }
10695:         my $versionresult;
10696:         if (-e $copyfile) {
10697:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
10698:         } else {
10699:             $versionresult = 'ok';
10700:         }
10701:         if ($versionresult eq 'ok') {
10702:             if (copy($source,$copyfile)) {
10703:                 print $logfile "\nCopied original source to ".$copyfile."\n";
10704:                 $output = 'ok';
10705:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
10706:                 push(@{$modified_urls},[$copyfile,$source]);
10707:                 my $metaoutput = 
10708:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
10709:                 unless ($registered_cleanup) {
10710:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10711:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10712:                     $registered_cleanup=1;
10713:                 }
10714:             } else {
10715:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
10716:                 $output = &mt('Failed to copy file to RES space').", $!";
10717:             }
10718:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
10719:                 my $inputfile = $filepath.'/'.$file;
10720:                 my $outfile = $filepath.'/'.'tn-'.$file;
10721:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
10722:                 if ($fullwidth ne '' && $fullheight ne '') { 
10723:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
10724:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
10725:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
10726:                         system({$args[0]} @args);
10727:                         chmod(0660, $filepath.'/tn-'.$file);
10728:                         if (-e $outfile) {
10729:                             my $copyfile=$targetdir.'/tn-'.$file;
10730:                             if (copy($outfile,$copyfile)) {
10731:                                 print $logfile "\nCopied source to ".$copyfile."\n";
10732:                                 my $thumb_metaoutput = 
10733:                                     &write_metadata($dom,$confname,$formname,
10734:                                                     $targetdir,'tn-'.$file,$logfile);
10735:                                 push(@{$modified_urls},[$copyfile,$outfile]);
10736:                                 unless ($registered_cleanup) {
10737:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10738:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10739:                                     $registered_cleanup=1;
10740:                                 }
10741:                                 $madethumb = 1;
10742:                             } else {
10743:                                 print $logfile "\nUnable to write ".$copyfile.
10744:                                                ':'.$!."\n";
10745:                             }
10746:                         }
10747:                     }
10748:                 }
10749:             }
10750:         } else {
10751:             $output = $versionresult;
10752:         }
10753:     }
10754:     return ($output,$logourl,$madethumb);
10755: }
10756: 
10757: sub logo_versioning {
10758:     my ($targetdir,$file,$logfile) = @_;
10759:     my $target = $targetdir.'/'.$file;
10760:     my ($maxversion,$fn,$extn,$output);
10761:     $maxversion = 0;
10762:     if ($file =~ /^(.+)\.(\w+)$/) {
10763:         $fn=$1;
10764:         $extn=$2;
10765:     }
10766:     opendir(DIR,$targetdir);
10767:     while (my $filename=readdir(DIR)) {
10768:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
10769:             $maxversion=($1>$maxversion)?$1:$maxversion;
10770:         }
10771:     }
10772:     $maxversion++;
10773:     print $logfile "\nCreating old version ".$maxversion."\n";
10774:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
10775:     if (copy($target,$copyfile)) {
10776:         print $logfile "Copied old target to ".$copyfile."\n";
10777:         $copyfile=$copyfile.'.meta';
10778:         if (copy($target.'.meta',$copyfile)) {
10779:             print $logfile "Copied old target metadata to ".$copyfile."\n";
10780:             $output = 'ok';
10781:         } else {
10782:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
10783:             $output = &mt('Failed to copy old meta').", $!, ";
10784:         }
10785:     } else {
10786:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
10787:         $output = &mt('Failed to copy old target').", $!, ";
10788:     }
10789:     return $output;
10790: }
10791: 
10792: sub write_metadata {
10793:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
10794:     my (%metadatafields,%metadatakeys,$output);
10795:     $metadatafields{'title'}=$formname;
10796:     $metadatafields{'creationdate'}=time;
10797:     $metadatafields{'lastrevisiondate'}=time;
10798:     $metadatafields{'copyright'}='public';
10799:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
10800:                                          $env{'user.domain'};
10801:     $metadatafields{'authorspace'}=$confname.':'.$dom;
10802:     $metadatafields{'domain'}=$dom;
10803:     {
10804:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
10805:         my $mfh;
10806:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
10807:             foreach (sort(keys(%metadatafields))) {
10808:                 unless ($_=~/\./) {
10809:                     my $unikey=$_;
10810:                     $unikey=~/^([A-Za-z]+)/;
10811:                     my $tag=$1;
10812:                     $tag=~tr/A-Z/a-z/;
10813:                     print $mfh "\n\<$tag";
10814:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
10815:                         my $value=$metadatafields{$unikey.'.'.$_};
10816:                         $value=~s/\"/\'\'/g;
10817:                         print $mfh ' '.$_.'="'.$value.'"';
10818:                     }
10819:                     print $mfh '>'.
10820:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
10821:                             .'</'.$tag.'>';
10822:                 }
10823:             }
10824:             $output = 'ok';
10825:             print $logfile "\nWrote metadata";
10826:             close($mfh);
10827:         } else {
10828:             print $logfile "\nFailed to open metadata file";
10829:             $output = &mt('Could not write metadata');
10830:         }
10831:     }
10832:     return $output;
10833: }
10834: 
10835: sub notifysubscribed {
10836:     foreach my $targetsource (@{$modified_urls}){
10837:         next unless (ref($targetsource) eq 'ARRAY');
10838:         my ($target,$source)=@{$targetsource};
10839:         if ($source ne '') {
10840:             if (open(my $logfh,">>",$source.'.log')) {
10841:                 print $logfh "\nCleanup phase: Notifications\n";
10842:                 my @subscribed=&subscribed_hosts($target);
10843:                 foreach my $subhost (@subscribed) {
10844:                     print $logfh "\nNotifying host ".$subhost.':';
10845:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
10846:                     print $logfh $reply;
10847:                 }
10848:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
10849:                 foreach my $subhost (@subscribedmeta) {
10850:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
10851:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
10852:                                                         $subhost);
10853:                     print $logfh $reply;
10854:                 }
10855:                 print $logfh "\n============ Done ============\n";
10856:                 close($logfh);
10857:             }
10858:         }
10859:     }
10860:     return OK;
10861: }
10862: 
10863: sub subscribed_hosts {
10864:     my ($target) = @_;
10865:     my @subscribed;
10866:     if (open(my $fh,"<","$target.subscription")) {
10867:         while (my $subline=<$fh>) {
10868:             if ($subline =~ /^($match_lonid):/) {
10869:                 my $host = $1;
10870:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
10871:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
10872:                         push(@subscribed,$host);
10873:                     }
10874:                 }
10875:             }
10876:         }
10877:     }
10878:     return @subscribed;
10879: }
10880: 
10881: sub check_switchserver {
10882:     my ($dom,$confname) = @_;
10883:     my ($allowed,$switchserver);
10884:     my $home = &Apache::lonnet::homeserver($confname,$dom);
10885:     if ($home eq 'no_host') {
10886:         $home = &Apache::lonnet::domain($dom,'primary');
10887:     }
10888:     my @ids=&Apache::lonnet::current_machine_ids();
10889:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
10890:     if (!$allowed) {
10891: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
10892:     }
10893:     return $switchserver;
10894: }
10895: 
10896: sub modify_quotas {
10897:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10898:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
10899:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
10900:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
10901:         $validationfieldsref);
10902:     if ($action eq 'quotas') {
10903:         $context = 'tools'; 
10904:     } else {
10905:         $context = $action;
10906:     }
10907:     if ($context eq 'requestcourses') {
10908:         @usertools = ('official','unofficial','community','textbook','placement','lti');
10909:         @options =('norequest','approval','validate','autolimit');
10910:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
10911:         %titles = &courserequest_titles();
10912:         $toolregexp = join('|',@usertools);
10913:         %conditions = &courserequest_conditions();
10914:         $confname = $dom.'-domainconfig';
10915:         my $servadm = $r->dir_config('lonAdmEMail');
10916:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10917:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
10918:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
10919:     } elsif ($context eq 'requestauthor') {
10920:         @usertools = ('author');
10921:         %titles = &authorrequest_titles();
10922:     } else {
10923:         @usertools = ('aboutme','blog','webdav','portfolio');
10924:         %titles = &tool_titles();
10925:     }
10926:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10927:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10928:     foreach my $key (keys(%env)) {
10929:         if ($context eq 'requestcourses') {
10930:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
10931:                 my $item = $1;
10932:                 my $type = $2;
10933:                 if ($type =~ /^limit_(.+)/) {
10934:                     $limithash{$item}{$1} = $env{$key};
10935:                 } else {
10936:                     $confhash{$item}{$type} = $env{$key};
10937:                 }
10938:             }
10939:         } elsif ($context eq 'requestauthor') {
10940:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
10941:                 $confhash{$1} = $env{$key};
10942:             }
10943:         } else {
10944:             if ($key =~ /^form\.quota_(.+)$/) {
10945:                 $confhash{'defaultquota'}{$1} = $env{$key};
10946:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
10947:                 $confhash{'authorquota'}{$1} = $env{$key};
10948:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
10949:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
10950:             }
10951:         }
10952:     }
10953:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10954:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
10955:         @approvalnotify = sort(@approvalnotify);
10956:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
10957:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
10958:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
10959:         foreach my $type (@hasuniquecode) {
10960:             if (grep(/^\Q$type\E$/,@crstypes)) {
10961:                 $confhash{'uniquecode'}{$type} = 1;
10962:             }
10963:         }
10964:         my (%newbook,%allpos);
10965:         if ($context eq 'requestcourses') {
10966:             foreach my $type ('textbooks','templates') {
10967:                 @{$allpos{$type}} = (); 
10968:                 my $invalid;
10969:                 if ($type eq 'textbooks') {
10970:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
10971:                 } else {
10972:                     $invalid = &mt('Invalid LON-CAPA course for template');
10973:                 }
10974:                 if ($env{'form.'.$type.'_addbook'}) {
10975:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
10976:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
10977:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
10978:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
10979:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10980:                         } else {
10981:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
10982:                             my $position = $env{'form.'.$type.'_addbook_pos'};
10983:                             $position =~ s/\D+//g;
10984:                             if ($position ne '') {
10985:                                 $allpos{$type}[$position] = $newbook{$type};
10986:                             }
10987:                         }
10988:                     } else {
10989:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10990:                     }
10991:                 }
10992:             } 
10993:         }
10994:         if (ref($domconfig{$action}) eq 'HASH') {
10995:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
10996:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
10997:                     $changes{'notify'}{'approval'} = 1;
10998:                 }
10999:             } else {
11000:                 if ($confhash{'notify'}{'approval'}) {
11001:                     $changes{'notify'}{'approval'} = 1;
11002:                 }
11003:             }
11004:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
11005:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
11006:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
11007:                         unless ($confhash{'uniquecode'}{$crstype}) {
11008:                             $changes{'uniquecode'} = 1;
11009:                         }
11010:                     }
11011:                     unless ($changes{'uniquecode'}) {
11012:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
11013:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
11014:                                 $changes{'uniquecode'} = 1;
11015:                             }
11016:                         }
11017:                     }
11018:                } else {
11019:                    $changes{'uniquecode'} = 1;
11020:                }
11021:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
11022:                 $changes{'uniquecode'} = 1;
11023:             }
11024:             if ($context eq 'requestcourses') {
11025:                 foreach my $type ('textbooks','templates') {
11026:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
11027:                         my %deletions;
11028:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
11029:                         if (@todelete) {
11030:                             map { $deletions{$_} = 1; } @todelete;
11031:                         }
11032:                         my %imgdeletions;
11033:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
11034:                         if (@todeleteimages) {
11035:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
11036:                         }
11037:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
11038:                         for (my $i=0; $i<=$maxnum; $i++) {
11039:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
11040:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
11041:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
11042:                                 if ($deletions{$key}) {
11043:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
11044:                                         #FIXME need to obsolete item in RES space
11045:                                     }
11046:                                     next;
11047:                                 } else {
11048:                                     my $newpos = $env{'form.'.$itemid};
11049:                                     $newpos =~ s/\D+//g;
11050:                                     foreach my $item ('subject','title','publisher','author') {
11051:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
11052:                                                  ($type eq 'templates'));
11053:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
11054:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
11055:                                             $changes{$type}{$key} = 1;
11056:                                         }
11057:                                     }
11058:                                     $allpos{$type}[$newpos] = $key;
11059:                                 }
11060:                                 if ($imgdeletions{$key}) {
11061:                                     $changes{$type}{$key} = 1;
11062:                                     #FIXME need to obsolete item in RES space
11063:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
11064:                                     my ($cdom,$cnum) = split(/_/,$key);
11065:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
11066:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
11067:                                     } else {
11068:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
11069:                                                                                       $cdom,$cnum,$type,$configuserok,
11070:                                                                                       $switchserver,$author_ok);
11071:                                         if ($imgurl) {
11072:                                             $confhash{$type}{$key}{'image'} = $imgurl;
11073:                                             $changes{$type}{$key} = 1; 
11074:                                         }
11075:                                         if ($error) {
11076:                                             &Apache::lonnet::logthis($error);
11077:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11078:                                         }
11079:                                     }
11080:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
11081:                                     $confhash{$type}{$key}{'image'} = 
11082:                                         $domconfig{$action}{$type}{$key}{'image'};
11083:                                 }
11084:                             }
11085:                         }
11086:                     }
11087:                 }
11088:             }
11089:         } else {
11090:             if ($confhash{'notify'}{'approval'}) {
11091:                 $changes{'notify'}{'approval'} = 1;
11092:             }
11093:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
11094:                 $changes{'uniquecode'} = 1;
11095:             }
11096:         }
11097:         if ($context eq 'requestcourses') {
11098:             foreach my $type ('textbooks','templates') {
11099:                 if ($newbook{$type}) {
11100:                     $changes{$type}{$newbook{$type}} = 1;
11101:                     foreach my $item ('subject','title','publisher','author') {
11102:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
11103:                                  ($type eq 'template'));
11104:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
11105:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
11106:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
11107:                         }
11108:                     }
11109:                     if ($type eq 'textbooks') {
11110:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
11111:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
11112:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
11113:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
11114:                             } else {
11115:                                 my ($imageurl,$error) =
11116:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
11117:                                                             $configuserok,$switchserver,$author_ok);
11118:                                 if ($imageurl) {
11119:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
11120:                                 }
11121:                                 if ($error) {
11122:                                     &Apache::lonnet::logthis($error);
11123:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11124:                                 }
11125:                             }
11126:                         }
11127:                     }
11128:                 }
11129:                 if (@{$allpos{$type}} > 0) {
11130:                     my $idx = 0;
11131:                     foreach my $item (@{$allpos{$type}}) {
11132:                         if ($item ne '') {
11133:                             $confhash{$type}{$item}{'order'} = $idx;
11134:                             if (ref($domconfig{$action}) eq 'HASH') {
11135:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
11136:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
11137:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
11138:                                             $changes{$type}{$item} = 1;
11139:                                         }
11140:                                     }
11141:                                 }
11142:                             }
11143:                             $idx ++;
11144:                         }
11145:                     }
11146:                 }
11147:             }
11148:             if (ref($validationitemsref) eq 'ARRAY') {
11149:                 foreach my $item (@{$validationitemsref}) {
11150:                     if ($item eq 'fields') {
11151:                         my @changed;
11152:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
11153:                         if (@{$confhash{'validation'}{$item}} > 0) {
11154:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
11155:                         }
11156:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11157:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11158:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
11159:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
11160:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
11161:                                 } else {
11162:                                     @changed = @{$confhash{'validation'}{$item}};
11163:                                 }
11164:                             } else {
11165:                                 @changed = @{$confhash{'validation'}{$item}};
11166:                             }
11167:                         } else {
11168:                             @changed = @{$confhash{'validation'}{$item}};
11169:                         }
11170:                         if (@changed) {
11171:                             if ($confhash{'validation'}{$item}) {
11172:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
11173:                             } else {
11174:                                 $changes{'validation'}{$item} = &mt('None');
11175:                             }
11176:                         }
11177:                     } else {
11178:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
11179:                         if ($item eq 'markup') {
11180:                             if ($env{'form.requestcourses_validation_'.$item}) {
11181:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
11182:                             }
11183:                         }
11184:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11185:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11186:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
11187:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11188:                                 }
11189:                             } else {
11190:                                 if ($confhash{'validation'}{$item} ne '') {
11191:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11192:                                 }
11193:                             }
11194:                         } else {
11195:                             if ($confhash{'validation'}{$item} ne '') {
11196:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11197:                             }
11198:                         }
11199:                     }
11200:                 }
11201:             }
11202:             if ($env{'form.validationdc'}) {
11203:                 my $newval = $env{'form.validationdc'};
11204:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
11205:                 if (exists($domcoords{$newval})) {
11206:                     $confhash{'validation'}{'dc'} = $newval;
11207:                 }
11208:             }
11209:             if (ref($confhash{'validation'}) eq 'HASH') {
11210:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11211:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11212:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
11213:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
11214:                                 if ($confhash{'validation'}{'dc'} eq '') {
11215:                                     $changes{'validation'}{'dc'} = &mt('None');
11216:                                 } else {
11217:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11218:                                 }
11219:                             }
11220:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
11221:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11222:                         }
11223:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
11224:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11225:                     }
11226:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
11227:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11228:                 }
11229:             } else {
11230:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11231:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11232:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
11233:                             $changes{'validation'}{'dc'} = &mt('None');
11234:                         }
11235:                     }
11236:                 }
11237:             }
11238:         }
11239:     } else {
11240:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
11241:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
11242:     }
11243:     foreach my $item (@usertools) {
11244:         foreach my $type (@{$types},'default','_LC_adv') {
11245:             my $unset; 
11246:             if ($context eq 'requestcourses') {
11247:                 $unset = '0';
11248:                 if ($type eq '_LC_adv') {
11249:                     $unset = '';
11250:                 }
11251:                 if ($confhash{$item}{$type} eq 'autolimit') {
11252:                     $confhash{$item}{$type} .= '=';
11253:                     unless ($limithash{$item}{$type} =~ /\D/) {
11254:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
11255:                     }
11256:                 }
11257:             } elsif ($context eq 'requestauthor') {
11258:                 $unset = '0';
11259:                 if ($type eq '_LC_adv') {
11260:                     $unset = '';
11261:                 }
11262:             } else {
11263:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
11264:                     $confhash{$item}{$type} = 1;
11265:                 } else {
11266:                     $confhash{$item}{$type} = 0;
11267:                 }
11268:             }
11269:             if (ref($domconfig{$action}) eq 'HASH') {
11270:                 if ($action eq 'requestauthor') {
11271:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
11272:                         $changes{$type} = 1;
11273:                     }
11274:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
11275:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
11276:                         $changes{$item}{$type} = 1;
11277:                     }
11278:                 } else {
11279:                     if ($context eq 'requestcourses') {
11280:                         if ($confhash{$item}{$type} ne $unset) {
11281:                             $changes{$item}{$type} = 1;
11282:                         }
11283:                     } else {
11284:                         if (!$confhash{$item}{$type}) {
11285:                             $changes{$item}{$type} = 1;
11286:                         }
11287:                     }
11288:                 }
11289:             } else {
11290:                 if ($context eq 'requestcourses') {
11291:                     if ($confhash{$item}{$type} ne $unset) {
11292:                         $changes{$item}{$type} = 1;
11293:                     }
11294:                 } elsif ($context eq 'requestauthor') {
11295:                     if ($confhash{$type} ne $unset) {
11296:                         $changes{$type} = 1;
11297:                     }
11298:                 } else {
11299:                     if (!$confhash{$item}{$type}) {
11300:                         $changes{$item}{$type} = 1;
11301:                     }
11302:                 }
11303:             }
11304:         }
11305:     }
11306:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
11307:         if (ref($domconfig{'quotas'}) eq 'HASH') {
11308:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
11309:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
11310:                     if (exists($confhash{'defaultquota'}{$key})) {
11311:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
11312:                             $changes{'defaultquota'}{$key} = 1;
11313:                         }
11314:                     } else {
11315:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
11316:                     }
11317:                 }
11318:             } else {
11319:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
11320:                     if (exists($confhash{'defaultquota'}{$key})) {
11321:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
11322:                             $changes{'defaultquota'}{$key} = 1;
11323:                         }
11324:                     } else {
11325:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
11326:                     }
11327:                 }
11328:             }
11329:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
11330:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
11331:                     if (exists($confhash{'authorquota'}{$key})) {
11332:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
11333:                             $changes{'authorquota'}{$key} = 1;
11334:                         }
11335:                     } else {
11336:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
11337:                     }
11338:                 }
11339:             }
11340:         }
11341:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
11342:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
11343:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11344:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
11345:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
11346:                             $changes{'defaultquota'}{$key} = 1;
11347:                         }
11348:                     } else {
11349:                         if (!exists($domconfig{'quotas'}{$key})) {
11350:                             $changes{'defaultquota'}{$key} = 1;
11351:                         }
11352:                     }
11353:                 } else {
11354:                     $changes{'defaultquota'}{$key} = 1;
11355:                 }
11356:             }
11357:         }
11358:         if (ref($confhash{'authorquota'}) eq 'HASH') {
11359:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
11360:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11361:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
11362:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
11363:                             $changes{'authorquota'}{$key} = 1;
11364:                         }
11365:                     } else {
11366:                         $changes{'authorquota'}{$key} = 1;
11367:                     }
11368:                 } else {
11369:                     $changes{'authorquota'}{$key} = 1;
11370:                 }
11371:             }
11372:         }
11373:     }
11374: 
11375:     if ($context eq 'requestauthor') {
11376:         $domdefaults{'requestauthor'} = \%confhash;
11377:     } else {
11378:         foreach my $key (keys(%confhash)) {
11379:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
11380:                 $domdefaults{$key} = $confhash{$key};
11381:             }
11382:         }
11383:     }
11384: 
11385:     my %quotahash = (
11386:                       $action => { %confhash }
11387:                     );
11388:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
11389:                                              $dom);
11390:     if ($putresult eq 'ok') {
11391:         if (keys(%changes) > 0) {
11392:             my $cachetime = 24*60*60;
11393:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11394:             if (ref($lastactref) eq 'HASH') {
11395:                 $lastactref->{'domdefaults'} = 1;
11396:             }
11397:             $resulttext = &mt('Changes made:').'<ul>';
11398:             unless (($context eq 'requestcourses') ||
11399:                     ($context eq 'requestauthor')) {
11400:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
11401:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
11402:                     foreach my $type (@{$types},'default') {
11403:                         if (defined($changes{'defaultquota'}{$type})) {
11404:                             my $typetitle = $usertypes->{$type};
11405:                             if ($type eq 'default') {
11406:                                 $typetitle = $othertitle;
11407:                             }
11408:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
11409:                         }
11410:                     }
11411:                     $resulttext .= '</ul></li>';
11412:                 }
11413:                 if (ref($changes{'authorquota'}) eq 'HASH') {
11414:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
11415:                     foreach my $type (@{$types},'default') {
11416:                         if (defined($changes{'authorquota'}{$type})) {
11417:                             my $typetitle = $usertypes->{$type};
11418:                             if ($type eq 'default') {
11419:                                 $typetitle = $othertitle;
11420:                             }
11421:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
11422:                         }
11423:                     }
11424:                     $resulttext .= '</ul></li>';
11425:                 }
11426:             }
11427:             my %newenv;
11428:             foreach my $item (@usertools) {
11429:                 my (%haschgs,%inconf);
11430:                 if ($context eq 'requestauthor') {
11431:                     %haschgs = %changes;
11432:                     %inconf = %confhash;
11433:                 } else {
11434:                     if (ref($changes{$item}) eq 'HASH') {
11435:                         %haschgs = %{$changes{$item}};
11436:                     }
11437:                     if (ref($confhash{$item}) eq 'HASH') {
11438:                         %inconf = %{$confhash{$item}};
11439:                     }
11440:                 }
11441:                 if (keys(%haschgs) > 0) {
11442:                     my $newacc = 
11443:                         &Apache::lonnet::usertools_access($env{'user.name'},
11444:                                                           $env{'user.domain'},
11445:                                                           $item,'reload',$context);
11446:                     if (($context eq 'requestcourses') ||
11447:                         ($context eq 'requestauthor')) {
11448:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
11449:                             $newenv{'environment.canrequest.'.$item} = $newacc;
11450:                         }
11451:                     } else {
11452:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
11453:                             $newenv{'environment.availabletools.'.$item} = $newacc;
11454:                         }
11455:                     }
11456:                     unless ($context eq 'requestauthor') {
11457:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
11458:                     }
11459:                     foreach my $type (@{$types},'default','_LC_adv') {
11460:                         if ($haschgs{$type}) {
11461:                             my $typetitle = $usertypes->{$type};
11462:                             if ($type eq 'default') {
11463:                                 $typetitle = $othertitle;
11464:                             } elsif ($type eq '_LC_adv') {
11465:                                 $typetitle = 'LON-CAPA Advanced Users'; 
11466:                             }
11467:                             if ($inconf{$type}) {
11468:                                 if ($context eq 'requestcourses') {
11469:                                     my $cond;
11470:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
11471:                                         if ($1 eq '') {
11472:                                             $cond = &mt('(Automatic processing of any request).');
11473:                                         } else {
11474:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
11475:                                         }
11476:                                     } else { 
11477:                                         $cond = $conditions{$inconf{$type}};
11478:                                     }
11479:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
11480:                                 } elsif ($context eq 'requestauthor') {
11481:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
11482:                                                              $titles{$inconf{$type}},$typetitle);
11483: 
11484:                                 } else {
11485:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
11486:                                 }
11487:                             } else {
11488:                                 if ($type eq '_LC_adv') {
11489:                                     if ($inconf{$type} eq '0') {
11490:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11491:                                     } else { 
11492:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
11493:                                     }
11494:                                 } else {
11495:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11496:                                 }
11497:                             }
11498:                         }
11499:                     }
11500:                     unless ($context eq 'requestauthor') {
11501:                         $resulttext .= '</ul></li>';
11502:                     }
11503:                 }
11504:             }
11505:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
11506:                 if (ref($changes{'notify'}) eq 'HASH') {
11507:                     if ($changes{'notify'}{'approval'}) {
11508:                         if (ref($confhash{'notify'}) eq 'HASH') {
11509:                             if ($confhash{'notify'}{'approval'}) {
11510:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
11511:                             } else {
11512:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
11513:                             }
11514:                         }
11515:                     }
11516:                 }
11517:             }
11518:             if ($action eq 'requestcourses') {
11519:                 my @offon = ('off','on');
11520:                 if ($changes{'uniquecode'}) {
11521:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
11522:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
11523:                         $resulttext .= '<li>'.
11524:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
11525:                                        '</li>';
11526:                     } else {
11527:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
11528:                                        '</li>';
11529:                     }
11530:                 }
11531:                 foreach my $type ('textbooks','templates') {
11532:                     if (ref($changes{$type}) eq 'HASH') {
11533:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
11534:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
11535:                             my %coursehash = &Apache::lonnet::coursedescription($key);
11536:                             my $coursetitle = $coursehash{'description'};
11537:                             my $position = $confhash{$type}{$key}{'order'} + 1;
11538:                             $resulttext .= '<li>';
11539:                             foreach my $item ('subject','title','publisher','author') {
11540:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
11541:                                          ($type eq 'templates'));
11542:                                 my $name = $item.':';
11543:                                 $name =~ s/^(\w)/\U$1/;
11544:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
11545:                             }
11546:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
11547:                             if ($type eq 'textbooks') {
11548:                                 if ($confhash{$type}{$key}{'image'}) {
11549:                                     $resulttext .= ' '.&mt('Image: [_1]',
11550:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
11551:                                                    ' alt="Textbook cover" />').'<br />';
11552:                                 }
11553:                             }
11554:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
11555:                         }
11556:                         $resulttext .= '</ul></li>';
11557:                     }
11558:                 }
11559:                 if (ref($changes{'validation'}) eq 'HASH') {
11560:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
11561:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
11562:                         foreach my $item (@{$validationitemsref}) {
11563:                             if (exists($changes{'validation'}{$item})) {
11564:                                 if ($item eq 'markup') {
11565:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11566:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
11567:                                 } else {
11568:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11569:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
11570:                                 }
11571:                             }
11572:                         }
11573:                         if (exists($changes{'validation'}{'dc'})) {
11574:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
11575:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
11576:                         }
11577:                     }
11578:                 }
11579:             }
11580:             $resulttext .= '</ul>';
11581:             if (keys(%newenv)) {
11582:                 &Apache::lonnet::appenv(\%newenv);
11583:             }
11584:         } else {
11585:             if ($context eq 'requestcourses') {
11586:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
11587:             } elsif ($context eq 'requestauthor') {
11588:                 $resulttext = &mt('No changes made to rights to request author space.');
11589:             } else {
11590:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
11591:             }
11592:         }
11593:     } else {
11594:         $resulttext = '<span class="LC_error">'.
11595: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11596:     }
11597:     if ($errors) {
11598:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
11599:                        '<ul>'.$errors.'</ul></p>';
11600:     }
11601:     return $resulttext;
11602: }
11603: 
11604: sub process_textbook_image {
11605:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
11606:     my $filename = $env{'form.'.$caller.'.filename'};
11607:     my ($error,$url);
11608:     my ($width,$height) = (50,50);
11609:     if ($configuserok eq 'ok') {
11610:         if ($switchserver) {
11611:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
11612:                          $switchserver);
11613:         } elsif ($author_ok eq 'ok') {
11614:             my ($result,$imageurl) =
11615:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11616:                              "$type/$cdom/$cnum/cover",$width,$height);
11617:             if ($result eq 'ok') {
11618:                 $url = $imageurl;
11619:             } else {
11620:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11621:             }
11622:         } else {
11623:             $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);
11624:         }
11625:     } else {
11626:         $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);
11627:     }
11628:     return ($url,$error);
11629: }
11630: 
11631: sub modify_ltitools {
11632:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11633:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11634:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11635:     my $confname = $dom.'-domainconfig';
11636:     my $servadm = $r->dir_config('lonAdmEMail');
11637:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11638:     my (%posslti,%possfield);
11639:     my @courseroles = ('cc','in','ta','ep','st');
11640:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
11641:     map { $posslti{$_} = 1; } @ltiroles;
11642:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
11643:     map { $possfield{$_} = 1; } @allfields;
11644:     my %lt = &ltitools_names(); 
11645:     if ($env{'form.ltitools_add'}) {
11646:         my $title = $env{'form.ltitools_add_title'};
11647:         $title =~ s/(`)/'/g;
11648:         ($newid,my $error) = &get_ltitools_id($dom,$title);
11649:         if ($newid) {
11650:             my $position = $env{'form.ltitools_add_pos'};
11651:             $position =~ s/\D+//g;
11652:             if ($position ne '') {
11653:                 $allpos[$position] = $newid;
11654:             }
11655:             $changes{$newid} = 1;
11656:             foreach my $item ('title','url','key','secret','lifetime') {
11657:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
11658:                 if ($item eq 'lifetime') {
11659:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
11660:                 }
11661:                 if ($env{'form.ltitools_add_'.$item}) {
11662:                     if (($item eq 'key') || ($item eq 'secret')) {
11663:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11664:                     } else {
11665:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11666:                     }
11667:                 }
11668:             }
11669:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
11670:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
11671:             }
11672:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
11673:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
11674:             }
11675:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
11676:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
11677:             } else {
11678:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
11679:             }
11680:             foreach my $item ('width','height','linktext','explanation') {
11681:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
11682:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
11683:                 if (($item eq 'width') || ($item eq 'height')) {
11684:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
11685:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
11686:                     }
11687:                 } else {
11688:                     if ($env{'form.ltitools_add_'.$item} ne '') {
11689:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
11690:                     }
11691:                 }
11692:             }
11693:             if ($env{'form.ltitools_add_target'} eq 'window') {
11694:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11695:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
11696:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11697:             } else {
11698:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
11699:             }
11700:             foreach my $item ('passback','roster') {
11701:                 if ($env{'form.ltitools_'.$item.'_add'}) {
11702:                     $confhash{$newid}{$item} = 1;
11703:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
11704:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
11705:                         $lifetime =~ s/^\s+|\s+$//g;
11706:                         if ($lifetime =~ /^\d+\.?\d*$/) {
11707:                             $confhash{$newid}{$item.'valid'} = $lifetime;
11708:                         }
11709:                     }
11710:                 }
11711:             }
11712:             if ($env{'form.ltitools_add_image.filename'} ne '') {
11713:                 my ($imageurl,$error) =
11714:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
11715:                                             $configuserok,$switchserver,$author_ok);
11716:                 if ($imageurl) {
11717:                     $confhash{$newid}{'image'} = $imageurl;
11718:                 }
11719:                 if ($error) {
11720:                     &Apache::lonnet::logthis($error);
11721:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11722:                 }
11723:             }
11724:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
11725:             foreach my $field (@fields) {
11726:                 if ($possfield{$field}) {
11727:                     if ($field eq 'roles') {
11728:                         foreach my $role (@courseroles) {
11729:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
11730:                             if (($choice ne '') && ($posslti{$choice})) {
11731:                                 $confhash{$newid}{'roles'}{$role} = $choice;
11732:                                 if ($role eq 'cc') {
11733:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
11734:                                 }
11735:                             }
11736:                         }
11737:                     } else {
11738:                         $confhash{$newid}{'fields'}{$field} = 1;
11739:                     }
11740:                 }
11741:             }
11742:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
11743:                 if ($confhash{$newid}{'fields'}{'user'}) {
11744:                     if ($env{'form.ltitools_userincdom_add'}) {
11745:                         $confhash{$newid}{'incdom'} = 1;
11746:                     }
11747:                 }
11748:             }
11749:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
11750:             foreach my $item (@courseconfig) {
11751:                 $confhash{$newid}{'crsconf'}{$item} = 1;
11752:             }
11753:             if ($env{'form.ltitools_add_custom'}) {
11754:                 my $name = $env{'form.ltitools_add_custom_name'};
11755:                 my $value = $env{'form.ltitools_add_custom_value'};
11756:                 $value =~ s/(`)/'/g;
11757:                 $name =~ s/(`)/'/g;
11758:                 $confhash{$newid}{'custom'}{$name} = $value;
11759:             }
11760:         } else {
11761:             my $error = &mt('Failed to acquire unique ID for new external tool');   
11762:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11763:         }
11764:     }
11765:     if (ref($domconfig{$action}) eq 'HASH') {
11766:         my %deletions;
11767:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
11768:         if (@todelete) {
11769:             map { $deletions{$_} = 1; } @todelete;
11770:         }
11771:         my %customadds;
11772:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
11773:         if (@newcustom) {
11774:             map { $customadds{$_} = 1; } @newcustom;
11775:         } 
11776:         my %imgdeletions;
11777:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
11778:         if (@todeleteimages) {
11779:             map { $imgdeletions{$_} = 1; } @todeleteimages;
11780:         }
11781:         my $maxnum = $env{'form.ltitools_maxnum'};
11782:         for (my $i=0; $i<=$maxnum; $i++) {
11783:             my $itemid = $env{'form.ltitools_id_'.$i};
11784:             $itemid =~ s/\D+//g;
11785:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
11786:                 if ($deletions{$itemid}) {
11787:                     if ($domconfig{$action}{$itemid}{'image'}) {
11788:                         #FIXME need to obsolete item in RES space
11789:                     }
11790:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
11791:                     next;
11792:                 } else {
11793:                     my $newpos = $env{'form.ltitools_'.$itemid};
11794:                     $newpos =~ s/\D+//g;
11795:                     foreach my $item ('title','url','lifetime') {
11796:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11797:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
11798:                             $changes{$itemid} = 1;
11799:                         }
11800:                     }
11801:                     foreach my $item ('key','secret') {
11802:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11803:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
11804:                             $changes{$itemid} = 1;
11805:                         }
11806:                     }
11807:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
11808:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
11809:                     }
11810:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
11811:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
11812:                     }
11813:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
11814:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
11815:                     } else {
11816:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
11817:                     }
11818:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
11819:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
11820:                             $changes{$itemid} = 1;
11821:                         }
11822:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
11823:                         $changes{$itemid} = 1;
11824:                     }
11825:                     foreach my $size ('width','height') {
11826:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
11827:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
11828:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
11829:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
11830:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11831:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
11832:                                     $changes{$itemid} = 1;
11833:                                 }
11834:                             } else {
11835:                                 $changes{$itemid} = 1;
11836:                             }
11837:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11838:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
11839:                                 $changes{$itemid} = 1;
11840:                             }
11841:                         }
11842:                     }
11843:                     foreach my $item ('linktext','explanation') {
11844:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
11845:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
11846:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
11847:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
11848:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11849:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
11850:                                     $changes{$itemid} = 1;
11851:                                 }
11852:                             } else {
11853:                                 $changes{$itemid} = 1;
11854:                             }
11855:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11856:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
11857:                                 $changes{$itemid} = 1;
11858:                             }
11859:                         }
11860:                     }
11861:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
11862:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11863:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
11864:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
11865:                     } else {
11866:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
11867:                     }
11868:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
11869:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
11870:                             $changes{$itemid} = 1;
11871:                         }
11872:                     } else {
11873:                         $changes{$itemid} = 1;
11874:                     }
11875:                     foreach my $extra ('passback','roster') {
11876:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
11877:                             $confhash{$itemid}{$extra} = 1;
11878:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
11879:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
11880:                                 $lifetime =~ s/^\s+|\s+$//g;
11881:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
11882:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
11883:                                 }
11884:                             }
11885:                         }
11886:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
11887:                             $changes{$itemid} = 1;
11888:                         }
11889:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
11890:                             $changes{$itemid} = 1;
11891:                         }
11892:                     }
11893:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
11894:                     foreach my $item ('label','title','target','linktext','explanation','append') {
11895:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
11896:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
11897:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
11898:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
11899:                                     $changes{$itemid} = 1;
11900:                                 }
11901:                             } else {
11902:                                 $changes{$itemid} = 1;
11903:                             }
11904:                         }
11905:                     }
11906:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
11907:                     foreach my $field (@fields) {
11908:                         if ($possfield{$field}) {
11909:                             if ($field eq 'roles') {
11910:                                 foreach my $role (@courseroles) {
11911:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
11912:                                     if (($choice ne '') && ($posslti{$choice})) {
11913:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
11914:                                         if ($role eq 'cc') {
11915:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
11916:                                         }
11917:                                     }
11918:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
11919:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
11920:                                             $changes{$itemid} = 1;
11921:                                         }
11922:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
11923:                                         $changes{$itemid} = 1;
11924:                                     }
11925:                                 }
11926:                             } else {
11927:                                 $confhash{$itemid}{'fields'}{$field} = 1;
11928:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
11929:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
11930:                                         $changes{$itemid} = 1;
11931:                                     }
11932:                                 } else {
11933:                                     $changes{$itemid} = 1;
11934:                                 }
11935:                             }
11936:                         }
11937:                     }
11938:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11939:                         if ($confhash{$itemid}{'fields'}{'user'}) {
11940:                             if ($env{'form.ltitools_userincdom_'.$i}) {
11941:                                 $confhash{$itemid}{'incdom'} = 1;
11942:                             }
11943:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
11944:                                 $changes{$itemid} = 1;
11945:                             }
11946:                         }
11947:                     }
11948:                     $allpos[$newpos] = $itemid;
11949:                 }
11950:                 if ($imgdeletions{$itemid}) {
11951:                     $changes{$itemid} = 1;
11952:                     #FIXME need to obsolete item in RES space
11953:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
11954:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
11955:                                                                  $itemid,$configuserok,$switchserver,
11956:                                                                  $author_ok);
11957:                     if ($imgurl) {
11958:                         $confhash{$itemid}{'image'} = $imgurl;
11959:                         $changes{$itemid} = 1;
11960:                     }
11961:                     if ($error) {
11962:                         &Apache::lonnet::logthis($error);
11963:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11964:                     }
11965:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
11966:                     $confhash{$itemid}{'image'} =
11967:                        $domconfig{$action}{$itemid}{'image'};
11968:                 }
11969:                 if ($customadds{$i}) {
11970:                     my $name = $env{'form.ltitools_custom_name_'.$i};
11971:                     $name =~ s/(`)/'/g;
11972:                     $name =~ s/^\s+//;
11973:                     $name =~ s/\s+$//;
11974:                     my $value = $env{'form.ltitools_custom_value_'.$i};
11975:                     $value =~ s/(`)/'/g;
11976:                     $value =~ s/^\s+//;
11977:                     $value =~ s/\s+$//;
11978:                     if ($name ne '') {
11979:                         $confhash{$itemid}{'custom'}{$name} = $value;
11980:                         $changes{$itemid} = 1;
11981:                     }
11982:                 }
11983:                 my %customdels;
11984:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
11985:                 if (@customdeletions) {
11986:                     $changes{$itemid} = 1;
11987:                 }
11988:                 map { $customdels{$_} = 1; } @customdeletions;
11989:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
11990:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
11991:                         unless ($customdels{$key}) {
11992:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
11993:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
11994:                             }
11995:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
11996:                                 $changes{$itemid} = 1;
11997:                             }
11998:                         }
11999:                     }
12000:                 }
12001:                 unless ($changes{$itemid}) {
12002:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
12003:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
12004:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
12005:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
12006:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
12007:                                         $changes{$itemid} = 1;
12008:                                         last;
12009:                                     }
12010:                                 }
12011:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
12012:                                 $changes{$itemid} = 1;
12013:                             }
12014:                         }
12015:                         last if ($changes{$itemid});
12016:                     }
12017:                 }
12018:             }
12019:         }
12020:     }
12021:     if (@allpos > 0) {
12022:         my $idx = 0;
12023:         foreach my $itemid (@allpos) {
12024:             if ($itemid ne '') {
12025:                 $confhash{$itemid}{'order'} = $idx;
12026:                 if (ref($domconfig{$action}) eq 'HASH') {
12027:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12028:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
12029:                             $changes{$itemid} = 1;
12030:                         }
12031:                     }
12032:                 }
12033:                 $idx ++;
12034:             }
12035:         }
12036:     }
12037:     my %ltitoolshash = (
12038:                           $action => { %confhash }
12039:                        );
12040:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
12041:                                              $dom);
12042:     if ($putresult eq 'ok') {
12043:         my %ltienchash = (
12044:                              $action => { %encconfig }
12045:                          );
12046:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
12047:         if (keys(%changes) > 0) {
12048:             my $cachetime = 24*60*60;
12049:             my %ltiall = %confhash;
12050:             foreach my $id (keys(%ltiall)) {
12051:                 if (ref($encconfig{$id}) eq 'HASH') {
12052:                     foreach my $item ('key','secret') {
12053:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
12054:                     }
12055:                 }
12056:             }
12057:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
12058:             if (ref($lastactref) eq 'HASH') {
12059:                 $lastactref->{'ltitools'} = 1;
12060:             }
12061:             $resulttext = &mt('Changes made:').'<ul>';
12062:             my %bynum;
12063:             foreach my $itemid (sort(keys(%changes))) {
12064:                 my $position = $confhash{$itemid}{'order'};
12065:                 $bynum{$position} = $itemid;
12066:             }
12067:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12068:                 my $itemid = $bynum{$pos}; 
12069:                 if (ref($confhash{$itemid}) ne 'HASH') {
12070:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12071:                 } else {
12072:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
12073:                     if ($confhash{$itemid}{'image'}) {
12074:                         $resulttext .= '&nbsp;'.
12075:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
12076:                                        ' alt="'.&mt('Tool Provider icon').'" />';
12077:                     }
12078:                     $resulttext .= '</li><ul>';
12079:                     my $position = $pos + 1;
12080:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12081:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
12082:                         if ($confhash{$itemid}{$item} ne '') {
12083:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
12084:                         }
12085:                     }
12086:                     if ($encconfig{$itemid}{'key'} ne '') {
12087:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
12088:                     }
12089:                     if ($encconfig{$itemid}{'secret'} ne '') {
12090:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
12091:                         my $num = length($encconfig{$itemid}{'secret'});
12092:                         $resulttext .= ('*'x$num).'</li>';
12093:                     }
12094:                     $resulttext .= '<li>'.&mt('Configurable in course:');
12095:                     my @possconfig = ('label','title','target','linktext','explanation','append');
12096:                     my $numconfig = 0; 
12097:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
12098:                         foreach my $item (@possconfig) {
12099:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
12100:                                 $numconfig ++;
12101:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
12102:                             }
12103:                         }
12104:                     }
12105:                     if (!$numconfig) {
12106:                         $resulttext .= &mt('None');
12107:                     }
12108:                     $resulttext .= '</li>';
12109:                     foreach my $item ('passback','roster') {
12110:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
12111:                         if ($confhash{$itemid}{$item}) {
12112:                             $resulttext .= &mt('Yes');
12113:                             if ($confhash{$itemid}{$item.'valid'}) {
12114:                                 if ($item eq 'passback') {
12115:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
12116:                                                            $confhash{$itemid}{$item.'valid'});
12117:                                 } else {
12118:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
12119:                                                            $confhash{$itemid}{$item.'valid'});
12120:                                 }
12121:                             }
12122:                         } else {
12123:                             $resulttext .= &mt('No');
12124:                         }
12125:                         $resulttext .= '</li>';
12126:                     }
12127:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
12128:                         my $displaylist;
12129:                         if ($confhash{$itemid}{'display'}{'target'}) {
12130:                             $displaylist = &mt('Display target').':&nbsp;'.
12131:                                            $confhash{$itemid}{'display'}{'target'}.',';
12132:                         }
12133:                         foreach my $size ('width','height') { 
12134:                             if ($confhash{$itemid}{'display'}{$size}) {
12135:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
12136:                                                 $confhash{$itemid}{'display'}{$size}.',';
12137:                             }
12138:                         }
12139:                         if ($displaylist) {
12140:                             $displaylist =~ s/,$//;
12141:                             $resulttext .= '<li>'.$displaylist.'</li>';
12142:                         }
12143:                         foreach my $item ('linktext','explanation') {
12144:                             if ($confhash{$itemid}{'display'}{$item}) {
12145:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
12146:                             }
12147:                         }
12148:                     }
12149:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
12150:                         my $fieldlist;
12151:                         foreach my $field (@allfields) {
12152:                             if ($confhash{$itemid}{'fields'}{$field}) {
12153:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
12154:                             }
12155:                         }
12156:                         if ($fieldlist) {
12157:                             $fieldlist =~ s/,$//;
12158:                             if ($confhash{$itemid}{'fields'}{'user'}) {
12159:                                 if ($confhash{$itemid}{'incdom'}) {
12160:                                     $fieldlist .= ' ('.&mt('username:domain').')';
12161:                                 } else {
12162:                                     $fieldlist .= ' ('.&mt('username').')';
12163:                                 }
12164:                             }
12165:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
12166:                         }
12167:                     }
12168:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
12169:                         my $rolemaps;
12170:                         foreach my $role (@courseroles) {
12171:                             if ($confhash{$itemid}{'roles'}{$role}) {
12172:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
12173:                                              $confhash{$itemid}{'roles'}{$role}.',';
12174:                             }
12175:                         }
12176:                         if ($rolemaps) {
12177:                             $rolemaps =~ s/,$//; 
12178:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
12179:                         }
12180:                     }
12181:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
12182:                         my $customlist;
12183:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
12184:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
12185:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
12186:                             } 
12187:                         }
12188:                         if ($customlist) {
12189:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
12190:                         }
12191:                     } 
12192:                     $resulttext .= '</ul></li>';
12193:                 }
12194:             }
12195:             $resulttext .= '</ul>';
12196:         } else {
12197:             $resulttext = &mt('No changes made.');
12198:         }
12199:     } else {
12200:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12201:     }
12202:     if ($errors) {
12203:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12204:                        $errors.'</ul>';
12205:     }
12206:     return $resulttext;
12207: }
12208: 
12209: sub process_ltitools_image {
12210:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
12211:     my $filename = $env{'form.'.$caller.'.filename'};
12212:     my ($error,$url);
12213:     my ($width,$height) = (21,21);
12214:     if ($configuserok eq 'ok') {
12215:         if ($switchserver) {
12216:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
12217:                          $switchserver);
12218:         } elsif ($author_ok eq 'ok') {
12219:             my ($result,$imageurl,$madethumb) =
12220:                 &publishlogo($r,'upload',$caller,$dom,$confname,
12221:                              "ltitools/$itemid/icon",$width,$height);
12222:             if ($result eq 'ok') {
12223:                 if ($madethumb) {
12224:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
12225:                     my $imagethumb = "$path/tn-".$imagefile;
12226:                     $url = $imagethumb;
12227:                 } else {
12228:                     $url = $imageurl;
12229:                 }
12230:             } else {
12231:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
12232:             }
12233:         } else {
12234:             $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);
12235:         }
12236:     } else {
12237:         $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);
12238:     }
12239:     return ($url,$error);
12240: }
12241: 
12242: sub get_ltitools_id {
12243:     my ($cdom,$title) = @_;
12244:     # get lock on ltitools db
12245:     my $lockhash = {
12246:                       lock => $env{'user.name'}.
12247:                               ':'.$env{'user.domain'},
12248:                    };
12249:     my $tries = 0;
12250:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
12251:     my ($id,$error);
12252:  
12253:     while (($gotlock ne 'ok') && ($tries<10)) {
12254:         $tries ++;
12255:         sleep (0.1);
12256:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
12257:     }
12258:     if ($gotlock eq 'ok') {
12259:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
12260:         if ($currids{'lock'}) {
12261:             delete($currids{'lock'});
12262:             if (keys(%currids)) {
12263:                 my @curr = sort { $a <=> $b } keys(%currids);
12264:                 if ($curr[-1] =~ /^\d+$/) {
12265:                     $id = 1 + $curr[-1];
12266:                 }
12267:             } else {
12268:                 $id = 1;
12269:             }
12270:             if ($id) {
12271:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12272:                     $error = 'nostore';
12273:                 }
12274:             } else {
12275:                 $error = 'nonumber';
12276:             }
12277:         }
12278:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
12279:     } else {
12280:         $error = 'nolock';
12281:     }
12282:     return ($id,$error);
12283: }
12284: 
12285: sub modify_lti {
12286:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12287:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12288:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
12289:     my (%posslti,%posslticrs,%posscrstype);
12290:     my @courseroles = ('cc','in','ta','ep','st');
12291:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
12292:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
12293:     my @coursetypes = ('official','unofficial','community','textbook','placement');
12294:     my %coursetypetitles = &Apache::lonlocal::texthash (
12295:                                official   => 'Official',
12296:                                unofficial => 'Unofficial',
12297:                                community  => 'Community',
12298:                                textbook   => 'Textbook',
12299:                                placement  => 'Placement Test',
12300:     );
12301:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12302:     my %lt = &lti_names();
12303:     map { $posslti{$_} = 1; } @ltiroles;
12304:     map { $posslticrs{$_} = 1; } @lticourseroles;
12305:     map { $posscrstype{$_} = 1; } @coursetypes;
12306: 
12307:     my %menutitles = &ltimenu_titles();
12308: 
12309:     my (@items,%deletions,%itemids);
12310:     if ($env{'form.lti_add'}) {
12311:         my $consumer = $env{'form.lti_consumer_add'};
12312:         $consumer =~ s/(`)/'/g;
12313:         ($newid,my $error) = &get_lti_id($dom,$consumer);
12314:         if ($newid) {
12315:             $itemids{'add'} = $newid;
12316:             push(@items,'add');
12317:             $changes{$newid} = 1;
12318:         } else {
12319:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
12320:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12321:         }
12322:     }
12323:     if (ref($domconfig{$action}) eq 'HASH') {
12324:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
12325:         if (@todelete) {
12326:             map { $deletions{$_} = 1; } @todelete;
12327:         }
12328:         my $maxnum = $env{'form.lti_maxnum'};
12329:         for (my $i=0; $i<=$maxnum; $i++) {
12330:             my $itemid = $env{'form.lti_id_'.$i};
12331:             $itemid =~ s/\D+//g;
12332:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12333:                 if ($deletions{$itemid}) {
12334:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
12335:                 } else {
12336:                    push(@items,$i);
12337:                    $itemids{$i} = $itemid;
12338:                 }
12339:             }
12340:         }
12341:     }
12342:     foreach my $idx (@items) {
12343:         my $itemid = $itemids{$idx};
12344:         next unless ($itemid);
12345:         my $position = $env{'form.lti_pos_'.$idx};
12346:         $position =~ s/\D+//g;
12347:         if ($position ne '') {
12348:             $allpos[$position] = $itemid;
12349:         }
12350:         foreach my $item ('consumer','key','secret','lifetime','requser') {
12351:             my $formitem = 'form.lti_'.$item.'_'.$idx;
12352:             $env{$formitem} =~ s/(`)/'/g;
12353:             if ($item eq 'lifetime') {
12354:                 $env{$formitem} =~ s/[^\d.]//g;
12355:             }
12356:             if ($env{$formitem} ne '') {
12357:                 if (($item eq 'key') || ($item eq 'secret')) {
12358:                     $encconfig{$itemid}{$item} = $env{$formitem};
12359:                 } else {
12360:                     $confhash{$itemid}{$item} = $env{$formitem};
12361:                     unless (($idx eq 'add') || ($changes{$itemid})) {
12362:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
12363:                             $changes{$itemid} = 1;
12364:                         }
12365:                     }
12366:                 }
12367:             }
12368:         }
12369:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
12370:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
12371:         }
12372:         if ($confhash{$itemid}{'requser'}) {
12373:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
12374:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
12375:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
12376:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
12377:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
12378:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
12379:                 $mapuser =~ s/(`)/'/g;
12380:                 $mapuser =~ s/^\s+|\s+$//g; 
12381:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
12382:             }
12383:             foreach my $ltirole (@lticourseroles) {
12384:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
12385:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
12386:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
12387:                 }
12388:             }
12389:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
12390:             my @makeuser;
12391:             foreach my $ltirole (sort(@possmakeuser)) {
12392:                 if ($posslti{$ltirole}) {
12393:                     push(@makeuser,$ltirole);
12394:                 }
12395:             }
12396:             $confhash{$itemid}{'makeuser'} = \@makeuser;
12397:             if (@makeuser) {
12398:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
12399:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
12400:                     $confhash{$itemid}{'lcauth'} = $lcauth;
12401:                     if ($lcauth ne 'internal') {
12402:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
12403:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
12404:                         $lcauthparm =~ s/`//g;
12405:                         if ($lcauthparm ne '') {
12406:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
12407:                         }
12408:                     }
12409:                 } else {
12410:                     $confhash{$itemid}{'lcauth'} = 'lti';
12411:                 }
12412:             }
12413:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
12414:             if (@possinstdata) {
12415:                 foreach my $field (@possinstdata) {
12416:                     if (exists($fieldtitles{$field})) {
12417:                         push(@{$confhash{$itemid}{'instdata'}});
12418:                     }
12419:                 }
12420:             }
12421:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
12422:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
12423:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
12424:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
12425:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
12426:                 $mapcrs =~ s/(`)/'/g;
12427:                 $mapcrs =~ s/^\s+|\s+$//g;
12428:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
12429:             }
12430:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
12431:             my @crstypes;
12432:             foreach my $type (sort(@posstypes)) {
12433:                 if ($posscrstype{$type}) {
12434:                     push(@crstypes,$type);
12435:                 }
12436:             }
12437:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
12438:             if ($env{'form.lti_makecrs_'.$idx}) {
12439:                 $confhash{$itemid}{'makecrs'} = 1;
12440:             }
12441:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
12442:             my @selfenroll;
12443:             foreach my $type (sort(@possenroll)) {
12444:                 if ($posslticrs{$type}) {
12445:                     push(@selfenroll,$type);
12446:                 }
12447:             }
12448:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
12449:             if ($env{'form.lti_crssec_'.$idx}) {
12450:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
12451:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
12452:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
12453:                     my $section = $env{'form.lti_customsection_'.$idx};
12454:                     $section =~ s/(`)/'/g;
12455:                     $section =~ s/^\s+|\s+$//g;
12456:                     if ($section ne '') {
12457:                         $confhash{$itemid}{'section'} = $section;
12458:                     }
12459:                 }
12460:             }
12461:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
12462:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
12463:                     $confhash{$itemid}{$field} = 1;
12464:                 }
12465:             }
12466:             if ($env{'form.lti_passback_'.$idx}) {
12467:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
12468:                     $confhash{$itemid}{'passbackformat'} = '1.0';
12469:                 } else {
12470:                     $confhash{$itemid}{'passbackformat'} = '1.1';
12471:                 }
12472:             }
12473:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
12474:                 $confhash{$itemid}{lcmenu} = [];
12475:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
12476:                 foreach my $field (@possmenu) {
12477:                     if (exists($menutitles{$field})) {
12478:                         if ($field eq 'grades') {
12479:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
12480:                         }
12481:                         push(@{$confhash{$itemid}{lcmenu}},$field);
12482:                     }
12483:                 }
12484:             }
12485:             unless (($idx eq 'add') || ($changes{$itemid})) {
12486:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu') {
12487:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
12488:                         $changes{$itemid} = 1;
12489:                     }
12490:                 }
12491:                 unless ($changes{$itemid}) {
12492:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
12493:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
12494:                             $changes{$itemid} = 1;
12495:                         }
12496:                     }
12497:                 }
12498:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
12499:                     unless ($changes{$itemid}) {
12500:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
12501:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12502:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
12503:                                                                                $confhash{$itemid}{$field});
12504:                                 if (@diffs) {
12505:                                     $changes{$itemid} = 1;
12506:                                 }
12507:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
12508:                                 $changes{$itemid} = 1;
12509:                             }
12510:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12511:                             if (@{$confhash{$itemid}{$field}} > 0) {
12512:                                 $changes{$itemid} = 1;
12513:                             }
12514:                         }
12515:                     }
12516:                 }
12517:                 unless ($changes{$itemid}) {
12518:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
12519:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12520:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
12521:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
12522:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
12523:                                     $changes{$itemid} = 1;
12524:                                     last;
12525:                                 }
12526:                             }
12527:                             unless ($changes{$itemid}) {
12528:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
12529:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
12530:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
12531:                                         $changes{$itemid} = 1;
12532:                                         last;
12533:                                     }
12534:                                 }
12535:                             }
12536:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
12537:                             $changes{$itemid} = 1;
12538:                         }
12539:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12540:                         unless ($changes{$itemid}) {
12541:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
12542:                                 $changes{$itemid} = 1;
12543:                             }
12544:                         }
12545:                     }
12546:                 }
12547:             }
12548:         }
12549:     }
12550:     if (@allpos > 0) {
12551:         my $idx = 0;
12552:         foreach my $itemid (@allpos) {
12553:             if ($itemid ne '') {
12554:                 $confhash{$itemid}{'order'} = $idx;
12555:                 if (ref($domconfig{$action}) eq 'HASH') {
12556:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12557:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
12558:                             $changes{$itemid} = 1;
12559:                         }
12560:                     }
12561:                 }
12562:                 $idx ++;
12563:             }
12564:         }
12565:     }
12566:     my %ltihash = (
12567:                           $action => { %confhash }
12568:                        );
12569:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
12570:                                              $dom);
12571:     if ($putresult eq 'ok') {
12572:         my %ltienchash = (
12573:                              $action => { %encconfig }
12574:                          );
12575:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
12576:         if (keys(%changes) > 0) {
12577:             my $cachetime = 24*60*60;
12578:             my %ltiall = %confhash;
12579:             foreach my $id (keys(%ltiall)) {
12580:                 if (ref($encconfig{$id}) eq 'HASH') {
12581:                     foreach my $item ('key','secret') {
12582:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
12583:                     }
12584:                 }
12585:             }
12586:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
12587:             if (ref($lastactref) eq 'HASH') {
12588:                 $lastactref->{'lti'} = 1;
12589:             }
12590:             $resulttext = &mt('Changes made:').'<ul>';
12591:             my %bynum;
12592:             foreach my $itemid (sort(keys(%changes))) {
12593:                 my $position = $confhash{$itemid}{'order'};
12594:                 $bynum{$position} = $itemid;
12595:             }
12596:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12597:                 my $itemid = $bynum{$pos};
12598:                 if (ref($confhash{$itemid}) ne 'HASH') {
12599:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12600:                 } else {
12601:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
12602:                     my $position = $pos + 1;
12603:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12604:                     foreach my $item ('version','lifetime') {
12605:                         if ($confhash{$itemid}{$item} ne '') {
12606:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
12607:                         }
12608:                     }
12609:                     if ($encconfig{$itemid}{'key'} ne '') {
12610:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
12611:                     }
12612:                     if ($encconfig{$itemid}{'secret'} ne '') {
12613:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
12614:                         my $num = length($encconfig{$itemid}{'secret'});
12615:                         $resulttext .= ('*'x$num).'</li>';
12616:                     }
12617:                     if ($confhash{$itemid}{'requser'}) {
12618:                         if ($confhash{$itemid}{'mapuser'}) {
12619:                             my $shownmapuser;
12620:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
12621:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
12622:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
12623:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
12624:                             } else {
12625:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
12626:                             }
12627:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
12628:                         }
12629:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12630:                             my $rolemaps;
12631:                             foreach my $role (@ltiroles) {
12632:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
12633:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
12634:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
12635:                                                                             'Course').',';
12636:                                 }
12637:                             }
12638:                             if ($rolemaps) {
12639:                                 $rolemaps =~ s/,$//;
12640:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
12641:                             }
12642:                         }
12643:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
12644:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
12645:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
12646:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
12647:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
12648:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
12649:                                 } else {
12650:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
12651:                                                        $confhash{$itemid}{'lcauth'});
12652:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
12653:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
12654:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
12655:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
12656:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
12657:                                         }
12658:                                     } else {
12659:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
12660:                                     }
12661:                                 }
12662:                                 $resulttext .= '</li>';
12663:                             } else {
12664:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
12665:                             }
12666:                         }
12667:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
12668:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
12669:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
12670:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
12671:                             } else {
12672:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
12673:                             }
12674:                         }
12675:                         if ($confhash{$itemid}{'mapcrs'}) {
12676:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
12677:                         }
12678:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
12679:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
12680:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
12681:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
12682:                                                '</li>';
12683:                             } else {
12684:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
12685:                             }
12686:                         }
12687:                         if ($confhash{$itemid}{'makecrs'}) {
12688:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
12689:                         } else {
12690:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
12691:                         }
12692:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
12693:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
12694:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
12695:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
12696:                                                '</li>';
12697:                             } else {
12698:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
12699:                             }
12700:                         }
12701:                         if ($confhash{$itemid}{'section'}) {
12702:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
12703:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
12704:                                                      ' (course_section_sourcedid)'.'</li>';  
12705:                             } else {
12706:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
12707:                                                       $confhash{$itemid}{'section'}.'</li>';
12708:                             }
12709:                         } else {
12710:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
12711:                         }
12712:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
12713:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
12714:                             if ($confhash{$itemid}{$item}) {
12715:                                 $resulttext .= &mt('Yes');
12716:                                 if ($item eq 'passback') {
12717:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
12718:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
12719:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
12720:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
12721:                                     }
12722:                                 }
12723:                             } else {
12724:                                 $resulttext .= &mt('No');
12725:                             }
12726:                             $resulttext .= '</li>';
12727:                         }
12728:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
12729:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
12730:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
12731:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
12732:                             } else {
12733:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
12734:                             }
12735:                         }
12736:                     }
12737:                     $resulttext .= '</ul></li>';
12738:                 }
12739:             }
12740:             $resulttext .= '</ul>';
12741:         } else {
12742:             $resulttext = &mt('No changes made.');
12743:         }
12744:     } else {
12745:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12746:     }
12747:     if ($errors) {
12748:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12749:                        $errors.'</ul>';
12750:     }
12751:     return $resulttext;
12752: }
12753: 
12754: sub get_lti_id {
12755:     my ($domain,$consumer) = @_;
12756:     # get lock on lti db
12757:     my $lockhash = {
12758:                       lock => $env{'user.name'}.
12759:                               ':'.$env{'user.domain'},
12760:                    };
12761:     my $tries = 0;
12762:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12763:     my ($id,$error);
12764: 
12765:     while (($gotlock ne 'ok') && ($tries<10)) {
12766:         $tries ++;
12767:         sleep (0.1);
12768:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
12769:     }
12770:     if ($gotlock eq 'ok') {
12771:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
12772:         if ($currids{'lock'}) {
12773:             delete($currids{'lock'});
12774:             if (keys(%currids)) {
12775:                 my @curr = sort { $a <=> $b } keys(%currids);
12776:                 if ($curr[-1] =~ /^\d+$/) {
12777:                     $id = 1 + $curr[-1];
12778:                 }
12779:             } else {
12780:                 $id = 1;
12781:             }
12782:             if ($id) {
12783:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
12784:                     $error = 'nostore';
12785:                 }
12786:             } else {
12787:                 $error = 'nonumber';
12788:             }
12789:         }
12790:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
12791:     } else {
12792:         $error = 'nolock';
12793:     }
12794:     return ($id,$error);
12795: }
12796: 
12797: sub modify_autoenroll {
12798:     my ($dom,$lastactref,%domconfig) = @_;
12799:     my ($resulttext,%changes);
12800:     my %currautoenroll;
12801:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
12802:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
12803:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
12804:         }
12805:     }
12806:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
12807:     my %title = ( run => 'Auto-enrollment active',
12808:                   sender => 'Sender for notification messages',
12809:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
12810:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
12811:     my @offon = ('off','on');
12812:     my $sender_uname = $env{'form.sender_uname'};
12813:     my $sender_domain = $env{'form.sender_domain'};
12814:     if ($sender_domain eq '') {
12815:         $sender_uname = '';
12816:     } elsif ($sender_uname eq '') {
12817:         $sender_domain = '';
12818:     }
12819:     my $coowners = $env{'form.autoassign_coowners'};
12820:     my $failsafe = $env{'form.autoenroll_failsafe'};
12821:     $failsafe =~ s{^\s+|\s+$}{}g;
12822:     if ($failsafe =~ /\D/) {
12823:         undef($failsafe);
12824:     }
12825:     my %autoenrollhash =  (
12826:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
12827:                                        'sender_uname' => $sender_uname,
12828:                                        'sender_domain' => $sender_domain,
12829:                                        'co-owners' => $coowners,
12830:                                        'autofailsafe' => $failsafe,
12831:                                 }
12832:                      );
12833:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
12834:                                              $dom);
12835:     if ($putresult eq 'ok') {
12836:         if (exists($currautoenroll{'run'})) {
12837:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
12838:                  $changes{'run'} = 1;
12839:              }
12840:         } elsif ($autorun) {
12841:             if ($env{'form.autoenroll_run'} ne '1') {
12842:                  $changes{'run'} = 1;
12843:             }
12844:         }
12845:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
12846:             $changes{'sender'} = 1;
12847:         }
12848:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
12849:             $changes{'sender'} = 1;
12850:         }
12851:         if ($currautoenroll{'co-owners'} ne '') {
12852:             if ($currautoenroll{'co-owners'} ne $coowners) {
12853:                 $changes{'coowners'} = 1;
12854:             }
12855:         } elsif ($coowners) {
12856:             $changes{'coowners'} = 1;
12857:         }
12858:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
12859:             $changes{'autofailsafe'} = 1;
12860:         }
12861:         if (keys(%changes) > 0) {
12862:             $resulttext = &mt('Changes made:').'<ul>';
12863:             if ($changes{'run'}) {
12864:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
12865:             }
12866:             if ($changes{'sender'}) {
12867:                 if ($sender_uname eq '' || $sender_domain eq '') {
12868:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
12869:                 } else {
12870:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
12871:                 }
12872:             }
12873:             if ($changes{'coowners'}) {
12874:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
12875:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12876:                 if (ref($lastactref) eq 'HASH') {
12877:                     $lastactref->{'domainconfig'} = 1;
12878:                 }
12879:             }
12880:             if ($changes{'autofailsafe'}) {
12881:                 if ($failsafe ne '') {
12882:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
12883:                 } else {
12884:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
12885:                 }
12886:                 &Apache::lonnet::get_domain_defaults($dom,1);
12887:                 if (ref($lastactref) eq 'HASH') {
12888:                     $lastactref->{'domdefaults'} = 1;
12889:                 }
12890:             }
12891:             $resulttext .= '</ul>';
12892:         } else {
12893:             $resulttext = &mt('No changes made to auto-enrollment settings');
12894:         }
12895:     } else {
12896:         $resulttext = '<span class="LC_error">'.
12897: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12898:     }
12899:     return $resulttext;
12900: }
12901: 
12902: sub modify_autoupdate {
12903:     my ($dom,%domconfig) = @_;
12904:     my ($resulttext,%currautoupdate,%fields,%changes);
12905:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
12906:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
12907:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
12908:         }
12909:     }
12910:     my @offon = ('off','on');
12911:     my %title = &Apache::lonlocal::texthash (
12912:                    run => 'Auto-update:',
12913:                    classlists => 'Updates to user information in classlists?'
12914:                 );
12915:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12916:     my %fieldtitles = &Apache::lonlocal::texthash (
12917:                         id => 'Student/Employee ID',
12918:                         permanentemail => 'E-mail address',
12919:                         lastname => 'Last Name',
12920:                         firstname => 'First Name',
12921:                         middlename => 'Middle Name',
12922:                         generation => 'Generation',
12923:                       );
12924:     $othertitle = &mt('All users');
12925:     if (keys(%{$usertypes}) >  0) {
12926:         $othertitle = &mt('Other users');
12927:     }
12928:     foreach my $key (keys(%env)) {
12929:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
12930:             my ($usertype,$item) = ($1,$2);
12931:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
12932:                 if ($usertype eq 'default') {   
12933:                     push(@{$fields{$1}},$2);
12934:                 } elsif (ref($types) eq 'ARRAY') {
12935:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
12936:                         push(@{$fields{$1}},$2);
12937:                     }
12938:                 }
12939:             }
12940:         }
12941:     }
12942:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
12943:     @lockablenames = sort(@lockablenames);
12944:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
12945:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12946:         if (@changed) {
12947:             $changes{'lockablenames'} = 1;
12948:         }
12949:     } else {
12950:         if (@lockablenames) {
12951:             $changes{'lockablenames'} = 1;
12952:         }
12953:     }
12954:     my %updatehash = (
12955:                       autoupdate => { run => $env{'form.autoupdate_run'},
12956:                                       classlists => $env{'form.classlists'},
12957:                                       fields => {%fields},
12958:                                       lockablenames => \@lockablenames,
12959:                                     }
12960:                      );
12961:     foreach my $key (keys(%currautoupdate)) {
12962:         if (($key eq 'run') || ($key eq 'classlists')) {
12963:             if (exists($updatehash{autoupdate}{$key})) {
12964:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
12965:                     $changes{$key} = 1;
12966:                 }
12967:             }
12968:         } elsif ($key eq 'fields') {
12969:             if (ref($currautoupdate{$key}) eq 'HASH') {
12970:                 foreach my $item (@{$types},'default') {
12971:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
12972:                         my $change = 0;
12973:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
12974:                             if (!exists($fields{$item})) {
12975:                                 $change = 1;
12976:                                 last;
12977:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
12978:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
12979:                                     $change = 1;
12980:                                     last;
12981:                                 }
12982:                             }
12983:                         }
12984:                         if ($change) {
12985:                             push(@{$changes{$key}},$item);
12986:                         }
12987:                     } 
12988:                 }
12989:             }
12990:         } elsif ($key eq 'lockablenames') {
12991:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
12992:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
12993:                 if (@changed) {
12994:                     $changes{'lockablenames'} = 1;
12995:                 }
12996:             } else {
12997:                 if (@lockablenames) {
12998:                     $changes{'lockablenames'} = 1;
12999:                 }
13000:             }
13001:         }
13002:     }
13003:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
13004:         if (@lockablenames) {
13005:             $changes{'lockablenames'} = 1;
13006:         }
13007:     }
13008:     foreach my $item (@{$types},'default') {
13009:         if (defined($fields{$item})) {
13010:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
13011:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
13012:                     my $change = 0;
13013:                     if (ref($fields{$item}) eq 'ARRAY') {
13014:                         foreach my $type (@{$fields{$item}}) {
13015:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
13016:                                 $change = 1;
13017:                                 last;
13018:                             }
13019:                         }
13020:                     }
13021:                     if ($change) {
13022:                         push(@{$changes{'fields'}},$item);
13023:                     }
13024:                 } else {
13025:                     push(@{$changes{'fields'}},$item);
13026:                 }
13027:             } else {
13028:                 push(@{$changes{'fields'}},$item);
13029:             }
13030:         }
13031:     }
13032:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
13033:                                              $dom);
13034:     if ($putresult eq 'ok') {
13035:         if (keys(%changes) > 0) {
13036:             $resulttext = &mt('Changes made:').'<ul>';
13037:             foreach my $key (sort(keys(%changes))) {
13038:                 if ($key eq 'lockablenames') {
13039:                     $resulttext .= '<li>';
13040:                     if (@lockablenames) {
13041:                         $usertypes->{'default'} = $othertitle;
13042:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
13043:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
13044:                     } else {
13045:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
13046:                     }
13047:                     $resulttext .= '</li>';
13048:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
13049:                     foreach my $item (@{$changes{$key}}) {
13050:                         my @newvalues;
13051:                         foreach my $type (@{$fields{$item}}) {
13052:                             push(@newvalues,$fieldtitles{$type});
13053:                         }
13054:                         my $newvaluestr;
13055:                         if (@newvalues > 0) {
13056:                             $newvaluestr = join(', ',@newvalues);
13057:                         } else {
13058:                             $newvaluestr = &mt('none');
13059:                         }
13060:                         if ($item eq 'default') {
13061:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
13062:                         } else {
13063:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
13064:                         }
13065:                     }
13066:                 } else {
13067:                     my $newvalue;
13068:                     if ($key eq 'run') {
13069:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
13070:                     } else {
13071:                         $newvalue = $offon[$env{'form.'.$key}];
13072:                     }
13073:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
13074:                 }
13075:             }
13076:             $resulttext .= '</ul>';
13077:         } else {
13078:             $resulttext = &mt('No changes made to autoupdates');
13079:         }
13080:     } else {
13081:         $resulttext = '<span class="LC_error">'.
13082: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13083:     }
13084:     return $resulttext;
13085: }
13086: 
13087: sub modify_autocreate {
13088:     my ($dom,%domconfig) = @_;
13089:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
13090:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
13091:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
13092:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
13093:         }
13094:     }
13095:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
13096:                  req => 'Auto-creation of validated requests for official courses',
13097:                  xmldc => 'Identity of course creator of courses from XML files',
13098:                );
13099:     my @types = ('xml','req');
13100:     foreach my $item (@types) {
13101:         $newvals{$item} = $env{'form.autocreate_'.$item};
13102:         $newvals{$item} =~ s/\D//g;
13103:         $newvals{$item} = 0 if ($newvals{$item} eq '');
13104:     }
13105:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
13106:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13107:     unless (exists($domcoords{$newvals{'xmldc'}})) {
13108:         $newvals{'xmldc'} = '';
13109:     } 
13110:     %autocreatehash =  (
13111:                         autocreate => { xml => $newvals{'xml'},
13112:                                         req => $newvals{'req'},
13113:                                       }
13114:                        );
13115:     if ($newvals{'xmldc'} ne '') {
13116:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
13117:     }
13118:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
13119:                                              $dom);
13120:     if ($putresult eq 'ok') {
13121:         my @items = @types;
13122:         if ($newvals{'xml'}) {
13123:             push(@items,'xmldc');
13124:         }
13125:         foreach my $item (@items) {
13126:             if (exists($currautocreate{$item})) {
13127:                 if ($currautocreate{$item} ne $newvals{$item}) {
13128:                     $changes{$item} = 1;
13129:                 }
13130:             } elsif ($newvals{$item}) {
13131:                 $changes{$item} = 1;
13132:             }
13133:         }
13134:         if (keys(%changes) > 0) {
13135:             my @offon = ('off','on'); 
13136:             $resulttext = &mt('Changes made:').'<ul>';
13137:             foreach my $item (@types) {
13138:                 if ($changes{$item}) {
13139:                     my $newtxt = $offon[$newvals{$item}];
13140:                     $resulttext .= '<li>'.
13141:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
13142:                                        '<b>','</b>').
13143:                                    '</li>';
13144:                 }
13145:             }
13146:             if ($changes{'xmldc'}) {
13147:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
13148:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
13149:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
13150:             }
13151:             $resulttext .= '</ul>';
13152:         } else {
13153:             $resulttext = &mt('No changes made to auto-creation settings');
13154:         }
13155:     } else {
13156:         $resulttext = '<span class="LC_error">'.
13157:             &mt('An error occurred: [_1]',$putresult).'</span>';
13158:     }
13159:     return $resulttext;
13160: }
13161: 
13162: sub modify_directorysrch {
13163:     my ($dom,$lastactref,%domconfig) = @_;
13164:     my ($resulttext,%changes);
13165:     my %currdirsrch;
13166:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
13167:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
13168:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
13169:         }
13170:     }
13171:     my %title = ( available => 'Institutional directory search available',
13172:                   localonly => 'Other domains can search institution',
13173:                   lcavailable => 'LON-CAPA directory search available',
13174:                   lclocalonly => 'Other domains can search LON-CAPA domain',
13175:                   searchby => 'Search types',
13176:                   searchtypes => 'Search latitude');
13177:     my @offon = ('off','on');
13178:     my @otherdoms = ('Yes','No');
13179: 
13180:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
13181:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
13182:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
13183: 
13184:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13185:     if (keys(%{$usertypes}) == 0) {
13186:         @cansearch = ('default');
13187:     } else {
13188:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
13189:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
13190:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
13191:                     push(@{$changes{'cansearch'}},$type);
13192:                 }
13193:             }
13194:             foreach my $type (@cansearch) {
13195:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
13196:                     push(@{$changes{'cansearch'}},$type);
13197:                 }
13198:             }
13199:         } else {
13200:             push(@{$changes{'cansearch'}},@cansearch);
13201:         }
13202:     }
13203: 
13204:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
13205:         foreach my $by (@{$currdirsrch{'searchby'}}) {
13206:             if (!grep(/^\Q$by\E$/,@searchby)) {
13207:                 push(@{$changes{'searchby'}},$by);
13208:             }
13209:         }
13210:         foreach my $by (@searchby) {
13211:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
13212:                 push(@{$changes{'searchby'}},$by);
13213:             }
13214:         }
13215:     } else {
13216:         push(@{$changes{'searchby'}},@searchby);
13217:     }
13218: 
13219:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
13220:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
13221:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
13222:                 push(@{$changes{'searchtypes'}},$type);
13223:             }
13224:         }
13225:         foreach my $type (@searchtypes) {
13226:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
13227:                 push(@{$changes{'searchtypes'}},$type);
13228:             }
13229:         }
13230:     } else {
13231:         if (exists($currdirsrch{'searchtypes'})) {
13232:             foreach my $type (@searchtypes) {  
13233:                 if ($type ne $currdirsrch{'searchtypes'}) { 
13234:                     push(@{$changes{'searchtypes'}},$type);
13235:                 }
13236:             }
13237:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
13238:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
13239:             }   
13240:         } else {
13241:             push(@{$changes{'searchtypes'}},@searchtypes); 
13242:         }
13243:     }
13244: 
13245:     my %dirsrch_hash =  (
13246:             directorysrch => { available => $env{'form.dirsrch_available'},
13247:                                cansearch => \@cansearch,
13248:                                localonly => $env{'form.dirsrch_instlocalonly'},
13249:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
13250:                                lcavailable => $env{'form.dirsrch_domavailable'},
13251:                                searchby => \@searchby,
13252:                                searchtypes => \@searchtypes,
13253:                              }
13254:             );
13255:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
13256:                                              $dom);
13257:     if ($putresult eq 'ok') {
13258:         if (exists($currdirsrch{'available'})) {
13259:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
13260:                  $changes{'available'} = 1;
13261:              }
13262:         } else {
13263:             if ($env{'form.dirsrch_available'} eq '1') {
13264:                 $changes{'available'} = 1;
13265:             }
13266:         }
13267:         if (exists($currdirsrch{'lcavailable'})) {
13268:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
13269:                 $changes{'lcavailable'} = 1;
13270:             }
13271:         } else {
13272:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
13273:                 $changes{'lcavailable'} = 1;
13274:             }
13275:         }
13276:         if (exists($currdirsrch{'localonly'})) {
13277:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
13278:                 $changes{'localonly'} = 1;
13279:             }
13280:         } else {
13281:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
13282:                 $changes{'localonly'} = 1;
13283:             }
13284:         }
13285:         if (exists($currdirsrch{'lclocalonly'})) {
13286:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
13287:                 $changes{'lclocalonly'} = 1;
13288:             }
13289:         } else {
13290:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
13291:                 $changes{'lclocalonly'} = 1;
13292:             }
13293:         }
13294:         if (keys(%changes) > 0) {
13295:             $resulttext = &mt('Changes made:').'<ul>';
13296:             if ($changes{'available'}) {
13297:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
13298:             }
13299:             if ($changes{'lcavailable'}) {
13300:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
13301:             }
13302:             if ($changes{'localonly'}) {
13303:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
13304:             }
13305:             if ($changes{'lclocalonly'}) {
13306:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
13307:             }
13308:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
13309:                 my $chgtext;
13310:                 if (ref($usertypes) eq 'HASH') {
13311:                     if (keys(%{$usertypes}) > 0) {
13312:                         foreach my $type (@{$types}) {
13313:                             if (grep(/^\Q$type\E$/,@cansearch)) {
13314:                                 $chgtext .= $usertypes->{$type}.'; ';
13315:                             }
13316:                         }
13317:                         if (grep(/^default$/,@cansearch)) {
13318:                             $chgtext .= $othertitle;
13319:                         } else {
13320:                             $chgtext =~ s/\; $//;
13321:                         }
13322:                         $resulttext .=
13323:                             '<li>'.
13324:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
13325:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
13326:                             '</li>';
13327:                     }
13328:                 }
13329:             }
13330:             if (ref($changes{'searchby'}) eq 'ARRAY') {
13331:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
13332:                 my $chgtext;
13333:                 foreach my $type (@{$titleorder}) {
13334:                     if (grep(/^\Q$type\E$/,@searchby)) {
13335:                         if (defined($searchtitles->{$type})) {
13336:                             $chgtext .= $searchtitles->{$type}.'; ';
13337:                         }
13338:                     }
13339:                 }
13340:                 $chgtext =~ s/\; $//;
13341:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
13342:             }
13343:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
13344:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
13345:                 my $chgtext;
13346:                 foreach my $type (@{$srchtypeorder}) {
13347:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
13348:                         if (defined($srchtypes_desc->{$type})) {
13349:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
13350:                         }
13351:                     }
13352:                 }
13353:                 $chgtext =~ s/\; $//;
13354:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
13355:             }
13356:             $resulttext .= '</ul>';
13357:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
13358:             if (ref($lastactref) eq 'HASH') {
13359:                 $lastactref->{'directorysrch'} = 1;
13360:             }
13361:         } else {
13362:             $resulttext = &mt('No changes made to directory search settings');
13363:         }
13364:     } else {
13365:         $resulttext = '<span class="LC_error">'.
13366:                       &mt('An error occurred: [_1]',$putresult).'</span>';
13367:     }
13368:     return $resulttext;
13369: }
13370: 
13371: sub modify_contacts {
13372:     my ($dom,$lastactref,%domconfig) = @_;
13373:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
13374:     if (ref($domconfig{'contacts'}) eq 'HASH') {
13375:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
13376:             $currsetting{$key} = $domconfig{'contacts'}{$key};
13377:         }
13378:     }
13379:     my (%others,%to,%bcc,%includestr,%includeloc);
13380:     my @contacts = ('supportemail','adminemail');
13381:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
13382:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
13383:     my @toggles = ('reporterrors','reportupdates','reportstatus');
13384:     my @lonstatus = ('threshold','sysmail','weights','excluded');
13385:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
13386:     foreach my $type (@mailings) {
13387:         @{$newsetting{$type}} = 
13388:             &Apache::loncommon::get_env_multiple('form.'.$type);
13389:         foreach my $item (@contacts) {
13390:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
13391:                 $contacts_hash{contacts}{$type}{$item} = 1;
13392:             } else {
13393:                 $contacts_hash{contacts}{$type}{$item} = 0;
13394:             }
13395:         }
13396:         $others{$type} = $env{'form.'.$type.'_others'};
13397:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
13398:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13399:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
13400:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
13401:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
13402:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
13403:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
13404:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13405:             }
13406:         }
13407:     }
13408:     foreach my $item (@contacts) {
13409:         $to{$item} = $env{'form.'.$item};
13410:         $contacts_hash{'contacts'}{$item} = $to{$item};
13411:     }
13412:     foreach my $item (@toggles) {
13413:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13414:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
13415:         }
13416:     }
13417:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
13418:     foreach my $item (@lonstatus) {
13419:         if ($item eq 'excluded') {
13420:             my (%serverhomes,@excluded);
13421:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
13422:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
13423:             if (@possexcluded) {
13424:                 foreach my $id (sort(@possexcluded)) {
13425:                     if ($serverhomes{$id}) {
13426:                         push(@excluded,$id);
13427:                     }
13428:                 }
13429:             }
13430:             if (@excluded) {
13431:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
13432:             }
13433:         } elsif ($item eq 'weights') {
13434:             foreach my $type ('E','W','N') {
13435:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
13436:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
13437:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
13438:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
13439:                             $env{'form.error'.$item.'_'.$type};
13440:                     }
13441:                 }
13442:             }
13443:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
13444:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
13445:             if ($env{'form.error'.$item} =~ /^\d+$/) {
13446:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
13447:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
13448:                 }
13449:             }
13450:         }
13451:     }
13452:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
13453:         foreach my $field (@{$fields}) {
13454:             if (ref($possoptions->{$field}) eq 'ARRAY') {
13455:                 my $value = $env{'form.helpform_'.$field};
13456:                 $value =~ s/^\s+|\s+$//g;
13457:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
13458:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
13459:                     if ($field eq 'screenshot') {
13460:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
13461:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
13462:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
13463:                         }
13464:                     }
13465:                 }
13466:             }
13467:         }
13468:     }
13469:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13470:     my (@statuses,%usertypeshash,@overrides);
13471:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
13472:         @statuses = @{$types};
13473:         if (ref($usertypes) eq 'HASH') {
13474:             %usertypeshash = %{$usertypes};
13475:         }
13476:     }
13477:     if (@statuses) {
13478:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
13479:         foreach my $type (@possoverrides) {
13480:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
13481:                 push(@overrides,$type);
13482:             }
13483:         }
13484:         if (@overrides) {
13485:             foreach my $type (@overrides) {
13486:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
13487:                 foreach my $item (@contacts) {
13488:                     if (grep(/^\Q$item\E$/,@standard)) {
13489:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
13490:                         $newsetting{'override_'.$type}{$item} = 1;
13491:                     } else {
13492:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
13493:                         $newsetting{'override_'.$type}{$item} = 0;
13494:                     }
13495:                 }
13496:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
13497:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13498:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
13499:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13500:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
13501:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
13502:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
13503:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13504:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
13505:                 }
13506:             }     
13507:         }
13508:     }
13509:     if (keys(%currsetting) > 0) {
13510:         foreach my $item (@contacts) {
13511:             if ($to{$item} ne $currsetting{$item}) {
13512:                 $changes{$item} = 1;
13513:             }
13514:         }
13515:         foreach my $type (@mailings) {
13516:             foreach my $item (@contacts) {
13517:                 if (ref($currsetting{$type}) eq 'HASH') {
13518:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
13519:                         push(@{$changes{$type}},$item);
13520:                     }
13521:                 } else {
13522:                     push(@{$changes{$type}},@{$newsetting{$type}});
13523:                 }
13524:             }
13525:             if ($others{$type} ne $currsetting{$type}{'others'}) {
13526:                 push(@{$changes{$type}},'others');
13527:             }
13528:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13529:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
13530:                     push(@{$changes{$type}},'bcc'); 
13531:                 }
13532:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
13533:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
13534:                     push(@{$changes{$type}},'include');
13535:                 }
13536:             }
13537:         }
13538:         if (ref($fields) eq 'ARRAY') {
13539:             if (ref($currsetting{'helpform'}) eq 'HASH') {
13540:                 foreach my $field (@{$fields}) {
13541:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
13542:                         push(@{$changes{'helpform'}},$field);
13543:                     }
13544:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13545:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
13546:                             push(@{$changes{'helpform'}},'maxsize');
13547:                         }
13548:                     }
13549:                 }
13550:             } else {
13551:                 foreach my $field (@{$fields}) {
13552:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13553:                         push(@{$changes{'helpform'}},$field);
13554:                     }
13555:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13556:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13557:                             push(@{$changes{'helpform'}},'maxsize');
13558:                         }
13559:                     }
13560:                 }
13561:             }
13562:         }
13563:         if (@statuses) {
13564:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
13565:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
13566:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
13567:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
13568:                             foreach my $item (@contacts,'bcc','others','include') {
13569:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
13570:                                     push(@{$changes{'overrides'}},$key);
13571:                                     last;
13572:                                 }
13573:                             }
13574:                         } else {
13575:                             push(@{$changes{'overrides'}},$key);
13576:                         }
13577:                     }
13578:                 }
13579:                 foreach my $key (@overrides) {
13580:                     unless (exists($currsetting{'overrides'}{$key})) {
13581:                         push(@{$changes{'overrides'}},$key);
13582:                     }
13583:                 }
13584:             } else {
13585:                 foreach my $key (@overrides) {
13586:                     push(@{$changes{'overrides'}},$key); 
13587:                 }
13588:             }
13589:         }
13590:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
13591:             foreach my $key ('excluded','weights','threshold','sysmail') {
13592:                 if ($key eq 'excluded') {
13593:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13594:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
13595:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13596:                             (@{$currsetting{'lonstatus'}{$key}})) {
13597:                             my @diffs =
13598:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
13599:                                                                    $currsetting{'lonstatus'}{$key});
13600:                             if (@diffs) {
13601:                                 push(@{$changes{'lonstatus'}},$key);
13602:                             }
13603:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
13604:                             push(@{$changes{'lonstatus'}},$key);
13605:                         }
13606:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13607:                              (@{$currsetting{'lonstatus'}{$key}})) {
13608:                         push(@{$changes{'lonstatus'}},$key);
13609:                     }
13610:                 } elsif ($key eq 'weights') {
13611:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13612:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
13613:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13614:                             foreach my $type ('E','W','N','U') {
13615:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
13616:                                         $currsetting{'lonstatus'}{$key}{$type}) {
13617:                                     push(@{$changes{'lonstatus'}},$key);
13618:                                     last;
13619:                                 }
13620:                             }
13621:                         } else {
13622:                             foreach my $type ('E','W','N','U') {
13623:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
13624:                                     push(@{$changes{'lonstatus'}},$key);
13625:                                     last;
13626:                                 }
13627:                             }
13628:                         }
13629:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13630:                         foreach my $type ('E','W','N','U') {
13631:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
13632:                                 push(@{$changes{'lonstatus'}},$key);
13633:                                 last;
13634:                             }
13635:                         }
13636:                     }
13637:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
13638:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13639:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13640:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
13641:                                 push(@{$changes{'lonstatus'}},$key);
13642:                             }
13643:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
13644:                             push(@{$changes{'lonstatus'}},$key);
13645:                         }
13646:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13647:                         push(@{$changes{'lonstatus'}},$key);
13648:                     }
13649:                 }
13650:             }
13651:         } else {
13652:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13653:                 foreach my $key ('excluded','weights','threshold','sysmail') {
13654:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13655:                         push(@{$changes{'lonstatus'}},$key);
13656:                     }
13657:                 }
13658:             }
13659:         }
13660:     } else {
13661:         my %default;
13662:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
13663:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
13664:         $default{'errormail'} = 'adminemail';
13665:         $default{'packagesmail'} = 'adminemail';
13666:         $default{'helpdeskmail'} = 'supportemail';
13667:         $default{'otherdomsmail'} = 'supportemail';
13668:         $default{'lonstatusmail'} = 'adminemail';
13669:         $default{'requestsmail'} = 'adminemail';
13670:         $default{'updatesmail'} = 'adminemail';
13671:         $default{'hostipmail'} = 'adminemail';
13672:         foreach my $item (@contacts) {
13673:            if ($to{$item} ne $default{$item}) {
13674:                $changes{$item} = 1;
13675:            }
13676:         }
13677:         foreach my $type (@mailings) {
13678:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
13679:                 push(@{$changes{$type}},@{$newsetting{$type}});
13680:             }
13681:             if ($others{$type} ne '') {
13682:                 push(@{$changes{$type}},'others');
13683:             }
13684:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13685:                 if ($bcc{$type} ne '') {
13686:                     push(@{$changes{$type}},'bcc');
13687:                 }
13688:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
13689:                     push(@{$changes{$type}},'include');
13690:                 }
13691:             }
13692:         }
13693:         if (ref($fields) eq 'ARRAY') {
13694:             foreach my $field (@{$fields}) {
13695:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13696:                     push(@{$changes{'helpform'}},$field);
13697:                 }
13698:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13699:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13700:                         push(@{$changes{'helpform'}},'maxsize');
13701:                     }
13702:                 }
13703:             }
13704:         }
13705:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13706:             foreach my $key ('excluded','weights','threshold','sysmail') {
13707:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13708:                     push(@{$changes{'lonstatus'}},$key);
13709:                 }
13710:             }
13711:         }
13712:     }
13713:     foreach my $item (@toggles) {
13714:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
13715:             $changes{$item} = 1;
13716:         } elsif ((!$env{'form.'.$item}) &&
13717:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
13718:             $changes{$item} = 1;
13719:         }
13720:     }
13721:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
13722:                                              $dom);
13723:     if ($putresult eq 'ok') {
13724:         if (keys(%changes) > 0) {
13725:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13726:             if (ref($lastactref) eq 'HASH') {
13727:                 $lastactref->{'domainconfig'} = 1;
13728:             }
13729:             my ($titles,$short_titles)  = &contact_titles();
13730:             $resulttext = &mt('Changes made:').'<ul>';
13731:             foreach my $item (@contacts) {
13732:                 if ($changes{$item}) {
13733:                     $resulttext .= '<li>'.$titles->{$item}.
13734:                                     &mt(' set to: ').
13735:                                     '<span class="LC_cusr_emph">'.
13736:                                     $to{$item}.'</span></li>';
13737:                 }
13738:             }
13739:             foreach my $type (@mailings) {
13740:                 if (ref($changes{$type}) eq 'ARRAY') {
13741:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13742:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
13743:                     } else {
13744:                         $resulttext .= '<li>'.$titles->{$type}.': ';
13745:                     }
13746:                     my @text;
13747:                     foreach my $item (@{$newsetting{$type}}) {
13748:                         push(@text,$short_titles->{$item});
13749:                     }
13750:                     if ($others{$type} ne '') {
13751:                         push(@text,$others{$type});
13752:                     }
13753:                     if (@text) {
13754:                         $resulttext .= '<span class="LC_cusr_emph">'.
13755:                                        join(', ',@text).'</span>';
13756:                     }
13757:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13758:                         if ($bcc{$type} ne '') {
13759:                             my $bcctext;
13760:                             if (@text) {
13761:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
13762:                             } else {
13763:                                 $bcctext = '(Bcc)';
13764:                             }
13765:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
13766:                         } elsif (!@text) {
13767:                             $resulttext .= &mt('No one');
13768:                         }   
13769:                         if ($includestr{$type} ne '') {
13770:                             if ($includeloc{$type} eq 'b') {
13771:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
13772:                             } elsif ($includeloc{$type} eq 's') {
13773:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
13774:                             }
13775:                         }
13776:                     } elsif (!@text) {
13777:                         $resulttext .= &mt('No recipients');
13778:                     }
13779:                     $resulttext .= '</li>';
13780:                 }
13781:             }
13782:             if (ref($changes{'overrides'}) eq 'ARRAY') {
13783:                 my @deletions;
13784:                 foreach my $type (@{$changes{'overrides'}}) {
13785:                     if ($usertypeshash{$type}) {
13786:                         if (grep(/^\Q$type\E/,@overrides)) {
13787:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
13788:                                                       $usertypeshash{$type}).'<ul><li>';
13789:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
13790:                                 my @text;
13791:                                 foreach my $item (@contacts) {
13792:                                     if ($newsetting{'override_'.$type}{$item}) { 
13793:                                         push(@text,$short_titles->{$item});
13794:                                     }
13795:                                 }
13796:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
13797:                                     push(@text,$newsetting{'override_'.$type}{'others'});
13798:                                 }
13799:   
13800:                                 if (@text) {
13801:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
13802:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
13803:                                 }
13804:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
13805:                                     my $bcctext;
13806:                                     if (@text) {
13807:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
13808:                                     } else {
13809:                                         $bcctext = '(Bcc)';
13810:                                     }
13811:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
13812:                                 } elsif (!@text) {
13813:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
13814:                                 }
13815:                                 $resulttext .= '</li>';
13816:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
13817:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
13818:                                     if ($loc eq 'b') {
13819:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
13820:                                     } elsif ($loc eq 's') {
13821:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
13822:                                     }
13823:                                 }
13824:                             }
13825:                             $resulttext .= '</li></ul></li>';
13826:                         } else {
13827:                             push(@deletions,$usertypeshash{$type});
13828:                         }
13829:                     }
13830:                 }
13831:                 if (@deletions) {
13832:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
13833:                                               join(', ',@deletions)).'</li>';
13834:                 }
13835:             }
13836:             my @offon = ('off','on');
13837:             my $corelink = &core_link_msu();
13838:             if ($changes{'reporterrors'}) {
13839:                 $resulttext .= '<li>'.
13840:                                &mt('E-mail error reports to [_1] set to "'.
13841:                                    $offon[$env{'form.reporterrors'}].'".',
13842:                                    $corelink).
13843:                                '</li>';
13844:             }
13845:             if ($changes{'reportupdates'}) {
13846:                 $resulttext .= '<li>'.
13847:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
13848:                                     $offon[$env{'form.reportupdates'}].'".',
13849:                                     $corelink).
13850:                                 '</li>';
13851:             }
13852:             if ($changes{'reportstatus'}) {
13853:                 $resulttext .= '<li>'.
13854:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
13855:                                     $offon[$env{'form.reportstatus'}].'".',
13856:                                     $corelink).
13857:                                 '</li>';
13858:             }
13859:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
13860:                 $resulttext .= '<li>'.
13861:                                &mt('Nightly status check e-mail settings').':<ul>';
13862:                 my (%defval,%use_def,%shown);
13863:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
13864:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
13865:                 $defval{'weights'} =
13866:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
13867:                 $defval{'excluded'} = &mt('None');
13868:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
13869:                     foreach my $item ('threshold','sysmail','weights','excluded') {
13870:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
13871:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
13872:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
13873:                             } elsif ($item eq 'weights') {
13874:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
13875:                                     foreach my $type ('E','W','N','U') {
13876:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
13877:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
13878:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
13879:                                         } else {
13880:                                             $shown{$item} .= $lonstatus_defs->{$type};
13881:                                         }
13882:                                         $shown{$item} .= ', ';
13883:                                     }
13884:                                     $shown{$item} =~ s/, $//;
13885:                                 } else {
13886:                                     $shown{$item} = $defval{$item};
13887:                                 }
13888:                             } elsif ($item eq 'excluded') {
13889:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
13890:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
13891:                                 } else {
13892:                                     $shown{$item} = $defval{$item};
13893:                                 }
13894:                             }
13895:                         } else {
13896:                             $shown{$item} = $defval{$item};
13897:                         }
13898:                     }
13899:                 } else {
13900:                     foreach my $item ('threshold','weights','excluded','sysmail') {
13901:                         $shown{$item} = $defval{$item};
13902:                     }
13903:                 }
13904:                 foreach my $item ('threshold','weights','excluded','sysmail') {
13905:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
13906:                                           $shown{$item}).'</li>';
13907:                 }
13908:                 $resulttext .= '</ul></li>';
13909:             }
13910:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
13911:                 my (@optional,@required,@unused,$maxsizechg);
13912:                 foreach my $field (@{$changes{'helpform'}}) {
13913:                     if ($field eq 'maxsize') {
13914:                         $maxsizechg = 1;
13915:                         next;
13916:                     }
13917:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
13918:                         push(@optional,$field);
13919:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
13920:                         push(@unused,$field);
13921:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
13922:                         push(@required,$field);
13923:                     }
13924:                 }
13925:                 if (@optional) {
13926:                     $resulttext .= '<li>'.
13927:                                    &mt('Help form fields changed to "Optional": [_1].',
13928:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
13929:                                    '</li>';
13930:                 }
13931:                 if (@required) {
13932:                     $resulttext .= '<li>'.
13933:                                    &mt('Help form fields changed to "Required": [_1].',
13934:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
13935:                                    '</li>';
13936:                 }
13937:                 if (@unused) {
13938:                     $resulttext .= '<li>'.
13939:                                    &mt('Help form fields changed to "Not shown": [_1].',
13940:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
13941:                                    '</li>';
13942:                 }
13943:                 if ($maxsizechg) {
13944:                     $resulttext .= '<li>'.
13945:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
13946:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
13947:                                    '</li>';
13948:                 }
13949:             }
13950:             $resulttext .= '</ul>';
13951:         } else {
13952:             $resulttext = &mt('No changes made to contacts and form settings');
13953:         }
13954:     } else {
13955:         $resulttext = '<span class="LC_error">'.
13956:             &mt('An error occurred: [_1].',$putresult).'</span>';
13957:     }
13958:     return $resulttext;
13959: }
13960: 
13961: sub modify_passwords {
13962:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13963:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,$updatedefaults);
13964:     my $customfn = 'resetpw.html';
13965:     if (ref($domconfig{'passwords'}) eq 'HASH') {
13966:         %current = %{$domconfig{'passwords'}};
13967:     }
13968:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13969:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13970:     if (ref($types) eq 'ARRAY') {
13971:         @oktypes = @{$types};
13972:     }
13973:     push(@oktypes,'default');
13974: 
13975:     my %titles = &Apache::lonlocal::texthash (
13976:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
13977:         intauth_check  => 'Check bcrypt cost if authenticated',
13978:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
13979:         permanent      => 'Permanent e-mail address',
13980:         critical       => 'Critical notification address',
13981:         notify         => 'Notification address',
13982:         min            => 'Minimum password length',
13983:         max            => 'Maximum password length',
13984:         chars          => 'Required characters',
13985:         expire         => 'Password expiration (days)',
13986:         reset          => 'Resetting Forgotten Password',
13987:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
13988:         rules          => 'Rules for LON-CAPA Passwords',
13989:         crsownerchg    => 'Course Owner Changing Student Passwords',
13990:         username       => 'Username',
13991:         email          => 'E-mail address',
13992:     );
13993: 
13994: #
13995: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
13996: #
13997:     my (%curr_defaults,%save_defaults);
13998:     if (ref($domconfig{'defaults'}) eq 'HASH') {
13999:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
14000:             if ($key =~ /^intauth_(cost|check|switch)$/) {
14001:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
14002:             } else {
14003:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
14004:             }
14005:         }
14006:     }
14007:     my %staticdefaults = (
14008:         'resetlink'      => 2,
14009:         'resetcase'      => \@oktypes,
14010:         'resetprelink'   => 'both',
14011:         'resetemail'     => ['critical','notify','permanent'],
14012:         'intauth_cost'   => 10,
14013:         'intauth_check'  => 0,
14014:         'intauth_switch' => 0,
14015:         'min'            => 7,
14016:     );
14017:     foreach my $type (@oktypes) {
14018:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
14019:     }
14020:     my $linklife = $env{'form.passwords_link'};
14021:     $linklife =~ s/^\s+|\s+$//g;
14022:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
14023:         $newvalues{'resetlink'} = $linklife;
14024:         if ($current{'resetlink'}) {
14025:             if ($current{'resetlink'} ne $linklife) {
14026:                 $changes{'reset'} = 1;
14027:             }
14028:         } elsif (!exists($domconfig{passwords})) {
14029:             if ($staticdefaults{'resetlink'} ne $linklife) {
14030:                 $changes{'reset'} = 1;
14031:             }
14032:         }
14033:     } elsif ($current{'resetlink'}) {
14034:         $changes{'reset'} = 1;
14035:     }
14036:     my @casesens;
14037:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
14038:     foreach my $case (sort(@posscase)) {
14039:         if (grep(/^\Q$case\E$/,@oktypes)) {
14040:             push(@casesens,$case);
14041:         }
14042:     }
14043:     $newvalues{'resetcase'} = \@casesens;
14044:     if (ref($current{'resetcase'}) eq 'ARRAY') {
14045:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
14046:         if (@diffs > 0) {
14047:             $changes{'reset'} = 1;
14048:         }
14049:     } elsif (!exists($domconfig{passwords})) {
14050:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
14051:         if (@diffs > 0) {
14052:             $changes{'reset'} = 1;
14053:         }
14054:     }
14055:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
14056:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
14057:         if (exists($current{'resetprelink'})) {
14058:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
14059:                 $changes{'reset'} = 1;
14060:             }
14061:         } elsif (!exists($domconfig{passwords})) {
14062:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
14063:                 $changes{'reset'} = 1;
14064:             }
14065:         }
14066:     } elsif ($current{'resetprelink'}) {
14067:         $changes{'reset'} = 1;
14068:     }
14069:     foreach my $type (@oktypes) {
14070:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
14071:         my @postlink;
14072:         foreach my $item (sort(@possplink)) {
14073:             if ($item =~ /^(email|username)$/) {
14074:                 push(@postlink,$item);
14075:             }
14076:         }
14077:         $newvalues{'resetpostlink'}{$type} = \@postlink;
14078:         unless ($changes{'reset'}) {
14079:             if (ref($current{'resetpostlink'}) eq 'HASH') {
14080:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
14081:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
14082:                     if (@diffs > 0) {
14083:                         $changes{'reset'} = 1;
14084:                     }
14085:                 } else {
14086:                     $changes{'reset'} = 1;
14087:                 }
14088:             } elsif (!exists($domconfig{passwords})) {
14089:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
14090:                 if (@diffs > 0) {
14091:                     $changes{'reset'} = 1;
14092:                 }
14093:             }
14094:         }
14095:     }
14096:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
14097:     my @resetemail;
14098:     foreach my $item (sort(@possemailsrc)) {
14099:         if ($item =~ /^(permanent|critical|notify)$/) {
14100:             push(@resetemail,$item);
14101:         }
14102:     }
14103:     $newvalues{'resetemail'} = \@resetemail;
14104:     unless ($changes{'reset'}) {
14105:         if (ref($current{'resetemail'}) eq 'ARRAY') {
14106:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
14107:             if (@diffs > 0) {
14108:                 $changes{'reset'} = 1;
14109:             }
14110:         } elsif (!exists($domconfig{passwords})) {
14111:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
14112:             if (@diffs > 0) {
14113:                 $changes{'reset'} = 1;
14114:             }
14115:         }
14116:     }
14117:     if ($env{'form.passwords_stdtext'} == 0) {
14118:         $newvalues{'resetremove'} = 1;
14119:         unless ($current{'resetremove'}) {
14120:             $changes{'reset'} = 1;
14121:         }
14122:     } elsif ($current{'resetremove'}) {
14123:         $changes{'reset'} = 1;
14124:     }
14125:     if ($env{'form.passwords_customfile.filename'} ne '') {
14126:         my $servadm = $r->dir_config('lonAdmEMail');
14127:         my ($configuserok,$author_ok,$switchserver) =
14128:             &config_check($dom,$confname,$servadm);
14129:         my $error;
14130:         if ($configuserok eq 'ok') {
14131:             if ($switchserver) {
14132:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
14133:             } else {
14134:                 if ($author_ok eq 'ok') {
14135:                     my ($result,$customurl) =
14136:                         &publishlogo($r,'upload','passwords_customfile',$dom,
14137:                                      $confname,'customtext/resetpw','','',$customfn);
14138:                     if ($result eq 'ok') {
14139:                         $newvalues{'resetcustom'} = $customurl;
14140:                         $changes{'reset'} = 1;
14141:                     } else {
14142:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
14143:                     }
14144:                 } else {
14145:                     $error = &mt("Upload of [_1] failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$customfn,$confname,$dom,$author_ok);
14146:                 }
14147:             }
14148:         } else {
14149:             $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$customfn,$confname,$dom,$configuserok);
14150:         }
14151:         if ($error) {
14152:             &Apache::lonnet::logthis($error);
14153:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14154:         }
14155:     } elsif ($current{'resetcustom'}) {
14156:         if ($env{'form.passwords_custom_del'}) {
14157:             $changes{'reset'} = 1;
14158:         } else {
14159:             $newvalues{'resetcustom'} = $current{'resetcustom'};
14160:         }
14161:     }
14162:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
14163:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
14164:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
14165:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
14166:             $changes{'intauth'} = 1;
14167:         }
14168:     } else {
14169:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
14170:     }
14171:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
14172:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
14173:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
14174:             $changes{'intauth'} = 1;
14175:         }
14176:     } else {
14177:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
14178:     }
14179:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
14180:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
14181:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
14182:             $changes{'intauth'} = 1;
14183:         }
14184:     } else {
14185:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
14186:     }
14187:     foreach my $item ('cost','check','switch') {
14188:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
14189:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
14190:             $updatedefaults = 1;
14191:         }
14192:     }
14193:     foreach my $rule ('min','max','expire') {
14194:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
14195:         if ($env{'form.passwords_'.$rule} =~ /^(|\d+(|\.\d*))$/) {
14196:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
14197:             if (exists($current{$rule})) {
14198:                 if ($newvalues{$rule} ne $current{$rule}) {
14199:                     $changes{'rules'} = 1;
14200:                 }
14201:             } elsif ($rule eq 'min') {
14202:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
14203:                     $changes{'rules'} = 1;
14204:                 }
14205:             }
14206:         } elsif (exists($current{$rule})) {
14207:             $changes{'rules'} = 1;
14208:         }
14209:     }
14210:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
14211:     my @chars;
14212:     foreach my $item (sort(@posschars)) {
14213:         if ($item =~ /^(uc|lc|num|spec)$/) {
14214:             push(@chars,$item);
14215:         }
14216:     }
14217:     $newvalues{'chars'} = \@chars;
14218:     unless ($changes{'rules'}) {
14219:         if (ref($current{'chars'}) eq 'ARRAY') {
14220:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
14221:             if (@diffs > 0) {
14222:                 $changes{'rules'} = 1;
14223:             }
14224:         } else {
14225:             if (@chars > 0) {
14226:                 $changes{'rules'} = 1;
14227:             }
14228:         }
14229:     }
14230:     if ($env{'form.passwords_crsowner'}) {
14231:         $newvalues{'crsownerchg'} = 1;
14232:         unless ($current{'crsownerchg'}) {
14233:             $changes{'crsownerchg'} = 1;
14234:         }
14235:     } elsif ($current{'crsownerchg'}) {
14236:         $changes{'crsownerchg'} = 1;
14237:     }
14238: 
14239:     my %confighash = (
14240:                         defaults  => \%save_defaults,
14241:                         passwords => \%newvalues,
14242:                      );
14243:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
14244: 
14245:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
14246:     if ($putresult eq 'ok') {
14247:         if (keys(%changes) > 0) {
14248:             $resulttext = &mt('Changes made: ').'<ul>';
14249:             foreach my $key ('reset','intauth','rules','crsownerchg') {
14250:                 if ($changes{$key}) {
14251:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
14252:                     if ($key eq 'reset') {
14253:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
14254:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
14255:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
14256:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
14257:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />'.
14258:                                            &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchapub'}).'</br>'.
14259:                                            &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchapriv'}).'</li>';
14260:                         } else {
14261:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
14262:                         }
14263:                         if ($confighash{'passwords'}{'resetlink'}) {
14264:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
14265:                         } else {
14266:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
14267:                                                   &mt('Will default to 2 hours').'</li>';
14268:                         }
14269:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
14270:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
14271:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
14272:                             } else {
14273:                                 my $casesens;
14274:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
14275:                                     if ($type eq 'default') {
14276:                                         $casesens .= $othertitle.', ';
14277:                                     } elsif ($usertypes->{$type} ne '') {
14278:                                         $casesens .= $usertypes->{$type}.', ';
14279:                                     }
14280:                                 }
14281:                                 $casesens =~ s/\Q, \E$//;
14282:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
14283:                             }
14284:                         } else {
14285:                             $resulttext .= '<li>'.&mt('Case-sensitivity not set for "Forgot Password" web form').' '.&mt('Will default to case-sensitive for username and/or e-mail address for all').'</li>';
14286:                         }
14287:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
14288:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
14289:                         } else {
14290:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
14291:                         }
14292:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
14293:                             my $output;
14294:                             if (ref($types) eq 'ARRAY') {
14295:                                 foreach my $type (@{$types}) {
14296:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
14297:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
14298:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
14299:                                         } else {
14300:                                             $output .= $usertypes->{$type}.' -- '.
14301:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
14302:                                         }
14303:                                     }
14304:                                 }
14305:                             }
14306:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
14307:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
14308:                                     $output .= $othertitle.' -- '.&mt('none');
14309:                                 } else {
14310:                                     $output .= $othertitle.' -- '.
14311:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
14312:                                 }
14313:                             }
14314:                             if ($output) {
14315:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
14316:                             } else {
14317:                                 $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
14318:                             }
14319:                         } else {
14320:                             $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
14321:                         }
14322:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
14323:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
14324:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
14325:                             } else {
14326:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
14327:                             }
14328:                         } else {
14329:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
14330:                         }
14331:                         if ($confighash{'passwords'}{'resetremove'}) {
14332:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
14333:                         } else {
14334:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
14335:                         }
14336:                         if ($confighash{'passwords'}{'resetcustom'}) {
14337:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
14338:                                                                             $titles{custom},600,500);
14339:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes [_1]',$customlink).'</li>';
14340:                         } else {
14341:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
14342:                         }
14343:                     } elsif ($key eq 'intauth') {
14344:                         foreach my $item ('cost','switch','check') {
14345:                             my $value = $save_defaults{$key.'_'.$item};
14346:                             if ($item eq 'switch') {
14347:                                 my %optiondesc = &Apache::lonlocal::texthash (
14348:                                                      0 => 'No',
14349:                                                      1 => 'Yes',
14350:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
14351:                                                  );
14352:                                 if ($value =~ /^(0|1|2)$/) {
14353:                                     $value = $optiondesc{$value};
14354:                                 } else {
14355:                                     $value = &mt('none -- defaults to No');
14356:                                 }
14357:                             } elsif ($item eq 'check') {
14358:                                 my %optiondesc = &Apache::lonlocal::texthash (
14359:                                                      0 => 'No',
14360:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
14361:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
14362:                                                  );
14363:                                 if ($value =~ /^(0|1|2)$/) {
14364:                                     $value = $optiondesc{$value};
14365:                                 } else {
14366:                                     $value = &mt('none -- defaults to No');
14367:                                 }
14368:                             }
14369:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
14370:                         }
14371:                     } elsif ($key eq 'rules') {
14372:                         foreach my $rule ('min','max','expire') {
14373:                             if ($confighash{'passwords'}{$rule} eq '') {
14374:                                 $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
14375:                                 if ($rule eq 'min') {
14376:                                    $resulttext .= ' '.&mt('Default of 7 will be used');
14377:                                 }
14378:                                 $resulttext .= '</li>';
14379:                             } else {
14380:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
14381:                             }
14382:                         }
14383:                     } elsif ($key eq 'crsownerchg') {
14384:                         if ($confighash{'passwords'}{'crsownerchg'}) {
14385:                             $resulttext .= '<li>'.&mt('Course owner may change student passwords.').'</li>';
14386:                         } else {
14387:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.');
14388:                         }
14389:                     }
14390:                     $resulttext .= '</ul></li>';
14391:                 }
14392:             }
14393:             $resulttext .= '</ul>';
14394:         } else {
14395:             $resulttext = &mt('No changes made to password settings');
14396:         }
14397:         if ($updatedefaults) {
14398:             my $cachetime = 24*60*60;
14399:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14400:             if (ref($lastactref) eq 'HASH') {
14401:                 $lastactref->{'domdefaults'} = 1;
14402:             }
14403:         }
14404:     } else {
14405:         $resulttext = '<span class="LC_error">'.
14406:             &mt('An error occurred: [_1]',$putresult).'</span>';
14407:     }
14408:     if ($errors) {
14409:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
14410:                        $errors.'</ul></p>';
14411:     }
14412:     return $resulttext;
14413: }
14414: 
14415: sub modify_usercreation {
14416:     my ($dom,%domconfig) = @_;
14417:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
14418:     my $warningmsg;
14419:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
14420:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
14421:             if ($key eq 'cancreate') {
14422:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
14423:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
14424:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
14425:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
14426:                         } else {
14427:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
14428:                         }
14429:                     }
14430:                 }
14431:             } elsif ($key eq 'email_rule') {
14432:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
14433:             } else {
14434:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
14435:             }
14436:         }
14437:     }
14438:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
14439:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
14440:     my @contexts = ('author','course','requestcrs');
14441:     foreach my $item(@contexts) {
14442:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
14443:     }
14444:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
14445:         foreach my $item (@contexts) {
14446:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
14447:                 push(@{$changes{'cancreate'}},$item);
14448:             }
14449:         }
14450:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
14451:         foreach my $item (@contexts) {
14452:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
14453:                 if ($cancreate{$item} ne 'any') {
14454:                     push(@{$changes{'cancreate'}},$item);
14455:                 }
14456:             } else {
14457:                 if ($cancreate{$item} ne 'none') {
14458:                     push(@{$changes{'cancreate'}},$item);
14459:                 }
14460:             }
14461:         }
14462:     } else {
14463:         foreach my $item (@contexts)  {
14464:             push(@{$changes{'cancreate'}},$item);
14465:         }
14466:     }
14467: 
14468:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
14469:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
14470:             if (!grep(/^\Q$type\E$/,@username_rule)) {
14471:                 push(@{$changes{'username_rule'}},$type);
14472:             }
14473:         }
14474:         foreach my $type (@username_rule) {
14475:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
14476:                 push(@{$changes{'username_rule'}},$type);
14477:             }
14478:         }
14479:     } else {
14480:         push(@{$changes{'username_rule'}},@username_rule);
14481:     }
14482: 
14483:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
14484:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
14485:             if (!grep(/^\Q$type\E$/,@id_rule)) {
14486:                 push(@{$changes{'id_rule'}},$type);
14487:             }
14488:         }
14489:         foreach my $type (@id_rule) {
14490:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
14491:                 push(@{$changes{'id_rule'}},$type);
14492:             }
14493:         }
14494:     } else {
14495:         push(@{$changes{'id_rule'}},@id_rule);
14496:     }
14497: 
14498:     my @authen_contexts = ('author','course','domain');
14499:     my @authtypes = ('int','krb4','krb5','loc','lti');
14500:     my %authhash;
14501:     foreach my $item (@authen_contexts) {
14502:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
14503:         foreach my $auth (@authtypes) {
14504:             if (grep(/^\Q$auth\E$/,@authallowed)) {
14505:                 $authhash{$item}{$auth} = 1;
14506:             } else {
14507:                 $authhash{$item}{$auth} = 0;
14508:             }
14509:         }
14510:     }
14511:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
14512:         foreach my $item (@authen_contexts) {
14513:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
14514:                 foreach my $auth (@authtypes) {
14515:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
14516:                         push(@{$changes{'authtypes'}},$item);
14517:                         last;
14518:                     }
14519:                 }
14520:             }
14521:         }
14522:     } else {
14523:         foreach my $item (@authen_contexts) {
14524:             push(@{$changes{'authtypes'}},$item);
14525:         }
14526:     }
14527: 
14528:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
14529:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
14530:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
14531:     $save_usercreate{'id_rule'} = \@id_rule;
14532:     $save_usercreate{'username_rule'} = \@username_rule,
14533:     $save_usercreate{'authtypes'} = \%authhash;
14534: 
14535:     my %usercreation_hash =  (
14536:         usercreation     => \%save_usercreate,
14537:     );
14538: 
14539:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
14540:                                              $dom);
14541: 
14542:     if ($putresult eq 'ok') {
14543:         if (keys(%changes) > 0) {
14544:             $resulttext = &mt('Changes made:').'<ul>';
14545:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
14546:                 my %lt = &usercreation_types();
14547:                 foreach my $type (@{$changes{'cancreate'}}) {
14548:                     my $chgtext = $lt{$type}.', ';
14549:                     if ($cancreate{$type} eq 'none') {
14550:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
14551:                     } elsif ($cancreate{$type} eq 'any') {
14552:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
14553:                     } elsif ($cancreate{$type} eq 'official') {
14554:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
14555:                     } elsif ($cancreate{$type} eq 'unofficial') {
14556:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
14557:                     }
14558:                     $resulttext .= '<li>'.$chgtext.'</li>';
14559:                 }
14560:             }
14561:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
14562:                 my ($rules,$ruleorder) = 
14563:                     &Apache::lonnet::inst_userrules($dom,'username');
14564:                 my $chgtext = '<ul>';
14565:                 foreach my $type (@username_rule) {
14566:                     if (ref($rules->{$type}) eq 'HASH') {
14567:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
14568:                     }
14569:                 }
14570:                 $chgtext .= '</ul>';
14571:                 if (@username_rule > 0) {
14572:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
14573:                 } else {
14574:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
14575:                 }
14576:             }
14577:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
14578:                 my ($idrules,$idruleorder) = 
14579:                     &Apache::lonnet::inst_userrules($dom,'id');
14580:                 my $chgtext = '<ul>';
14581:                 foreach my $type (@id_rule) {
14582:                     if (ref($idrules->{$type}) eq 'HASH') {
14583:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
14584:                     }
14585:                 }
14586:                 $chgtext .= '</ul>';
14587:                 if (@id_rule > 0) {
14588:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
14589:                 } else {
14590:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
14591:                 }
14592:             }
14593:             my %authname = &authtype_names();
14594:             my %context_title = &context_names();
14595:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
14596:                 my $chgtext = '<ul>';
14597:                 foreach my $type (@{$changes{'authtypes'}}) {
14598:                     my @allowed;
14599:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
14600:                     foreach my $auth (@authtypes) {
14601:                         if ($authhash{$type}{$auth}) {
14602:                             push(@allowed,$authname{$auth});
14603:                         }
14604:                     }
14605:                     if (@allowed > 0) {
14606:                         $chgtext .= join(', ',@allowed).'</li>';
14607:                     } else {
14608:                         $chgtext .= &mt('none').'</li>';
14609:                     }
14610:                 }
14611:                 $chgtext .= '</ul>';
14612:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
14613:                 $resulttext .= '</li>';
14614:             }
14615:             $resulttext .= '</ul>';
14616:         } else {
14617:             $resulttext = &mt('No changes made to user creation settings');
14618:         }
14619:     } else {
14620:         $resulttext = '<span class="LC_error">'.
14621:             &mt('An error occurred: [_1]',$putresult).'</span>';
14622:     }
14623:     if ($warningmsg ne '') {
14624:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
14625:     }
14626:     return $resulttext;
14627: }
14628: 
14629: sub modify_selfcreation {
14630:     my ($dom,$lastactref,%domconfig) = @_;
14631:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
14632:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
14633:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14634:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
14635:     if (ref($typesref) eq 'ARRAY') {
14636:         @types = @{$typesref};
14637:     }
14638:     if (ref($usertypesref) eq 'HASH') {
14639:         %usertypes = %{$usertypesref};
14640:     }
14641:     $usertypes{'default'} = $othertitle;
14642: #
14643: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
14644: #
14645:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
14646:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
14647:             if ($key eq 'cancreate') {
14648:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
14649:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
14650:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
14651:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
14652:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
14653:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
14654:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
14655:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
14656:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
14657:                         } else {
14658:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
14659:                         }
14660:                     }
14661:                 }
14662:             } elsif ($key eq 'email_rule') {
14663:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
14664:             } else {
14665:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
14666:             }
14667:         }
14668:     }
14669: #
14670: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
14671: #
14672:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
14673:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
14674:             if ($key eq 'selfcreate') {
14675:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
14676:             } else {
14677:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
14678:             }
14679:         }
14680:     }
14681: #
14682: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
14683: #
14684:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
14685:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
14686:             if ($key eq 'inststatusguest') {
14687:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
14688:             } else {
14689:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
14690:             }
14691:         }
14692:     }
14693: 
14694:     my @contexts = ('selfcreate');
14695:     @{$cancreate{'selfcreate'}} = ();
14696:     %{$cancreate{'emailusername'}} = ();
14697:     if (@types) {
14698:         @{$cancreate{'statustocreate'}} = ();
14699:     }
14700:     %{$cancreate{'selfcreateprocessing'}} = ();
14701:     %{$cancreate{'shibenv'}} = ();
14702:     %{$cancreate{'emailverified'}} = ();
14703:     %{$cancreate{'emailoptions'}} = ();
14704:     %{$cancreate{'emaildomain'}} = ();
14705:     my %selfcreatetypes = (
14706:                              sso   => 'users authenticated by institutional single sign on',
14707:                              login => 'users authenticated by institutional log-in',
14708:                              email => 'users verified by e-mail',
14709:                           );
14710: #
14711: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
14712: # is permitted.
14713: #
14714:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
14715: 
14716:     my (@statuses,%email_rule);
14717:     foreach my $item ('login','sso','email') {
14718:         if ($item eq 'email') {
14719:             if ($env{'form.cancreate_email'}) {
14720:                 if (@types) {
14721:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
14722:                     foreach my $status (@poss_statuses) {
14723:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
14724:                             push(@statuses,$status);
14725:                         }
14726:                     }
14727:                     $save_inststatus{'inststatusguest'} = \@statuses;
14728:                 } else {
14729:                     push(@statuses,'default');
14730:                 }
14731:                 if (@statuses) {
14732:                     my %curr_rule;
14733:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
14734:                         foreach my $type (@statuses) {
14735:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
14736:                         }
14737:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
14738:                         foreach my $type (@statuses) {
14739:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
14740:                         }
14741:                     }
14742:                     push(@{$cancreate{'selfcreate'}},'email');
14743:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
14744:                     my %curremaildom;
14745:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
14746:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
14747:                     }
14748:                     foreach my $type (@statuses) {
14749:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
14750:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
14751:                         }
14752:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
14753:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
14754:                         }
14755:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
14756: #
14757: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
14758: #
14759:                             my $chosen = $1;
14760:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
14761:                                 my $emaildom;
14762:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
14763:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
14764:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
14765:                                     if (ref($curremaildom{$type}) eq 'HASH') {
14766:                                         if (exists($curremaildom{$type}{$chosen})) {
14767:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
14768:                                                 push(@{$changes{'cancreate'}},'emaildomain');
14769:                                             }
14770:                                         } elsif ($emaildom ne '') {
14771:                                             push(@{$changes{'cancreate'}},'emaildomain');
14772:                                         }
14773:                                     } elsif ($emaildom ne '') {
14774:                                         push(@{$changes{'cancreate'}},'emaildomain');
14775:                                     } 
14776:                                 }
14777:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
14778:                             } elsif ($chosen eq 'custom') {
14779:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
14780:                                 $email_rule{$type} = [];
14781:                                 if (ref($emailrules) eq 'HASH') {
14782:                                     foreach my $rule (@possemail_rules) {
14783:                                         if (exists($emailrules->{$rule})) {
14784:                                             push(@{$email_rule{$type}},$rule);
14785:                                         }
14786:                                     }
14787:                                 }
14788:                                 if (@{$email_rule{$type}}) {
14789:                                     $cancreate{'emailoptions'}{$type} = 'custom';
14790:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
14791:                                         if (@{$curr_rule{$type}} > 0) {
14792:                                             foreach my $rule (@{$curr_rule{$type}}) {
14793:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
14794:                                                     push(@{$changes{'email_rule'}},$type);
14795:                                                 }
14796:                                             }
14797:                                         }
14798:                                         foreach my $type (@{$email_rule{$type}}) {
14799:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
14800:                                                 push(@{$changes{'email_rule'}},$type);
14801:                                             }
14802:                                         }
14803:                                     } else {
14804:                                         push(@{$changes{'email_rule'}},$type);
14805:                                     }
14806:                                 }
14807:                             } else {
14808:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
14809:                             }
14810:                         }
14811:                     }
14812:                     if (@types) {
14813:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
14814:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
14815:                             if (@changed) {
14816:                                 push(@{$changes{'inststatus'}},'inststatusguest');
14817:                             }
14818:                         } else {
14819:                             push(@{$changes{'inststatus'}},'inststatusguest');
14820:                         }
14821:                     }
14822:                 } else {
14823:                     delete($env{'form.cancreate_email'});
14824:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
14825:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
14826:                             push(@{$changes{'inststatus'}},'inststatusguest');
14827:                         }
14828:                     }
14829:                 }
14830:             } else {
14831:                 $save_inststatus{'inststatusguest'} = [];
14832:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
14833:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
14834:                         push(@{$changes{'inststatus'}},'inststatusguest');
14835:                     }
14836:                 }
14837:             }
14838:         } else {
14839:             if ($env{'form.cancreate_'.$item}) {
14840:                 push(@{$cancreate{'selfcreate'}},$item);
14841:             }
14842:         }
14843:     }
14844:     my (%userinfo,%savecaptcha);
14845:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
14846: #
14847: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
14848: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
14849: #
14850: 
14851:     if ($env{'form.cancreate_email'}) {
14852:         push(@contexts,'emailusername');
14853:         if (@statuses) {
14854:             foreach my $type (@statuses) {
14855:                 if (ref($infofields) eq 'ARRAY') {
14856:                     foreach my $field (@{$infofields}) {
14857:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
14858:                             $cancreate{'emailusername'}{$type}{$field} = $1;
14859:                         }
14860:                     }
14861:                 }
14862:             }
14863:         }
14864: #
14865: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
14866: # queued requests for self-creation of account verified by e-mail.
14867: #
14868: 
14869:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
14870:         @approvalnotify = sort(@approvalnotify);
14871:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
14872:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
14873:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
14874:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
14875:                     push(@{$changes{'cancreate'}},'notify');
14876:                 }
14877:             } else {
14878:                 if ($cancreate{'notify'}{'approval'}) {
14879:                     push(@{$changes{'cancreate'}},'notify');
14880:                 }
14881:             }
14882:         } elsif ($cancreate{'notify'}{'approval'}) {
14883:             push(@{$changes{'cancreate'}},'notify');
14884:         }
14885: 
14886:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
14887:     }
14888: #  
14889: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
14890: # institutional log-in.
14891: #
14892:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
14893:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
14894:                ($domdefaults{'auth_def'} eq 'localauth'))) {
14895:             $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.').' '.
14896:                           &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.');
14897:         }
14898:     }
14899:     my @fields = ('lastname','firstname','middlename','generation',
14900:                   'permanentemail','id');
14901:     my @shibfields = (@fields,'inststatus');
14902:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14903: #
14904: # Where usernames may created for institutional log-in and/or institutional single sign on:
14905: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
14906: # may self-create accounts 
14907: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
14908: # which the user may supply, if institutional data is unavailable.
14909: #
14910:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
14911:         if (@types) {
14912:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
14913:             push(@contexts,'statustocreate');
14914:             foreach my $type (@types) {
14915:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
14916:                 foreach my $field (@fields) {
14917:                     if (grep(/^\Q$field\E$/,@modifiable)) {
14918:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
14919:                     } else {
14920:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
14921:                     }
14922:                 }
14923:             }
14924:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
14925:                 foreach my $type (@types) {
14926:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
14927:                         foreach my $field (@fields) {
14928:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
14929:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
14930:                                 push(@{$changes{'selfcreate'}},$type);
14931:                                 last;
14932:                             }
14933:                         }
14934:                     }
14935:                 }
14936:             } else {
14937:                 foreach my $type (@types) {
14938:                     push(@{$changes{'selfcreate'}},$type);
14939:                 }
14940:             }
14941:         }
14942:         foreach my $field (@shibfields) {
14943:             if ($env{'form.shibenv_'.$field} ne '') {
14944:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
14945:             }
14946:         }
14947:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
14948:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
14949:                 foreach my $field (@shibfields) {
14950:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
14951:                         push(@{$changes{'cancreate'}},'shibenv');
14952:                     }
14953:                 }
14954:             } else {
14955:                 foreach my $field (@shibfields) {
14956:                     if ($env{'form.shibenv_'.$field}) {
14957:                         push(@{$changes{'cancreate'}},'shibenv');
14958:                         last;
14959:                     }
14960:                 }
14961:             }
14962:         }
14963:     }
14964:     foreach my $item (@contexts) {
14965:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
14966:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
14967:                 if (ref($cancreate{$item}) eq 'ARRAY') {
14968:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
14969:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14970:                             push(@{$changes{'cancreate'}},$item);
14971:                         }
14972:                     }
14973:                 }
14974:             }
14975:             if (ref($cancreate{$item}) eq 'ARRAY') {
14976:                 foreach my $type (@{$cancreate{$item}}) {
14977:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
14978:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14979:                             push(@{$changes{'cancreate'}},$item);
14980:                         }
14981:                     }
14982:                 }
14983:             }
14984:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
14985:             if (ref($cancreate{$item}) eq 'HASH') {
14986:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
14987:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
14988:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
14989:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
14990:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14991:                                     push(@{$changes{'cancreate'}},$item);
14992:                                 }
14993:                             }
14994:                         }
14995:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
14996:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
14997:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
14998:                                 push(@{$changes{'cancreate'}},$item);
14999:                             }
15000:                         }
15001:                     }
15002:                 }
15003:                 foreach my $type (keys(%{$cancreate{$item}})) {
15004:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
15005:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
15006:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
15007:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
15008:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15009:                                         push(@{$changes{'cancreate'}},$item);
15010:                                     }
15011:                                 }
15012:                             } else {
15013:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15014:                                     push(@{$changes{'cancreate'}},$item);
15015:                                 }
15016:                             }
15017:                         }
15018:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
15019:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
15020:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15021:                                 push(@{$changes{'cancreate'}},$item);
15022:                             }
15023:                         }
15024:                     }
15025:                 }
15026:             }
15027:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
15028:             if (ref($cancreate{$item}) eq 'ARRAY') {
15029:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
15030:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15031:                         push(@{$changes{'cancreate'}},$item);
15032:                     }
15033:                 }
15034:             }
15035:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
15036:             if (ref($cancreate{$item}) eq 'HASH') {
15037:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15038:                     push(@{$changes{'cancreate'}},$item);
15039:                 }
15040:             }
15041:         } elsif ($item eq 'emailusername') {
15042:             if (ref($cancreate{$item}) eq 'HASH') {
15043:                 foreach my $type (keys(%{$cancreate{$item}})) {
15044:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
15045:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
15046:                             if ($cancreate{$item}{$type}{$field}) {
15047:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15048:                                     push(@{$changes{'cancreate'}},$item);
15049:                                 }
15050:                                 last;
15051:                             }
15052:                         }
15053:                     }
15054:                 }
15055:             }
15056:         }
15057:     }
15058: #
15059: # Populate %save_usercreate hash with updates to self-creation configuration.
15060: #
15061:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
15062:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
15063:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
15064:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
15065:     if (ref($cancreate{'notify'}) eq 'HASH') {
15066:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
15067:     }
15068:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
15069:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
15070:     }
15071:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
15072:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
15073:     }
15074:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
15075:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
15076:     }
15077:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
15078:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
15079:     }
15080:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
15081:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
15082:     }
15083:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
15084:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
15085:     }
15086:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
15087:     $save_usercreate{'email_rule'} = \%email_rule;
15088: 
15089:     my %userconfig_hash = (
15090:             usercreation     => \%save_usercreate,
15091:             usermodification => \%save_usermodify,
15092:             inststatus       => \%save_inststatus,
15093:     );
15094: 
15095:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
15096:                                              $dom);
15097: #
15098: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
15099: #
15100:     if ($putresult eq 'ok') {
15101:         if (keys(%changes) > 0) {
15102:             $resulttext = &mt('Changes made:').'<ul>';
15103:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
15104:                 my %lt = &selfcreation_types();
15105:                 foreach my $type (@{$changes{'cancreate'}}) {
15106:                     my $chgtext = '';
15107:                     if ($type eq 'selfcreate') {
15108:                         if (@{$cancreate{$type}} == 0) {
15109:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
15110:                         } else {
15111:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
15112:                                         '<ul>';
15113:                             foreach my $case (@{$cancreate{$type}}) {
15114:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
15115:                             }
15116:                             $chgtext .= '</ul>';
15117:                             if (ref($cancreate{$type}) eq 'ARRAY') {
15118:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
15119:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
15120:                                         if (@{$cancreate{'statustocreate'}} == 0) {
15121:                                             $chgtext .= '<span class="LC_warning">'.
15122:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
15123:                                                         '</span><br />';
15124:                                         }
15125:                                     }
15126:                                 }
15127:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
15128:                                     if (!@statuses) {
15129:                                         $chgtext .= '<span class="LC_warning">'.
15130:                                                     &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.").
15131:                                                     '</span><br />';
15132: 
15133:                                     }
15134:                                 }
15135:                             }
15136:                         }
15137:                     } elsif ($type eq 'shibenv') {
15138:                         if (keys(%{$cancreate{$type}}) == 0) {
15139:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
15140:                         } else {
15141:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
15142:                                         '<ul>';
15143:                             foreach my $field (@shibfields) {
15144:                                 next if ($cancreate{$type}{$field} eq '');
15145:                                 if ($field eq 'inststatus') {
15146:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
15147:                                 } else {
15148:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
15149:                                 }
15150:                             }
15151:                             $chgtext .= '</ul>';
15152:                         }
15153:                     } elsif ($type eq 'statustocreate') {
15154:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
15155:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
15156:                             if (@{$cancreate{'selfcreate'}} > 0) {
15157:                                 if (@{$cancreate{'statustocreate'}} == 0) {
15158:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
15159:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
15160:                                         $chgtext .= '<br />'.
15161:                                                     '<span class="LC_warning">'.
15162:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
15163:                                                     '</span>';
15164:                                     }
15165:                                 } elsif (keys(%usertypes) > 0) {
15166:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
15167:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
15168:                                     } else {
15169:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
15170:                                     }
15171:                                     $chgtext .= '<ul>';
15172:                                     foreach my $case (@{$cancreate{$type}}) {
15173:                                         if ($case eq 'default') {
15174:                                             $chgtext .= '<li>'.$othertitle.'</li>';
15175:                                         } else {
15176:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
15177:                                         }
15178:                                     }
15179:                                     $chgtext .= '</ul>';
15180:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
15181:                                         $chgtext .= '<span class="LC_warning">'.
15182:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
15183:                                                     '</span>';
15184:                                     }
15185:                                 }
15186:                             } else {
15187:                                 if (@{$cancreate{$type}} == 0) {
15188:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
15189:                                 } else {
15190:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
15191:                                 }
15192:                             }
15193:                             $chgtext .= '<br />';
15194:                         }
15195:                     } elsif ($type eq 'selfcreateprocessing') {
15196:                         my %choices = &Apache::lonlocal::texthash (
15197:                                                                     automatic => 'Automatic approval',
15198:                                                                     approval  => 'Queued for approval',
15199:                                                                   );
15200:                         if (@types) {
15201:                             if (@statuses) {
15202:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
15203:                                             '<ul>';
15204:                                 foreach my $status (@statuses) {
15205:                                     if ($status eq 'default') {
15206:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
15207:                                     } else {
15208:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
15209:                                     }
15210:                                 }
15211:                                 $chgtext .= '</ul>';
15212:                             }
15213:                         } else {
15214:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
15215:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
15216:                         }
15217:                     } elsif ($type eq 'emailverified') {
15218:                         my %options = &Apache::lonlocal::texthash (
15219:                                                                     all   => 'Same as e-mail',
15220:                                                                     first => 'Omit @domain',
15221:                                                                     free  => 'Free to choose',
15222:                                                                   );
15223:                         if (@types) {
15224:                             if (@statuses) {
15225:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
15226:                                             '<ul>';
15227:                                 foreach my $status (@statuses) {
15228:                                     if ($type eq 'default') {
15229:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
15230:                                     } else {
15231:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
15232:                                     }
15233:                                 }
15234:                                 $chgtext .= '</ul>';
15235:                             }
15236:                         } else {
15237:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
15238:                                             $options{$cancreate{'emailverified'}{'default'}});
15239:                         }
15240:                     } elsif ($type eq 'emailoptions') {
15241:                         my %options = &Apache::lonlocal::texthash (
15242:                                                                     any     => 'Any e-mail',
15243:                                                                     inst    => 'Institutional only',
15244:                                                                     noninst => 'Non-institutional only',
15245:                                                                     custom  => 'Custom restrictions',
15246:                                                                   );
15247:                         if (@types) {
15248:                             if (@statuses) {
15249:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
15250:                                             '<ul>';
15251:                                 foreach my $status (@statuses) {
15252:                                     if ($type eq 'default') {
15253:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
15254:                                     } else {
15255:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
15256:                                     }
15257:                                 }
15258:                                 $chgtext .= '</ul>';
15259:                             }
15260:                         } else {
15261:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
15262:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
15263:                             } else {
15264:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
15265:                                                 $options{$cancreate{'emailoptions'}{'default'}});
15266:                             }
15267:                         }
15268:                     } elsif ($type eq 'emaildomain') {
15269:                         my $output;
15270:                         if (@statuses) {
15271:                             foreach my $type (@statuses) {
15272:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
15273:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
15274:                                         if ($type eq 'default') {
15275:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15276:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
15277:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
15278:                                             } else {
15279:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
15280:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
15281:                                             }
15282:                                         } else {
15283:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15284:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
15285:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
15286:                                             } else {
15287:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
15288:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
15289:                                             }
15290:                                         }
15291:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
15292:                                         if ($type eq 'default') {
15293:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15294:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
15295:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
15296:                                             } else {
15297:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
15298:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
15299:                                             }
15300:                                         } else {
15301:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15302:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
15303:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
15304:                                             } else {
15305:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
15306:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
15307:                                             }
15308:                                         }
15309:                                     }
15310:                                 }
15311:                             }
15312:                         }
15313:                         if ($output ne '') {
15314:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
15315:                                         '<ul>'.$output.'</ul>';
15316:                         }
15317:                     } elsif ($type eq 'captcha') {
15318:                         if ($savecaptcha{$type} eq 'notused') {
15319:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
15320:                         } else {
15321:                             my %captchas = &captcha_phrases();
15322:                             if ($captchas{$savecaptcha{$type}}) {
15323:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
15324:                             } else {
15325:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
15326:                             }
15327:                         }
15328:                     } elsif ($type eq 'recaptchakeys') {
15329:                         my ($privkey,$pubkey);
15330:                         if (ref($savecaptcha{$type}) eq 'HASH') {
15331:                             $pubkey = $savecaptcha{$type}{'public'};
15332:                             $privkey = $savecaptcha{$type}{'private'};
15333:                         }
15334:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
15335:                         if (!$pubkey) {
15336:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
15337:                         } else {
15338:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
15339:                         }
15340:                         if (!$privkey) {
15341:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
15342:                         } else {
15343:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
15344:                         }
15345:                         $chgtext .= '</ul>';
15346:                     } elsif ($type eq 'recaptchaversion') {
15347:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
15348:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
15349:                         }
15350:                     } elsif ($type eq 'emailusername') {
15351:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
15352:                             if (@statuses) {
15353:                                 foreach my $type (@statuses) {
15354:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
15355:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
15356:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
15357:                                                     '<ul>';
15358:                                             foreach my $field (@{$infofields}) {
15359:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
15360:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
15361:                                                 }
15362:                                             }
15363:                                             $chgtext .= '</ul>';
15364:                                         } else {
15365:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
15366:                                         }
15367:                                     } else {
15368:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
15369:                                     }
15370:                                 }
15371:                             }
15372:                         }
15373:                     } elsif ($type eq 'notify') {
15374:                         my $numapprove = 0;
15375:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
15376:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
15377:                                 if ($cancreate{'notify'}{'approval'}) {
15378:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
15379:                                     $numapprove ++;
15380:                                 }
15381:                             }
15382:                         }
15383:                         unless ($numapprove) {
15384:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
15385:                         }
15386:                     }
15387:                     if ($chgtext) {
15388:                         $resulttext .= '<li>'.$chgtext.'</li>';
15389:                     }
15390:                 }
15391:             }
15392:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
15393:                 my ($emailrules,$emailruleorder) =
15394:                     &Apache::lonnet::inst_userrules($dom,'email');
15395:                 foreach my $type (@{$changes{'email_rule'}}) {
15396:                     if (ref($email_rule{$type}) eq 'ARRAY') {
15397:                         my $chgtext = '<ul>';
15398:                         foreach my $rule (@{$email_rule{$type}}) {
15399:                             if (ref($emailrules->{$rule}) eq 'HASH') {
15400:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
15401:                             }
15402:                         }
15403:                         $chgtext .= '</ul>';
15404:                         my $typename;
15405:                         if (@types) {
15406:                             if ($type eq 'default') {
15407:                                 $typename = $othertitle;
15408:                             } else {
15409:                                 $typename = $usertypes{$type};
15410:                             } 
15411:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
15412:                         }
15413:                         if (@{$email_rule{$type}} > 0) {
15414:                             $resulttext .= '<li>'.
15415:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
15416:                                                $usertypes{$type}).
15417:                                            $chgtext.
15418:                                            '</li>';
15419:                         } else {
15420:                             $resulttext .= '<li>'.
15421:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
15422:                                            '</li>'.
15423:                                            &mt('(Affiliation: [_1])',$typename);
15424:                         }
15425:                     }
15426:                 }
15427:             }
15428:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
15429:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
15430:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
15431:                         my $chgtext = '<ul>';
15432:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
15433:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
15434:                         }
15435:                         $chgtext .= '</ul>';
15436:                         $resulttext .= '<li>'.
15437:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
15438:                                           $chgtext.
15439:                                        '</li>';
15440:                     } else {
15441:                         $resulttext .= '<li>'.
15442:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
15443:                                        '</li>';
15444:                     }
15445:                 }
15446:             }
15447:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
15448:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
15449:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15450:                 foreach my $type (@{$changes{'selfcreate'}}) {
15451:                     my $typename = $type;
15452:                     if (keys(%usertypes) > 0) {
15453:                         if ($usertypes{$type} ne '') {
15454:                             $typename = $usertypes{$type};
15455:                         }
15456:                     }
15457:                     my @modifiable;
15458:                     $resulttext .= '<li>'.
15459:                                     &mt('Self-creation of account by users with status: [_1]',
15460:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
15461:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
15462:                     foreach my $field (@fields) {
15463:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
15464:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
15465:                         }
15466:                     }
15467:                     if (@modifiable > 0) {
15468:                         $resulttext .= join(', ',@modifiable);
15469:                     } else {
15470:                         $resulttext .= &mt('none');
15471:                     }
15472:                     $resulttext .= '</li>';
15473:                 }
15474:                 $resulttext .= '</ul></li>';
15475:             }
15476:             $resulttext .= '</ul>';
15477:             my $cachetime = 24*60*60;
15478:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
15479:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15480:             if (ref($lastactref) eq 'HASH') {
15481:                 $lastactref->{'domdefaults'} = 1;
15482:             }
15483:         } else {
15484:             $resulttext = &mt('No changes made to self-creation settings');
15485:         }
15486:     } else {
15487:         $resulttext = '<span class="LC_error">'.
15488:             &mt('An error occurred: [_1]',$putresult).'</span>';
15489:     }
15490:     if ($warningmsg ne '') {
15491:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
15492:     }
15493:     return $resulttext;
15494: }
15495: 
15496: sub process_captcha {
15497:     my ($container,$changes,$newsettings,$current) = @_;
15498:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
15499:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
15500:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
15501:         $newsettings->{'captcha'} = 'original';
15502:     }
15503:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
15504:         if ($container eq 'cancreate') {
15505:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
15506:                 push(@{$changes->{'cancreate'}},'captcha');
15507:             } elsif (!defined($changes->{'cancreate'})) {
15508:                 $changes->{'cancreate'} = ['captcha'];
15509:             }
15510:         } else {
15511:             $changes->{'captcha'} = 1;
15512:         }
15513:     }
15514:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
15515:     if ($newsettings->{'captcha'} eq 'recaptcha') {
15516:         $newpub = $env{'form.'.$container.'_recaptchapub'};
15517:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
15518:         $newpub =~ s/[^\w\-]//g;
15519:         $newpriv =~ s/[^\w\-]//g;
15520:         $newsettings->{'recaptchakeys'} = {
15521:                                              public  => $newpub,
15522:                                              private => $newpriv,
15523:                                           };
15524:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
15525:         $newversion =~ s/\D//g;
15526:         if ($newversion ne '2') {
15527:             $newversion = 1;
15528:         }
15529:         $newsettings->{'recaptchaversion'} = $newversion;
15530:     }
15531:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
15532:         $currpub = $current->{'recaptchakeys'}{'public'};
15533:         $currpriv = $current->{'recaptchakeys'}{'private'};
15534:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
15535:             $newsettings->{'recaptchakeys'} = {
15536:                                                  public  => '',
15537:                                                  private => '',
15538:                                               }
15539:         }
15540:     }
15541:     if ($current->{'captcha'} eq 'recaptcha') {
15542:         $currversion = $current->{'recaptchaversion'};
15543:         if ($currversion ne '2') {
15544:             $currversion = 1;
15545:         }
15546:     }
15547:     if ($currversion ne $newversion) {
15548:         if ($container eq 'cancreate') {
15549:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
15550:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
15551:             } elsif (!defined($changes->{'cancreate'})) {
15552:                 $changes->{'cancreate'} = ['recaptchaversion'];
15553:             }
15554:         } else {
15555:             $changes->{'recaptchaversion'} = 1;
15556:         }
15557:     }
15558:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
15559:         if ($container eq 'cancreate') {
15560:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
15561:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
15562:             } elsif (!defined($changes->{'cancreate'})) {
15563:                 $changes->{'cancreate'} = ['recaptchakeys'];
15564:             }
15565:         } else {
15566:             $changes->{'recaptchakeys'} = 1;
15567:         }
15568:     }
15569:     return;
15570: }
15571: 
15572: sub modify_usermodification {
15573:     my ($dom,%domconfig) = @_;
15574:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
15575:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
15576:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
15577:             if ($key eq 'selfcreate') {
15578:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
15579:             } else {  
15580:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
15581:             }
15582:         }
15583:     }
15584:     my @contexts = ('author','course');
15585:     my %context_title = (
15586:                            author => 'In author context',
15587:                            course => 'In course context',
15588:                         );
15589:     my @fields = ('lastname','firstname','middlename','generation',
15590:                   'permanentemail','id');
15591:     my %roles = (
15592:                   author => ['ca','aa'],
15593:                   course => ['st','ep','ta','in','cr'],
15594:                 );
15595:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15596:     foreach my $context (@contexts) {
15597:         foreach my $role (@{$roles{$context}}) {
15598:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
15599:             foreach my $item (@fields) {
15600:                 if (grep(/^\Q$item\E$/,@modifiable)) {
15601:                     $modifyhash{$context}{$role}{$item} = 1;
15602:                 } else {
15603:                     $modifyhash{$context}{$role}{$item} = 0;
15604:                 }
15605:             }
15606:         }
15607:         if (ref($curr_usermodification{$context}) eq 'HASH') {
15608:             foreach my $role (@{$roles{$context}}) {
15609:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
15610:                     foreach my $field (@fields) {
15611:                         if ($modifyhash{$context}{$role}{$field} ne 
15612:                                 $curr_usermodification{$context}{$role}{$field}) {
15613:                             push(@{$changes{$context}},$role);
15614:                             last;
15615:                         }
15616:                     }
15617:                 }
15618:             }
15619:         } else {
15620:             foreach my $context (@contexts) {
15621:                 foreach my $role (@{$roles{$context}}) {
15622:                     push(@{$changes{$context}},$role);
15623:                 }
15624:             }
15625:         }
15626:     }
15627:     my %usermodification_hash =  (
15628:                                    usermodification => \%modifyhash,
15629:                                  );
15630:     my $putresult = &Apache::lonnet::put_dom('configuration',
15631:                                              \%usermodification_hash,$dom);
15632:     if ($putresult eq 'ok') {
15633:         if (keys(%changes) > 0) {
15634:             $resulttext = &mt('Changes made: ').'<ul>';
15635:             foreach my $context (@contexts) {
15636:                 if (ref($changes{$context}) eq 'ARRAY') {
15637:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
15638:                     if (ref($changes{$context}) eq 'ARRAY') {
15639:                         foreach my $role (@{$changes{$context}}) {
15640:                             my $rolename;
15641:                             if ($role eq 'cr') {
15642:                                 $rolename = &mt('Custom');
15643:                             } else {
15644:                                 $rolename = &Apache::lonnet::plaintext($role);
15645:                             }
15646:                             my @modifiable;
15647:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
15648:                             foreach my $field (@fields) {
15649:                                 if ($modifyhash{$context}{$role}{$field}) {
15650:                                     push(@modifiable,$fieldtitles{$field});
15651:                                 }
15652:                             }
15653:                             if (@modifiable > 0) {
15654:                                 $resulttext .= join(', ',@modifiable);
15655:                             } else {
15656:                                 $resulttext .= &mt('none'); 
15657:                             }
15658:                             $resulttext .= '</li>';
15659:                         }
15660:                         $resulttext .= '</ul></li>';
15661:                     }
15662:                 }
15663:             }
15664:             $resulttext .= '</ul>';
15665:         } else {
15666:             $resulttext = &mt('No changes made to user modification settings');
15667:         }
15668:     } else {
15669:         $resulttext = '<span class="LC_error">'.
15670:             &mt('An error occurred: [_1]',$putresult).'</span>';
15671:     }
15672:     return $resulttext;
15673: }
15674: 
15675: sub modify_defaults {
15676:     my ($dom,$lastactref,%domconfig) = @_;
15677:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
15678:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15679:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
15680:                  'portal_def');
15681:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
15682:     foreach my $item (@items) {
15683:         $newvalues{$item} = $env{'form.'.$item};
15684:         if ($item eq 'auth_def') {
15685:             if ($newvalues{$item} ne '') {
15686:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
15687:                     push(@errors,$item);
15688:                 }
15689:             }
15690:         } elsif ($item eq 'lang_def') {
15691:             if ($newvalues{$item} ne '') {
15692:                 if ($newvalues{$item} =~ /^(\w+)/) {
15693:                     my $langcode = $1;
15694:                     if ($langcode ne 'x_chef') {
15695:                         if (code2language($langcode) eq '') {
15696:                             push(@errors,$item);
15697:                         }
15698:                     }
15699:                 } else {
15700:                     push(@errors,$item);
15701:                 }
15702:             }
15703:         } elsif ($item eq 'timezone_def') {
15704:             if ($newvalues{$item} ne '') {
15705:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
15706:                     push(@errors,$item);   
15707:                 }
15708:             }
15709:         } elsif ($item eq 'datelocale_def') {
15710:             if ($newvalues{$item} ne '') {
15711:                 my @datelocale_ids = DateTime::Locale->ids();
15712:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
15713:                     push(@errors,$item);
15714:                 }
15715:             }
15716:         } elsif ($item eq 'portal_def') {
15717:             if ($newvalues{$item} ne '') {
15718:                 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])\/?$/) {
15719:                     push(@errors,$item);
15720:                 }
15721:             }
15722:         }
15723:         if (grep(/^\Q$item\E$/,@errors)) {
15724:             $newvalues{$item} = $domdefaults{$item};
15725:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
15726:             $changes{$item} = 1;
15727:         }
15728:         $domdefaults{$item} = $newvalues{$item};
15729:     }
15730:     my %staticdefaults = (
15731:                            'intauth_cost'   => 10,
15732:                            'intauth_check'  => 0,
15733:                            'intauth_switch' => 0,
15734:                          );
15735:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
15736:         if (exists($domdefaults{$item})) {
15737:             $newvalues{$item} = $domdefaults{$item};
15738:         } else {
15739:             $newvalues{$item} = $staticdefaults{$item};
15740:         }
15741:     }
15742:     my %defaults_hash = (
15743:                          defaults => \%newvalues,
15744:                         );
15745:     my $title = &defaults_titles();
15746: 
15747:     my $currinststatus;
15748:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
15749:         $currinststatus = $domconfig{'inststatus'};
15750:     } else {
15751:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15752:         $currinststatus = {
15753:                              inststatustypes => $usertypes,
15754:                              inststatusorder => $types,
15755:                              inststatusguest => [],
15756:                           };
15757:     }
15758:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
15759:     my @allpos;
15760:     my %alltypes;
15761:     my @inststatusguest;
15762:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
15763:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
15764:             unless (grep(/^\Q$type\E$/,@todelete)) {
15765:                 push(@inststatusguest,$type);
15766:             }
15767:         }
15768:     }
15769:     my ($currtitles,$currorder);
15770:     if (ref($currinststatus) eq 'HASH') {
15771:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
15772:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
15773:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
15774:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
15775:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
15776:                     }
15777:                 }
15778:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
15779:                     my $position = $env{'form.inststatus_pos_'.$type};
15780:                     $position =~ s/\D+//g;
15781:                     $allpos[$position] = $type;
15782:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
15783:                     $alltypes{$type} =~ s/`//g;
15784:                 }
15785:             }
15786:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
15787:             $currtitles =~ s/,$//;
15788:         }
15789:     }
15790:     if ($env{'form.addinststatus'}) {
15791:         my $newtype = $env{'form.addinststatus'};
15792:         $newtype =~ s/\W//g;
15793:         unless (exists($alltypes{$newtype})) {
15794:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
15795:             $alltypes{$newtype} =~ s/`//g; 
15796:             my $position = $env{'form.addinststatus_pos'};
15797:             $position =~ s/\D+//g;
15798:             if ($position ne '') {
15799:                 $allpos[$position] = $newtype;
15800:             }
15801:         }
15802:     }
15803:     my @orderedstatus;
15804:     foreach my $type (@allpos) {
15805:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
15806:             push(@orderedstatus,$type);
15807:         }
15808:     }
15809:     foreach my $type (keys(%alltypes)) {
15810:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
15811:             delete($alltypes{$type});
15812:         }
15813:     }
15814:     $defaults_hash{'inststatus'} = {
15815:                                      inststatustypes => \%alltypes,
15816:                                      inststatusorder => \@orderedstatus,
15817:                                      inststatusguest => \@inststatusguest,
15818:                                    };
15819:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
15820:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
15821:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
15822:         }
15823:     }
15824:     if ($currorder ne join(',',@orderedstatus)) {
15825:         $changes{'inststatus'}{'inststatusorder'} = 1;
15826:     }
15827:     my $newtitles;
15828:     foreach my $item (@orderedstatus) {
15829:         $newtitles .= $alltypes{$item}.',';
15830:     }
15831:     $newtitles =~ s/,$//;
15832:     if ($currtitles ne $newtitles) {
15833:         $changes{'inststatus'}{'inststatustypes'} = 1;
15834:     }
15835:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
15836:                                              $dom);
15837:     if ($putresult eq 'ok') {
15838:         if (keys(%changes) > 0) {
15839:             $resulttext = &mt('Changes made:').'<ul>';
15840:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
15841:             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";
15842:             foreach my $item (sort(keys(%changes))) {
15843:                 if ($item eq 'inststatus') {
15844:                     if (ref($changes{'inststatus'}) eq 'HASH') {
15845:                         if (@orderedstatus) {
15846:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
15847:                             foreach my $type (@orderedstatus) { 
15848:                                 $resulttext .= $alltypes{$type}.', ';
15849:                             }
15850:                             $resulttext =~ s/, $//;
15851:                             $resulttext .= '</li>';
15852:                         } else {
15853:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
15854:                         }
15855:                     }
15856:                 } else {
15857:                     my $value = $env{'form.'.$item};
15858:                     if ($value eq '') {
15859:                         $value = &mt('none');
15860:                     } elsif ($item eq 'auth_def') {
15861:                         my %authnames = &authtype_names();
15862:                         my %shortauth = (
15863:                                           internal   => 'int',
15864:                                           krb4       => 'krb4',
15865:                                           krb5       => 'krb5',
15866:                                           localauth  => 'loc',
15867:                                           lti        => 'lti',
15868:                         );
15869:                         $value = $authnames{$shortauth{$value}};
15870:                     }
15871:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
15872:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
15873:                 }
15874:             }
15875:             $resulttext .= '</ul>';
15876:             $mailmsgtext .= "\n";
15877:             my $cachetime = 24*60*60;
15878:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15879:             if (ref($lastactref) eq 'HASH') {
15880:                 $lastactref->{'domdefaults'} = 1;
15881:             }
15882:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
15883:                 my $notify = 1;
15884:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
15885:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
15886:                         $notify = 0;
15887:                     }
15888:                 }
15889:                 if ($notify) {
15890:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
15891:                                                "LON-CAPA Domain Settings Change - $dom",
15892:                                                $mailmsgtext);
15893:                 }
15894:             }
15895:         } else {
15896:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
15897:         }
15898:     } else {
15899:         $resulttext = '<span class="LC_error">'.
15900:             &mt('An error occurred: [_1]',$putresult).'</span>';
15901:     }
15902:     if (@errors > 0) {
15903:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
15904:         foreach my $item (@errors) {
15905:             $resulttext .= ' "'.$title->{$item}.'",';
15906:         }
15907:         $resulttext =~ s/,$//;
15908:     }
15909:     return $resulttext;
15910: }
15911: 
15912: sub modify_scantron {
15913:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15914:     my ($resulttext,%confhash,%changes,$errors);
15915:     my $custom = 'custom.tab';
15916:     my $default = 'default.tab';
15917:     my $servadm = $r->dir_config('lonAdmEMail');
15918:     my ($configuserok,$author_ok,$switchserver) =
15919:         &config_check($dom,$confname,$servadm);
15920:     if ($env{'form.scantronformat.filename'} ne '') {
15921:         my $error;
15922:         if ($configuserok eq 'ok') {
15923:             if ($switchserver) {
15924:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
15925:             } else {
15926:                 if ($author_ok eq 'ok') {
15927:                     my ($result,$scantronurl) =
15928:                         &publishlogo($r,'upload','scantronformat',$dom,
15929:                                      $confname,'scantron','','',$custom);
15930:                     if ($result eq 'ok') {
15931:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
15932:                         $changes{'scantronformat'} = 1;
15933:                     } else {
15934:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
15935:                     }
15936:                 } else {
15937:                     $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);
15938:                 }
15939:             }
15940:         } else {
15941:             $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);
15942:         }
15943:         if ($error) {
15944:             &Apache::lonnet::logthis($error);
15945:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15946:         }
15947:     }
15948:     if (ref($domconfig{'scantron'}) eq 'HASH') {
15949:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
15950:             if ($env{'form.scantronformat_del'}) {
15951:                 $confhash{'scantron'}{'scantronformat'} = '';
15952:                 $changes{'scantronformat'} = 1;
15953:             } else {
15954:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
15955:             }
15956:         }
15957:     }
15958:     my @options = ('hdr','pad','rem');
15959:     my @fields = &scantroncsv_fields();
15960:     my %titles = &scantronconfig_titles();
15961:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
15962:     my ($newdat,$currdat,%newcol,%currcol);
15963:     if (grep(/^dat$/,@formats)) {
15964:         $confhash{'scantron'}{config}{dat} = 1;
15965:         $newdat = 1;
15966:     } else {
15967:         $newdat = 0;
15968:     }
15969:     if (grep(/^csv$/,@formats)) {
15970:         my %bynum;
15971:         foreach my $field (@fields) {
15972:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
15973:                 my $posscol = $1;
15974:                 if (($posscol < 20) && (!$bynum{$posscol})) {
15975:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
15976:                     $bynum{$posscol} = $field;
15977:                     $newcol{$field} = $posscol;
15978:                 }
15979:             }
15980:         }
15981:         if (keys(%newcol)) {
15982:             foreach my $option (@options) {
15983:                 if ($env{'form.scantroncsv_'.$option}) {
15984:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
15985:                 }
15986:             }
15987:         }
15988:     }
15989:     $currdat = 1;
15990:     if (ref($domconfig{'scantron'}) eq 'HASH') {
15991:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
15992:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
15993:                 $currdat = 0;
15994:             }
15995:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
15996:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
15997:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
15998:                 }
15999:             }
16000:         }
16001:     }
16002:     if ($currdat != $newdat) {
16003:         $changes{'config'} = 1;
16004:     } else {
16005:         foreach my $field (@fields) {
16006:             if ($currcol{$field} ne '') {
16007:                 if ($currcol{$field} ne $newcol{$field}) {
16008:                     $changes{'config'} = 1;
16009:                     last;
16010:                 }
16011:             } elsif ($newcol{$field} ne '') {
16012:                 $changes{'config'} = 1;
16013:                 last;
16014:             }
16015:         }
16016:     }
16017:     if (keys(%confhash) > 0) {
16018:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
16019:                                                  $dom);
16020:         if ($putresult eq 'ok') {
16021:             if (keys(%changes) > 0) {
16022:                 if (ref($confhash{'scantron'}) eq 'HASH') {
16023:                     $resulttext = &mt('Changes made:').'<ul>';
16024:                     if ($changes{'scantronformat'}) {
16025:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
16026:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
16027:                         } else {
16028:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
16029:                         }
16030:                     }
16031:                     if ($changes{'config'}) {
16032:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
16033:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
16034:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
16035:                             }
16036:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
16037:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
16038:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
16039:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
16040:                                         foreach my $field (@fields) {
16041:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
16042:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
16043:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
16044:                                             }
16045:                                         }
16046:                                         $resulttext .= '</ul></li>';
16047:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
16048:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
16049:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
16050:                                                 foreach my $option (@options) {
16051:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
16052:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
16053:                                                     }
16054:                                                 }
16055:                                                 $resulttext .= '</ul></li>';
16056:                                             }
16057:                                         }
16058:                                     }
16059:                                 }
16060:                             }
16061:                         } else {
16062:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
16063:                         }
16064:                     }
16065:                     $resulttext .= '</ul>';
16066:                 } else {
16067:                     $resulttext = &mt('Changes made to bubblesheet format file.');
16068:                 }
16069:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
16070:                 if (ref($lastactref) eq 'HASH') {
16071:                     $lastactref->{'domainconfig'} = 1;
16072:                 }
16073:             } else {
16074:                 $resulttext = &mt('No changes made to bubblesheet format settings');
16075:             }
16076:         } else {
16077:             $resulttext = '<span class="LC_error">'.
16078:                 &mt('An error occurred: [_1]',$putresult).'</span>';
16079:         }
16080:     } else {
16081:         $resulttext = &mt('No changes made to bubblesheet format file'); 
16082:     }
16083:     if ($errors) {
16084:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
16085:                        $errors.'</ul></p>';
16086:     }
16087:     return $resulttext;
16088: }
16089: 
16090: sub modify_coursecategories {
16091:     my ($dom,$lastactref,%domconfig) = @_;
16092:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
16093:         $cathash);
16094:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
16095:     my @catitems = ('unauth','auth');
16096:     my @cattypes = ('std','domonly','codesrch','none');
16097:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
16098:         $cathash = $domconfig{'coursecategories'}{'cats'};
16099:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
16100:             $changes{'togglecats'} = 1;
16101:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
16102:         }
16103:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
16104:             $changes{'categorize'} = 1;
16105:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
16106:         }
16107:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
16108:             $changes{'togglecatscomm'} = 1;
16109:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
16110:         }
16111:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
16112:             $changes{'categorizecomm'} = 1;
16113:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
16114: 
16115:         }
16116:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
16117:             $changes{'togglecatsplace'} = 1;
16118:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
16119:         }
16120:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
16121:             $changes{'categorizeplace'} = 1;
16122:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
16123:         }
16124:         foreach my $item (@catitems) {
16125:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
16126:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
16127:                     $changes{$item} = 1;
16128:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
16129:                 }
16130:             }
16131:         }
16132:     } else {
16133:         $changes{'togglecats'} = 1;
16134:         $changes{'categorize'} = 1;
16135:         $changes{'togglecatscomm'} = 1;
16136:         $changes{'categorizecomm'} = 1;
16137:         $changes{'togglecatsplace'} = 1;
16138:         $changes{'categorizeplace'} = 1;
16139:         $domconfig{'coursecategories'} = {
16140:                                              togglecats => $env{'form.togglecats'},
16141:                                              categorize => $env{'form.categorize'},
16142:                                              togglecatscomm => $env{'form.togglecatscomm'},
16143:                                              categorizecomm => $env{'form.categorizecomm'},
16144:                                              togglecatsplace => $env{'form.togglecatsplace'},
16145:                                              categorizeplace => $env{'form.categorizeplace'},
16146:                                          };
16147:         foreach my $item (@catitems) {
16148:             if ($env{'form.coursecat_'.$item} ne 'std') {
16149:                 $changes{$item} = 1;
16150:             }
16151:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
16152:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
16153:             }
16154:         }
16155:     }
16156:     if (ref($cathash) eq 'HASH') {
16157:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
16158:             push (@deletecategory,'instcode::0');
16159:         }
16160:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
16161:             push(@deletecategory,'communities::0');
16162:         }
16163:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
16164:             push(@deletecategory,'placement::0');
16165:         }
16166:     }
16167:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
16168:     if (ref($cathash) eq 'HASH') {
16169:         if (@deletecategory > 0) {
16170:             #FIXME Need to remove category from all courses using a deleted category 
16171:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
16172:             foreach my $item (@deletecategory) {
16173:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
16174:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
16175:                     $deletions{$item} = 1;
16176:                     &recurse_cat_deletes($item,$cathash,\%deletions);
16177:                 }
16178:             }
16179:         }
16180:         foreach my $item (keys(%{$cathash})) {
16181:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
16182:             if ($cathash->{$item} ne $env{'form.'.$item}) {
16183:                 $reorderings{$item} = 1;
16184:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
16185:             }
16186:             if ($env{'form.addcategory_name_'.$item} ne '') {
16187:                 my $newcat = $env{'form.addcategory_name_'.$item};
16188:                 my $newdepth = $depth+1;
16189:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
16190:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
16191:                 $adds{$newitem} = 1; 
16192:             }
16193:             if ($env{'form.subcat_'.$item} ne '') {
16194:                 my $newcat = $env{'form.subcat_'.$item};
16195:                 my $newdepth = $depth+1;
16196:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
16197:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
16198:                 $adds{$newitem} = 1;
16199:             }
16200:         }
16201:     }
16202:     if ($env{'form.instcode'} eq '1') {
16203:         if (ref($cathash) eq 'HASH') {
16204:             my $newitem = 'instcode::0';
16205:             if ($cathash->{$newitem} eq '') {  
16206:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
16207:                 $adds{$newitem} = 1;
16208:             }
16209:         } else {
16210:             my $newitem = 'instcode::0';
16211:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
16212:             $adds{$newitem} = 1;
16213:         }
16214:     }
16215:     if ($env{'form.communities'} eq '1') {
16216:         if (ref($cathash) eq 'HASH') {
16217:             my $newitem = 'communities::0';
16218:             if ($cathash->{$newitem} eq '') {
16219:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
16220:                 $adds{$newitem} = 1;
16221:             }
16222:         } else {
16223:             my $newitem = 'communities::0';
16224:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
16225:             $adds{$newitem} = 1;
16226:         }
16227:     }
16228:     if ($env{'form.placement'} eq '1') {
16229:         if (ref($cathash) eq 'HASH') {
16230:             my $newitem = 'placement::0';
16231:             if ($cathash->{$newitem} eq '') {
16232:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
16233:                 $adds{$newitem} = 1;
16234:             }
16235:         } else {
16236:             my $newitem = 'placement::0';
16237:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
16238:             $adds{$newitem} = 1;
16239:         }
16240:     }
16241:     if ($env{'form.addcategory_name'} ne '') {
16242:         if (($env{'form.addcategory_name'} ne 'instcode') &&
16243:             ($env{'form.addcategory_name'} ne 'communities') &&
16244:             ($env{'form.addcategory_name'} ne 'placement')) {
16245:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
16246:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
16247:             $adds{$newitem} = 1;
16248:         }
16249:     }
16250:     my $putresult;
16251:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
16252:         if (keys(%deletions) > 0) {
16253:             foreach my $key (keys(%deletions)) {
16254:                 if ($predelallitems{$key} ne '') {
16255:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
16256:                 }
16257:             }
16258:         }
16259:         my (@chkcats,@chktrails,%chkallitems);
16260:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
16261:         if (ref($chkcats[0]) eq 'ARRAY') {
16262:             my $depth = 0;
16263:             my $chg = 0;
16264:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
16265:                 my $name = $chkcats[0][$i];
16266:                 my $item;
16267:                 if ($name eq '') {
16268:                     $chg ++;
16269:                 } else {
16270:                     $item = &escape($name).'::0';
16271:                     if ($chg) {
16272:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
16273:                     }
16274:                     $depth ++; 
16275:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
16276:                     $depth --;
16277:                 }
16278:             }
16279:         }
16280:     }
16281:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
16282:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
16283:         if ($putresult eq 'ok') {
16284:             my %title = (
16285:                          togglecats     => 'Show/Hide a course in catalog',
16286:                          categorize     => 'Assign a category to a course',
16287:                          togglecatscomm => 'Show/Hide a community in catalog',
16288:                          categorizecomm => 'Assign a category to a community',
16289:                         );
16290:             my %level = (
16291:                          dom  => 'set in Domain ("Modify Course/Community")',
16292:                          crs  => 'set in Course ("Course Configuration")',
16293:                          comm => 'set in Community ("Community Configuration")',
16294:                          none     => 'No catalog',
16295:                          std      => 'Standard catalog',
16296:                          domonly  => 'Domain-only catalog',
16297:                          codesrch => 'Code search form',
16298:                         );
16299:             $resulttext = &mt('Changes made:').'<ul>';
16300:             if ($changes{'togglecats'}) {
16301:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
16302:             }
16303:             if ($changes{'categorize'}) {
16304:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
16305:             }
16306:             if ($changes{'togglecatscomm'}) {
16307:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
16308:             }
16309:             if ($changes{'categorizecomm'}) {
16310:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
16311:             }
16312:             if ($changes{'unauth'}) {
16313:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
16314:             }
16315:             if ($changes{'auth'}) {
16316:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
16317:             }
16318:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
16319:                 my $cathash;
16320:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
16321:                     $cathash = $domconfig{'coursecategories'}{'cats'};
16322:                 } else {
16323:                     $cathash = {};
16324:                 } 
16325:                 my (@cats,@trails,%allitems);
16326:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
16327:                 if (keys(%deletions) > 0) {
16328:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
16329:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
16330:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
16331:                     }
16332:                     $resulttext .= '</ul></li>';
16333:                 }
16334:                 if (keys(%reorderings) > 0) {
16335:                     my %sort_by_trail;
16336:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
16337:                     foreach my $key (keys(%reorderings)) {
16338:                         if ($allitems{$key} ne '') {
16339:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
16340:                         }
16341:                     }
16342:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
16343:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
16344:                     }
16345:                     $resulttext .= '</ul></li>';
16346:                 }
16347:                 if (keys(%adds) > 0) {
16348:                     my %sort_by_trail;
16349:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
16350:                     foreach my $key (keys(%adds)) {
16351:                         if ($allitems{$key} ne '') {
16352:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
16353:                         }
16354:                     }
16355:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
16356:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
16357:                     }
16358:                     $resulttext .= '</ul></li>';
16359:                 }
16360:             }
16361:             $resulttext .= '</ul>';
16362:             if ($changes{'unauth'} || $changes{'auth'}) {
16363:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
16364:                 if ($changes{'auth'}) {
16365:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
16366:                 }
16367:                 if ($changes{'unauth'}) {
16368:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
16369:                 }
16370:                 my $cachetime = 24*60*60;
16371:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16372:                 if (ref($lastactref) eq 'HASH') {
16373:                     $lastactref->{'domdefaults'} = 1;
16374:                 }
16375:             }
16376:         } else {
16377:             $resulttext = '<span class="LC_error">'.
16378:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16379:         }
16380:     } else {
16381:         $resulttext = &mt('No changes made to course and community categories');
16382:     }
16383:     return $resulttext;
16384: }
16385: 
16386: sub modify_serverstatuses {
16387:     my ($dom,%domconfig) = @_;
16388:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
16389:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
16390:         %currserverstatus = %{$domconfig{'serverstatuses'}};
16391:     }
16392:     my @pages = &serverstatus_pages();
16393:     foreach my $type (@pages) {
16394:         $newserverstatus{$type}{'namedusers'} = '';
16395:         $newserverstatus{$type}{'machines'} = '';
16396:         if (defined($env{'form.'.$type.'_namedusers'})) {
16397:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
16398:             my @okusers;
16399:             foreach my $user (@users) {
16400:                 my ($uname,$udom) = split(/:/,$user);
16401:                 if (($udom =~ /^$match_domain$/) &&   
16402:                     (&Apache::lonnet::domain($udom)) &&
16403:                     ($uname =~ /^$match_username$/)) {
16404:                     if (!grep(/^\Q$user\E/,@okusers)) {
16405:                         push(@okusers,$user);
16406:                     }
16407:                 }
16408:             }
16409:             if (@okusers > 0) {
16410:                  @okusers = sort(@okusers);
16411:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
16412:             }
16413:         }
16414:         if (defined($env{'form.'.$type.'_machines'})) {
16415:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
16416:             my @okmachines;
16417:             foreach my $ip (@machines) {
16418:                 my @parts = split(/\./,$ip);
16419:                 next if (@parts < 4);
16420:                 my $badip = 0;
16421:                 for (my $i=0; $i<4; $i++) {
16422:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
16423:                         $badip = 1;
16424:                         last;
16425:                     }
16426:                 }
16427:                 if (!$badip) {
16428:                     push(@okmachines,$ip);     
16429:                 }
16430:             }
16431:             @okmachines = sort(@okmachines);
16432:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
16433:         }
16434:     }
16435:     my %serverstatushash =  (
16436:                                 serverstatuses => \%newserverstatus,
16437:                             );
16438:     foreach my $type (@pages) {
16439:         foreach my $setting ('namedusers','machines') {
16440:             my (@current,@new);
16441:             if (ref($currserverstatus{$type}) eq 'HASH') {
16442:                 if ($currserverstatus{$type}{$setting} ne '') { 
16443:                     @current = split(/,/,$currserverstatus{$type}{$setting});
16444:                 }
16445:             }
16446:             if ($newserverstatus{$type}{$setting} ne '') {
16447:                 @new = split(/,/,$newserverstatus{$type}{$setting});
16448:             }
16449:             if (@current > 0) {
16450:                 if (@new > 0) {
16451:                     foreach my $item (@current) {
16452:                         if (!grep(/^\Q$item\E$/,@new)) {
16453:                             $changes{$type}{$setting} = 1;
16454:                             last;
16455:                         }
16456:                     }
16457:                     foreach my $item (@new) {
16458:                         if (!grep(/^\Q$item\E$/,@current)) {
16459:                             $changes{$type}{$setting} = 1;
16460:                             last;
16461:                         }
16462:                     }
16463:                 } else {
16464:                     $changes{$type}{$setting} = 1;
16465:                 }
16466:             } elsif (@new > 0) {
16467:                 $changes{$type}{$setting} = 1;
16468:             }
16469:         }
16470:     }
16471:     if (keys(%changes) > 0) {
16472:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
16473:         my $putresult = &Apache::lonnet::put_dom('configuration',
16474:                                                  \%serverstatushash,$dom);
16475:         if ($putresult eq 'ok') {
16476:             $resulttext .= &mt('Changes made:').'<ul>';
16477:             foreach my $type (@pages) {
16478:                 if (ref($changes{$type}) eq 'HASH') {
16479:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
16480:                     if ($changes{$type}{'namedusers'}) {
16481:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
16482:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
16483:                         } else {
16484:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
16485:                         }
16486:                     }
16487:                     if ($changes{$type}{'machines'}) {
16488:                         if ($newserverstatus{$type}{'machines'} eq '') {
16489:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
16490:                         } else {
16491:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
16492:                         }
16493: 
16494:                     }
16495:                     $resulttext .= '</ul></li>';
16496:                 }
16497:             }
16498:             $resulttext .= '</ul>';
16499:         } else {
16500:             $resulttext = '<span class="LC_error">'.
16501:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
16502: 
16503:         }
16504:     } else {
16505:         $resulttext = &mt('No changes made to access to server status pages');
16506:     }
16507:     return $resulttext;
16508: }
16509: 
16510: sub modify_helpsettings {
16511:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
16512:     my ($resulttext,$errors,%changes,%helphash);
16513:     my %defaultchecked = ('submitbugs' => 'on');
16514:     my @offon = ('off','on');
16515:     my @toggles = ('submitbugs');
16516:     my %current = ('submitbugs' => '',
16517:                    'adhoc'      => {},
16518:                   );
16519:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
16520:         %current = %{$domconfig{'helpsettings'}};
16521:     }
16522:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16523:     foreach my $item (@toggles) {
16524:         if ($defaultchecked{$item} eq 'on') { 
16525:             if ($current{$item} eq '') {
16526:                 if ($env{'form.'.$item} eq '0') {
16527:                     $changes{$item} = 1;
16528:                 }
16529:             } elsif ($current{$item} ne $env{'form.'.$item}) {
16530:                 $changes{$item} = 1;
16531:             }
16532:         } elsif ($defaultchecked{$item} eq 'off') {
16533:             if ($current{$item} eq '') {
16534:                 if ($env{'form.'.$item} eq '1') {
16535:                     $changes{$item} = 1;
16536:                 }
16537:             } elsif ($current{$item} ne $env{'form.'.$item}) {
16538:                 $changes{$item} = 1;
16539:             }
16540:         }
16541:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
16542:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
16543:         }
16544:     }
16545:     my $maxnum = $env{'form.helproles_maxnum'};
16546:     my $confname = $dom.'-domainconfig';
16547:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
16548:     my (@allpos,%newsettings,%changedprivs,$newrole);
16549:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16550:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
16551:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
16552:     my %lt = &Apache::lonlocal::texthash(
16553:                     s      => 'system',
16554:                     d      => 'domain',
16555:                     order  => 'Display order',
16556:                     access => 'Role usage',
16557:                     all    => 'All with domain helpdesk or helpdesk assistant role',
16558:                     dh     => 'All with domain helpdesk role',
16559:                     da     => 'All with domain helpdesk assistant role',
16560:                     none   => 'None',
16561:                     status => 'Determined based on institutional status',
16562:                     inc    => 'Include all, but exclude specific personnel',
16563:                     exc    => 'Exclude all, but include specific personnel',
16564:     );
16565:     for (my $num=0; $num<=$maxnum; $num++) {
16566:         my ($prefix,$identifier,$rolename,%curr);
16567:         if ($num == $maxnum) {
16568:             next unless ($env{'form.newcusthelp'} == $maxnum);
16569:             $identifier = 'custhelp'.$num;
16570:             $prefix = 'helproles_'.$num;
16571:             $rolename = $env{'form.custhelpname'.$num};
16572:             $rolename=~s/[^A-Za-z0-9]//gs;
16573:             next if ($rolename eq '');
16574:             next if (exists($existing{'rolesdef_'.$rolename}));
16575:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
16576:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
16577:                                                      $newprivs{'c'},$confname,$dom);
16578:             if ($result ne 'ok') {
16579:                 $errors .= '<li><span class="LC_error">'.
16580:                            &mt('An error occurred storing the new custom role: [_1]',
16581:                            $result).'</span></li>';
16582:                 next;
16583:             } else {
16584:                 $changedprivs{$rolename} = \%newprivs;
16585:                 $newrole = $rolename;
16586:             }
16587:         } else {
16588:             $prefix = 'helproles_'.$num;
16589:             $rolename = $env{'form.'.$prefix};
16590:             next if ($rolename eq '');
16591:             next unless (exists($existing{'rolesdef_'.$rolename}));
16592:             $identifier = 'custhelp'.$num;
16593:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
16594:             my %currprivs;
16595:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
16596:                 split(/\_/,$existing{'rolesdef_'.$rolename});
16597:             foreach my $level ('c','d','s') {
16598:                 if ($newprivs{$level} ne $currprivs{$level}) {
16599:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
16600:                                                              $newprivs{'c'},$confname,$dom);
16601:                     if ($result ne 'ok') {
16602:                         $errors .= '<li><span class="LC_error">'.
16603:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
16604:                                        $rolename,$result).'</span></li>';
16605:                     } else {
16606:                         $changedprivs{$rolename} = \%newprivs;
16607:                     }
16608:                     last;
16609:                 }
16610:             }
16611:             if (ref($current{'adhoc'}) eq 'HASH') {
16612:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
16613:                     %curr = %{$current{'adhoc'}{$rolename}};
16614:                 }
16615:             }
16616:         }
16617:         my $newpos = $env{'form.'.$prefix.'_pos'};
16618:         $newpos =~ s/\D+//g;
16619:         $allpos[$newpos] = $rolename;
16620:         my $newdesc = $env{'form.'.$prefix.'_desc'};
16621:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
16622:         if ($curr{'desc'}) {
16623:             if ($curr{'desc'} ne $newdesc) {
16624:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
16625:                 $newsettings{$rolename}{'desc'} = $newdesc;
16626:             }
16627:         } elsif ($newdesc ne '') {
16628:             $changes{'customrole'}{$rolename}{'desc'} = 1;
16629:             $newsettings{$rolename}{'desc'} = $newdesc;
16630:         }
16631:         my $access = $env{'form.'.$prefix.'_access'};
16632:         if (grep(/^\Q$access\E$/,@accesstypes)) {
16633:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
16634:             if ($access eq 'status') {
16635:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
16636:                 if (scalar(@statuses) == 0) {
16637:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
16638:                 } else {
16639:                     my (@shownstatus,$numtypes);
16640:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
16641:                     if (ref($types) eq 'ARRAY') {
16642:                         $numtypes = scalar(@{$types});
16643:                         foreach my $type (sort(@statuses)) {
16644:                             if ($type eq 'default') {
16645:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
16646:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
16647:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
16648:                                 push(@shownstatus,$usertypes->{$type});
16649:                             }
16650:                         }
16651:                     }
16652:                     if (grep(/^default$/,@statuses)) {
16653:                         push(@shownstatus,$othertitle);
16654:                     }
16655:                     if (scalar(@shownstatus) == 1+$numtypes) {
16656:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
16657:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
16658:                     } else {
16659:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
16660:                         if (ref($curr{'status'}) eq 'ARRAY') {
16661:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
16662:                             if (@diffs) {
16663:                                 $changes{'customrole'}{$rolename}{$access} = 1;
16664:                             }
16665:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
16666:                             $changes{'customrole'}{$rolename}{$access} = 1;
16667:                         }
16668:                     }
16669:                 }
16670:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
16671:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
16672:                 my @newspecstaff;
16673:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
16674:                 foreach my $person (sort(@personnel)) {
16675:                     if ($domhelpdesk{$person}) {
16676:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
16677:                     }
16678:                 }
16679:                 if (ref($curr{$access}) eq 'ARRAY') {
16680:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
16681:                     if (@diffs) {
16682:                         $changes{'customrole'}{$rolename}{$access} = 1;
16683:                     }
16684:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
16685:                     $changes{'customrole'}{$rolename}{$access} = 1;
16686:                 }
16687:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
16688:                     my ($uname,$udom) = split(/:/,$person);
16689:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
16690:                 }
16691:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
16692:             }
16693:         } else {
16694:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
16695:         }
16696:         unless ($curr{'access'} eq $access) {
16697:             $changes{'customrole'}{$rolename}{'access'} = 1;
16698:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
16699:         }
16700:     }
16701:     if (@allpos > 0) {
16702:         my $idx = 0;
16703:         foreach my $rolename (@allpos) {
16704:             if ($rolename ne '') {
16705:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
16706:                 if (ref($current{'adhoc'}) eq 'HASH') {
16707:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
16708:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
16709:                             $changes{'customrole'}{$rolename}{'order'} = 1;
16710:                             $newsettings{$rolename}{'order'} = $idx+1;
16711:                         }
16712:                     }
16713:                 }
16714:                 $idx ++;
16715:             }
16716:         }
16717:     }
16718:     my $putresult;
16719:     if (keys(%changes) > 0) {
16720:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
16721:         if ($putresult eq 'ok') {
16722:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
16723:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
16724:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
16725:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
16726:                 }
16727:             }
16728:             my $cachetime = 24*60*60;
16729:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16730:             if (ref($lastactref) eq 'HASH') {
16731:                 $lastactref->{'domdefaults'} = 1;
16732:             }
16733:         } else {
16734:             $errors .= '<li><span class="LC_error">'.
16735:                        &mt('An error occurred storing the settings: [_1]',
16736:                            $putresult).'</span></li>';
16737:         }
16738:     }
16739:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
16740:         $resulttext = &mt('Changes made:').'<ul>';
16741:         my (%shownprivs,@levelorder);
16742:         @levelorder = ('c','d','s');
16743:         if ((keys(%changes)) && ($putresult eq 'ok')) {
16744:             foreach my $item (sort(keys(%changes))) {
16745:                 if ($item eq 'submitbugs') {
16746:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
16747:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
16748:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
16749:                 } elsif ($item eq 'customrole') {
16750:                     if (ref($changes{'customrole'}) eq 'HASH') {
16751:                         my @keyorder = ('order','desc','access','status','exc','inc');
16752:                         my %keytext = &Apache::lonlocal::texthash(
16753:                                                                    order  => 'Order',
16754:                                                                    desc   => 'Role description',
16755:                                                                    access => 'Role usage',
16756:                                                                    status => 'Allowed institutional types',
16757:                                                                    exc    => 'Allowed personnel',
16758:                                                                    inc    => 'Disallowed personnel',
16759:                         );
16760:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
16761:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
16762:                                 if ($role eq $newrole) {
16763:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
16764:                                                               $role).'<ul>';
16765:                                 } else {
16766:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
16767:                                                               $role).'<ul>';
16768:                                 }
16769:                                 foreach my $key (@keyorder) {
16770:                                     if ($changes{'customrole'}{$role}{$key}) {
16771:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
16772:                                                                   $keytext{$key},$newsettings{$role}{$key}).
16773:                                                        '</li>';
16774:                                     }
16775:                                 }
16776:                                 if (ref($changedprivs{$role}) eq 'HASH') {
16777:                                     $shownprivs{$role} = 1;
16778:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
16779:                                     foreach my $level (@levelorder) {
16780:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
16781:                                             next if ($item eq '');
16782:                                             my ($priv) = split(/\&/,$item,2);
16783:                                             if (&Apache::lonnet::plaintext($priv)) {
16784:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
16785:                                                 unless ($level eq 'c') {
16786:                                                     $resulttext .= ' ('.$lt{$level}.')';
16787:                                                 }
16788:                                                 $resulttext .= '</li>';
16789:                                             }
16790:                                         }
16791:                                     }
16792:                                     $resulttext .= '</ul>';
16793:                                 }
16794:                                 $resulttext .= '</ul></li>';
16795:                             }
16796:                         }
16797:                     }
16798:                 }
16799:             }
16800:         }
16801:         if (keys(%changedprivs)) {
16802:             foreach my $role (sort(keys(%changedprivs))) {
16803:                 unless ($shownprivs{$role}) {
16804:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
16805:                                               $role).'<ul>'.
16806:                                    '<li>'.&mt('Privileges set to :').'<ul>';
16807:                     foreach my $level (@levelorder) {
16808:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
16809:                             next if ($item eq '');
16810:                             my ($priv) = split(/\&/,$item,2);
16811:                             if (&Apache::lonnet::plaintext($priv)) {
16812:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
16813:                                 unless ($level eq 'c') {
16814:                                     $resulttext .= ' ('.$lt{$level}.')';
16815:                                 }
16816:                                 $resulttext .= '</li>';
16817:                             }
16818:                         }
16819:                     }
16820:                     $resulttext .= '</ul></li></ul></li>';
16821:                 }
16822:             }
16823:         }
16824:         $resulttext .= '</ul>';
16825:     } else {
16826:         $resulttext = &mt('No changes made to help settings');
16827:     }
16828:     if ($errors) {
16829:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
16830:                                     $errors.'</ul>';
16831:     }
16832:     return $resulttext;
16833: }
16834: 
16835: sub modify_coursedefaults {
16836:     my ($dom,$lastactref,%domconfig) = @_;
16837:     my ($resulttext,$errors,%changes,%defaultshash);
16838:     my %defaultchecked = (
16839:                            'canuse_pdfforms' => 'off',
16840:                            'uselcmath'       => 'on',
16841:                            'usejsme'         => 'on'
16842:                          );
16843:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
16844:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
16845:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
16846:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
16847:                    'mysqltables_textbook','mysqltables_placement');
16848:     my @types = ('official','unofficial','community','textbook','placement');
16849:     my %staticdefaults = (
16850:                            anonsurvey_threshold => 10,
16851:                            uploadquota          => 500,
16852:                            postsubmit           => 60,
16853:                            mysqltables          => 172800,
16854:                          );
16855:     my %texoptions = (
16856:                         MathJax  => 'MathJax',
16857:                         mimetex  => &mt('Convert to Images'),
16858:                         tth      => &mt('TeX to HTML'),
16859:                      );
16860:     $defaultshash{'coursedefaults'} = {};
16861: 
16862:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
16863:         if ($domconfig{'coursedefaults'} eq '') {
16864:             $domconfig{'coursedefaults'} = {};
16865:         }
16866:     }
16867: 
16868:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
16869:         foreach my $item (@toggles) {
16870:             if ($defaultchecked{$item} eq 'on') {
16871:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
16872:                     ($env{'form.'.$item} eq '0')) {
16873:                     $changes{$item} = 1;
16874:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
16875:                     $changes{$item} = 1;
16876:                 }
16877:             } elsif ($defaultchecked{$item} eq 'off') {
16878:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
16879:                     ($env{'form.'.$item} eq '1')) {
16880:                     $changes{$item} = 1;
16881:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
16882:                     $changes{$item} = 1;
16883:                 }
16884:             }
16885:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
16886:         }
16887:         foreach my $item (@numbers) {
16888:             my ($currdef,$newdef);
16889:             $newdef = $env{'form.'.$item};
16890:             if ($item eq 'anonsurvey_threshold') {
16891:                 $currdef = $domconfig{'coursedefaults'}{$item};
16892:                 $newdef =~ s/\D//g;
16893:                 if ($newdef eq '' || $newdef < 1) {
16894:                     $newdef = 1;
16895:                 }
16896:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
16897:             } else {
16898:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
16899:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
16900:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
16901:                 }
16902:                 $newdef =~ s/[^\w.\-]//g;
16903:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
16904:             }
16905:             if ($currdef ne $newdef) {
16906:                 if ($item eq 'anonsurvey_threshold') {
16907:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
16908:                         $changes{$item} = 1;
16909:                     }
16910:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
16911:                     my $setting = $1;
16912:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
16913:                         $changes{$setting} = 1;
16914:                     }
16915:                 }
16916:             }
16917:         }
16918:         my $texengine;
16919:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
16920:             $texengine = $env{'form.texengine'};
16921:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
16922:             if ($currdef eq '') {
16923:                 unless ($texengine eq $Apache::lonnet::deftex) {
16924:                     $changes{'texengine'} = 1;
16925:                 }
16926:             } elsif ($currdef ne $texengine) {
16927:                 $changes{'texengine'} = 1;
16928:             }
16929:         }
16930:         if ($texengine ne '') {
16931:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
16932:         }
16933:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
16934:         my @currclonecode;
16935:         if (ref($currclone) eq 'HASH') {
16936:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
16937:                 @currclonecode = @{$currclone->{'instcode'}};
16938:             }
16939:         }
16940:         my $newclone;
16941:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
16942:             $newclone = $env{'form.canclone'};
16943:         }
16944:         if ($newclone eq 'instcode') {
16945:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
16946:             my (%codedefaults,@code_order,@clonecode);
16947:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
16948:                                                     \@code_order);
16949:             foreach my $item (@code_order) {
16950:                 if (grep(/^\Q$item\E$/,@newcodes)) {
16951:                     push(@clonecode,$item);
16952:                 }
16953:             }
16954:             if (@clonecode) {
16955:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
16956:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
16957:                 if (@diffs) {
16958:                     $changes{'canclone'} = 1;
16959:                 }
16960:             } else {
16961:                 $newclone eq '';
16962:             }
16963:         } elsif ($newclone ne '') {
16964:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
16965:         }
16966:         if ($newclone ne $currclone) {
16967:             $changes{'canclone'} = 1;
16968:         }
16969:         my %credits;
16970:         foreach my $type (@types) {
16971:             unless ($type eq 'community') {
16972:                 $credits{$type} = $env{'form.'.$type.'_credits'};
16973:                 $credits{$type} =~ s/[^\d.]+//g;
16974:             }
16975:         }
16976:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
16977:             ($env{'form.coursecredits'} eq '1')) {
16978:             $changes{'coursecredits'} = 1;
16979:             foreach my $type (keys(%credits)) {
16980:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
16981:             }
16982:         } else {
16983:             if ($env{'form.coursecredits'} eq '1') {
16984:                 foreach my $type (@types) {
16985:                     unless ($type eq 'community') {
16986:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
16987:                             $changes{'coursecredits'} = 1;
16988:                         }
16989:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
16990:                     }
16991:                 }
16992:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
16993:                 foreach my $type (@types) {
16994:                     unless ($type eq 'community') {
16995:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
16996:                             $changes{'coursecredits'} = 1;
16997:                             last;
16998:                         }
16999:                     }
17000:                 }
17001:             }
17002:         }
17003:         if ($env{'form.postsubmit'} eq '1') {
17004:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
17005:             my %currtimeout;
17006:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17007:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
17008:                     $changes{'postsubmit'} = 1;
17009:                 }
17010:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
17011:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
17012:                 }
17013:             } else {
17014:                 $changes{'postsubmit'} = 1;
17015:             }
17016:             foreach my $type (@types) {
17017:                 my $timeout = $env{'form.'.$type.'_timeout'};
17018:                 $timeout =~ s/\D//g;
17019:                 if ($timeout == $staticdefaults{'postsubmit'}) {
17020:                     $timeout = '';
17021:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
17022:                     $timeout = '0';
17023:                 }
17024:                 unless ($timeout eq '') {
17025:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
17026:                 }
17027:                 if (exists($currtimeout{$type})) {
17028:                     if ($timeout ne $currtimeout{$type}) {
17029:                         $changes{'postsubmit'} = 1;
17030:                     }
17031:                 } elsif ($timeout ne '') {
17032:                     $changes{'postsubmit'} = 1;
17033:                 }
17034:             }
17035:         } else {
17036:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
17037:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17038:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
17039:                     $changes{'postsubmit'} = 1;
17040:                 }
17041:             } else {
17042:                 $changes{'postsubmit'} = 1;
17043:             }
17044:         }
17045:     }
17046:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17047:                                              $dom);
17048:     if ($putresult eq 'ok') {
17049:         if (keys(%changes) > 0) {
17050:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17051:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
17052:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
17053:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
17054:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
17055:                     if ($changes{$item}) {
17056:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
17057:                     }
17058:                 }
17059:                 if ($changes{'coursecredits'}) {
17060:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
17061:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
17062:                             $domdefaults{$type.'credits'} =
17063:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
17064:                         }
17065:                     }
17066:                 }
17067:                 if ($changes{'postsubmit'}) {
17068:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17069:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
17070:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
17071:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
17072:                                 $domdefaults{$type.'postsubtimeout'} =
17073:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
17074:                             }
17075:                         }
17076:                     }
17077:                 }
17078:                 if ($changes{'uploadquota'}) {
17079:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
17080:                         foreach my $type (@types) {
17081:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
17082:                         }
17083:                     }
17084:                 }
17085:                 if ($changes{'canclone'}) {
17086:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
17087:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
17088:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
17089:                             if (@clonecodes) {
17090:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
17091:                             }
17092:                         }
17093:                     } else {
17094:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
17095:                     }
17096:                 }
17097:                 my $cachetime = 24*60*60;
17098:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17099:                 if (ref($lastactref) eq 'HASH') {
17100:                     $lastactref->{'domdefaults'} = 1;
17101:                 }
17102:             }
17103:             $resulttext = &mt('Changes made:').'<ul>';
17104:             foreach my $item (sort(keys(%changes))) {
17105:                 if ($item eq 'canuse_pdfforms') {
17106:                     if ($env{'form.'.$item} eq '1') {
17107:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
17108:                     } else {
17109:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
17110:                     }
17111:                 } elsif ($item eq 'uselcmath') {
17112:                     if ($env{'form.'.$item} eq '1') {
17113:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
17114:                     } else {
17115:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
17116:                     }
17117:                 } elsif ($item eq 'usejsme') {
17118:                     if ($env{'form.'.$item} eq '1') {
17119:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
17120:                     } else {
17121:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
17122:                     }
17123:                 } elsif ($item eq 'texengine') {
17124:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
17125:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
17126:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
17127:                     }
17128:                 } elsif ($item eq 'anonsurvey_threshold') {
17129:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
17130:                 } elsif ($item eq 'uploadquota') {
17131:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
17132:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
17133:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
17134:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
17135:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
17136:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
17137:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
17138:                                        '</ul>'.
17139:                                        '</li>';
17140:                     } else {
17141:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
17142:                     }
17143:                 } elsif ($item eq 'mysqltables') {
17144:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
17145:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
17146:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
17147:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
17148:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
17149:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
17150:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
17151:                                        '</ul>'.
17152:                                        '</li>';
17153:                     } else {
17154:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
17155:                     }
17156:                 } elsif ($item eq 'postsubmit') {
17157:                     if ($domdefaults{'postsubmit'} eq 'off') {
17158:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
17159:                     } else {
17160:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
17161:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17162:                             $resulttext .= &mt('durations:').'<ul>';
17163:                             foreach my $type (@types) {
17164:                                 $resulttext .= '<li>';
17165:                                 my $timeout;
17166:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
17167:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
17168:                                 }
17169:                                 my $display;
17170:                                 if ($timeout eq '0') {
17171:                                     $display = &mt('unlimited');
17172:                                 } elsif ($timeout eq '') {
17173:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
17174:                                 } else {
17175:                                     $display = &mt('[quant,_1,second]',$timeout);
17176:                                 }
17177:                                 if ($type eq 'community') {
17178:                                     $resulttext .= &mt('Communities');
17179:                                 } elsif ($type eq 'official') {
17180:                                     $resulttext .= &mt('Official courses');
17181:                                 } elsif ($type eq 'unofficial') {
17182:                                     $resulttext .= &mt('Unofficial courses');
17183:                                 } elsif ($type eq 'textbook') {
17184:                                     $resulttext .= &mt('Textbook courses');
17185:                                 } elsif ($type eq 'placement') {
17186:                                     $resulttext .= &mt('Placement tests');
17187:                                 }
17188:                                 $resulttext .= ' -- '.$display.'</li>';
17189:                             }
17190:                             $resulttext .= '</ul>';
17191:                         }
17192:                         $resulttext .= '</li>';
17193:                     }
17194:                 } elsif ($item eq 'coursecredits') {
17195:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
17196:                         if (($domdefaults{'officialcredits'} eq '') &&
17197:                             ($domdefaults{'unofficialcredits'} eq '') &&
17198:                             ($domdefaults{'textbookcredits'} eq '')) {
17199:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
17200:                         } else {
17201:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
17202:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
17203:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
17204:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
17205:                                            '</ul>'.
17206:                                            '</li>';
17207:                         }
17208:                     } else {
17209:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
17210:                     }
17211:                 } elsif ($item eq 'canclone') {
17212:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
17213:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
17214:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
17215:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
17216:                         }
17217:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
17218:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
17219:                     } else {
17220:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
17221:                     }
17222:                 }
17223:             }
17224:             $resulttext .= '</ul>';
17225:         } else {
17226:             $resulttext = &mt('No changes made to course defaults');
17227:         }
17228:     } else {
17229:         $resulttext = '<span class="LC_error">'.
17230:             &mt('An error occurred: [_1]',$putresult).'</span>';
17231:     }
17232:     return $resulttext;
17233: }
17234: 
17235: sub modify_selfenrollment {
17236:     my ($dom,$lastactref,%domconfig) = @_;
17237:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
17238:     my @types = ('official','unofficial','community','textbook','placement');
17239:     my %titles = &tool_titles();
17240:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
17241:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
17242:     $ordered{'default'} = ['types','registered','approval','limit'];
17243: 
17244:     my (%roles,%shown,%toplevel);
17245:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
17246: 
17247:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
17248:         if ($domconfig{'selfenrollment'} eq '') {
17249:             $domconfig{'selfenrollment'} = {};
17250:         }
17251:     }
17252:     %toplevel = (
17253:                   admin      => 'Configuration Rights',
17254:                   default    => 'Default settings',
17255:                   validation => 'Validation of self-enrollment requests',
17256:                 );
17257:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
17258: 
17259:     if (ref($ordered{'admin'}) eq 'ARRAY') {
17260:         foreach my $item (@{$ordered{'admin'}}) {
17261:             foreach my $type (@types) {
17262:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
17263:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
17264:                 } else {
17265:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
17266:                 }
17267:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
17268:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
17269:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
17270:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
17271:                             push(@{$changes{'admin'}{$type}},$item);
17272:                         }
17273:                     } else {
17274:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
17275:                             push(@{$changes{'admin'}{$type}},$item);
17276:                         }
17277:                     }
17278:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
17279:                     push(@{$changes{'admin'}{$type}},$item);
17280:                 }
17281:             }
17282:         }
17283:     }
17284: 
17285:     foreach my $item (@{$ordered{'default'}}) {
17286:         foreach my $type (@types) {
17287:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
17288:             if ($item eq 'types') {
17289:                 unless (($value eq 'all') || ($value eq 'dom')) {
17290:                     $value = '';
17291:                 }
17292:             } elsif ($item eq 'registered') {
17293:                 unless ($value eq '1') {
17294:                     $value = 0;
17295:                 }
17296:             } elsif ($item eq 'approval') {
17297:                 unless ($value =~ /^[012]$/) {
17298:                     $value = 0;
17299:                 }
17300:             } else {
17301:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
17302:                     $value = 'none';
17303:                 }
17304:             }
17305:             $selfenrollhash{'default'}{$type}{$item} = $value;
17306:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
17307:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
17308:                     if ($selfenrollhash{'default'}{$type}{$item} ne
17309:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
17310:                          push(@{$changes{'default'}{$type}},$item);
17311:                     }
17312:                 } else {
17313:                     push(@{$changes{'default'}{$type}},$item);
17314:                 }
17315:             } else {
17316:                 push(@{$changes{'default'}{$type}},$item);
17317:             }
17318:             if ($item eq 'limit') {
17319:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
17320:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
17321:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
17322:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
17323:                     }
17324:                 } else {
17325:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
17326:                 }
17327:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
17328:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
17329:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
17330:                          push(@{$changes{'default'}{$type}},'cap');
17331:                     }
17332:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
17333:                     push(@{$changes{'default'}{$type}},'cap');
17334:                 }
17335:             }
17336:         }
17337:     }
17338: 
17339:     foreach my $item (@{$itemsref}) {
17340:         if ($item eq 'fields') {
17341:             my @changed;
17342:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
17343:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
17344:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
17345:             }
17346:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
17347:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
17348:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
17349:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
17350:                 } else {
17351:                     @changed = @{$selfenrollhash{'validation'}{$item}};
17352:                 }
17353:             } else {
17354:                 @changed = @{$selfenrollhash{'validation'}{$item}};
17355:             }
17356:             if (@changed) {
17357:                 if ($selfenrollhash{'validation'}{$item}) { 
17358:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
17359:                 } else {
17360:                     $changes{'validation'}{$item} = &mt('None');
17361:                 }
17362:             }
17363:         } else {
17364:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
17365:             if ($item eq 'markup') {
17366:                if ($env{'form.selfenroll_validation_'.$item}) {
17367:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
17368:                }
17369:             }
17370:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
17371:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
17372:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
17373:                 }
17374:             }
17375:         }
17376:     }
17377: 
17378:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
17379:                                              $dom);
17380:     if ($putresult eq 'ok') {
17381:         if (keys(%changes) > 0) {
17382:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17383:             $resulttext = &mt('Changes made:').'<ul>';
17384:             foreach my $key ('admin','default','validation') {
17385:                 if (ref($changes{$key}) eq 'HASH') {
17386:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
17387:                     if ($key eq 'validation') {
17388:                         foreach my $item (@{$itemsref}) {
17389:                             if (exists($changes{$key}{$item})) {
17390:                                 if ($item eq 'markup') {
17391:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
17392:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
17393:                                 } else {  
17394:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
17395:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
17396:                                 }
17397:                             }
17398:                         }
17399:                     } else {
17400:                         foreach my $type (@types) {
17401:                             if ($type eq 'community') {
17402:                                 $roles{'1'} = &mt('Community personnel');
17403:                             } else {
17404:                                 $roles{'1'} = &mt('Course personnel');
17405:                             }
17406:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
17407:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
17408:                                     if ($key eq 'admin') {
17409:                                         my @mgrdc = ();
17410:                                         if (ref($ordered{$key}) eq 'ARRAY') {
17411:                                             foreach my $item (@{$ordered{'admin'}}) {
17412:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
17413:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
17414:                                                         push(@mgrdc,$item);
17415:                                                     }
17416:                                                 }
17417:                                             }
17418:                                             if (@mgrdc) {
17419:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
17420:                                             } else {
17421:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
17422:                                             }
17423:                                         }
17424:                                     } else {
17425:                                         if (ref($ordered{$key}) eq 'ARRAY') {
17426:                                             foreach my $item (@{$ordered{$key}}) {
17427:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
17428:                                                     $domdefaults{$type.'selfenroll'.$item} =
17429:                                                         $selfenrollhash{$key}{$type}{$item};
17430:                                                 }
17431:                                             }
17432:                                         }
17433:                                     }
17434:                                 }
17435:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
17436:                                 foreach my $item (@{$ordered{$key}}) {
17437:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
17438:                                         $resulttext .= '<li>';
17439:                                         if ($key eq 'admin') {
17440:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
17441:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
17442:                                         } else {
17443:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
17444:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
17445:                                         }
17446:                                         $resulttext .= '</li>';
17447:                                     }
17448:                                 }
17449:                                 $resulttext .= '</ul></li>';
17450:                             }
17451:                         }
17452:                         $resulttext .= '</ul></li>'; 
17453:                     }
17454:                 }
17455:             }
17456:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
17457:                 my $cachetime = 24*60*60;
17458:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17459:                 if (ref($lastactref) eq 'HASH') {
17460:                     $lastactref->{'domdefaults'} = 1;
17461:                 }
17462:             }
17463:             $resulttext .= '</ul>';
17464:         } else {
17465:             $resulttext = &mt('No changes made to self-enrollment settings');
17466:         }
17467:     } else {
17468:         $resulttext = '<span class="LC_error">'.
17469:             &mt('An error occurred: [_1]',$putresult).'</span>';
17470:     }
17471:     return $resulttext;
17472: }
17473: 
17474: sub modify_usersessions {
17475:     my ($dom,$lastactref,%domconfig) = @_;
17476:     my @hostingtypes = ('version','excludedomain','includedomain');
17477:     my @offloadtypes = ('primary','default');
17478:     my %types = (
17479:                   remote => \@hostingtypes,
17480:                   hosted => \@hostingtypes,
17481:                   spares => \@offloadtypes,
17482:                 );
17483:     my @prefixes = ('remote','hosted','spares');
17484:     my @lcversions = &Apache::lonnet::all_loncaparevs();
17485:     my (%by_ip,%by_location,@intdoms,@instdoms);
17486:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17487:     my @locations = sort(keys(%by_location));
17488:     my (%defaultshash,%changes);
17489:     foreach my $prefix (@prefixes) {
17490:         $defaultshash{'usersessions'}{$prefix} = {};
17491:     }
17492:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17493:     my $resulttext;
17494:     my %iphost = &Apache::lonnet::get_iphost();
17495:     foreach my $prefix (@prefixes) {
17496:         next if ($prefix eq 'spares');
17497:         foreach my $type (@{$types{$prefix}}) {
17498:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
17499:             if ($type eq 'version') {
17500:                 my $value = $env{'form.'.$prefix.'_'.$type};
17501:                 my $okvalue;
17502:                 if ($value ne '') {
17503:                     if (grep(/^\Q$value\E$/,@lcversions)) {
17504:                         $okvalue = $value;
17505:                     }
17506:                 }
17507:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
17508:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
17509:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
17510:                             if ($inuse == 0) {
17511:                                 $changes{$prefix}{$type} = 1;
17512:                             } else {
17513:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
17514:                                     $changes{$prefix}{$type} = 1;
17515:                                 }
17516:                                 if ($okvalue ne '') {
17517:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
17518:                                 } 
17519:                             }
17520:                         } else {
17521:                             if (($inuse == 1) && ($okvalue ne '')) {
17522:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
17523:                                 $changes{$prefix}{$type} = 1;
17524:                             }
17525:                         }
17526:                     } else {
17527:                         if (($inuse == 1) && ($okvalue ne '')) {
17528:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
17529:                             $changes{$prefix}{$type} = 1;
17530:                         }
17531:                     }
17532:                 } else {
17533:                     if (($inuse == 1) && ($okvalue ne '')) {
17534:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
17535:                         $changes{$prefix}{$type} = 1;
17536:                     }
17537:                 }
17538:             } else {
17539:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
17540:                 my @okvals;
17541:                 foreach my $val (@vals) {
17542:                     if ($val =~ /:/) {
17543:                         my @items = split(/:/,$val);
17544:                         foreach my $item (@items) {
17545:                             if (ref($by_location{$item}) eq 'ARRAY') {
17546:                                 push(@okvals,$item);
17547:                             }
17548:                         }
17549:                     } else {
17550:                         if (ref($by_location{$val}) eq 'ARRAY') {
17551:                             push(@okvals,$val);
17552:                         }
17553:                     }
17554:                 }
17555:                 @okvals = sort(@okvals);
17556:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
17557:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
17558:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
17559:                             if ($inuse == 0) {
17560:                                 $changes{$prefix}{$type} = 1; 
17561:                             } else {
17562:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
17563:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
17564:                                 if (@changed > 0) {
17565:                                     $changes{$prefix}{$type} = 1;
17566:                                 }
17567:                             }
17568:                         } else {
17569:                             if ($inuse == 1) {
17570:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
17571:                                 $changes{$prefix}{$type} = 1;
17572:                             }
17573:                         } 
17574:                     } else {
17575:                         if ($inuse == 1) {
17576:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
17577:                             $changes{$prefix}{$type} = 1;
17578:                         }
17579:                     }
17580:                 } else {
17581:                     if ($inuse == 1) {
17582:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
17583:                         $changes{$prefix}{$type} = 1;
17584:                     }
17585:                 }
17586:             }
17587:         }
17588:     }
17589: 
17590:     my @alldoms = &Apache::lonnet::all_domains();
17591:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17592:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
17593:     my $savespares;
17594: 
17595:     foreach my $lonhost (sort(keys(%servers))) {
17596:         my $serverhomeID =
17597:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
17598:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
17599:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
17600:         my %spareschg;
17601:         foreach my $type (@{$types{'spares'}}) {
17602:             my @okspares;
17603:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
17604:             foreach my $server (@checked) {
17605:                 if (&Apache::lonnet::hostname($server) ne '') {
17606:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
17607:                         unless (grep(/^\Q$server\E$/,@okspares)) {
17608:                             push(@okspares,$server);
17609:                         }
17610:                     }
17611:                 }
17612:             }
17613:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
17614:             my $newspare;
17615:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
17616:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
17617:                     $newspare = $new;
17618:                 }
17619:             }
17620:             my @spares;
17621:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
17622:                 @spares = sort(@okspares,$newspare);
17623:             } else {
17624:                 @spares = sort(@okspares);
17625:             }
17626:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
17627:             if (ref($spareid{$lonhost}) eq 'HASH') {
17628:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
17629:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
17630:                     if (@diffs > 0) {
17631:                         $spareschg{$type} = 1;
17632:                     }
17633:                 }
17634:             }
17635:         }
17636:         if (keys(%spareschg) > 0) {
17637:             $changes{'spares'}{$lonhost} = \%spareschg;
17638:         }
17639:     }
17640:     $defaultshash{'usersessions'}{'offloadnow'} = {};
17641:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
17642:     my @okoffload;
17643:     if (@offloadnow) {
17644:         foreach my $server (@offloadnow) {
17645:             if (&Apache::lonnet::hostname($server) ne '') {
17646:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
17647:                     push(@okoffload,$server);
17648:                 }
17649:             }
17650:         }
17651:         if (@okoffload) {
17652:             foreach my $lonhost (@okoffload) {
17653:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
17654:             }
17655:         }
17656:     }
17657:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
17658:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
17659:             if (ref($changes{'spares'}) eq 'HASH') {
17660:                 if (keys(%{$changes{'spares'}}) > 0) {
17661:                     $savespares = 1;
17662:                 }
17663:             }
17664:         } else {
17665:             $savespares = 1;
17666:         }
17667:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
17668:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
17669:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
17670:                     $changes{'offloadnow'} = 1;
17671:                     last;
17672:                 }
17673:             }
17674:             unless ($changes{'offloadnow'}) {
17675:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
17676:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
17677:                         $changes{'offloadnow'} = 1;
17678:                         last;
17679:                     }
17680:                 }
17681:             }
17682:         } elsif (@okoffload) {
17683:             $changes{'offloadnow'} = 1;
17684:         }
17685:     } elsif (@okoffload) {
17686:         $changes{'offloadnow'} = 1;
17687:     }
17688:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
17689:     if ((keys(%changes) > 0) || ($savespares)) {
17690:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17691:                                                  $dom);
17692:         if ($putresult eq 'ok') {
17693:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
17694:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
17695:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
17696:                 }
17697:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
17698:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
17699:                 }
17700:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
17701:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
17702:                 }
17703:             }
17704:             my $cachetime = 24*60*60;
17705:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17706:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
17707:             if (ref($lastactref) eq 'HASH') {
17708:                 $lastactref->{'domdefaults'} = 1;
17709:                 $lastactref->{'usersessions'} = 1;
17710:             }
17711:             if (keys(%changes) > 0) {
17712:                 my %lt = &usersession_titles();
17713:                 $resulttext = &mt('Changes made:').'<ul>';
17714:                 foreach my $prefix (@prefixes) {
17715:                     if (ref($changes{$prefix}) eq 'HASH') {
17716:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
17717:                         if ($prefix eq 'spares') {
17718:                             if (ref($changes{$prefix}) eq 'HASH') {
17719:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
17720:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
17721:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
17722:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
17723:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
17724:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
17725:                                         foreach my $type (@{$types{$prefix}}) {
17726:                                             if ($changes{$prefix}{$lonhost}{$type}) {
17727:                                                 my $offloadto = &mt('None');
17728:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
17729:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
17730:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
17731:                                                     }
17732:                                                 }
17733:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
17734:                                             }
17735:                                         }
17736:                                     }
17737:                                     $resulttext .= '</li>';
17738:                                 }
17739:                             }
17740:                         } else {
17741:                             foreach my $type (@{$types{$prefix}}) {
17742:                                 if (defined($changes{$prefix}{$type})) {
17743:                                     my ($newvalue,$notinuse);
17744:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
17745:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
17746:                                             if ($type eq 'version') {
17747:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
17748:                                             } else {
17749:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
17750:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
17751:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
17752:                                                     }
17753:                                                 } else {
17754:                                                     $notinuse = 1;
17755:                                                 }
17756:                                             }
17757:                                         }
17758:                                     }
17759:                                     if ($newvalue eq '') {
17760:                                         if ($type eq 'version') {
17761:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
17762:                                         } elsif ($notinuse) {
17763:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
17764:                                         } else {
17765:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
17766:                                         }
17767:                                     } else {
17768:                                         if ($type eq 'version') {
17769:                                             $newvalue .= ' '.&mt('(or later)');
17770:                                         }
17771:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
17772:                                     }
17773:                                 }
17774:                             }
17775:                         }
17776:                         $resulttext .= '</ul>';
17777:                     }
17778:                 }
17779:                 if ($changes{'offloadnow'}) {
17780:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
17781:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
17782:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
17783:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
17784:                                 $resulttext .= '<li>'.$lonhost.'</li>';
17785:                             }
17786:                             $resulttext .= '</ul>';
17787:                         } else {
17788:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
17789:                         }
17790:                     } else {
17791:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
17792:                     }
17793:                 }
17794:                 $resulttext .= '</ul>';
17795:             } else {
17796:                 $resulttext = $nochgmsg;
17797:             }
17798:         } else {
17799:             $resulttext = '<span class="LC_error">'.
17800:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17801:         }
17802:     } else {
17803:         $resulttext = $nochgmsg;
17804:     }
17805:     return $resulttext;
17806: }
17807: 
17808: sub modify_ssl {
17809:     my ($dom,$lastactref,%domconfig) = @_;
17810:     my (%by_ip,%by_location,@intdoms,@instdoms);
17811:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17812:     my @locations = sort(keys(%by_location));
17813:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17814:     my (%defaultshash,%changes);
17815:     my $action = 'ssl';
17816:     my @prefixes = ('connto','connfrom','replication');
17817:     foreach my $prefix (@prefixes) {
17818:         $defaultshash{$action}{$prefix} = {};
17819:     }
17820:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17821:     my $resulttext;
17822:     my %iphost = &Apache::lonnet::get_iphost();
17823:     my @reptypes = ('certreq','nocertreq');
17824:     my @connecttypes = ('dom','intdom','other');
17825:     my %types = (
17826:                   connto      => \@connecttypes,
17827:                   connfrom    => \@connecttypes,
17828:                   replication => \@reptypes,
17829:                 );
17830:     foreach my $prefix (sort(keys(%types))) {
17831:         foreach my $type (@{$types{$prefix}}) {
17832:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
17833:                 my $value = 'yes';
17834:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
17835:                     $value = $env{'form.'.$prefix.'_'.$type};
17836:                 }
17837:                 if (ref($domconfig{$action}) eq 'HASH') {
17838:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
17839:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
17840:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
17841:                                 $changes{$prefix}{$type} = 1;
17842:                             }
17843:                             $defaultshash{$action}{$prefix}{$type} = $value;
17844:                         } else {
17845:                             $defaultshash{$action}{$prefix}{$type} = $value;
17846:                             $changes{$prefix}{$type} = 1;
17847:                         }
17848:                     } else {
17849:                         $defaultshash{$action}{$prefix}{$type} = $value;
17850:                         $changes{$prefix}{$type} = 1;
17851:                     }
17852:                 } else {
17853:                     $defaultshash{$action}{$prefix}{$type} = $value;
17854:                     $changes{$prefix}{$type} = 1;
17855:                 }
17856:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
17857:                     delete($changes{$prefix}{$type});
17858:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
17859:                     delete($changes{$prefix}{$type});
17860:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
17861:                     delete($changes{$prefix}{$type});
17862:                 }
17863:             } elsif ($prefix eq 'replication') {
17864:                 if (@locations > 0) {
17865:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
17866:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
17867:                     my @okvals;
17868:                     foreach my $val (@vals) {
17869:                         if ($val =~ /:/) {
17870:                             my @items = split(/:/,$val);
17871:                             foreach my $item (@items) {
17872:                                 if (ref($by_location{$item}) eq 'ARRAY') {
17873:                                     push(@okvals,$item);
17874:                                 }
17875:                             }
17876:                         } else {
17877:                             if (ref($by_location{$val}) eq 'ARRAY') {
17878:                                 push(@okvals,$val);
17879:                             }
17880:                         }
17881:                     }
17882:                     @okvals = sort(@okvals);
17883:                     if (ref($domconfig{$action}) eq 'HASH') {
17884:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
17885:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
17886:                                 if ($inuse == 0) {
17887:                                     $changes{$prefix}{$type} = 1;
17888:                                 } else {
17889:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
17890:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
17891:                                     if (@changed > 0) {
17892:                                         $changes{$prefix}{$type} = 1;
17893:                                     }
17894:                                 }
17895:                             } else {
17896:                                 if ($inuse == 1) {
17897:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
17898:                                     $changes{$prefix}{$type} = 1;
17899:                                 }
17900:                             }
17901:                         } else {
17902:                             if ($inuse == 1) {
17903:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
17904:                                 $changes{$prefix}{$type} = 1;
17905:                             }
17906:                         }
17907:                     } else {
17908:                         if ($inuse == 1) {
17909:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
17910:                             $changes{$prefix}{$type} = 1;
17911:                         }
17912:                     }
17913:                 }
17914:             }
17915:         }
17916:     }
17917:     if (keys(%changes)) {
17918:         foreach my $prefix (keys(%changes)) {
17919:             if (ref($changes{$prefix}) eq 'HASH') {
17920:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
17921:                     delete($changes{$prefix});
17922:                 }
17923:             } else {
17924:                 delete($changes{$prefix});
17925:             }
17926:         }
17927:     }
17928:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
17929:     if (keys(%changes) > 0) {
17930:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17931:                                                  $dom);
17932:         if ($putresult eq 'ok') {
17933:             if (ref($defaultshash{$action}) eq 'HASH') {
17934:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
17935:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
17936:                 }
17937:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
17938:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
17939:                 }
17940:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
17941:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
17942:                 }
17943:             }
17944:             my $cachetime = 24*60*60;
17945:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17946:             if (ref($lastactref) eq 'HASH') {
17947:                 $lastactref->{'domdefaults'} = 1;
17948:             }
17949:             if (keys(%changes) > 0) {
17950:                 my %titles = &ssl_titles();
17951:                 $resulttext = &mt('Changes made:').'<ul>';
17952:                 foreach my $prefix (@prefixes) {
17953:                     if (ref($changes{$prefix}) eq 'HASH') {
17954:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
17955:                         foreach my $type (@{$types{$prefix}}) {
17956:                             if (defined($changes{$prefix}{$type})) {
17957:                                 my ($newvalue,$notinuse);
17958:                                 if (ref($defaultshash{$action}) eq 'HASH') {
17959:                                     if (ref($defaultshash{$action}{$prefix})) {
17960:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
17961:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
17962:                                         } else {
17963:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
17964:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
17965:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
17966:                                                 }
17967:                                             } else {
17968:                                                 $notinuse = 1;
17969:                                             }
17970:                                         }
17971:                                     }
17972:                                     if ($notinuse) {
17973:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
17974:                                     } elsif ($newvalue eq '') {
17975:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
17976:                                     } else {
17977:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
17978:                                     }
17979:                                 }
17980:                             }
17981:                         }
17982:                         $resulttext .= '</ul>';
17983:                     }
17984:                 }
17985:             } else {
17986:                 $resulttext = $nochgmsg;
17987:             }
17988:         } else {
17989:             $resulttext = '<span class="LC_error">'.
17990:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17991:         }
17992:     } else {
17993:         $resulttext = $nochgmsg;
17994:     }
17995:     return $resulttext;
17996: }
17997: 
17998: sub modify_trust {
17999:     my ($dom,$lastactref,%domconfig) = @_;
18000:     my (%by_ip,%by_location,@intdoms,@instdoms);
18001:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
18002:     my @locations = sort(keys(%by_location));
18003:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
18004:     my @types = ('exc','inc');
18005:     my (%defaultshash,%changes);
18006:     foreach my $prefix (@prefixes) {
18007:         $defaultshash{'trust'}{$prefix} = {};
18008:     }
18009:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18010:     my $resulttext;
18011:     foreach my $prefix (@prefixes) {
18012:         foreach my $type (@types) {
18013:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
18014:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
18015:             my @okvals;
18016:             foreach my $val (@vals) {
18017:                 if ($val =~ /:/) {
18018:                     my @items = split(/:/,$val);
18019:                     foreach my $item (@items) {
18020:                         if (ref($by_location{$item}) eq 'ARRAY') {
18021:                             push(@okvals,$item);
18022:                         }
18023:                     }
18024:                 } else {
18025:                     if (ref($by_location{$val}) eq 'ARRAY') {
18026:                         push(@okvals,$val);
18027:                     }
18028:                 }
18029:             }
18030:             @okvals = sort(@okvals);
18031:             if (ref($domconfig{'trust'}) eq 'HASH') {
18032:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
18033:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
18034:                         if ($inuse == 0) {
18035:                             $changes{$prefix}{$type} = 1;
18036:                         } else {
18037:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18038:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
18039:                             if (@changed > 0) {
18040:                                 $changes{$prefix}{$type} = 1;
18041:                             }
18042:                         }
18043:                     } else {
18044:                         if ($inuse == 1) {
18045:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18046:                             $changes{$prefix}{$type} = 1;
18047:                         }
18048:                     }
18049:                 } else {
18050:                     if ($inuse == 1) {
18051:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18052:                         $changes{$prefix}{$type} = 1;
18053:                     }
18054:                 }
18055:             } else {
18056:                 if ($inuse == 1) {
18057:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18058:                     $changes{$prefix}{$type} = 1;
18059:                 }
18060:             }
18061:         }
18062:     }
18063:     my $nochgmsg = &mt('No changes made to trust settings.');
18064:     if (keys(%changes) > 0) {
18065:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
18066:                                                  $dom);
18067:         if ($putresult eq 'ok') {
18068:             if (ref($defaultshash{'trust'}) eq 'HASH') {
18069:                 foreach my $prefix (@prefixes) {
18070:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
18071:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
18072:                     }
18073:                 }
18074:             }
18075:             my $cachetime = 24*60*60;
18076:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18077:             if (ref($lastactref) eq 'HASH') {
18078:                 $lastactref->{'domdefaults'} = 1;
18079:             }
18080:             if (keys(%changes) > 0) {
18081:                 my %lt = &trust_titles();
18082:                 $resulttext = &mt('Changes made:').'<ul>';
18083:                 foreach my $prefix (@prefixes) {
18084:                     if (ref($changes{$prefix}) eq 'HASH') {
18085:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
18086:                         foreach my $type (@types) {
18087:                             if (defined($changes{$prefix}{$type})) {
18088:                                 my ($newvalue,$notinuse);
18089:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
18090:                                     if (ref($defaultshash{'trust'}{$prefix})) {
18091:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
18092:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
18093:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
18094:                                             }
18095:                                         } else {
18096:                                             $notinuse = 1;
18097:                                         }
18098:                                     }
18099:                                 }
18100:                                 if ($notinuse) {
18101:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
18102:                                 } elsif ($newvalue eq '') {
18103:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
18104:                                 } else {
18105:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
18106:                                 }
18107:                             }
18108:                         }
18109:                         $resulttext .= '</ul>';
18110:                     }
18111:                 }
18112:                 $resulttext .= '</ul>';
18113:             } else {
18114:                 $resulttext = $nochgmsg;
18115:             }
18116:         } else {
18117:             $resulttext = '<span class="LC_error">'.
18118:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18119:         }
18120:     } else {
18121:         $resulttext = $nochgmsg;
18122:     }
18123:     return $resulttext;
18124: }
18125: 
18126: sub modify_loadbalancing {
18127:     my ($dom,%domconfig) = @_;
18128:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
18129:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
18130:     my ($othertitle,$usertypes,$types) =
18131:         &Apache::loncommon::sorted_inst_types($dom);
18132:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
18133:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
18134:     my @sparestypes = ('primary','default');
18135:     my %typetitles = &sparestype_titles();
18136:     my $resulttext;
18137:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
18138:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
18139:         %existing = %{$domconfig{'loadbalancing'}};
18140:     }
18141:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
18142:                               \%currtargets,\%currrules,\%currcookies);
18143:     my ($saveloadbalancing,%defaultshash,%changes);
18144:     my ($alltypes,$othertypes,$titles) =
18145:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
18146:     my %ruletitles = &offloadtype_text();
18147:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
18148:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
18149:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
18150:         if ($balancer eq '') {
18151:             next;
18152:         }
18153:         if (!exists($servers{$balancer})) {
18154:             if (exists($currbalancer{$balancer})) {
18155:                 push(@{$changes{'delete'}},$balancer);
18156:             }
18157:             next;
18158:         }
18159:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
18160:             push(@{$changes{'delete'}},$balancer);
18161:             next;
18162:         }
18163:         if (!exists($currbalancer{$balancer})) {
18164:             push(@{$changes{'add'}},$balancer);
18165:         }
18166:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
18167:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
18168:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
18169:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
18170:             $saveloadbalancing = 1;
18171:         }
18172:         foreach my $sparetype (@sparestypes) {
18173:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
18174:             my @offloadto;
18175:             foreach my $target (@targets) {
18176:                 if (($servers{$target}) && ($target ne $balancer)) {
18177:                     if ($sparetype eq 'default') {
18178:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
18179:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
18180:                         }
18181:                     }
18182:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
18183:                         push(@offloadto,$target);
18184:                     }
18185:                 }
18186:             }
18187:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
18188:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
18189:                     push(@offloadto,$balancer);
18190:                 }
18191:             }
18192:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
18193:         }
18194:         if ($env{'form.loadbalancing_cookie_'.$i}) {
18195:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
18196:             if (exists($currbalancer{$balancer})) { 
18197:                 unless ($currcookies{$balancer}) {
18198:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
18199:                 }
18200:             }
18201:         } elsif (exists($currbalancer{$balancer})) {
18202:             if ($currcookies{$balancer}) {
18203:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
18204:             }
18205:         }
18206:         if (ref($currtargets{$balancer}) eq 'HASH') {
18207:             foreach my $sparetype (@sparestypes) {
18208:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
18209:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
18210:                     if (@targetdiffs > 0) {
18211:                         $changes{'curr'}{$balancer}{'targets'} = 1;
18212:                     }
18213:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18214:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
18215:                         $changes{'curr'}{$balancer}{'targets'} = 1;
18216:                     }
18217:                 }
18218:             }
18219:         } else {
18220:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
18221:                 foreach my $sparetype (@sparestypes) {
18222:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18223:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
18224:                             $changes{'curr'}{$balancer}{'targets'} = 1;
18225:                         }
18226:                     }
18227:                 }
18228:             }
18229:         }
18230:         my $ishomedom;
18231:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
18232:             $ishomedom = 1;
18233:         }
18234:         if (ref($alltypes) eq 'ARRAY') {
18235:             foreach my $type (@{$alltypes}) {
18236:                 my $rule;
18237:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
18238:                          (!$ishomedom)) {
18239:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
18240:                 }
18241:                 if ($rule eq 'specific') {
18242:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
18243:                     if (exists($servers{$specifiedhost})) {
18244:                         $rule = $specifiedhost;
18245:                     }
18246:                 }
18247:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
18248:                 if (ref($currrules{$balancer}) eq 'HASH') {
18249:                     if ($rule ne $currrules{$balancer}{$type}) {
18250:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
18251:                     }
18252:                 } elsif ($rule ne '') {
18253:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
18254:                 }
18255:             }
18256:         }
18257:     }
18258:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
18259:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
18260:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
18261:             $defaultshash{'loadbalancing'} = {};
18262:         }
18263:         my $putresult = &Apache::lonnet::put_dom('configuration',
18264:                                                  \%defaultshash,$dom);
18265:         if ($putresult eq 'ok') {
18266:             if (keys(%changes) > 0) {
18267:                 my %toupdate;
18268:                 if (ref($changes{'delete'}) eq 'ARRAY') {
18269:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
18270:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
18271:                         $toupdate{$balancer} = 1;
18272:                     }
18273:                 }
18274:                 if (ref($changes{'add'}) eq 'ARRAY') {
18275:                     foreach my $balancer (sort(@{$changes{'add'}})) {
18276:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
18277:                         $toupdate{$balancer} = 1;
18278:                     }
18279:                 }
18280:                 if (ref($changes{'curr'}) eq 'HASH') {
18281:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
18282:                         $toupdate{$balancer} = 1;
18283:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
18284:                             if ($changes{'curr'}{$balancer}{'targets'}) {
18285:                                 my %offloadstr;
18286:                                 foreach my $sparetype (@sparestypes) {
18287:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18288:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
18289:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
18290:                                         }
18291:                                     }
18292:                                 }
18293:                                 if (keys(%offloadstr) == 0) {
18294:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
18295:                                 } else {
18296:                                     my $showoffload;
18297:                                     foreach my $sparetype (@sparestypes) {
18298:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
18299:                                         if (defined($offloadstr{$sparetype})) {
18300:                                             $showoffload .= $offloadstr{$sparetype};
18301:                                         } else {
18302:                                             $showoffload .= &mt('None');
18303:                                         }
18304:                                         $showoffload .= ('&nbsp;'x3);
18305:                                     }
18306:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
18307:                                 }
18308:                             }
18309:                         }
18310:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
18311:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
18312:                                 foreach my $type (@{$alltypes}) {
18313:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
18314:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
18315:                                         my $balancetext;
18316:                                         if ($rule eq '') {
18317:                                             $balancetext =  $ruletitles{'default'};
18318:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
18319:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
18320:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
18321:                                                 foreach my $sparetype (@sparestypes) {
18322:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18323:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
18324:                                                     }
18325:                                                 }
18326:                                                 foreach my $item (@{$alltypes}) {
18327:                                                     next if ($item =~  /^_LC_ipchange/);
18328:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
18329:                                                     if ($hasrule eq 'homeserver') {
18330:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
18331:                                                     } else {
18332:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
18333:                                                             if ($servers{$hasrule}) {
18334:                                                                 $toupdate{$hasrule} = 1;
18335:                                                             }
18336:                                                         }
18337:                                                     }
18338:                                                 }
18339:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
18340:                                                     $balancetext =  $ruletitles{$rule};
18341:                                                 } else {
18342:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
18343:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
18344:                                                     if ($receiver) {
18345:                                                         $toupdate{$receiver};
18346:                                                     }
18347:                                                 }
18348:                                             } else {
18349:                                                 $balancetext =  $ruletitles{$rule};
18350:                                             }
18351:                                         } else {
18352:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
18353:                                         }
18354:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
18355:                                     }
18356:                                 }
18357:                             }
18358:                         }
18359:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
18360:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
18361:                                                       $balancer).'</li>'; 
18362:                         }
18363:                         if (keys(%toupdate)) {
18364:                             my %thismachine;
18365:                             my $updatedhere;
18366:                             my $cachetime = 60*60*24;
18367:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
18368:                             foreach my $lonhost (keys(%toupdate)) {
18369:                                 if ($thismachine{$lonhost}) {
18370:                                     unless ($updatedhere) {
18371:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
18372:                                                                       $defaultshash{'loadbalancing'},
18373:                                                                       $cachetime);
18374:                                         $updatedhere = 1;
18375:                                     }
18376:                                 } else {
18377:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
18378:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
18379:                                 }
18380:                             }
18381:                         }
18382:                     }
18383:                 }
18384:                 if ($resulttext ne '') {
18385:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
18386:                 } else {
18387:                     $resulttext = $nochgmsg;
18388:                 }
18389:             } else {
18390:                 $resulttext = $nochgmsg;
18391:             }
18392:         } else {
18393:             $resulttext = '<span class="LC_error">'.
18394:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18395:         }
18396:     } else {
18397:         $resulttext = $nochgmsg;
18398:     }
18399:     return $resulttext;
18400: }
18401: 
18402: sub recurse_check {
18403:     my ($chkcats,$categories,$depth,$name) = @_;
18404:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
18405:         my $chg = 0;
18406:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
18407:             my $category = $chkcats->[$depth]{$name}[$j];
18408:             my $item;
18409:             if ($category eq '') {
18410:                 $chg ++;
18411:             } else {
18412:                 my $deeper = $depth + 1;
18413:                 $item = &escape($category).':'.&escape($name).':'.$depth;
18414:                 if ($chg) {
18415:                     $categories->{$item} -= $chg;
18416:                 }
18417:                 &recurse_check($chkcats,$categories,$deeper,$category);
18418:                 $deeper --;
18419:             }
18420:         }
18421:     }
18422:     return;
18423: }
18424: 
18425: sub recurse_cat_deletes {
18426:     my ($item,$coursecategories,$deletions) = @_;
18427:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
18428:     my $subdepth = $depth + 1;
18429:     if (ref($coursecategories) eq 'HASH') {
18430:         foreach my $subitem (keys(%{$coursecategories})) {
18431:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
18432:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
18433:                 delete($coursecategories->{$subitem});
18434:                 $deletions->{$subitem} = 1;
18435:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
18436:             }
18437:         }
18438:     }
18439:     return;
18440: }
18441: 
18442: sub active_dc_picker {
18443:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
18444:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
18445:     my @domcoord = keys(%domcoords);
18446:     if (keys(%currhash)) {
18447:         foreach my $dc (keys(%currhash)) {
18448:             unless (exists($domcoords{$dc})) {
18449:                 push(@domcoord,$dc);
18450:             }
18451:         }
18452:     }
18453:     @domcoord = sort(@domcoord);
18454:     my $numdcs = scalar(@domcoord);
18455:     my $rows = 0;
18456:     my $table;
18457:     if ($numdcs > 1) {
18458:         $table = '<table>';
18459:         for (my $i=0; $i<@domcoord; $i++) {
18460:             my $rem = $i%($numinrow);
18461:             if ($rem == 0) {
18462:                 if ($i > 0) {
18463:                     $table .= '</tr>';
18464:                 }
18465:                 $table .= '<tr>';
18466:                 $rows ++;
18467:             }
18468:             my $check = '';
18469:             if ($inputtype eq 'radio') {
18470:                 if (keys(%currhash) == 0) {
18471:                     if (!$i) {
18472:                         $check = ' checked="checked"';
18473:                     }
18474:                 } elsif (exists($currhash{$domcoord[$i]})) {
18475:                     $check = ' checked="checked"';
18476:                 }
18477:             } else {
18478:                 if (exists($currhash{$domcoord[$i]})) {
18479:                     $check = ' checked="checked"';
18480:                 }
18481:             }
18482:             if ($i == @domcoord - 1) {
18483:                 my $colsleft = $numinrow - $rem;
18484:                 if ($colsleft > 1) {
18485:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
18486:                 } else {
18487:                     $table .= '<td class="LC_left_item">';
18488:                 }
18489:             } else {
18490:                 $table .= '<td class="LC_left_item">';
18491:             }
18492:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
18493:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
18494:             $table .= '<span class="LC_nobreak"><label>'.
18495:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
18496:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
18497:             if ($user ne $dcname.':'.$dcdom) {
18498:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
18499:             }
18500:             $table .= '</label></span></td>';
18501:         }
18502:         $table .= '</tr></table>';
18503:     } elsif ($numdcs == 1) {
18504:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
18505:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
18506:         if ($inputtype eq 'radio') {
18507:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
18508:             if ($user ne $dcname.':'.$dcdom) {
18509:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
18510:             }
18511:         } else {
18512:             my $check;
18513:             if (exists($currhash{$domcoord[0]})) {
18514:                 $check = ' checked="checked"';
18515:             }
18516:             $table = '<span class="LC_nobreak"><label>'.
18517:                      '<input type="checkbox" name="'.$name.'" '.
18518:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
18519:             if ($user ne $dcname.':'.$dcdom) {
18520:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
18521:             }
18522:             $table .= '</label></span>';
18523:             $rows ++;
18524:         }
18525:     }
18526:     return ($numdcs,$table,$rows);
18527: }
18528: 
18529: sub usersession_titles {
18530:     return &Apache::lonlocal::texthash(
18531:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
18532:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
18533:                spares => 'Servers offloaded to, when busy',
18534:                version => 'LON-CAPA version requirement',
18535:                excludedomain => 'Allow all, but exclude specific domains',
18536:                includedomain => 'Deny all, but include specific domains',
18537:                primary => 'Primary (checked first)',
18538:                default => 'Default',
18539:            );
18540: }
18541: 
18542: sub id_for_thisdom {
18543:     my (%servers) = @_;
18544:     my %altids;
18545:     foreach my $server (keys(%servers)) {
18546:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
18547:         if ($serverhome ne $server) {
18548:             $altids{$serverhome} = $server;
18549:         }
18550:     }
18551:     return %altids;
18552: }
18553: 
18554: sub count_servers {
18555:     my ($currbalancer,%servers) = @_;
18556:     my (@spares,$numspares);
18557:     foreach my $lonhost (sort(keys(%servers))) {
18558:         next if ($currbalancer eq $lonhost);
18559:         push(@spares,$lonhost);
18560:     }
18561:     if ($currbalancer) {
18562:         $numspares = scalar(@spares);
18563:     } else {
18564:         $numspares = scalar(@spares) - 1;
18565:     }
18566:     return ($numspares,@spares);
18567: }
18568: 
18569: sub lonbalance_targets_js {
18570:     my ($dom,$types,$servers,$settings) = @_;
18571:     my $select = &mt('Select');
18572:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
18573:     if (ref($servers) eq 'HASH') {
18574:         $alltargets = join("','",sort(keys(%{$servers})));
18575:         my @homedoms;
18576:         foreach my $server (sort(keys(%{$servers}))) {
18577:             if (&Apache::lonnet::host_domain($server) eq $dom) {
18578:                 push(@homedoms,'1');
18579:             } else {
18580:                 push(@homedoms,'0');
18581:             }
18582:         }
18583:         $allishome = join("','",@homedoms);
18584:     }
18585:     if (ref($types) eq 'ARRAY') {
18586:         if (@{$types} > 0) {
18587:             @alltypes = @{$types};
18588:         }
18589:     }
18590:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
18591:     $allinsttypes = join("','",@alltypes);
18592:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
18593:     if (ref($settings) eq 'HASH') {
18594:         %existing = %{$settings};
18595:     }
18596:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
18597:                               \%currtargets,\%currrules,\%currcookies);
18598:     my $balancers = join("','",sort(keys(%currbalancer)));
18599:     return <<"END";
18600: 
18601: <script type="text/javascript">
18602: // <![CDATA[
18603: 
18604: currBalancers = new Array('$balancers');
18605: 
18606: function toggleTargets(balnum) {
18607:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
18608:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
18609:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
18610:     var prevbalancer = prevhostitem.value;
18611:     var baltotal = document.getElementById('loadbalancing_total').value;
18612:     prevhostitem.value = balancer;
18613:     if (prevbalancer != '') {
18614:         var prevIdx = currBalancers.indexOf(prevbalancer);
18615:         if (prevIdx != -1) {
18616:             currBalancers.splice(prevIdx,1);
18617:         }
18618:     }
18619:     if (balancer == '') {
18620:         hideSpares(balnum);
18621:     } else {
18622:         var currIdx = currBalancers.indexOf(balancer);
18623:         if (currIdx == -1) {
18624:             currBalancers.push(balancer);
18625:         }
18626:         var homedoms = new Array('$allishome');
18627:         var ishomedom = homedoms[lonhostitem.selectedIndex];
18628:         showSpares(balancer,ishomedom,balnum);
18629:     }
18630:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
18631:     return;
18632: }
18633: 
18634: function showSpares(balancer,ishomedom,balnum) {
18635:     var alltargets = new Array('$alltargets');
18636:     var insttypes = new Array('$allinsttypes');
18637:     var offloadtypes = new Array('primary','default');
18638: 
18639:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
18640:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
18641:  
18642:     for (var i=0; i<offloadtypes.length; i++) {
18643:         var count = 0;
18644:         for (var j=0; j<alltargets.length; j++) {
18645:             if (alltargets[j] != balancer) {
18646:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
18647:                 item.value = alltargets[j];
18648:                 item.style.textAlign='left';
18649:                 item.style.textFace='normal';
18650:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
18651:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
18652:                     item.disabled = '';
18653:                 } else {
18654:                     item.disabled = 'disabled';
18655:                     item.checked = false;
18656:                 }
18657:                 count ++;
18658:             }
18659:         }
18660:     }
18661:     for (var k=0; k<insttypes.length; k++) {
18662:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
18663:             if (ishomedom == 1) {
18664:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
18665:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
18666:             } else {
18667:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
18668:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
18669:             }
18670:         } else {
18671:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
18672:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
18673:         }
18674:         if ((insttypes[k] != '_LC_external') && 
18675:             ((insttypes[k] != '_LC_internetdom') ||
18676:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
18677:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
18678:             item.options.length = 0;
18679:             item.options[0] = new Option("","",true,true);
18680:             var idx = 0;
18681:             for (var m=0; m<alltargets.length; m++) {
18682:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
18683:                     idx ++;
18684:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
18685:                 }
18686:             }
18687:         }
18688:     }
18689:     return;
18690: }
18691: 
18692: function hideSpares(balnum) {
18693:     var alltargets = new Array('$alltargets');
18694:     var insttypes = new Array('$allinsttypes');
18695:     var offloadtypes = new Array('primary','default');
18696: 
18697:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
18698:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
18699: 
18700:     var total = alltargets.length - 1;
18701:     for (var i=0; i<offloadtypes; i++) {
18702:         for (var j=0; j<total; j++) {
18703:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
18704:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
18705:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
18706:         }
18707:     }
18708:     for (var k=0; k<insttypes.length; k++) {
18709:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
18710:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
18711:         if (insttypes[k] != '_LC_external') {
18712:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
18713:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
18714:         }
18715:     }
18716:     return;
18717: }
18718: 
18719: function checkOffloads(item,balnum,type) {
18720:     var alltargets = new Array('$alltargets');
18721:     var offloadtypes = new Array('primary','default');
18722:     if (item.checked) {
18723:         var total = alltargets.length - 1;
18724:         var other;
18725:         if (type == offloadtypes[0]) {
18726:             other = offloadtypes[1];
18727:         } else {
18728:             other = offloadtypes[0];
18729:         }
18730:         for (var i=0; i<total; i++) {
18731:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
18732:             if (server == item.value) {
18733:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
18734:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
18735:                 }
18736:             }
18737:         }
18738:     }
18739:     return;
18740: }
18741: 
18742: function singleServerToggle(balnum,type) {
18743:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
18744:     if (offloadtoSelIdx == 0) {
18745:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
18746:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
18747: 
18748:     } else {
18749:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
18750:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
18751:     }
18752:     return;
18753: }
18754: 
18755: function balanceruleChange(formname,balnum,type) {
18756:     if (type == '_LC_external') {
18757:         return;
18758:     }
18759:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
18760:     for (var i=0; i<typesRules.length; i++) {
18761:         if (formname.elements[typesRules[i]].checked) {
18762:             if (formname.elements[typesRules[i]].value != 'specific') {
18763:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
18764:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
18765:             } else {
18766:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
18767:             }
18768:         }
18769:     }
18770:     return;
18771: }
18772: 
18773: function balancerDeleteChange(balnum) {
18774:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
18775:     var baltotal = document.getElementById('loadbalancing_total').value;
18776:     var addtarget;
18777:     var removetarget;
18778:     var action = 'delete';
18779:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
18780:         var lonhost = hostitem.value;
18781:         var currIdx = currBalancers.indexOf(lonhost);
18782:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
18783:             if (currIdx != -1) {
18784:                 currBalancers.splice(currIdx,1);
18785:             }
18786:             addtarget = lonhost;
18787:         } else {
18788:             if (currIdx == -1) {
18789:                 currBalancers.push(lonhost);
18790:             }
18791:             removetarget = lonhost;
18792:             action = 'undelete';
18793:         }
18794:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
18795:     }
18796:     return;
18797: }
18798: 
18799: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
18800:     if (baltotal > 1) {
18801:         var offloadtypes = new Array('primary','default');
18802:         var alltargets = new Array('$alltargets');
18803:         var insttypes = new Array('$allinsttypes');
18804:         for (var i=0; i<baltotal; i++) {
18805:             if (i != balnum) {
18806:                 for (var j=0; j<offloadtypes.length; j++) {
18807:                     var total = alltargets.length - 1;
18808:                     for (var k=0; k<total; k++) {
18809:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
18810:                         var server = serveritem.value;
18811:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
18812:                             if (server == addtarget) {
18813:                                 serveritem.disabled = '';
18814:                             }
18815:                         }
18816:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
18817:                             if (server == removetarget) {
18818:                                 serveritem.disabled = 'disabled';
18819:                                 serveritem.checked = false;
18820:                             }
18821:                         }
18822:                     }
18823:                 }
18824:                 for (var j=0; j<insttypes.length; j++) {
18825:                     if (insttypes[j] != '_LC_external') {
18826:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
18827:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
18828:                             var currSel = singleserver.selectedIndex;
18829:                             var currVal = singleserver.options[currSel].value;
18830:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
18831:                                 var numoptions = singleserver.options.length;
18832:                                 var needsnew = 1;
18833:                                 for (var k=0; k<numoptions; k++) {
18834:                                     if (singleserver.options[k] == addtarget) {
18835:                                         needsnew = 0;
18836:                                         break;
18837:                                     }
18838:                                 }
18839:                                 if (needsnew == 1) {
18840:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
18841:                                 }
18842:                             }
18843:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
18844:                                 singleserver.options.length = 0;
18845:                                 if ((currVal) && (currVal != removetarget)) {
18846:                                     singleserver.options[0] = new Option("","",false,false);
18847:                                 } else {
18848:                                     singleserver.options[0] = new Option("","",true,true);
18849:                                 }
18850:                                 var idx = 0;
18851:                                 for (var m=0; m<alltargets.length; m++) {
18852:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
18853:                                         idx ++;
18854:                                         if (currVal == alltargets[m]) {
18855:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
18856:                                         } else {
18857:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
18858:                                         }
18859:                                     }
18860:                                 }
18861:                             }
18862:                         }
18863:                     }
18864:                 }
18865:             }
18866:         }
18867:     }
18868:     return;
18869: }
18870: 
18871: // ]]>
18872: </script>
18873: 
18874: END
18875: }
18876: 
18877: sub new_spares_js {
18878:     my @sparestypes = ('primary','default');
18879:     my $types = join("','",@sparestypes);
18880:     my $select = &mt('Select');
18881:     return <<"END";
18882: 
18883: <script type="text/javascript">
18884: // <![CDATA[
18885: 
18886: function updateNewSpares(formname,lonhost) {
18887:     var types = new Array('$types');
18888:     var include = new Array();
18889:     var exclude = new Array();
18890:     for (var i=0; i<types.length; i++) {
18891:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
18892:         for (var j=0; j<spareboxes.length; j++) {
18893:             if (formname.elements[spareboxes[j]].checked) {
18894:                 exclude.push(formname.elements[spareboxes[j]].value);
18895:             } else {
18896:                 include.push(formname.elements[spareboxes[j]].value);
18897:             }
18898:         }
18899:     }
18900:     for (var i=0; i<types.length; i++) {
18901:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
18902:         var selIdx = newSpare.selectedIndex;
18903:         var currnew = newSpare.options[selIdx].value;
18904:         var okSpares = new Array();
18905:         for (var j=0; j<newSpare.options.length; j++) {
18906:             var possible = newSpare.options[j].value;
18907:             if (possible != '') {
18908:                 if (exclude.indexOf(possible) == -1) {
18909:                     okSpares.push(possible);
18910:                 } else {
18911:                     if (currnew == possible) {
18912:                         selIdx = 0;
18913:                     }
18914:                 }
18915:             }
18916:         }
18917:         for (var k=0; k<include.length; k++) {
18918:             if (okSpares.indexOf(include[k]) == -1) {
18919:                 okSpares.push(include[k]);
18920:             }
18921:         }
18922:         okSpares.sort();
18923:         newSpare.options.length = 0;
18924:         if (selIdx == 0) {
18925:             newSpare.options[0] = new Option("$select","",true,true);
18926:         } else {
18927:             newSpare.options[0] = new Option("$select","",false,false);
18928:         }
18929:         for (var m=0; m<okSpares.length; m++) {
18930:             var idx = m+1;
18931:             var selThis = 0;
18932:             if (selIdx != 0) {
18933:                 if (okSpares[m] == currnew) {
18934:                     selThis = 1;
18935:                 }
18936:             }
18937:             if (selThis == 1) {
18938:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
18939:             } else {
18940:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
18941:             }
18942:         }
18943:     }
18944:     return;
18945: }
18946: 
18947: function checkNewSpares(lonhost,type) {
18948:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
18949:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
18950:     if (chosen != '') { 
18951:         var othertype;
18952:         var othernewSpare;
18953:         if (type == 'primary') {
18954:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
18955:         }
18956:         if (type == 'default') {
18957:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
18958:         }
18959:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
18960:             othernewSpare.selectedIndex = 0;
18961:         }
18962:     }
18963:     return;
18964: }
18965: 
18966: // ]]>
18967: </script>
18968: 
18969: END
18970: 
18971: }
18972: 
18973: sub common_domprefs_js {
18974:     return <<"END";
18975: 
18976: <script type="text/javascript">
18977: // <![CDATA[
18978: 
18979: function getIndicesByName(formname,item) {
18980:     var group = new Array();
18981:     for (var i=0;i<formname.elements.length;i++) {
18982:         if (formname.elements[i].name == item) {
18983:             group.push(formname.elements[i].id);
18984:         }
18985:     }
18986:     return group;
18987: }
18988: 
18989: // ]]>
18990: </script>
18991: 
18992: END
18993: 
18994: }
18995: 
18996: sub recaptcha_js {
18997:     my %lt = &captcha_phrases();
18998:     return <<"END";
18999: 
19000: <script type="text/javascript">
19001: // <![CDATA[
19002: 
19003: function updateCaptcha(caller,context) {
19004:     var privitem;
19005:     var pubitem;
19006:     var privtext;
19007:     var pubtext;
19008:     var versionitem;
19009:     var versiontext;
19010:     if (document.getElementById(context+'_recaptchapub')) {
19011:         pubitem = document.getElementById(context+'_recaptchapub');
19012:     } else {
19013:         return;
19014:     }
19015:     if (document.getElementById(context+'_recaptchapriv')) {
19016:         privitem = document.getElementById(context+'_recaptchapriv');
19017:     } else {
19018:         return;
19019:     }
19020:     if (document.getElementById(context+'_recaptchapubtxt')) {
19021:         pubtext = document.getElementById(context+'_recaptchapubtxt');
19022:     } else {
19023:         return;
19024:     }
19025:     if (document.getElementById(context+'_recaptchaprivtxt')) {
19026:         privtext = document.getElementById(context+'_recaptchaprivtxt');
19027:     } else {
19028:         return;
19029:     }
19030:     if (document.getElementById(context+'_recaptchaversion')) {
19031:         versionitem = document.getElementById(context+'_recaptchaversion');
19032:     } else {
19033:         return;
19034:     }
19035:     if (document.getElementById(context+'_recaptchavertxt')) {
19036:         versiontext = document.getElementById(context+'_recaptchavertxt');
19037:     } else {
19038:         return;
19039:     }
19040:     if (caller.checked) {
19041:         if (caller.value == 'recaptcha') {
19042:             pubitem.type = 'text';
19043:             privitem.type = 'text';
19044:             pubitem.size = '40';
19045:             privitem.size = '40';
19046:             pubtext.innerHTML = "$lt{'pub'}";
19047:             privtext.innerHTML = "$lt{'priv'}";
19048:             versionitem.type = 'text';
19049:             versionitem.size = '3';
19050:             versiontext.innerHTML = "$lt{'ver'}";
19051:         } else {
19052:             pubitem.type = 'hidden';
19053:             privitem.type = 'hidden';
19054:             versionitem.type = 'hidden';
19055:             pubtext.innerHTML = '';
19056:             privtext.innerHTML = '';
19057:             versiontext.innerHTML = '';
19058:         }
19059:     }
19060:     return;
19061: }
19062: 
19063: // ]]>
19064: </script>
19065: 
19066: END
19067: 
19068: }
19069: 
19070: sub toggle_display_js {
19071:     return <<"END";
19072: 
19073: <script type="text/javascript">
19074: // <![CDATA[
19075: 
19076: function toggleDisplay(domForm,caller) {
19077:     if (document.getElementById(caller)) {
19078:         var divitem = document.getElementById(caller);
19079:         var optionsElement = domForm.coursecredits;
19080:         var checkval = 1;
19081:         var dispval = 'block';
19082:         var selfcreateRegExp = /^cancreate_emailverified/;
19083:         if (caller == 'emailoptions') {
19084:             optionsElement = domForm.cancreate_email; 
19085:         }
19086:         if (caller == 'studentsubmission') {
19087:             optionsElement = domForm.postsubmit;
19088:         }
19089:         if (caller == 'cloneinstcode') {
19090:             optionsElement = domForm.canclone;
19091:             checkval = 'instcode';
19092:         }
19093:         if (selfcreateRegExp.test(caller)) {
19094:             optionsElement = domForm.elements[caller];
19095:             checkval = 'other';
19096:             dispval = 'inline'
19097:         }
19098:         if (optionsElement.length) {
19099:             var currval;
19100:             for (var i=0; i<optionsElement.length; i++) {
19101:                 if (optionsElement[i].checked) {
19102:                    currval = optionsElement[i].value;
19103:                 }
19104:             }
19105:             if (currval == checkval) {
19106:                 divitem.style.display = dispval;
19107:             } else {
19108:                 divitem.style.display = 'none';
19109:             }
19110:         }
19111:     }
19112:     return;
19113: }
19114: 
19115: // ]]>
19116: </script>
19117: 
19118: END
19119: 
19120: }
19121: 
19122: sub captcha_phrases {
19123:     return &Apache::lonlocal::texthash (
19124:                  priv => 'Private key',
19125:                  pub  => 'Public key',
19126:                  original  => 'original (CAPTCHA)',
19127:                  recaptcha => 'successor (ReCAPTCHA)',
19128:                  notused   => 'unused',
19129:                  ver => 'ReCAPTCHA version (1 or 2)',
19130:     );
19131: }
19132: 
19133: sub devalidate_remote_domconfs {
19134:     my ($dom,$cachekeys) = @_;
19135:     return unless (ref($cachekeys) eq 'HASH');
19136:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19137:     my %thismachine;
19138:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
19139:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
19140:     if (keys(%servers)) {
19141:         foreach my $server (keys(%servers)) {
19142:             next if ($thismachine{$server});
19143:             my @cached;
19144:             foreach my $name (@posscached) {
19145:                 if ($cachekeys->{$name}) {
19146:                     push(@cached,&escape($name).':'.&escape($dom));
19147:                 }
19148:             }
19149:             if (@cached) {
19150:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
19151:             }
19152:         }
19153:     }
19154:     return;
19155: }
19156: 
19157: 1;

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