File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.398: download - view: text, annotated - select for diffs
Fri Dec 24 11:07:42 2021 UTC (2 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add option to choose inline preview (new) or pop-up (old) for Chemical
  Reaction response items.  Default is inline preview. Domain setting can
  be overridden in a course.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.398 2021/12/24 11:07:42 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: use Net::CIDR;
  180: 
  181: my $registered_cleanup;
  182: my $modified_urls;
  183: 
  184: sub handler {
  185:     my $r=shift;
  186:     if ($r->header_only) {
  187:         &Apache::loncommon::content_type($r,'text/html');
  188:         $r->send_http_header;
  189:         return OK;
  190:     }
  191: 
  192:     my $context = 'domain';
  193:     my $dom = $env{'request.role.domain'};
  194:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  195:     if (&Apache::lonnet::allowed('mau',$dom)) {
  196:         &Apache::loncommon::content_type($r,'text/html');
  197:         $r->send_http_header;
  198:     } else {
  199:         $env{'user.error.msg'}=
  200:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  201:         return HTTP_NOT_ACCEPTABLE;
  202:     }
  203: 
  204:     $registered_cleanup=0;
  205:     @{$modified_urls}=();
  206: 
  207:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  208:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  209:                                             ['phase','actions']);
  210:     my $phase = 'pickactions';
  211:     if ( exists($env{'form.phase'}) ) {
  212:         $phase = $env{'form.phase'};
  213:     }
  214:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  215:     my %domconfig =
  216:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  217:                 'quotas','autoenroll','autoupdate','autocreate',
  218:                 'directorysrch','usercreation','usermodification',
  219:                 'contacts','defaults','scantron','coursecategories',
  220:                 'serverstatuses','requestcourses','helpsettings',
  221:                 'coursedefaults','usersessions','loadbalancing',
  222:                 'requestauthor','selfenrollment','inststatus',
  223:                 'ltitools','ssl','trust','lti','privacy','passwords',
  224:                 'proctoring','wafproxy','ipaccess'],$dom);
  225:     my %encconfig =
  226:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring'],$dom,undef,1);
  227:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  228:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  229:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  230:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  231:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  232:                     foreach my $item ('key','secret') {
  233:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  234:                     }
  235:                 }
  236:             }
  237:         }
  238:     }
  239:     if (ref($domconfig{'lti'}) eq 'HASH') {
  240:         if (ref($encconfig{'lti'}) eq 'HASH') {
  241:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  242:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  243:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  244:                     foreach my $item ('key','secret') {
  245:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  252:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  253:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  254:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  255:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  256:                     foreach my $item ('key','secret') {
  257:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  258:                     }
  259:                 }
  260:             }
  261:         }
  262:     }
  263:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  264:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  265:                        'contacts','privacy','usercreation','selfcreation',
  266:                        'usermodification','scantron','requestcourses','requestauthor',
  267:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  268:                        'ltitools','proctoring','selfenrollment','usersessions','ssl',
  269:                        'trust','lti');
  270:     my %existing;
  271:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  272:         %existing = %{$domconfig{'loadbalancing'}};
  273:     }
  274:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  275:         push(@prefs_order,'loadbalancing');
  276:     }
  277:     my %prefs = (
  278:         'rolecolors' =>
  279:                    { text => 'Default color schemes',
  280:                      help => 'Domain_Configuration_Color_Schemes',
  281:                      header => [{col1 => 'Student Settings',
  282:                                  col2 => '',},
  283:                                 {col1 => 'Coordinator Settings',
  284:                                  col2 => '',},
  285:                                 {col1 => 'Author Settings',
  286:                                  col2 => '',},
  287:                                 {col1 => 'Administrator Settings',
  288:                                  col2 => '',}],
  289:                       print => \&print_rolecolors,
  290:                       modify => \&modify_rolecolors,
  291:                     },
  292:         'login' =>
  293:                     { text => 'Log-in page options',
  294:                       help => 'Domain_Configuration_Login_Page',
  295:                       header => [{col1 => 'Log-in Page Items',
  296:                                   col2 => '',},
  297:                                  {col1 => 'Log-in Help',
  298:                                   col2 => 'Value'},
  299:                                  {col1 => 'Custom HTML in document head',
  300:                                   col2 => 'Value'},
  301:                                  {col1 => 'SSO',
  302:                                   col2 => 'Dual login: SSO and non-SSO options'},
  303:                                 ],
  304:                       print => \&print_login,
  305:                       modify => \&modify_login,
  306:                     },
  307:         'defaults' => 
  308:                     { text => 'Default authentication/language/timezone/portal/types',
  309:                       help => 'Domain_Configuration_LangTZAuth',
  310:                       header => [{col1 => 'Setting',
  311:                                   col2 => 'Value'},
  312:                                  {col1 => 'Institutional user types',
  313:                                   col2 => 'Name displayed'}],
  314:                       print => \&print_defaults,
  315:                       modify => \&modify_defaults,
  316:                     },
  317:         'wafproxy' =>
  318:                     { text => 'Web Application Firewall/Reverse Proxy',
  319:                       help => 'Domain_Configuration_WAF_Proxy',
  320:                       header => [{col1 => 'Domain(s)',
  321:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  322:                                  },
  323:                                  {col1 => 'Domain(s)',
  324:                                   col2 => 'WAF Configuration',}],
  325:                       print => \&print_wafproxy,
  326:                       modify => \&modify_wafproxy,
  327:                     },
  328:         'passwords' =>
  329:                     { text => 'Passwords (Internal authentication)',
  330:                       help => 'Domain_Configuration_Passwords',
  331:                       header => [{col1 => 'Resetting Forgotten Password',
  332:                                   col2 => 'Settings'},
  333:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  334:                                   col2 => 'Settings'},
  335:                                  {col1 => 'Rules for LON-CAPA Passwords',
  336:                                   col2 => 'Settings'},
  337:                                  {col1 => 'Course Owner Changing Student Passwords',
  338:                                   col2 => 'Settings'}],
  339:                       print => \&print_passwords,
  340:                       modify => \&modify_passwords,
  341:                     },
  342:         'quotas' => 
  343:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  344:                       help => 'Domain_Configuration_Quotas',
  345:                       header => [{col1 => 'User affiliation',
  346:                                   col2 => 'Available tools',
  347:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  348:                       print => \&print_quotas,
  349:                       modify => \&modify_quotas,
  350:                     },
  351:         'autoenroll' =>
  352:                    { text => 'Auto-enrollment settings',
  353:                      help => 'Domain_Configuration_Auto_Enrollment',
  354:                      header => [{col1 => 'Configuration setting',
  355:                                  col2 => 'Value(s)'}],
  356:                      print => \&print_autoenroll,
  357:                      modify => \&modify_autoenroll,
  358:                    },
  359:         'autoupdate' => 
  360:                    { text => 'Auto-update settings',
  361:                      help => 'Domain_Configuration_Auto_Updates',
  362:                      header => [{col1 => 'Setting',
  363:                                  col2 => 'Value',},
  364:                                 {col1 => 'Setting',
  365:                                  col2 => 'Affiliation'},
  366:                                 {col1 => 'User population',
  367:                                  col2 => 'Updatable user data'}],
  368:                      print => \&print_autoupdate,
  369:                      modify => \&modify_autoupdate,
  370:                   },
  371:         'autocreate' => 
  372:                   { text => 'Auto-course creation settings',
  373:                      help => 'Domain_Configuration_Auto_Creation',
  374:                      header => [{col1 => 'Configuration Setting',
  375:                                  col2 => 'Value',}],
  376:                      print => \&print_autocreate,
  377:                      modify => \&modify_autocreate,
  378:                   },
  379:         'directorysrch' => 
  380:                   { text => 'Directory searches',
  381:                     help => 'Domain_Configuration_InstDirectory_Search',
  382:                     header => [{col1 => 'Institutional Directory Setting',
  383:                                 col2 => 'Value',},
  384:                                {col1 => 'LON-CAPA Directory Setting',
  385:                                 col2 => 'Value',}],
  386:                     print => \&print_directorysrch,
  387:                     modify => \&modify_directorysrch,
  388:                   },
  389:         'contacts' =>
  390:                   { text => 'E-mail addresses and helpform',
  391:                     help => 'Domain_Configuration_Contact_Info',
  392:                     header => [{col1 => 'Default e-mail addresses',
  393:                                 col2 => 'Value',},
  394:                                {col1 => 'Recipient(s) for notifications',
  395:                                 col2 => 'Value',},
  396:                                {col1 => 'Nightly status check e-mail',
  397:                                 col2 => 'Settings',},
  398:                                {col1 => 'Ask helpdesk form settings',
  399:                                 col2 => 'Value',},],
  400:                     print => \&print_contacts,
  401:                     modify => \&modify_contacts,
  402:                   },
  403:         'usercreation' => 
  404:                   { text => 'User creation',
  405:                     help => 'Domain_Configuration_User_Creation',
  406:                     header => [{col1 => 'Format rule type',
  407:                                 col2 => 'Format rules in force'},
  408:                                {col1 => 'User account creation',
  409:                                 col2 => 'Usernames which may be created',},
  410:                                {col1 => 'Context',
  411:                                 col2 => 'Assignable authentication types'}],
  412:                     print => \&print_usercreation,
  413:                     modify => \&modify_usercreation,
  414:                   },
  415:         'selfcreation' => 
  416:                   { text => 'Users self-creating accounts',
  417:                     help => 'Domain_Configuration_Self_Creation', 
  418:                     header => [{col1 => 'Self-creation with institutional username',
  419:                                 col2 => 'Enabled?'},
  420:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  421:                                 col2 => 'Information user can enter'},
  422:                                {col1 => 'Self-creation with e-mail verification',
  423:                                 col2 => 'Settings'}],
  424:                     print => \&print_selfcreation,
  425:                     modify => \&modify_selfcreation,
  426:                   },
  427:         'usermodification' =>
  428:                   { text => 'User modification',
  429:                     help => 'Domain_Configuration_User_Modification',
  430:                     header => [{col1 => 'Target user has role',
  431:                                 col2 => 'User information updatable in author context'},
  432:                                {col1 => 'Target user has role',
  433:                                 col2 => 'User information updatable in course context'}],
  434:                     print => \&print_usermodification,
  435:                     modify => \&modify_usermodification,
  436:                   },
  437:         'scantron' =>
  438:                   { text => 'Bubblesheet format',
  439:                     help => 'Domain_Configuration_Scantron_Format',
  440:                     header => [ {col1 => 'Bubblesheet format file',
  441:                                  col2 => ''},
  442:                                 {col1 => 'Bubblesheet data upload formats',
  443:                                  col2 => 'Settings'}],
  444:                     print => \&print_scantron,
  445:                     modify => \&modify_scantron,
  446:                   },
  447:         'requestcourses' => 
  448:                  {text => 'Request creation of courses',
  449:                   help => 'Domain_Configuration_Request_Courses',
  450:                   header => [{col1 => 'User affiliation',
  451:                               col2 => 'Availability/Processing of requests',},
  452:                              {col1 => 'Setting',
  453:                               col2 => 'Value'},
  454:                              {col1 => 'Available textbooks',
  455:                               col2 => ''},
  456:                              {col1 => 'Available templates',
  457:                               col2 => ''},
  458:                              {col1 => 'Validation (not official courses)',
  459:                               col2 => 'Value'},],
  460:                   print => \&print_quotas,
  461:                   modify => \&modify_quotas,
  462:                  },
  463:         'requestauthor' =>
  464:                  {text => 'Request Authoring Space',
  465:                   help => 'Domain_Configuration_Request_Author',
  466:                   header => [{col1 => 'User affiliation',
  467:                               col2 => 'Availability/Processing of requests',},
  468:                              {col1 => 'Setting',
  469:                               col2 => 'Value'}],
  470:                   print => \&print_quotas,
  471:                   modify => \&modify_quotas,
  472:                  },
  473:         'coursecategories' =>
  474:                   { text => 'Cataloging of courses/communities',
  475:                     help => 'Domain_Configuration_Cataloging_Courses',
  476:                     header => [{col1 => 'Catalog type/availability',
  477:                                 col2 => '',},
  478:                                {col1 => 'Category settings for standard catalog',
  479:                                 col2 => '',},
  480:                                {col1 => 'Categories',
  481:                                 col2 => '',
  482:                                }],
  483:                     print => \&print_coursecategories,
  484:                     modify => \&modify_coursecategories,
  485:                   },
  486:         'serverstatuses' =>
  487:                  {text   => 'Access to server status pages',
  488:                   help   => 'Domain_Configuration_Server_Status',
  489:                   header => [{col1 => 'Status Page',
  490:                               col2 => 'Other named users',
  491:                               col3 => 'Specific IPs',
  492:                             }],
  493:                   print => \&print_serverstatuses,
  494:                   modify => \&modify_serverstatuses,
  495:                  },
  496:         'helpsettings' =>
  497:                  {text   => 'Support settings',
  498:                   help   => 'Domain_Configuration_Help_Settings',
  499:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  500:                               col2 => 'Value'},
  501:                              {col1 => 'Helpdesk Roles',
  502:                               col2 => 'Settings'},],
  503:                   print  => \&print_helpsettings,
  504:                   modify => \&modify_helpsettings,
  505:                  },
  506:         'coursedefaults' => 
  507:                  {text => 'Course/Community defaults',
  508:                   help => 'Domain_Configuration_Course_Defaults',
  509:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  510:                               col2 => 'Value',},
  511:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  512:                               col2 => 'Value',},],
  513:                   print => \&print_coursedefaults,
  514:                   modify => \&modify_coursedefaults,
  515:                  },
  516:         'selfenrollment' => 
  517:                  {text   => 'Self-enrollment in Course/Community',
  518:                   help   => 'Domain_Configuration_Selfenrollment',
  519:                   header => [{col1 => 'Configuration Rights',
  520:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  521:                              {col1 => 'Defaults',
  522:                               col2 => 'Value'},
  523:                              {col1 => 'Self-enrollment validation (optional)',
  524:                               col2 => 'Value'},],
  525:                   print => \&print_selfenrollment,
  526:                   modify => \&modify_selfenrollment,
  527:                  },
  528:         'privacy' => 
  529:                  {text   => 'Availability of User Information',
  530:                   help   => 'Domain_Configuration_User_Privacy',
  531:                   header => [{col1 => 'Role assigned in different domain',
  532:                               col2 => 'Approval options'},
  533:                              {col1 => 'Role assigned in different domain to user of type',
  534:                               col2 => 'User information available in that domain'},
  535:                              {col1 => "Role assigned in user's domain",
  536:                               col2 => 'Information viewable by privileged user'},
  537:                              {col1 => "Role assigned in user's domain",
  538:                               col2 => 'Information viewable by unprivileged user'}],
  539:                   print => \&print_privacy,
  540:                   modify => \&modify_privacy,
  541:                  },
  542:         'usersessions' =>
  543:                  {text  => 'User session hosting/offloading',
  544:                   help  => 'Domain_Configuration_User_Sessions',
  545:                   header => [{col1 => 'Domain server',
  546:                               col2 => 'Servers to offload sessions to when busy'},
  547:                              {col1 => 'Hosting of users from other domains',
  548:                               col2 => 'Rules'},
  549:                              {col1 => "Hosting domain's own users elsewhere",
  550:                               col2 => 'Rules'}],
  551:                   print => \&print_usersessions,
  552:                   modify => \&modify_usersessions,
  553:                  },
  554:         'loadbalancing' =>
  555:                  {text  => 'Dedicated Load Balancer(s)',
  556:                   help  => 'Domain_Configuration_Load_Balancing',
  557:                   header => [{col1 => 'Balancers',
  558:                               col2 => 'Default destinations',
  559:                               col3 => 'User affiliation',
  560:                               col4 => 'Overrides'},
  561:                             ],
  562:                   print => \&print_loadbalancing,
  563:                   modify => \&modify_loadbalancing,
  564:                  },
  565:         'ltitools' => 
  566:                  {text => 'External Tools (LTI)',
  567:                   help => 'Domain_Configuration_LTI_Tools',
  568:                   header => [{col1 => 'Setting',
  569:                               col2 => 'Value',}],
  570:                   print => \&print_ltitools,
  571:                   modify => \&modify_ltitools,
  572:                  },
  573:         'proctoring' =>
  574:                  {text => 'Remote Proctoring Integration',
  575:                   help => 'Domain_Configuration_Proctoring',
  576:                   header => [{col1 => 'Name',
  577:                               col2 => 'Configuration'}],
  578:                   print => \&print_proctoring,
  579:                   modify => \&modify_proctoring,
  580:                  },
  581:         'ssl' =>
  582:                  {text  => 'LON-CAPA Network (SSL)',
  583:                   help  => 'Domain_Configuration_Network_SSL',
  584:                   header => [{col1 => 'Server',
  585:                               col2 => 'Certificate Status'},
  586:                              {col1 => 'Connections to other servers',
  587:                               col2 => 'Rules'},
  588:                              {col1 => 'Connections from other servers',
  589:                               col2 => 'Rules'},
  590:                              {col1 => "Replicating domain's published content",
  591:                               col2 => 'Rules'}],
  592:                   print => \&print_ssl,
  593:                   modify => \&modify_ssl,
  594:                  },
  595:         'trust' =>
  596:                  {text   => 'Trust Settings',
  597:                   help   => 'Domain_Configuration_Trust',
  598:                   header => [{col1 => "Access to this domain's content by others",
  599:                               col2 => 'Rules'},
  600:                              {col1 => "Access to other domain's content by this domain",
  601:                               col2 => 'Rules'},
  602:                              {col1 => "Enrollment in this domain's courses by others",
  603:                               col2 => 'Rules',},
  604:                              {col1 => "Co-author roles in this domain for others",
  605:                               col2 => 'Rules',},
  606:                              {col1 => "Co-author roles for this domain's users elsewhere",
  607:                               col2 => 'Rules',},
  608:                              {col1 => "Domain roles in this domain assignable to others",
  609:                               col2 => 'Rules'},
  610:                              {col1 => "Course catalog for this domain displayed elsewhere",
  611:                               col2 => 'Rules'},
  612:                              {col1 => "Requests for creation of courses in this domain by others",
  613:                               col2 => 'Rules'},
  614:                              {col1 => "Users in other domains can send messages to this domain",
  615:                               col2 => 'Rules'},],
  616:                   print => \&print_trust,
  617:                   modify => \&modify_trust,
  618:                  },
  619:           'lti' =>
  620:                  {text => 'LTI Provider',
  621:                   help => 'Domain_Configuration_LTI_Provider',
  622:                   header => [{col1 => 'Setting',
  623:                               col2 => 'Value',}],
  624:                   print => \&print_lti,
  625:                   modify => \&modify_lti,
  626:                  },
  627:            'ipaccess' =>
  628:                        {text => 'IP-based access control',
  629:                         help => 'Domain_Configuration_IP_Access',
  630:                         header => [{col1 => 'Setting',
  631:                                     col2 => 'Value'},],
  632:                         print  => \&print_ipaccess,
  633:                         modify => \&modify_ipaccess,
  634:                        },
  635:     );
  636:     if (keys(%servers) > 1) {
  637:         $prefs{'login'}  = { text   => 'Log-in page options',
  638:                              help   => 'Domain_Configuration_Login_Page',
  639:                             header => [{col1 => 'Log-in Service',
  640:                                         col2 => 'Server Setting',},
  641:                                        {col1 => 'Log-in Page Items',
  642:                                         col2 => ''},
  643:                                        {col1 => 'Log-in Help',
  644:                                         col2 => 'Value'},
  645:                                        {col1 => 'Custom HTML in document head',
  646:                                         col2 => 'Value'},
  647:                                        {col1 => 'SSO',
  648:                                         col2 => 'Dual login: SSO and non-SSO options'},
  649:                                       ],
  650:                             print => \&print_login,
  651:                             modify => \&modify_login,
  652:                            };
  653:     }
  654: 
  655:     my @roles = ('student','coordinator','author','admin');
  656:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  657:     &Apache::lonhtmlcommon::add_breadcrumb
  658:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  659:       text=>"Settings to display/modify"});
  660:     my $confname = $dom.'-domainconfig';
  661: 
  662:     if ($phase eq 'process') {
  663:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  664:                                                               \%prefs,\%domconfig,$confname,\@roles);
  665:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  666:             $r->rflush();
  667:             &devalidate_remote_domconfs($dom,$result);
  668:         }
  669:     } elsif ($phase eq 'display') {
  670:         my $js = &recaptcha_js().
  671:                  &toggle_display_js();
  672:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  673:             my ($othertitle,$usertypes,$types) =
  674:                 &Apache::loncommon::sorted_inst_types($dom);
  675:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  676:                                           $domconfig{'loadbalancing'}).
  677:                    &new_spares_js().
  678:                    &common_domprefs_js().
  679:                    &Apache::loncommon::javascript_array_indexof();
  680:         }
  681:         if (grep(/^requestcourses$/,@actions)) {
  682:             my $javascript_validations;
  683:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  684:             $js .= <<END;
  685: <script type="text/javascript">
  686: $javascript_validations
  687: </script>
  688: $coursebrowserjs
  689: END
  690:         } elsif (grep(/^ipaccess$/,@actions)) {
  691:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  692:         }
  693:         if (grep(/^selfcreation$/,@actions)) {
  694:             $js .= &selfcreate_javascript();
  695:         }
  696:         if (grep(/^contacts$/,@actions)) {
  697:             $js .= &contacts_javascript();
  698:         }
  699:         if (grep(/^scantron$/,@actions)) {
  700:             $js .= &scantron_javascript();
  701:         }
  702:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  703:     } else {
  704: # check if domconfig user exists for the domain.
  705:         my $servadm = $r->dir_config('lonAdmEMail');
  706:         my ($configuserok,$author_ok,$switchserver) =
  707:             &config_check($dom,$confname,$servadm);
  708:         unless ($configuserok eq 'ok') {
  709:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  710:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  711:                           $confname).
  712:                       '<br />'
  713:             );
  714:             if ($switchserver) {
  715:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  716:                           '<br />'.
  717:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  718:                           '<br />'.
  719:                           &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).
  720:                           '<br />'.
  721:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  722:                 );
  723:             } else {
  724:                 $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.').
  725:                           '<br />'.
  726:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  727:                 );
  728:             }
  729:             $r->print(&Apache::loncommon::end_page());
  730:             return OK;
  731:         }
  732:         if (keys(%domconfig) == 0) {
  733:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  734:             my @ids=&Apache::lonnet::current_machine_ids();
  735:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  736:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  737:                 my @loginimages = ('img','logo','domlogo','login');
  738:                 my $custom_img_count = 0;
  739:                 foreach my $img (@loginimages) {
  740:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  741:                         $custom_img_count ++;
  742:                     }
  743:                 }
  744:                 foreach my $role (@roles) {
  745:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  746:                         $custom_img_count ++;
  747:                     }
  748:                 }
  749:                 if ($custom_img_count > 0) {
  750:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  751:                     my $switch_server = &check_switchserver($dom,$confname);
  752:                     $r->print(
  753:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  754:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  755:     &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 />'.
  756:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  757:                     if ($switch_server) {
  758:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  759:                     }
  760:                     $r->print(&Apache::loncommon::end_page());
  761:                     return OK;
  762:                 }
  763:             }
  764:         }
  765:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  766:     }
  767:     return OK;
  768: }
  769: 
  770: sub process_changes {
  771:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  772:     my %domconfig;
  773:     if (ref($values) eq 'HASH') {
  774:         %domconfig = %{$values};
  775:     }
  776:     my $output;
  777:     if ($action eq 'login') {
  778:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  779:     } elsif ($action eq 'rolecolors') {
  780:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  781:                                      $lastactref,%domconfig);
  782:     } elsif ($action eq 'quotas') {
  783:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  784:     } elsif ($action eq 'autoenroll') {
  785:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  786:     } elsif ($action eq 'autoupdate') {
  787:         $output = &modify_autoupdate($dom,%domconfig);
  788:     } elsif ($action eq 'autocreate') {
  789:         $output = &modify_autocreate($dom,%domconfig);
  790:     } elsif ($action eq 'directorysrch') {
  791:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  792:     } elsif ($action eq 'usercreation') {
  793:         $output = &modify_usercreation($dom,%domconfig);
  794:     } elsif ($action eq 'selfcreation') {
  795:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  796:     } elsif ($action eq 'usermodification') {
  797:         $output = &modify_usermodification($dom,%domconfig);
  798:     } elsif ($action eq 'contacts') {
  799:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  800:     } elsif ($action eq 'defaults') {
  801:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  802:     } elsif ($action eq 'scantron') {
  803:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  804:     } elsif ($action eq 'coursecategories') {
  805:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  806:     } elsif ($action eq 'serverstatuses') {
  807:         $output = &modify_serverstatuses($dom,%domconfig);
  808:     } elsif ($action eq 'requestcourses') {
  809:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  810:     } elsif ($action eq 'requestauthor') {
  811:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  812:     } elsif ($action eq 'helpsettings') {
  813:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  814:     } elsif ($action eq 'coursedefaults') {
  815:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  816:     } elsif ($action eq 'selfenrollment') {
  817:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  818:     } elsif ($action eq 'usersessions') {
  819:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  820:     } elsif ($action eq 'loadbalancing') {
  821:         $output = &modify_loadbalancing($dom,%domconfig);
  822:     } elsif ($action eq 'ltitools') {
  823:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  824:     } elsif ($action eq 'proctoring') {
  825:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  826:     } elsif ($action eq 'ssl') {
  827:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  828:     } elsif ($action eq 'trust') {
  829:         $output = &modify_trust($dom,$lastactref,%domconfig);
  830:     } elsif ($action eq 'lti') {
  831:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  832:     } elsif ($action eq 'privacy') {
  833:         $output = &modify_privacy($dom,%domconfig);
  834:     } elsif ($action eq 'passwords') {
  835:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  836:     } elsif ($action eq 'wafproxy') {
  837:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  838:     } elsif ($action eq 'ipaccess') {
  839:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  840:     }
  841:     return $output;
  842: }
  843: 
  844: sub print_config_box {
  845:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  846:     my $rowtotal = 0;
  847:     my $output;
  848:     if ($action eq 'coursecategories') {
  849:         $output = &coursecategories_javascript($settings);
  850:     } elsif ($action eq 'defaults') {
  851:         $output = &defaults_javascript($settings); 
  852:     } elsif ($action eq 'passwords') {
  853:         $output = &passwords_javascript();
  854:     } elsif ($action eq 'helpsettings') {
  855:         my (%privs,%levelscurrent);
  856:         my %full=();
  857:         my %levels=(
  858:                      course => {},
  859:                      domain => {},
  860:                      system => {},
  861:                    );
  862:         my $context = 'domain';
  863:         my $crstype = 'Course';
  864:         my $formname = 'display';
  865:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  866:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  867:         $output =
  868:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  869:                                                       \@templateroles);
  870:     } elsif ($action eq 'ltitools') {
  871:         $output .= &ltitools_javascript($settings);
  872:     } elsif ($action eq 'lti') {
  873:         $output .= &lti_javascript($settings);
  874:     } elsif ($action eq 'proctoring') {
  875:         $output .= &proctoring_javascript($settings);
  876:     } elsif ($action eq 'wafproxy') {
  877:         $output .= &wafproxy_javascript($dom);
  878:     } elsif ($action eq 'autoupdate') {
  879:         $output .= &autoupdate_javascript();
  880:     } elsif ($action eq 'login') {
  881:         $output .= &saml_javascript();
  882:     } elsif ($action eq 'ipaccess') {
  883:         $output .= &ipaccess_javascript($settings);
  884:     }
  885:     $output .=
  886:          '<table class="LC_nested_outer">
  887:           <tr>
  888:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  889:            &mt($item->{text}).'&nbsp;'.
  890:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  891:           '</tr>';
  892:     $rowtotal ++;
  893:     my $numheaders = 1;
  894:     if (ref($item->{'header'}) eq 'ARRAY') {
  895:         $numheaders = scalar(@{$item->{'header'}});
  896:     }
  897:     if ($numheaders > 1) {
  898:         my $colspan = '';
  899:         my $rightcolspan = '';
  900:         my $leftnobr = '';
  901:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  902:             ($action eq 'directorysrch') ||
  903:             (($action eq 'login') && ($numheaders < 5))) {
  904:             $colspan = ' colspan="2"';
  905:         }
  906:         if ($action eq 'usersessions') {
  907:             $rightcolspan = ' colspan="3"'; 
  908:         }
  909:         if ($action eq 'passwords') {
  910:             $leftnobr = ' LC_nobreak';
  911:         }
  912:         $output .= '
  913:           <tr>
  914:            <td>
  915:             <table class="LC_nested">
  916:              <tr class="LC_info_row">
  917:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  918:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  919:              </tr>';
  920:         $rowtotal ++;
  921:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  922:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  923:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  924:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  925:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy')) {
  926:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  927:         } elsif ($action eq 'passwords') {
  928:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  929:         } elsif ($action eq 'coursecategories') {
  930:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  931:         } elsif ($action eq 'scantron') {
  932:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  933:         } elsif ($action eq 'login') {
  934:             if ($numheaders == 5) {
  935:                 $colspan = ' colspan="2"';
  936:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  937:             } else {
  938:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  939:             }
  940:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  941:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  942:         } elsif ($action eq 'rolecolors') {
  943:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  944:         }
  945:         $output .= '
  946:            </table>
  947:           </td>
  948:          </tr>
  949:          <tr>
  950:            <td>
  951:             <table class="LC_nested">
  952:              <tr class="LC_info_row">
  953:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  954:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  955:              </tr>';
  956:             $rowtotal ++;
  957:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  958:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  959:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  960:             ($action eq 'trust') || ($action eq 'contacts') ||
  961:             ($action eq 'privacy') || ($action eq 'passwords')) {
  962:             if ($action eq 'coursecategories') {
  963:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  964:                 $colspan = ' colspan="2"';
  965:             } elsif ($action eq 'trust') {
  966:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  967:             } elsif ($action eq 'passwords') {
  968:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  969:             } else {
  970:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  971:             }
  972:             if ($action eq 'trust') {
  973:                 $output .= '
  974:             </table>
  975:           </td>
  976:          </tr>';
  977:                 my @trusthdrs = qw(2 3 4 5 6 7);
  978:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  979:                 for (my $i=0; $i<@trusthdrs; $i++) {
  980:                     $output .= '
  981:          <tr>
  982:            <td>
  983:             <table class="LC_nested">
  984:              <tr class="LC_info_row">
  985:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  986:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  987:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  988:             </table>
  989:           </td>
  990:          </tr>';
  991:                 }
  992:                 $output .= '
  993:          <tr>
  994:            <td>
  995:             <table class="LC_nested">
  996:              <tr class="LC_info_row">
  997:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  998:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  999:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1000:             } else {
 1001:                 $output .= '
 1002:            </table>
 1003:           </td>
 1004:          </tr>
 1005:          <tr>
 1006:            <td>
 1007:             <table class="LC_nested">
 1008:              <tr class="LC_info_row">
 1009:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1010:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1011:              </tr>'."\n";
 1012:                 if ($action eq 'coursecategories') {
 1013:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1014:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'passwords')) {
 1015:                     if ($action eq 'passwords') {
 1016:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1017:                     } else {
 1018:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1019:                     }
 1020:                     $output .= '
 1021:              </tr>
 1022:             </table>
 1023:            </td>
 1024:           </tr>
 1025:           <tr>
 1026:            <td>
 1027:             <table class="LC_nested">
 1028:              <tr class="LC_info_row">
 1029:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1030:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1031:                     if ($action eq 'passwords') {
 1032:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1033:                     } else {
 1034:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1035:                     }
 1036:                     $output .= '
 1037:             </table>
 1038:           </td>
 1039:          </tr>
 1040:          <tr>';
 1041:                 } else {
 1042:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1043:                 }
 1044:             }
 1045:             $rowtotal ++;
 1046:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1047:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
 1048:                  ($action eq 'helpsettings') || ($action eq 'wafproxy')) {
 1049:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1050:         } elsif ($action eq 'scantron') {
 1051:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1052:         } elsif ($action eq 'ssl') {
 1053:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1054:             </table>
 1055:           </td>
 1056:          </tr>
 1057:          <tr>
 1058:            <td>
 1059:             <table class="LC_nested">
 1060:              <tr class="LC_info_row">
 1061:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1062:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1063:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1064:             </table>
 1065:           </td>
 1066:          </tr>
 1067:          <tr>
 1068:            <td>
 1069:             <table class="LC_nested">
 1070:              <tr class="LC_info_row">
 1071:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1072:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1073:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1074:         } elsif ($action eq 'login') {
 1075:             if ($numheaders == 5) {
 1076:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1077:            </table>
 1078:           </td>
 1079:          </tr>
 1080:          <tr>
 1081:            <td>
 1082:             <table class="LC_nested">
 1083:              <tr class="LC_info_row">
 1084:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1085:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1086:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1087:                 $rowtotal ++;
 1088:             } else {
 1089:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1090:             }
 1091:             $output .= '
 1092:            </table>
 1093:           </td>
 1094:          </tr>
 1095:          <tr>
 1096:            <td>
 1097:             <table class="LC_nested">
 1098:              <tr class="LC_info_row">';
 1099:             if ($numheaders == 5) {
 1100:                 $output .= '
 1101:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1102:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1103:              </tr>';
 1104:             } else {
 1105:                 $output .= '
 1106:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1107:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1108:              </tr>';
 1109:             }
 1110:             $rowtotal ++;
 1111:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1112:            </table>
 1113:           </td>
 1114:          </tr>
 1115:          <tr>
 1116:            <td>
 1117:             <table class="LC_nested">
 1118:              <tr class="LC_info_row">';
 1119:             if ($numheaders == 5) {
 1120:                 $output .= '
 1121:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1122:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1123:              </tr>';
 1124:             } else {
 1125:                 $output .= '
 1126:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1127:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1128:              </tr>';
 1129:             }
 1130:             $rowtotal ++;
 1131:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1132:         } elsif ($action eq 'requestcourses') {
 1133:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1134:             $rowtotal ++;
 1135:             $output .= &print_studentcode($settings,\$rowtotal).'
 1136:            </table>
 1137:           </td>
 1138:          </tr>
 1139:          <tr>
 1140:            <td>
 1141:             <table class="LC_nested">
 1142:              <tr class="LC_info_row">
 1143:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1144:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1145:                        &textbookcourses_javascript($settings).
 1146:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1147:             </table>
 1148:            </td>
 1149:           </tr>
 1150:          <tr>
 1151:            <td>
 1152:             <table class="LC_nested">
 1153:              <tr class="LC_info_row">
 1154:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1155:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1156:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1157:             </table>
 1158:            </td>
 1159:           </tr>
 1160:           <tr>
 1161:            <td>
 1162:             <table class="LC_nested">
 1163:              <tr class="LC_info_row">
 1164:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1165:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1166:              </tr>'.
 1167:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1168:         } elsif ($action eq 'requestauthor') {
 1169:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1170:             $rowtotal ++;
 1171:         } elsif ($action eq 'rolecolors') {
 1172:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1173:            </table>
 1174:           </td>
 1175:          </tr>
 1176:          <tr>
 1177:            <td>
 1178:             <table class="LC_nested">
 1179:              <tr class="LC_info_row">
 1180:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1181:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1182:               <td class="LC_right_item" style="vertical-align: top">'.
 1183:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1184:              </tr>'.
 1185:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1186:            </table>
 1187:           </td>
 1188:          </tr>
 1189:          <tr>
 1190:            <td>
 1191:             <table class="LC_nested">
 1192:              <tr class="LC_info_row">
 1193:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1194:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1195:              </tr>'.
 1196:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1197:             $rowtotal += 2;
 1198:         }
 1199:     } else {
 1200:         $output .= '
 1201:           <tr>
 1202:            <td>
 1203:             <table class="LC_nested">
 1204:              <tr class="LC_info_row">';
 1205:         if ($action eq 'login') {
 1206:             $output .= '  
 1207:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1208:         } elsif ($action eq 'serverstatuses') {
 1209:             $output .= '
 1210:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1211:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1212: 
 1213:         } else {
 1214:             $output .= '
 1215:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1216:         }
 1217:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1218:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1219:                        &mt($item->{'header'}->[0]->{'col2'});
 1220:             if ($action eq 'serverstatuses') {
 1221:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1222:             } 
 1223:         } else {
 1224:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1225:                        &mt($item->{'header'}->[0]->{'col2'});
 1226:         }
 1227:         $output .= '</td>';
 1228:         if ($item->{'header'}->[0]->{'col3'}) {
 1229:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1230:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1231:                             &mt($item->{'header'}->[0]->{'col3'});
 1232:             } else {
 1233:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1234:                            &mt($item->{'header'}->[0]->{'col3'});
 1235:             }
 1236:             if ($action eq 'serverstatuses') {
 1237:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1238:             }
 1239:             $output .= '</td>';
 1240:         }
 1241:         if ($item->{'header'}->[0]->{'col4'}) {
 1242:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1243:                        &mt($item->{'header'}->[0]->{'col4'});
 1244:         }
 1245:         $output .= '</tr>';
 1246:         $rowtotal ++;
 1247:         if ($action eq 'quotas') {
 1248:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1249:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1250:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1251:                  ($action eq 'ltitools') || ($action eq 'lti') ||
 1252:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1253:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1254:         }
 1255:     }
 1256:     $output .= '
 1257:    </table>
 1258:   </td>
 1259:  </tr>
 1260: </table><br />';
 1261:     return ($output,$rowtotal);
 1262: }
 1263: 
 1264: sub print_login {
 1265:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1266:     my ($css_class,$datatable,$switchserver,%lt);
 1267:     my %choices = &login_choices();
 1268:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1269:         %lt = &login_file_options();
 1270:         $switchserver = &check_switchserver($dom,$confname);
 1271:     }
 1272:     if ($caller eq 'service') {
 1273:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1274:         my $choice = $choices{'disallowlogin'};
 1275:         $css_class = ' class="LC_odd_row"';
 1276:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1277:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1278:                       '<th>'.$choices{'server'}.'</th>'.
 1279:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1280:                       '<th>'.$choices{'custompath'}.'</th>'.
 1281:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1282:         my %disallowed;
 1283:         if (ref($settings) eq 'HASH') {
 1284:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1285:                %disallowed = %{$settings->{'loginvia'}};
 1286:             }
 1287:         }
 1288:         foreach my $lonhost (sort(keys(%servers))) {
 1289:             my $direct = 'selected="selected"';
 1290:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1291:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1292:                     $direct = '';
 1293:                 }
 1294:             }
 1295:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1296:                           '<td><select name="'.$lonhost.'_server">'.
 1297:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1298:                           '</option>';
 1299:             foreach my $hostid (sort(keys(%servers))) {
 1300:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1301:                 my $selected = '';
 1302:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1303:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1304:                         $selected = 'selected="selected"';
 1305:                     }
 1306:                 }
 1307:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1308:                               $servers{$hostid}.'</option>';
 1309:             }
 1310:             $datatable .= '</select></td>'.
 1311:                           '<td><select name="'.$lonhost.'_serverpath">';
 1312:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1313:                 my $pathname = $path;
 1314:                 if ($path eq 'custom') {
 1315:                     $pathname = &mt('Custom Path').' ->';
 1316:                 }
 1317:                 my $selected = '';
 1318:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1319:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1320:                         $selected = 'selected="selected"';
 1321:                     }
 1322:                 } elsif ($path eq '') {
 1323:                     $selected = 'selected="selected"';
 1324:                 }
 1325:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1326:             }
 1327:             $datatable .= '</select></td>';
 1328:             my ($custom,$exempt);
 1329:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1330:                 $custom = $disallowed{$lonhost}{'custompath'};
 1331:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1332:             }
 1333:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1334:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1335:                           '</tr>';
 1336:         }
 1337:         $datatable .= '</table></td></tr>';
 1338:         return $datatable;
 1339:     } elsif ($caller eq 'page') {
 1340:         my %defaultchecked = ( 
 1341:                                'coursecatalog' => 'on',
 1342:                                'helpdesk'      => 'on',
 1343:                                'adminmail'     => 'off',
 1344:                                'newuser'       => 'off',
 1345:                              );
 1346:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1347:         my (%checkedon,%checkedoff);
 1348:         foreach my $item (@toggles) {
 1349:             if ($defaultchecked{$item} eq 'on') { 
 1350:                 $checkedon{$item} = ' checked="checked" ';
 1351:                 $checkedoff{$item} = ' ';
 1352:             } elsif ($defaultchecked{$item} eq 'off') {
 1353:                 $checkedoff{$item} = ' checked="checked" ';
 1354:                 $checkedon{$item} = ' ';
 1355:             }
 1356:         }
 1357:         my @images = ('img','logo','domlogo','login');
 1358:         my @logintext = ('textcol','bgcol');
 1359:         my @bgs = ('pgbg','mainbg','sidebg');
 1360:         my @links = ('link','alink','vlink');
 1361:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1362:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1363:         my (%is_custom,%designs);
 1364:         my %defaults = (
 1365:                        font => $defaultdesign{'login.font'},
 1366:                        );
 1367:         foreach my $item (@images) {
 1368:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1369:             $defaults{'showlogo'}{$item} = 1;
 1370:         }
 1371:         foreach my $item (@bgs) {
 1372:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1373:         }
 1374:         foreach my $item (@logintext) {
 1375:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1376:         }
 1377:         foreach my $item (@links) {
 1378:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1379:         }
 1380:         if (ref($settings) eq 'HASH') {
 1381:             foreach my $item (@toggles) {
 1382:                 if ($settings->{$item} eq '1') {
 1383:                     $checkedon{$item} =  ' checked="checked" ';
 1384:                     $checkedoff{$item} = ' ';
 1385:                 } elsif ($settings->{$item} eq '0') {
 1386:                     $checkedoff{$item} =  ' checked="checked" ';
 1387:                     $checkedon{$item} = ' ';
 1388:                 }
 1389:             }
 1390:             foreach my $item (@images) {
 1391:                 if (defined($settings->{$item})) {
 1392:                     $designs{$item} = $settings->{$item};
 1393:                     $is_custom{$item} = 1;
 1394:                 }
 1395:                 if (defined($settings->{'showlogo'}{$item})) {
 1396:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1397:                 }
 1398:             }
 1399:             foreach my $item (@logintext) {
 1400:                 if ($settings->{$item} ne '') {
 1401:                     $designs{'logintext'}{$item} = $settings->{$item};
 1402:                     $is_custom{$item} = 1;
 1403:                 }
 1404:             }
 1405:             if ($settings->{'font'} ne '') {
 1406:                 $designs{'font'} = $settings->{'font'};
 1407:                 $is_custom{'font'} = 1;
 1408:             }
 1409:             foreach my $item (@bgs) {
 1410:                 if ($settings->{$item} ne '') {
 1411:                     $designs{'bgs'}{$item} = $settings->{$item};
 1412:                     $is_custom{$item} = 1;
 1413:                 }
 1414:             }
 1415:             foreach my $item (@links) {
 1416:                 if ($settings->{$item} ne '') {
 1417:                     $designs{'links'}{$item} = $settings->{$item};
 1418:                     $is_custom{$item} = 1;
 1419:                 }
 1420:             }
 1421:         } else {
 1422:             if ($designhash{$dom.'.login.font'} ne '') {
 1423:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1424:                 $is_custom{'font'} = 1;
 1425:             }
 1426:             foreach my $item (@images) {
 1427:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1428:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1429:                     $is_custom{$item} = 1;
 1430:                 }
 1431:             }
 1432:             foreach my $item (@bgs) {
 1433:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1434:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1435:                     $is_custom{$item} = 1;
 1436:                 }
 1437:             }
 1438:             foreach my $item (@links) {
 1439:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1440:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1441:                     $is_custom{$item} = 1;
 1442:                 }
 1443:             }
 1444:         }
 1445:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1446:                                                       logo => 'Institution Logo',
 1447:                                                       domlogo => 'Domain Logo',
 1448:                                                       login => 'Login box');
 1449:         my $itemcount = 1;
 1450:         foreach my $item (@toggles) {
 1451:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1452:             $datatable .=  
 1453:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1454:                 '</td><td>'.
 1455:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1456:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1457:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1458:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1459:                 '</tr>';
 1460:             $itemcount ++;
 1461:         }
 1462:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1463:         $datatable .= '</tr></table></td></tr>';
 1464:     } elsif ($caller eq 'help') {
 1465:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1466:         my $itemcount = 1;
 1467:         $defaulturl = '/adm/loginproblems.html';
 1468:         $defaulttype = 'default';
 1469:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1470:         my @currlangs;
 1471:         if (ref($settings) eq 'HASH') {
 1472:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1473:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1474:                     next if ($settings->{'helpurl'}{$key} eq '');
 1475:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1476:                     $type{$key} = 'custom';
 1477:                     unless ($key eq 'nolang') {
 1478:                         push(@currlangs,$key);
 1479:                     }
 1480:                 }
 1481:             } elsif ($settings->{'helpurl'} ne '') {
 1482:                 $type{'nolang'} = 'custom';
 1483:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1484:             }
 1485:         }
 1486:         foreach my $lang ('nolang',sort(@currlangs)) {
 1487:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1488:             $datatable .= '<tr'.$css_class.'>';
 1489:             if ($url{$lang} eq '') {
 1490:                 $url{$lang} = $defaulturl;
 1491:             }
 1492:             if ($type{$lang} eq '') {
 1493:                 $type{$lang} = $defaulttype;
 1494:             }
 1495:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1496:             if ($lang eq 'nolang') {
 1497:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1498:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1499:             } else {
 1500:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1501:                                   $langchoices{$lang},
 1502:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1503:             }
 1504:             $datatable .= '</span></td>'."\n".
 1505:                           '<td class="LC_left_item">';
 1506:             if ($type{$lang} eq 'custom') {
 1507:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1508:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1509:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1510:             } else {
 1511:                 $datatable .= $lt{'upl'};
 1512:             }
 1513:             $datatable .='<br />';
 1514:             if ($switchserver) {
 1515:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1516:             } else {
 1517:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1518:             }
 1519:             $datatable .= '</td></tr>';
 1520:             $itemcount ++;
 1521:         }
 1522:         my @addlangs;
 1523:         foreach my $lang (sort(keys(%langchoices))) {
 1524:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1525:             push(@addlangs,$lang);
 1526:         }
 1527:         if (@addlangs > 0) {
 1528:             my %toadd;
 1529:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1530:             $toadd{''} = &mt('Select');
 1531:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1532:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1533:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1534:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1535:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1536:             if ($switchserver) {
 1537:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1538:             } else {
 1539:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1540:             }
 1541:             $datatable .= '</td></tr>';
 1542:             $itemcount ++;
 1543:         }
 1544:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1545:     } elsif ($caller eq 'headtag') {
 1546:         my %domservers = &Apache::lonnet::get_servers($dom);
 1547:         my $choice = $choices{'headtag'};
 1548:         $css_class = ' class="LC_odd_row"';
 1549:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1550:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1551:                       '<th>'.$choices{'current'}.'</th>'.
 1552:                       '<th>'.$choices{'action'}.'</th>'.
 1553:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1554:         my (%currurls,%currexempt);
 1555:         if (ref($settings) eq 'HASH') {
 1556:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1557:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1558:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1559:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1560:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1561:                     }
 1562:                 }
 1563:             }
 1564:         }
 1565:         foreach my $lonhost (sort(keys(%domservers))) {
 1566:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1567:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1568:             if ($currurls{$lonhost}) {
 1569:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1570:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1571:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1572:                               '">'.$lt{'curr'}.'</a></td>'.
 1573:                               '<td><span class="LC_nobreak"><label>'.
 1574:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1575:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1576:             } else {
 1577:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1578:             }
 1579:             $datatable .='<br />';
 1580:             if ($switchserver) {
 1581:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1582:             } else {
 1583:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1584:             }
 1585:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1586:         }
 1587:         $datatable .= '</table></td></tr>';
 1588:     } elsif ($caller eq 'saml') {
 1589:         my %domservers = &Apache::lonnet::get_servers($dom);
 1590:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1591:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1592:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1593:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1594:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso,%styleon,%styleoff);
 1595:         foreach my $lonhost (keys(%domservers)) {
 1596:             $samlurl{$lonhost} = '/adm/sso';
 1597:             $styleon{$lonhost} = 'display:none';
 1598:             $styleoff{$lonhost} = '';
 1599:         }
 1600:         if (ref($settings->{'saml'}) eq 'HASH') {
 1601:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1602:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1603:                     $saml{$lonhost} = 1;
 1604:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1605:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1606:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1607:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1608:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1609:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1610:                     $styleon{$lonhost} = '';
 1611:                     $styleoff{$lonhost} = 'display:none';
 1612:                 } else {
 1613:                     $styleon{$lonhost} = 'display:none';
 1614:                     $styleoff{$lonhost} = '';
 1615:                 }
 1616:             }
 1617:         }
 1618:         my $itemcount = 1;
 1619:         foreach my $lonhost (sort(keys(%domservers))) {
 1620:             my $samlon = ' ';
 1621:             my $samloff = ' checked="checked" ';
 1622:             if ($saml{$lonhost}) {
 1623:                 $samlon = $samloff;
 1624:                 $samloff = ' ';
 1625:             }
 1626:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1627:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1628:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1629:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1630:                           &mt('No').'</label>'.('&nbsp;'x2).
 1631:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1632:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1633:                           &mt('Yes').'</label></span></td>'.
 1634:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1635:                           '<table><tr><th colspan="5" align="center">'.&mt('SSO').'</th><th align="center">'.
 1636:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1637:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1638:                           '<th>'.&mt('Alt Text').'</th><th>'.&mt('URL').'</th>'.
 1639:                           '<th>'.&mt('Tool Tip').'</th><th>'.&mt('Text').'</th></tr>'.
 1640:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="8" value="'.
 1641:                           $samltext{$lonhost}.'" /></td><td>';
 1642:             if ($samlimg{$lonhost}) {
 1643:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1644:                               '<span class="LC_nobreak"><label>'.
 1645:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1646:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1647:             } else {
 1648:                 $datatable .= $lt{'upl'};
 1649:             }
 1650:             $datatable .='<br />';
 1651:             if ($switchserver) {
 1652:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1653:             } else {
 1654:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1655:             }
 1656:             $datatable .= '</td>'.
 1657:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="20" '.
 1658:                           'value="'.$samlalt{$lonhost}.'" /></td>'.
 1659:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="8" '.
 1660:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1661:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="15">'.
 1662:                           $samltitle{$lonhost}.'</textarea></td>'.
 1663:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="8" '.
 1664:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1665:                           '</table></td>'.
 1666:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1667:            $itemcount ++;
 1668:         }
 1669:         $datatable .= '</table></td></tr>';
 1670:     }
 1671:     return $datatable;
 1672: }
 1673: 
 1674: sub login_choices {
 1675:     my %choices =
 1676:         &Apache::lonlocal::texthash (
 1677:             coursecatalog => 'Display Course/Community Catalog link?',
 1678:             adminmail     => "Display Administrator's E-mail Address?",
 1679:             helpdesk      => 'Display "Contact Helpdesk" link',
 1680:             disallowlogin => "Login page requests redirected",
 1681:             hostid        => "Server",
 1682:             server        => "Redirect to:",
 1683:             serverpath    => "Path",
 1684:             custompath    => "Custom", 
 1685:             exempt        => "Exempt IP(s)",
 1686:             directlogin   => "No redirect",
 1687:             newuser       => "Link to create a user account",
 1688:             img           => "Header",
 1689:             logo          => "Main Logo",
 1690:             domlogo       => "Domain Logo",
 1691:             login         => "Log-in Header", 
 1692:             textcol       => "Text color",
 1693:             bgcol         => "Box color",
 1694:             bgs           => "Background colors",
 1695:             links         => "Link colors",
 1696:             font          => "Font color",
 1697:             pgbg          => "Header",
 1698:             mainbg        => "Page",
 1699:             sidebg        => "Login box",
 1700:             link          => "Link",
 1701:             alink         => "Active link",
 1702:             vlink         => "Visited link",
 1703:             headtag       => "Custom markup",
 1704:             action        => "Action",
 1705:             current       => "Current",
 1706:             samllanding   => "Dual login?",
 1707:             samloptions   => "Options",
 1708:         );
 1709:     return %choices;
 1710: }
 1711: 
 1712: sub login_file_options {
 1713:       return &Apache::lonlocal::texthash(
 1714:                                            del     => 'Delete?',
 1715:                                            rep     => 'Replace:',
 1716:                                            upl     => 'Upload:',
 1717:                                            curr    => 'View contents',
 1718:                                            default => 'Default',
 1719:                                            custom  => 'Custom',
 1720:                                            none    => 'None',
 1721:       );
 1722: }
 1723: 
 1724: sub print_ipaccess {
 1725:     my ($dom,$settings,$rowtotal) = @_;
 1726:     my $css_class;
 1727:     my $itemcount = 0;
 1728:     my $datatable;
 1729:     my %ordered;
 1730:     if (ref($settings) eq 'HASH') {
 1731:         foreach my $item (keys(%{$settings})) {
 1732:             if (ref($settings->{$item}) eq 'HASH') {
 1733:                 my $num = $settings->{$item}{'order'};
 1734:                 if ($num eq '') {
 1735:                     $num = scalar(keys(%{$settings}));
 1736:                 }
 1737:                 $ordered{$num} = $item;
 1738:             }
 1739:         }
 1740:     }
 1741:     my $maxnum = scalar(keys(%ordered));
 1742:     if (keys(%ordered)) {
 1743:         my @items = sort { $a <=> $b } keys(%ordered);
 1744:         for (my $i=0; $i<@items; $i++) {
 1745:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1746:             my $item = $ordered{$items[$i]};
 1747:             my ($name,$ipranges,%commblocks,%courses);
 1748:             if (ref($settings->{$item}) eq 'HASH') {
 1749:                 $name = $settings->{$item}->{'name'};
 1750:                 $ipranges = $settings->{$item}->{'ip'};
 1751:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1752:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1753:                 }
 1754:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1755:                     %courses = %{$settings->{$item}->{'courses'}};
 1756:                 }
 1757:             }
 1758:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1759:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1760:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1761:             for (my $k=0; $k<=$maxnum; $k++) {
 1762:                 my $vpos = $k+1;
 1763:                 my $selstr;
 1764:                 if ($k == $i) {
 1765:                     $selstr = ' selected="selected" ';
 1766:                 }
 1767:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1768:             }
 1769:             $datatable .= '</select>'.('&nbsp;'x2).
 1770:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1771:                 &mt('Delete?').'</label></span></td>'.
 1772:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1773:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1774:                 '</td></tr>';
 1775:             $itemcount ++;
 1776:         }
 1777:     }
 1778:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1779:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1780:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1781:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1782:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1783:     for (my $k=0; $k<$maxnum+1; $k++) {
 1784:         my $vpos = $k+1;
 1785:         my $selstr;
 1786:         if ($k == $maxnum) {
 1787:             $selstr = ' selected="selected" ';
 1788:         }
 1789:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1790:     }
 1791:     $datatable .= '</select>&nbsp;'."\n".
 1792:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1793:                   '<td colspan="2">'.
 1794:                   &ipaccess_options('add',$itemcount,$dom).
 1795:                   '</td>'."\n".
 1796:                   '</tr>'."\n";
 1797:     $$rowtotal ++;
 1798:     return $datatable;
 1799: }
 1800: 
 1801: sub ipaccess_options {
 1802:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1803:     my (%currblocks,%currcourses,$output);
 1804:     if (ref($blocksref) eq 'HASH') {
 1805:         %currblocks = %{$blocksref};
 1806:     }
 1807:     if (ref($coursesref) eq 'HASH') {
 1808:         %currcourses = %{$coursesref};
 1809:     }
 1810:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1811:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1812:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1813:               '</span></fieldset>'.
 1814:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1815:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1816:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1817:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1818:               $ipranges.'</textarea></fieldset>'.
 1819:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1820:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1821:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1822:               '<table>';
 1823:     foreach my $cid (sort(keys(%currcourses))) {
 1824:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1825:         $output .= '<tr><td><span class="LC_nobreak">'.
 1826:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1827:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1828:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1829:     }
 1830:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1831:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1832:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1833:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1834:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1835:                '</span></td></tr></table>'."\n".
 1836:                '</fieldset>';
 1837:     return $output;
 1838: }
 1839: 
 1840: sub blocker_checkboxes {
 1841:     my ($num,$blocks) = @_;
 1842:     my ($typeorder,$types) = &commblocktype_text();
 1843:     my $numinrow = 6;
 1844:     my $output = '<table>';
 1845:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1846:         my $block = $typeorder->[$i];
 1847:         my $blockstatus;
 1848:         if (ref($blocks) eq 'HASH') {
 1849:             if ($blocks->{$block} eq 'on') {
 1850:                 $blockstatus = 'checked="checked"';
 1851:             }
 1852:         }
 1853:         my $rem = $i%($numinrow);
 1854:         if ($rem == 0) {
 1855:             if ($i > 0) {
 1856:                 $output .= '</tr>';
 1857:             }
 1858:             $output .= '<tr>';
 1859:         }
 1860:         if ($i == scalar(@{$typeorder})-1) {
 1861:             my $colsleft = $numinrow-$rem;
 1862:             if ($colsleft > 1) {
 1863:                 $output .= '<td colspan="'.$colsleft.'">';
 1864:             } else {
 1865:                 $output .= '<td>';
 1866:             }
 1867:         } else {
 1868:             $output .= '<td>';
 1869:         }
 1870:         my $item = 'ipaccess_block_'.$num;
 1871:         if ($blockstatus) {
 1872:             $blockstatus = ' '.$blockstatus;
 1873:         }
 1874:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1875:                    '<input type="checkbox" name="'.$item.'"'.
 1876:                    $blockstatus.' value="'.$block.'"'.' />'.
 1877:                    $types->{$block}.'</label></span>'."\n".
 1878:                    '<br /></td>';
 1879:     }
 1880:     $output .= '</tr></table>';
 1881:     return $output;
 1882: }
 1883: 
 1884: sub commblocktype_text {
 1885:     my %types = &Apache::lonlocal::texthash(
 1886:         'com' => 'Messaging',
 1887:         'chat' => 'Chat Room',
 1888:         'boards' => 'Discussion',
 1889:         'port' => 'Portfolio',
 1890:         'groups' => 'Groups',
 1891:         'blogs' => 'Blogs',
 1892:         'about' => 'User Information',
 1893:         'printout' => 'Printouts',
 1894:         'passwd' => 'Change Password',
 1895:         'grades' => 'Gradebook',
 1896:         'search' => 'Course search',
 1897:         'wishlist' => 'Stored links',
 1898:         'annotate' => 'Annotations',
 1899:     );
 1900:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 1901:     return ($typeorder,\%types);
 1902: }
 1903: 
 1904: sub print_rolecolors {
 1905:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1906:     my %choices = &color_font_choices();
 1907:     my @bgs = ('pgbg','tabbg','sidebg');
 1908:     my @links = ('link','alink','vlink');
 1909:     my @images = ('img');
 1910:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1911:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1912:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1913:     my (%is_custom,%designs);
 1914:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1915:     if (ref($settings) eq 'HASH') {
 1916:         if (ref($settings->{$role}) eq 'HASH') {
 1917:             if ($settings->{$role}->{'img'} ne '') {
 1918:                 $designs{'img'} = $settings->{$role}->{'img'};
 1919:                 $is_custom{'img'} = 1;
 1920:             }
 1921:             if ($settings->{$role}->{'font'} ne '') {
 1922:                 $designs{'font'} = $settings->{$role}->{'font'};
 1923:                 $is_custom{'font'} = 1;
 1924:             }
 1925:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1926:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1927:                 $is_custom{'fontmenu'} = 1;
 1928:             }
 1929:             foreach my $item (@bgs) {
 1930:                 if ($settings->{$role}->{$item} ne '') {
 1931:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1932:                     $is_custom{$item} = 1;
 1933:                 }
 1934:             }
 1935:             foreach my $item (@links) {
 1936:                 if ($settings->{$role}->{$item} ne '') {
 1937:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1938:                     $is_custom{$item} = 1;
 1939:                 }
 1940:             }
 1941:         }
 1942:     } else {
 1943:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1944:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1945:             $is_custom{'img'} = 1;
 1946:         }
 1947:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1948:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1949:             $is_custom{'fontmenu'} = 1; 
 1950:         }
 1951:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1952:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1953:             $is_custom{'font'} = 1;
 1954:         }
 1955:         foreach my $item (@bgs) {
 1956:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1957:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1958:                 $is_custom{$item} = 1;
 1959:             
 1960:             }
 1961:         }
 1962:         foreach my $item (@links) {
 1963:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1964:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1965:                 $is_custom{$item} = 1;
 1966:             }
 1967:         }
 1968:     }
 1969:     my $itemcount = 1;
 1970:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1971:     $datatable .= '</tr></table></td></tr>';
 1972:     return $datatable;
 1973: }
 1974: 
 1975: sub role_defaults {
 1976:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1977:     my %defaults;
 1978:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1979:         return %defaults;
 1980:     }
 1981:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1982:     if ($role eq 'login') {
 1983:         %defaults = (
 1984:                        font => $defaultdesign{$role.'.font'},
 1985:                     );
 1986:         if (ref($logintext) eq 'ARRAY') {
 1987:             foreach my $item (@{$logintext}) {
 1988:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1989:             }
 1990:         }
 1991:         foreach my $item (@{$images}) {
 1992:             $defaults{'showlogo'}{$item} = 1;
 1993:         }
 1994:     } else {
 1995:         %defaults = (
 1996:                        img => $defaultdesign{$role.'.img'},
 1997:                        font => $defaultdesign{$role.'.font'},
 1998:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1999:                     );
 2000:     }
 2001:     foreach my $item (@{$bgs}) {
 2002:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2003:     }
 2004:     foreach my $item (@{$links}) {
 2005:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2006:     }
 2007:     foreach my $item (@{$images}) {
 2008:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2009:     }
 2010:     return %defaults;
 2011: }
 2012: 
 2013: sub display_color_options {
 2014:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2015:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2016:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2017:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2018:     my $datatable = '<tr'.$css_class.'>'.
 2019:         '<td>'.$choices->{'font'}.'</td>';
 2020:     if (!$is_custom->{'font'}) {
 2021:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2022:     } else {
 2023:         $datatable .= '<td>&nbsp;</td>';
 2024:     }
 2025:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2026: 
 2027:     $datatable .= '<td><span class="LC_nobreak">'.
 2028:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2029:                   ' value="'.$current_color.'" />&nbsp;'.
 2030:                   '&nbsp;</span></td></tr>';
 2031:     unless ($role eq 'login') { 
 2032:         $datatable .= '<tr'.$css_class.'>'.
 2033:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2034:         if (!$is_custom->{'fontmenu'}) {
 2035:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2036:         } else {
 2037:             $datatable .= '<td>&nbsp;</td>';
 2038:         }
 2039: 	$current_color = $designs->{'fontmenu'} ?
 2040: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2041:         $datatable .= '<td><span class="LC_nobreak">'.
 2042:                       '<input class="colorchooser" type="text" size="10" name="'
 2043: 		      .$role.'_fontmenu"'.
 2044:                       ' value="'.$current_color.'" />&nbsp;'.
 2045:                       '&nbsp;</span></td></tr>';
 2046:     }
 2047:     my $switchserver = &check_switchserver($dom,$confname);
 2048:     foreach my $img (@{$images}) {
 2049: 	$itemcount ++;
 2050:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2051:         $datatable .= '<tr'.$css_class.'>'.
 2052:                       '<td>'.$choices->{$img};
 2053:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 2054:         if ($role eq 'login') {
 2055:             if ($img eq 'login') {
 2056:                 $login_hdr_pick =
 2057:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2058:                 $logincolors =
 2059:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2060:                                        $designs,$defaults);
 2061:             } elsif ($img ne 'domlogo') {
 2062:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 2063:             }
 2064:         }
 2065:         $datatable .= '</td>';
 2066:         if ($designs->{$img} ne '') {
 2067:             $imgfile = $designs->{$img};
 2068: 	    $img_import = ($imgfile =~ m{^/adm/});
 2069:         } else {
 2070:             $imgfile = $defaults->{$img};
 2071:         }
 2072:         if ($imgfile) {
 2073:             my ($showfile,$fullsize);
 2074:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2075:                 my $urldir = $1;
 2076:                 my $filename = $2;
 2077:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2078:                 if (@info) {
 2079:                     my $thumbfile = 'tn-'.$filename;
 2080:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2081:                     if (@thumb) {
 2082:                         $showfile = $urldir.'/'.$thumbfile;
 2083:                     } else {
 2084:                         $showfile = $imgfile;
 2085:                     }
 2086:                 } else {
 2087:                     $showfile = '';
 2088:                 }
 2089:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2090:                 $showfile = $imgfile;
 2091:                 my $imgdir = $1;
 2092:                 my $filename = $2;
 2093:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2094:                     $showfile = "/$imgdir/tn-".$filename;
 2095:                 } else {
 2096:                     my $input = $londocroot.$imgfile;
 2097:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2098:                     if (!-e $output) {
 2099:                         my ($width,$height) = &thumb_dimensions();
 2100:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2101:                         if ($fullwidth ne '' && $fullheight ne '') {
 2102:                             if ($fullwidth > $width && $fullheight > $height) { 
 2103:                                 my $size = $width.'x'.$height;
 2104:                                 my @args = ('convert','-sample',$size,$input,$output);
 2105:                                 system({$args[0]} @args);
 2106:                                 $showfile = "/$imgdir/tn-".$filename;
 2107:                             }
 2108:                         }
 2109:                     }
 2110:                 }
 2111:             }
 2112:             if ($showfile) {
 2113:                 if ($showfile =~ m{^/(adm|res)/}) {
 2114:                     if ($showfile =~ m{^/res/}) {
 2115:                         my $local_showfile =
 2116:                             &Apache::lonnet::filelocation('',$showfile);
 2117:                         &Apache::lonnet::repcopy($local_showfile);
 2118:                     }
 2119:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2120:                 }
 2121:                 if ($imgfile) {
 2122:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2123:                         if ($imgfile =~ m{^/res/}) {
 2124:                             my $local_imgfile =
 2125:                                 &Apache::lonnet::filelocation('',$imgfile);
 2126:                             &Apache::lonnet::repcopy($local_imgfile);
 2127:                         }
 2128:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2129:                     } else {
 2130:                         $fullsize = $imgfile;
 2131:                     }
 2132:                 }
 2133:                 $datatable .= '<td>';
 2134:                 if ($img eq 'login') {
 2135:                     $datatable .= $login_hdr_pick;
 2136:                 } 
 2137:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2138:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2139:             } else {
 2140:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2141:                               &mt('Upload:').'<br />';
 2142:             }
 2143:         } else {
 2144:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2145:                           &mt('Upload:').'<br />';
 2146:         }
 2147:         if ($switchserver) {
 2148:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2149:         } else {
 2150:             if ($img ne 'login') { # suppress file selection for Log-in header
 2151:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2152:             }
 2153:         }
 2154:         $datatable .= '</td></tr>';
 2155:     }
 2156:     $itemcount ++;
 2157:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2158:     $datatable .= '<tr'.$css_class.'>'.
 2159:                   '<td>'.$choices->{'bgs'}.'</td>';
 2160:     my $bgs_def;
 2161:     foreach my $item (@{$bgs}) {
 2162:         if (!$is_custom->{$item}) {
 2163:             $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>';
 2164:         }
 2165:     }
 2166:     if ($bgs_def) {
 2167:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2168:     } else {
 2169:         $datatable .= '<td>&nbsp;</td>';
 2170:     }
 2171:     $datatable .= '<td class="LC_right_item">'.
 2172:                   '<table border="0"><tr>';
 2173: 
 2174:     foreach my $item (@{$bgs}) {
 2175:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2176: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2177:         if ($designs->{'bgs'}{$item}) {
 2178:             $datatable .= '&nbsp;';
 2179:         }
 2180:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2181:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2182:     }
 2183:     $datatable .= '</tr></table></td></tr>';
 2184:     $itemcount ++;
 2185:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2186:     $datatable .= '<tr'.$css_class.'>'.
 2187:                   '<td>'.$choices->{'links'}.'</td>';
 2188:     my $links_def;
 2189:     foreach my $item (@{$links}) {
 2190:         if (!$is_custom->{$item}) {
 2191:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2192:         }
 2193:     }
 2194:     if ($links_def) {
 2195:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2196:     } else {
 2197:         $datatable .= '<td>&nbsp;</td>';
 2198:     }
 2199:     $datatable .= '<td class="LC_right_item">'.
 2200:                   '<table border="0"><tr>';
 2201:     foreach my $item (@{$links}) {
 2202: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2203:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2204:         if ($designs->{'links'}{$item}) {
 2205:             $datatable.='&nbsp;';
 2206:         }
 2207:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2208:                       '" /></td>';
 2209:     }
 2210:     $$rowtotal += $itemcount;
 2211:     return $datatable;
 2212: }
 2213: 
 2214: sub logo_display_options {
 2215:     my ($img,$defaults,$designs) = @_;
 2216:     my $checkedon;
 2217:     if (ref($defaults) eq 'HASH') {
 2218:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2219:             if ($defaults->{'showlogo'}{$img}) {
 2220:                 $checkedon = 'checked="checked" ';     
 2221:             }
 2222:         } 
 2223:     }
 2224:     if (ref($designs) eq 'HASH') {
 2225:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2226:             if (defined($designs->{'showlogo'}{$img})) {
 2227:                 if ($designs->{'showlogo'}{$img} == 0) {
 2228:                     $checkedon = '';
 2229:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2230:                     $checkedon = 'checked="checked" ';
 2231:                 }
 2232:             }
 2233:         }
 2234:     }
 2235:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2236:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2237:            &mt('show').'</label>'."\n";
 2238: }
 2239: 
 2240: sub login_header_options  {
 2241:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2242:     my $output = '';
 2243:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2244:         $output .= &mt('Text default(s):').'<br />';
 2245:         if (!$is_custom->{'textcol'}) {
 2246:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2247:                        '&nbsp;&nbsp;&nbsp;';
 2248:         }
 2249:         if (!$is_custom->{'bgcol'}) {
 2250:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2251:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2252:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2253:         }
 2254:         $output .= '<br />';
 2255:     }
 2256:     $output .='<br />';
 2257:     return $output;
 2258: }
 2259: 
 2260: sub login_text_colors {
 2261:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2262:     my $color_menu = '<table border="0"><tr>';
 2263:     foreach my $item (@{$logintext}) {
 2264:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2265:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2266:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2267:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2268:     }
 2269:     $color_menu .= '</tr></table><br />';
 2270:     return $color_menu;
 2271: }
 2272: 
 2273: sub image_changes {
 2274:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2275:     my $output;
 2276:     if ($img eq 'login') {
 2277:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2278:     } elsif (!$is_custom) {
 2279:         if ($img ne 'domlogo') {
 2280:             $output = &mt('Default image:').'<br />';
 2281:         } else {
 2282:             $output = &mt('Default in use:').'<br />';
 2283:         }
 2284:     }
 2285:     if ($img ne 'login') {
 2286:         if ($img_import) {
 2287:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2288:         }
 2289:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2290:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2291:         if ($is_custom) {
 2292:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2293:                        '<input type="checkbox" name="'.
 2294:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2295:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2296:         } else {
 2297:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2298:         }
 2299:     }
 2300:     return $output;
 2301: }
 2302: 
 2303: sub print_quotas {
 2304:     my ($dom,$settings,$rowtotal,$action) = @_;
 2305:     my $context;
 2306:     if ($action eq 'quotas') {
 2307:         $context = 'tools';
 2308:     } else {
 2309:         $context = $action;
 2310:     }
 2311:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2312:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2313:     my $typecount = 0;
 2314:     my ($css_class,%titles);
 2315:     if ($context eq 'requestcourses') {
 2316:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2317:         @options =('norequest','approval','validate','autolimit');
 2318:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2319:         %titles = &courserequest_titles();
 2320:     } elsif ($context eq 'requestauthor') {
 2321:         @usertools = ('author');
 2322:         @options = ('norequest','approval','automatic');
 2323:         %titles = &authorrequest_titles();
 2324:     } else {
 2325:         @usertools = ('aboutme','blog','webdav','portfolio');
 2326:         %titles = &tool_titles();
 2327:     }
 2328:     if (ref($types) eq 'ARRAY') {
 2329:         foreach my $type (@{$types}) {
 2330:             my ($currdefquota,$currauthorquota);
 2331:             unless (($context eq 'requestcourses') ||
 2332:                     ($context eq 'requestauthor')) {
 2333:                 if (ref($settings) eq 'HASH') {
 2334:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2335:                         $currdefquota = $settings->{defaultquota}->{$type};
 2336:                     } else {
 2337:                         $currdefquota = $settings->{$type};
 2338:                     }
 2339:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2340:                         $currauthorquota = $settings->{authorquota}->{$type};
 2341:                     }
 2342:                 }
 2343:             }
 2344:             if (defined($usertypes->{$type})) {
 2345:                 $typecount ++;
 2346:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2347:                 $datatable .= '<tr'.$css_class.'>'.
 2348:                               '<td>'.$usertypes->{$type}.'</td>'.
 2349:                               '<td class="LC_left_item">';
 2350:                 if ($context eq 'requestcourses') {
 2351:                     $datatable .= '<table><tr>';
 2352:                 }
 2353:                 my %cell;  
 2354:                 foreach my $item (@usertools) {
 2355:                     if ($context eq 'requestcourses') {
 2356:                         my ($curroption,$currlimit);
 2357:                         if (ref($settings) eq 'HASH') {
 2358:                             if (ref($settings->{$item}) eq 'HASH') {
 2359:                                 $curroption = $settings->{$item}->{$type};
 2360:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2361:                                     $currlimit = $1; 
 2362:                                 }
 2363:                             }
 2364:                         }
 2365:                         if (!$curroption) {
 2366:                             $curroption = 'norequest';
 2367:                         }
 2368:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2369:                         foreach my $option (@options) {
 2370:                             my $val = $option;
 2371:                             if ($option eq 'norequest') {
 2372:                                 $val = 0;  
 2373:                             }
 2374:                             if ($option eq 'validate') {
 2375:                                 my $canvalidate = 0;
 2376:                                 if (ref($validations{$item}) eq 'HASH') { 
 2377:                                     if ($validations{$item}{$type}) {
 2378:                                         $canvalidate = 1;
 2379:                                     }
 2380:                                 }
 2381:                                 next if (!$canvalidate);
 2382:                             }
 2383:                             my $checked = '';
 2384:                             if ($option eq $curroption) {
 2385:                                 $checked = ' checked="checked"';
 2386:                             } elsif ($option eq 'autolimit') {
 2387:                                 if ($curroption =~ /^autolimit/) {
 2388:                                     $checked = ' checked="checked"';
 2389:                                 }                       
 2390:                             } 
 2391:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2392:                                   '<input type="radio" name="crsreq_'.$item.
 2393:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2394:                                   $titles{$option}.'</label>';
 2395:                             if ($option eq 'autolimit') {
 2396:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2397:                                                 $item.'_limit_'.$type.'" size="1" '.
 2398:                                                 'value="'.$currlimit.'" />';
 2399:                             }
 2400:                             $cell{$item} .= '</span> ';
 2401:                             if ($option eq 'autolimit') {
 2402:                                 $cell{$item} .= $titles{'unlimited'};
 2403:                             }
 2404:                         }
 2405:                     } elsif ($context eq 'requestauthor') {
 2406:                         my $curroption;
 2407:                         if (ref($settings) eq 'HASH') {
 2408:                             $curroption = $settings->{$type};
 2409:                         }
 2410:                         if (!$curroption) {
 2411:                             $curroption = 'norequest';
 2412:                         }
 2413:                         foreach my $option (@options) {
 2414:                             my $val = $option;
 2415:                             if ($option eq 'norequest') {
 2416:                                 $val = 0;
 2417:                             }
 2418:                             my $checked = '';
 2419:                             if ($option eq $curroption) {
 2420:                                 $checked = ' checked="checked"';
 2421:                             }
 2422:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2423:                                   '<input type="radio" name="authorreq_'.$type.
 2424:                                   '" value="'.$val.'"'.$checked.' />'.
 2425:                                   $titles{$option}.'</label></span>&nbsp; ';
 2426:                         }
 2427:                     } else {
 2428:                         my $checked = 'checked="checked" ';
 2429:                         if (ref($settings) eq 'HASH') {
 2430:                             if (ref($settings->{$item}) eq 'HASH') {
 2431:                                 if ($settings->{$item}->{$type} == 0) {
 2432:                                     $checked = '';
 2433:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2434:                                     $checked =  'checked="checked" ';
 2435:                                 }
 2436:                             }
 2437:                         }
 2438:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2439:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2440:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2441:                                       '</label></span>&nbsp; ';
 2442:                     }
 2443:                 }
 2444:                 if ($context eq 'requestcourses') {
 2445:                     $datatable .= '</tr><tr>';
 2446:                     foreach my $item (@usertools) {
 2447:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2448:                     }
 2449:                     $datatable .= '</tr></table>';
 2450:                 }
 2451:                 $datatable .= '</td>';
 2452:                 unless (($context eq 'requestcourses') ||
 2453:                         ($context eq 'requestauthor')) {
 2454:                     $datatable .= 
 2455:                               '<td class="LC_right_item">'.
 2456:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2457:                               '<input type="text" name="quota_'.$type.
 2458:                               '" value="'.$currdefquota.
 2459:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2460:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2461:                               '<input type="text" name="authorquota_'.$type.
 2462:                               '" value="'.$currauthorquota.
 2463:                               '" size="5" /></span></td>';
 2464:                 }
 2465:                 $datatable .= '</tr>';
 2466:             }
 2467:         }
 2468:     }
 2469:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2470:         $defaultquota = '20';
 2471:         $authorquota = '500';
 2472:         if (ref($settings) eq 'HASH') {
 2473:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2474:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2475:             } elsif (defined($settings->{'default'})) {
 2476:                 $defaultquota = $settings->{'default'};
 2477:             }
 2478:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2479:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2480:             }
 2481:         }
 2482:     }
 2483:     $typecount ++;
 2484:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2485:     $datatable .= '<tr'.$css_class.'>'.
 2486:                   '<td>'.$othertitle.'</td>'.
 2487:                   '<td class="LC_left_item">';
 2488:     if ($context eq 'requestcourses') {
 2489:         $datatable .= '<table><tr>';
 2490:     }
 2491:     my %defcell;
 2492:     foreach my $item (@usertools) {
 2493:         if ($context eq 'requestcourses') {
 2494:             my ($curroption,$currlimit);
 2495:             if (ref($settings) eq 'HASH') {
 2496:                 if (ref($settings->{$item}) eq 'HASH') {
 2497:                     $curroption = $settings->{$item}->{'default'};
 2498:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2499:                         $currlimit = $1;
 2500:                     }
 2501:                 }
 2502:             }
 2503:             if (!$curroption) {
 2504:                 $curroption = 'norequest';
 2505:             }
 2506:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2507:             foreach my $option (@options) {
 2508:                 my $val = $option;
 2509:                 if ($option eq 'norequest') {
 2510:                     $val = 0;
 2511:                 }
 2512:                 if ($option eq 'validate') {
 2513:                     my $canvalidate = 0;
 2514:                     if (ref($validations{$item}) eq 'HASH') {
 2515:                         if ($validations{$item}{'default'}) {
 2516:                             $canvalidate = 1;
 2517:                         }
 2518:                     }
 2519:                     next if (!$canvalidate);
 2520:                 }
 2521:                 my $checked = '';
 2522:                 if ($option eq $curroption) {
 2523:                     $checked = ' checked="checked"';
 2524:                 } elsif ($option eq 'autolimit') {
 2525:                     if ($curroption =~ /^autolimit/) {
 2526:                         $checked = ' checked="checked"';
 2527:                     }
 2528:                 }
 2529:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2530:                                   '<input type="radio" name="crsreq_'.$item.
 2531:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2532:                                   $titles{$option}.'</label>';
 2533:                 if ($option eq 'autolimit') {
 2534:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2535:                                        $item.'_limit_default" size="1" '.
 2536:                                        'value="'.$currlimit.'" />';
 2537:                 }
 2538:                 $defcell{$item} .= '</span> ';
 2539:                 if ($option eq 'autolimit') {
 2540:                     $defcell{$item} .= $titles{'unlimited'};
 2541:                 }
 2542:             }
 2543:         } elsif ($context eq 'requestauthor') {
 2544:             my $curroption;
 2545:             if (ref($settings) eq 'HASH') {
 2546:                 $curroption = $settings->{'default'};
 2547:             }
 2548:             if (!$curroption) {
 2549:                 $curroption = 'norequest';
 2550:             }
 2551:             foreach my $option (@options) {
 2552:                 my $val = $option;
 2553:                 if ($option eq 'norequest') {
 2554:                     $val = 0;
 2555:                 }
 2556:                 my $checked = '';
 2557:                 if ($option eq $curroption) {
 2558:                     $checked = ' checked="checked"';
 2559:                 }
 2560:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2561:                               '<input type="radio" name="authorreq_default"'.
 2562:                               ' value="'.$val.'"'.$checked.' />'.
 2563:                               $titles{$option}.'</label></span>&nbsp; ';
 2564:             }
 2565:         } else {
 2566:             my $checked = 'checked="checked" ';
 2567:             if (ref($settings) eq 'HASH') {
 2568:                 if (ref($settings->{$item}) eq 'HASH') {
 2569:                     if ($settings->{$item}->{'default'} == 0) {
 2570:                         $checked = '';
 2571:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2572:                         $checked = 'checked="checked" ';
 2573:                     }
 2574:                 }
 2575:             }
 2576:             $datatable .= '<span class="LC_nobreak"><label>'.
 2577:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2578:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2579:                           '</label></span>&nbsp; ';
 2580:         }
 2581:     }
 2582:     if ($context eq 'requestcourses') {
 2583:         $datatable .= '</tr><tr>';
 2584:         foreach my $item (@usertools) {
 2585:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2586:         }
 2587:         $datatable .= '</tr></table>';
 2588:     }
 2589:     $datatable .= '</td>';
 2590:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2591:         $datatable .= '<td class="LC_right_item">'.
 2592:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2593:                       '<input type="text" name="defaultquota" value="'.
 2594:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2595:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2596:                       '<input type="text" name="authorquota" value="'.
 2597:                       $authorquota.'" size="5" /></span></td>';
 2598:     }
 2599:     $datatable .= '</tr>';
 2600:     $typecount ++;
 2601:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2602:     $datatable .= '<tr'.$css_class.'>'.
 2603:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2604:     if ($context eq 'requestcourses') {
 2605:         $datatable .= &mt('(overrides affiliation, if set)').
 2606:                       '</td>'.
 2607:                       '<td class="LC_left_item">'.
 2608:                       '<table><tr>';
 2609:     } else {
 2610:         $datatable .= &mt('(overrides affiliation, if checked)').
 2611:                       '</td>'.
 2612:                       '<td class="LC_left_item" colspan="2">'.
 2613:                       '<br />';
 2614:     }
 2615:     my %advcell;
 2616:     foreach my $item (@usertools) {
 2617:         if ($context eq 'requestcourses') {
 2618:             my ($curroption,$currlimit);
 2619:             if (ref($settings) eq 'HASH') {
 2620:                 if (ref($settings->{$item}) eq 'HASH') {
 2621:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2622:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2623:                         $currlimit = $1;
 2624:                     }
 2625:                 }
 2626:             }
 2627:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2628:             my $checked = '';
 2629:             if ($curroption eq '') {
 2630:                 $checked = ' checked="checked"';
 2631:             }
 2632:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2633:                                '<input type="radio" name="crsreq_'.$item.
 2634:                                '__LC_adv" value=""'.$checked.' />'.
 2635:                                &mt('No override set').'</label></span>&nbsp; ';
 2636:             foreach my $option (@options) {
 2637:                 my $val = $option;
 2638:                 if ($option eq 'norequest') {
 2639:                     $val = 0;
 2640:                 }
 2641:                 if ($option eq 'validate') {
 2642:                     my $canvalidate = 0;
 2643:                     if (ref($validations{$item}) eq 'HASH') {
 2644:                         if ($validations{$item}{'_LC_adv'}) {
 2645:                             $canvalidate = 1;
 2646:                         }
 2647:                     }
 2648:                     next if (!$canvalidate);
 2649:                 }
 2650:                 my $checked = '';
 2651:                 if ($val eq $curroption) {
 2652:                     $checked = ' checked="checked"';
 2653:                 } elsif ($option eq 'autolimit') {
 2654:                     if ($curroption =~ /^autolimit/) {
 2655:                         $checked = ' checked="checked"';
 2656:                     }
 2657:                 }
 2658:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2659:                                   '<input type="radio" name="crsreq_'.$item.
 2660:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2661:                                   $titles{$option}.'</label>';
 2662:                 if ($option eq 'autolimit') {
 2663:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2664:                                        $item.'_limit__LC_adv" size="1" '.
 2665:                                        'value="'.$currlimit.'" />';
 2666:                 }
 2667:                 $advcell{$item} .= '</span> ';
 2668:                 if ($option eq 'autolimit') {
 2669:                     $advcell{$item} .= $titles{'unlimited'};
 2670:                 }
 2671:             }
 2672:         } elsif ($context eq 'requestauthor') {
 2673:             my $curroption;
 2674:             if (ref($settings) eq 'HASH') {
 2675:                 $curroption = $settings->{'_LC_adv'};
 2676:             }
 2677:             my $checked = '';
 2678:             if ($curroption eq '') {
 2679:                 $checked = ' checked="checked"';
 2680:             }
 2681:             $datatable .= '<span class="LC_nobreak"><label>'.
 2682:                           '<input type="radio" name="authorreq__LC_adv"'.
 2683:                           ' value=""'.$checked.' />'.
 2684:                           &mt('No override set').'</label></span>&nbsp; ';
 2685:             foreach my $option (@options) {
 2686:                 my $val = $option;
 2687:                 if ($option eq 'norequest') {
 2688:                     $val = 0;
 2689:                 }
 2690:                 my $checked = '';
 2691:                 if ($val eq $curroption) {
 2692:                     $checked = ' checked="checked"';
 2693:                 }
 2694:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2695:                               '<input type="radio" name="authorreq__LC_adv"'.
 2696:                               ' value="'.$val.'"'.$checked.' />'.
 2697:                               $titles{$option}.'</label></span>&nbsp; ';
 2698:             }
 2699:         } else {
 2700:             my $checked = 'checked="checked" ';
 2701:             if (ref($settings) eq 'HASH') {
 2702:                 if (ref($settings->{$item}) eq 'HASH') {
 2703:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2704:                         $checked = '';
 2705:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2706:                         $checked = 'checked="checked" ';
 2707:                     }
 2708:                 }
 2709:             }
 2710:             $datatable .= '<span class="LC_nobreak"><label>'.
 2711:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2712:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2713:                           '</label></span>&nbsp; ';
 2714:         }
 2715:     }
 2716:     if ($context eq 'requestcourses') {
 2717:         $datatable .= '</tr><tr>';
 2718:         foreach my $item (@usertools) {
 2719:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2720:         }
 2721:         $datatable .= '</tr></table>';
 2722:     }
 2723:     $datatable .= '</td></tr>';
 2724:     $$rowtotal += $typecount;
 2725:     return $datatable;
 2726: }
 2727: 
 2728: sub print_requestmail {
 2729:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2730:     my ($now,$datatable,%currapp);
 2731:     $now = time;
 2732:     if (ref($settings) eq 'HASH') {
 2733:         if (ref($settings->{'notify'}) eq 'HASH') {
 2734:             if ($settings->{'notify'}{'approval'} ne '') {
 2735:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2736:             }
 2737:         }
 2738:     }
 2739:     my $numinrow = 2;
 2740:     my $css_class;
 2741:     if ($$rowtotal%2) {
 2742:         $css_class = 'LC_odd_row';
 2743:     }
 2744:     if ($customcss) {
 2745:         $css_class .= " $customcss";
 2746:     }
 2747:     $css_class =~ s/^\s+//;
 2748:     if ($css_class) {
 2749:         $css_class = ' class="'.$css_class.'"';
 2750:     }
 2751:     if ($rowstyle) {
 2752:         $css_class .= ' style="'.$rowstyle.'"';
 2753:     }
 2754:     my $text;
 2755:     if ($action eq 'requestcourses') {
 2756:         $text = &mt('Receive notification of course requests requiring approval');
 2757:     } elsif ($action eq 'requestauthor') {
 2758:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2759:     } else {
 2760:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2761:     }
 2762:     $datatable = '<tr'.$css_class.'>'.
 2763:                  ' <td>'.$text.'</td>'.
 2764:                  ' <td class="LC_left_item">';
 2765:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2766:                                                  $action.'notifyapproval',%currapp);
 2767:     if ($numdc > 0) {
 2768:         $datatable .= $table;
 2769:     } else {
 2770:         $datatable .= &mt('There are no active Domain Coordinators');
 2771:     }
 2772:     $datatable .='</td></tr>';
 2773:     return $datatable;
 2774: }
 2775: 
 2776: sub print_studentcode {
 2777:     my ($settings,$rowtotal) = @_;
 2778:     my $rownum = 0; 
 2779:     my ($output,%current);
 2780:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2781:     if (ref($settings) eq 'HASH') {
 2782:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2783:             foreach my $type (@crstypes) {
 2784:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2785:             }
 2786:         }
 2787:     }
 2788:     $output .= '<tr>'.
 2789:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2790:                '<td class="LC_left_item">';
 2791:     foreach my $type (@crstypes) {
 2792:         my $check = ' ';
 2793:         if ($current{$type}) {
 2794:             $check = ' checked="checked" ';
 2795:         }
 2796:         $output .= '<span class="LC_nobreak"><label>'.
 2797:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2798:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2799:     }
 2800:     $output .= '</td></tr>';
 2801:     $$rowtotal ++;
 2802:     return $output;
 2803: }
 2804: 
 2805: sub print_textbookcourses {
 2806:     my ($dom,$type,$settings,$rowtotal) = @_;
 2807:     my $rownum = 0;
 2808:     my $css_class;
 2809:     my $itemcount = 1;
 2810:     my $maxnum = 0;
 2811:     my $bookshash;
 2812:     if (ref($settings) eq 'HASH') {
 2813:         $bookshash = $settings->{$type};
 2814:     }
 2815:     my %ordered;
 2816:     if (ref($bookshash) eq 'HASH') {
 2817:         foreach my $item (keys(%{$bookshash})) {
 2818:             if (ref($bookshash->{$item}) eq 'HASH') {
 2819:                 my $num = $bookshash->{$item}{'order'};
 2820:                 $ordered{$num} = $item;
 2821:             }
 2822:         }
 2823:     }
 2824:     my $confname = $dom.'-domainconfig';
 2825:     my $switchserver = &check_switchserver($dom,$confname);
 2826:     my $maxnum = scalar(keys(%ordered));
 2827:     my $datatable;
 2828:     if (keys(%ordered)) {
 2829:         my @items = sort { $a <=> $b } keys(%ordered);
 2830:         for (my $i=0; $i<@items; $i++) {
 2831:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2832:             my $key = $ordered{$items[$i]};
 2833:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2834:             my $coursetitle = $coursehash{'description'};
 2835:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2836:             if (ref($bookshash->{$key}) eq 'HASH') {
 2837:                 $subject = $bookshash->{$key}->{'subject'};
 2838:                 $title = $bookshash->{$key}->{'title'};
 2839:                 if ($type eq 'textbooks') {
 2840:                     $publisher = $bookshash->{$key}->{'publisher'};
 2841:                     $author = $bookshash->{$key}->{'author'};
 2842:                     $image = $bookshash->{$key}->{'image'};
 2843:                     if ($image ne '') {
 2844:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2845:                         my $imagethumb = "$path/tn-".$imagefile;
 2846:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2847:                     }
 2848:                 }
 2849:             }
 2850:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2851:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2852:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2853:             for (my $k=0; $k<=$maxnum; $k++) {
 2854:                 my $vpos = $k+1;
 2855:                 my $selstr;
 2856:                 if ($k == $i) {
 2857:                     $selstr = ' selected="selected" ';
 2858:                 }
 2859:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2860:             }
 2861:             $datatable .= '</select>'.('&nbsp;'x2).
 2862:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2863:                 &mt('Delete?').'</label></span></td>'.
 2864:                 '<td colspan="2">'.
 2865:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2866:                 ('&nbsp;'x2).
 2867:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2868:             if ($type eq 'textbooks') {
 2869:                 $datatable .= ('&nbsp;'x2).
 2870:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2871:                               ('&nbsp;'x2).
 2872:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2873:                               ('&nbsp;'x2).
 2874:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2875:                 if ($image) {
 2876:                     $datatable .= $imgsrc.
 2877:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2878:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2879:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2880:                 }
 2881:                 if ($switchserver) {
 2882:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2883:                 } else {
 2884:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2885:                 }
 2886:             }
 2887:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2888:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2889:                           $coursetitle.'</span></td></tr>'."\n";
 2890:             $itemcount ++;
 2891:         }
 2892:     }
 2893:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2894:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2895:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2896:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2897:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2898:     for (my $k=0; $k<$maxnum+1; $k++) {
 2899:         my $vpos = $k+1;
 2900:         my $selstr;
 2901:         if ($k == $maxnum) {
 2902:             $selstr = ' selected="selected" ';
 2903:         }
 2904:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2905:     }
 2906:     $datatable .= '</select>&nbsp;'."\n".
 2907:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2908:                   '<td colspan="2">'.
 2909:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2910:                   ('&nbsp;'x2).
 2911:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2912:                   ('&nbsp;'x2);
 2913:     if ($type eq 'textbooks') {
 2914:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2915:                       ('&nbsp;'x2).
 2916:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2917:                       ('&nbsp;'x2).
 2918:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2919:         if ($switchserver) {
 2920:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2921:         } else {
 2922:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2923:         }
 2924:         $datatable .= '</span>'."\n";
 2925:     }
 2926:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2927:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2928:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2929:                   &Apache::loncommon::selectcourse_link
 2930:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2931:                   '</span></td>'."\n".
 2932:                   '</tr>'."\n";
 2933:     $itemcount ++;
 2934:     return $datatable;
 2935: }
 2936: 
 2937: sub textbookcourses_javascript {
 2938:     my ($settings) = @_;
 2939:     return unless(ref($settings) eq 'HASH');
 2940:     my (%ordered,%total,%jstext);
 2941:     foreach my $type ('textbooks','templates') {
 2942:         $total{$type} = 0;
 2943:         if (ref($settings->{$type}) eq 'HASH') {
 2944:             foreach my $item (keys(%{$settings->{$type}})) {
 2945:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2946:                     my $num = $settings->{$type}->{$item}{'order'};
 2947:                     $ordered{$type}{$num} = $item;
 2948:                 }
 2949:             }
 2950:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2951:         }
 2952:         my @jsarray = ();
 2953:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2954:             push(@jsarray,$ordered{$type}{$item});
 2955:         }
 2956:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2957:     }
 2958:     return <<"ENDSCRIPT";
 2959: <script type="text/javascript">
 2960: // <![CDATA[
 2961: function reorderBooks(form,item,caller) {
 2962:     var changedVal;
 2963: $jstext{'textbooks'};
 2964: $jstext{'templates'};
 2965:     var newpos;
 2966:     var maxh;
 2967:     if (caller == 'textbooks') {  
 2968:         newpos = 'textbooks_addbook_pos';
 2969:         maxh = 1 + $total{'textbooks'};
 2970:     } else {
 2971:         newpos = 'templates_addbook_pos';
 2972:         maxh = 1 + $total{'templates'};
 2973:     }
 2974:     var current = new Array;
 2975:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2976:     if (item == newpos) {
 2977:         changedVal = newitemVal;
 2978:     } else {
 2979:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2980:         current[newitemVal] = newpos;
 2981:     }
 2982:     if (caller == 'textbooks') {
 2983:         for (var i=0; i<textbooks.length; i++) {
 2984:             var elementName = 'textbooks_'+textbooks[i];
 2985:             if (elementName != item) {
 2986:                 if (form.elements[elementName]) {
 2987:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2988:                     current[currVal] = elementName;
 2989:                 }
 2990:             }
 2991:         }
 2992:     }
 2993:     if (caller == 'templates') {
 2994:         for (var i=0; i<templates.length; i++) {
 2995:             var elementName = 'templates_'+templates[i];
 2996:             if (elementName != item) {
 2997:                 if (form.elements[elementName]) {
 2998:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2999:                     current[currVal] = elementName;
 3000:                 }
 3001:             }
 3002:         }
 3003:     }
 3004:     var oldVal;
 3005:     for (var j=0; j<maxh; j++) {
 3006:         if (current[j] == undefined) {
 3007:             oldVal = j;
 3008:         }
 3009:     }
 3010:     if (oldVal < changedVal) {
 3011:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3012:            var elementName = current[k];
 3013:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3014:         }
 3015:     } else {
 3016:         for (var k=changedVal; k<oldVal; k++) {
 3017:             var elementName = current[k];
 3018:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3019:         }
 3020:     }
 3021:     return;
 3022: }
 3023: 
 3024: // ]]>
 3025: </script>
 3026: 
 3027: ENDSCRIPT
 3028: }
 3029: 
 3030: sub ltitools_javascript {
 3031:     my ($settings) = @_;
 3032:     my $togglejs = &ltitools_toggle_js();
 3033:     unless (ref($settings) eq 'HASH') {
 3034:         return $togglejs;
 3035:     }
 3036:     my (%ordered,$total,%jstext);
 3037:     $total = 0;
 3038:     foreach my $item (keys(%{$settings})) {
 3039:         if (ref($settings->{$item}) eq 'HASH') {
 3040:             my $num = $settings->{$item}{'order'};
 3041:             $ordered{$num} = $item;
 3042:         }
 3043:     }
 3044:     $total = scalar(keys(%{$settings}));
 3045:     my @jsarray = ();
 3046:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3047:         push(@jsarray,$ordered{$item});
 3048:     }
 3049:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3050:     return <<"ENDSCRIPT";
 3051: <script type="text/javascript">
 3052: // <![CDATA[
 3053: function reorderLTITools(form,item) {
 3054:     var changedVal;
 3055: $jstext
 3056:     var newpos = 'ltitools_add_pos';
 3057:     var maxh = 1 + $total;
 3058:     var current = new Array;
 3059:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3060:     if (item == newpos) {
 3061:         changedVal = newitemVal;
 3062:     } else {
 3063:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3064:         current[newitemVal] = newpos;
 3065:     }
 3066:     for (var i=0; i<ltitools.length; i++) {
 3067:         var elementName = 'ltitools_'+ltitools[i];
 3068:         if (elementName != item) {
 3069:             if (form.elements[elementName]) {
 3070:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3071:                 current[currVal] = elementName;
 3072:             }
 3073:         }
 3074:     }
 3075:     var oldVal;
 3076:     for (var j=0; j<maxh; j++) {
 3077:         if (current[j] == undefined) {
 3078:             oldVal = j;
 3079:         }
 3080:     }
 3081:     if (oldVal < changedVal) {
 3082:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3083:            var elementName = current[k];
 3084:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3085:         }
 3086:     } else {
 3087:         for (var k=changedVal; k<oldVal; k++) {
 3088:             var elementName = current[k];
 3089:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3090:         }
 3091:     }
 3092:     return;
 3093: }
 3094: 
 3095: // ]]>
 3096: </script>
 3097: 
 3098: $togglejs
 3099: 
 3100: ENDSCRIPT
 3101: }
 3102: 
 3103: sub ltitools_toggle_js {
 3104:     return <<"ENDSCRIPT";
 3105: <script type="text/javascript">
 3106: // <![CDATA[
 3107: 
 3108: function toggleLTITools(form,setting,item) {
 3109:     var radioname = '';
 3110:     var divid = '';
 3111:     if ((setting == 'passback') || (setting == 'roster')) {
 3112:         radioname = 'ltitools_'+setting+'_'+item;
 3113:         divid = 'ltitools_'+setting+'time_'+item;
 3114:         var num = form.elements[radioname].length;
 3115:         if (num) {
 3116:             var setvis = '';
 3117:             for (var i=0; i<num; i++) {
 3118:                 if (form.elements[radioname][i].checked) {
 3119:                     if (form.elements[radioname][i].value == '1') {
 3120:                         if (document.getElementById(divid)) {
 3121:                             document.getElementById(divid).style.display = 'inline-block';
 3122:                         }
 3123:                         setvis = 1;
 3124:                     }
 3125:                     break;
 3126:                 }
 3127:             }
 3128:         }
 3129:         if (!setvis) {
 3130:             if (document.getElementById(divid)) {
 3131:                 document.getElementById(divid).style.display = 'none';
 3132:             }
 3133:         }
 3134:     }
 3135:     if (setting == 'user') {
 3136:         divid = 'ltitools_'+setting+'_div_'+item;
 3137:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3138:         if (document.getElementById(divid)) {
 3139:             if (document.getElementById(checkid)) {
 3140:                 if (document.getElementById(checkid).checked) {
 3141:                     document.getElementById(divid).style.display = 'inline-block';
 3142:                 } else {
 3143:                     document.getElementById(divid).style.display = 'none';
 3144:                 }
 3145:             }
 3146:         }
 3147:     }
 3148:     return;
 3149: }
 3150: // ]]>
 3151: </script>
 3152: 
 3153: ENDSCRIPT
 3154: }
 3155: 
 3156: sub wafproxy_javascript {
 3157:     my ($dom) = @_;
 3158:     return <<"ENDSCRIPT";
 3159: <script type="text/javascript">
 3160: // <![CDATA[
 3161: function updateWAF() {
 3162:     if (document.getElementById('wafproxy_remoteip')) {
 3163:         var wafremote = 0;
 3164:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3165:             wafremote = 1;
 3166:         }
 3167:         var fields = new Array('header','trust');
 3168:         for (var i=0; i<fields.length; i++) {
 3169:             if (document.getElementById('wafproxy_'+fields[i])) {
 3170:                 if (wafremote == 1) {
 3171:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3172:                 }
 3173:                 else {
 3174:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3175:                 }
 3176:             }
 3177:         }
 3178:         if (document.getElementById('wafproxyranges_$dom')) {
 3179:             if (wafremote == 1) {
 3180:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3181:             } else {
 3182:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3183:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3184:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3185:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3186:                         }
 3187:                     }
 3188:                 }
 3189:             }
 3190:         }
 3191:     }
 3192:     return;
 3193: }
 3194: 
 3195: function checkWAF() {
 3196:     if (document.getElementById('wafproxy_remoteip')) {
 3197:         var wafvpn = 0;
 3198:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3199:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3200:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3201:                     wafvpn = 1;
 3202:                 }
 3203:                 break;
 3204:             }
 3205:         }
 3206:         var vpn = new Array('vpnint','vpnext');
 3207:         for (var i=0; i<vpn.length; i++) {
 3208:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3209:                 if (wafvpn == 1) {
 3210:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3211:                 }
 3212:                 else {
 3213:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3214:                 }
 3215:             }
 3216:         }
 3217:         if (document.getElementById('wafproxyranges_$dom')) {
 3218:             if (wafvpn == 1) {
 3219:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3220:             }
 3221:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3222:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3223:             }
 3224:         }
 3225:     }
 3226:     return;
 3227: }
 3228: 
 3229: function toggleWAF() {
 3230:     if (document.getElementById('wafproxy_table')) {
 3231:         var wafproxy = 0;
 3232:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3233:              if (document.display.wafproxy_${dom}[i].checked) {
 3234:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3235:                      wafproxy = 1;
 3236:                      break;
 3237:                 }
 3238:             }
 3239:         }
 3240:         if (wafproxy == 1) {
 3241:             document.getElementById('wafproxy_table').style.display='inline';
 3242:         }
 3243:         else {
 3244:            document.getElementById('wafproxy_table').style.display='none';
 3245:         }
 3246:         if (document.getElementById('wafproxyrow_${dom}')) {
 3247:             if (wafproxy == 1) {
 3248:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3249:             }
 3250:             else {
 3251:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3252:             }
 3253:         }
 3254:         if (document.getElementById('nowafproxyrow_$dom')) {
 3255:             if (wafproxy == 1) {
 3256:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3257:             }
 3258:             else {
 3259:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3260:             }
 3261:         }
 3262:     }
 3263:     return;
 3264: }
 3265: // ]]>
 3266: </script>
 3267: 
 3268: ENDSCRIPT
 3269: }
 3270: 
 3271: sub proctoring_javascript {
 3272:     my ($settings) = @_;
 3273:     my (%ordered,$total,%jstext);
 3274:     $total = 0;
 3275:     if (ref($settings) eq 'HASH') {
 3276:         foreach my $item (keys(%{$settings})) {
 3277:             if (ref($settings->{$item}) eq 'HASH') {
 3278:                 my $num = $settings->{$item}{'order'};
 3279:                 $ordered{$num} = $item;
 3280:             }
 3281:         }
 3282:         $total = scalar(keys(%{$settings}));
 3283:     } else {
 3284:         %ordered = (
 3285:                        0 => 'proctorio',
 3286:                        1 => 'examity',
 3287:                    );
 3288:         $total = 2; 
 3289:     }
 3290:     my @jsarray = ();
 3291:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3292:         push(@jsarray,$ordered{$item});
 3293:     }
 3294:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3295:     return <<"ENDSCRIPT";
 3296: <script type="text/javascript">
 3297: // <![CDATA[
 3298: function reorderProctoring(form,item) {
 3299:     var changedVal;
 3300: $jstext
 3301:     var maxh = $total;
 3302:     var current = new Array;
 3303:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3304:     for (var i=0; i<proctors.length; i++) {
 3305:         var elementName = 'proctoring_pos_'+proctors[i];
 3306:         if (elementName != item) {
 3307:             if (form.elements[elementName]) {
 3308:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3309:                 current[currVal] = elementName;
 3310:             }
 3311:         }
 3312:     }
 3313:     var oldVal;
 3314:     for (var j=0; j<maxh; j++) {
 3315:         if (current[j] == undefined) {
 3316:             oldVal = j;
 3317:         }
 3318:     }
 3319:     if (oldVal < changedVal) {
 3320:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3321:            var elementName = current[k];
 3322:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3323:         }
 3324:     } else {
 3325:         for (var k=changedVal; k<oldVal; k++) {
 3326:             var elementName = current[k];
 3327:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3328:         }
 3329:     }
 3330:     return;
 3331: }
 3332: 
 3333: function toggleProctoring(form,item) {
 3334:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3335:     if (fieldsets.length) {
 3336:         var radioname = 'proctoring_available_'+item;
 3337:         var num = form.elements[radioname].length;
 3338:         if (num) {
 3339:             var setvis = '';
 3340:             for (var i=0; i<num; i++) {
 3341:                 if (form.elements[radioname][i].checked) {
 3342:                     if (form.elements[radioname][i].value == '1') {
 3343:                        setvis = 1;
 3344:                        break;
 3345:                     }
 3346:                 }
 3347:             }
 3348:             for (var j=0; j<fieldsets.length; j++) {
 3349:                 if (setvis) {
 3350:                     fieldsets[j].style.display = 'block';
 3351:                 } else {
 3352:                     fieldsets[j].style.display = 'none';
 3353:                 }
 3354:             }
 3355:         }
 3356:     }
 3357:     return;
 3358: }
 3359: 
 3360: // ]]>
 3361: </script>
 3362: 
 3363: ENDSCRIPT
 3364: }
 3365: 
 3366: 
 3367: sub lti_javascript {
 3368:     my ($settings) = @_;
 3369:     my $togglejs = &lti_toggle_js();
 3370:     unless (ref($settings) eq 'HASH') {
 3371:         return $togglejs;
 3372:     }
 3373:     my (%ordered,$total,%jstext);
 3374:     $total = scalar(keys(%{$settings}));
 3375:     foreach my $item (keys(%{$settings})) {
 3376:         if (ref($settings->{$item}) eq 'HASH') {
 3377:             my $num = $settings->{$item}{'order'};
 3378:             if ($num eq '') {
 3379:                 $num = $total - 1;
 3380:             }
 3381:             $ordered{$num} = $item;
 3382:         }
 3383:     }
 3384:     my @jsarray = ();
 3385:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3386:         push(@jsarray,$ordered{$item});
 3387:     }
 3388:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3389:     return <<"ENDSCRIPT";
 3390: <script type="text/javascript">
 3391: // <![CDATA[
 3392: function reorderLTI(form,item) {
 3393:     var changedVal;
 3394: $jstext
 3395:     var newpos = 'lti_pos_add';
 3396:     var maxh = 1 + $total;
 3397:     var current = new Array;
 3398:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3399:     if (item == newpos) {
 3400:         changedVal = newitemVal;
 3401:     } else {
 3402:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3403:         current[newitemVal] = newpos;
 3404:     }
 3405:     for (var i=0; i<lti.length; i++) {
 3406:         var elementName = 'lti_pos_'+lti[i];
 3407:         if (elementName != item) {
 3408:             if (form.elements[elementName]) {
 3409:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3410:                 current[currVal] = elementName;
 3411:             }
 3412:         }
 3413:     }
 3414:     var oldVal;
 3415:     for (var j=0; j<maxh; j++) {
 3416:         if (current[j] == undefined) {
 3417:             oldVal = j;
 3418:         }
 3419:     }
 3420:     if (oldVal < changedVal) {
 3421:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3422:            var elementName = current[k];
 3423:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3424:         }
 3425:     } else {
 3426:         for (var k=changedVal; k<oldVal; k++) {
 3427:             var elementName = current[k];
 3428:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3429:         }
 3430:     }
 3431:     return;
 3432: }
 3433: // ]]>
 3434: </script>
 3435: 
 3436: $togglejs
 3437: 
 3438: ENDSCRIPT
 3439: }
 3440: 
 3441: sub lti_toggle_js {
 3442:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3443:                             localauth => 'Local auth argument',
 3444:                             krb       => 'Kerberos domain',
 3445:                          );
 3446:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3447:     &js_escape(\$crsincalert);
 3448:     return <<"ENDSCRIPT";
 3449: <script type="text/javascript">
 3450: // <![CDATA[
 3451: 
 3452: function toggleLTI(form,setting,item) {
 3453:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3454:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3455:         var setvis = '';
 3456:         var radioname = 'lti_requser_'+item;
 3457:         var num = form.elements[radioname].length;
 3458:         if (num) {
 3459:             for (var i=0; i<num; i++) {
 3460:                 if (form.elements[radioname][i].checked) {
 3461:                     if (form.elements[radioname][i].value == '1') {
 3462:                         setvis = 1;
 3463:                         break;
 3464:                     }
 3465:                 }
 3466:             }
 3467:         }
 3468:         if (usrfieldsets.length) {
 3469:             for (var j=0; j<usrfieldsets.length; j++) {
 3470:                 if (setvis) {
 3471:                     usrfieldsets[j].style.display = 'block';
 3472:                 } else {
 3473:                     usrfieldsets[j].style.display = 'none';
 3474:                 }
 3475:             }
 3476:         }
 3477:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3478:         if (crsfieldsets.length) {
 3479:             radioname = 'lti_crsinc_'+item;
 3480:             var num = form.elements[radioname].length;
 3481:             if (num) {
 3482:                 var crsvis = '';
 3483:                 for (var i=0; i<num; i++) {
 3484:                     if (form.elements[radioname][i].checked) {
 3485:                         if (form.elements[radioname][i].value == '1') {
 3486:                             if (setvis == '') {
 3487:                                 if (setting == 'crsinc'){
 3488:                                     alert("$crsincalert");
 3489:                                     form.elements[radioname][0].checked = true;
 3490:                                 }
 3491:                             } else {
 3492:                                 crsvis = 1;
 3493:                             }
 3494:                             break;
 3495:                         }
 3496:                     }
 3497:                 }
 3498:                 setvis = crsvis;
 3499:             }
 3500:             for (var j=0; j<crsfieldsets.length; j++) {
 3501:                 if (setvis) {
 3502:                     crsfieldsets[j].style.display = 'block';
 3503:                 } else {
 3504:                     crsfieldsets[j].style.display = 'none';
 3505:                 }
 3506:             }
 3507:         }
 3508:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3509:         var radioname = '';
 3510:         var divid = '';
 3511:         if (setting == 'user') {
 3512:             radioname = 'lti_mapuser_'+item;
 3513:             divid = 'lti_userfield_'+item;
 3514:         } else if (setting == 'crs') {
 3515:             radioname = 'lti_mapcrs_'+item;
 3516:             divid = 'lti_crsfield_'+item;
 3517:         } else if (setting == 'callback') {
 3518:             radioname = 'lti_callback_'+item;
 3519:             divid = 'lti_callbackfield_'+item;
 3520:         } else {
 3521:             radioname = 'lti_passback_'+item;
 3522:             divid =  'lti_passback_'+item;
 3523:         }
 3524:         var num = form.elements[radioname].length;
 3525:         if (num) {
 3526:             var setvis = '';
 3527:             for (var i=0; i<num; i++) {
 3528:                if (form.elements[radioname][i].checked) {
 3529:                    if ((setting == 'passback') || (setting == 'callback')) {
 3530:                        if (form.elements[radioname][i].value == '1') {
 3531:                            if (document.getElementById(divid)) {
 3532:                                document.getElementById(divid).style.display = 'inline-block';
 3533:                            }
 3534:                            setvis = 1;
 3535:                            break;
 3536:                        }
 3537:                    } else {
 3538:                        if (form.elements[radioname][i].value == 'other') {
 3539:                            if (document.getElementById(divid)) {
 3540:                                document.getElementById(divid).style.display = 'inline-block';
 3541:                            }
 3542:                            setvis = 1;
 3543:                            break;
 3544:                        }
 3545:                    }
 3546:                } 
 3547:             }
 3548:             if (!setvis) {
 3549:                 if (document.getElementById(divid)) {
 3550:                     document.getElementById(divid).style.display = 'none';
 3551:                 }
 3552:             }
 3553:         }
 3554:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3555:         var numsec = form.elements['lti_crssec_'+item].length;
 3556:         if (numsec) {
 3557:             var setvis = '';
 3558:             for (var i=0; i<numsec; i++) {
 3559:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3560:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3561:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3562:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3563:                             setvis = 1;
 3564:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3565:                             if (numsrcsec) {
 3566:                                 var setsrcvis = '';
 3567:                                 for (var j=0; j<numsrcsec; j++) {
 3568:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3569:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3570:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3571:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3572:                                                 setsrcvis = 1;
 3573:                                             }
 3574:                                         }
 3575:                                     }
 3576:                                 }
 3577:                                 if (!setsrcvis) {
 3578:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3579:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3580:                                     }
 3581:                                 }
 3582:                             }
 3583:                         }
 3584:                     }
 3585:                 }
 3586:             }
 3587:             if (!setvis) {
 3588:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3589:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3590:                 }
 3591:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3592:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3593:                 }
 3594:             }
 3595:         }
 3596:     } else if (setting == 'lcauth') {
 3597:         var numauth = form.elements['lti_lcauth_'+item].length;
 3598:         if (numauth) {
 3599:             for (var i=0; i<numauth; i++) {
 3600:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3601:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3602:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3603:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3604:                         } else {
 3605:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3606:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3607:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3608:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3609:                                 } else {
 3610:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3611:                                 }
 3612:                             }
 3613:                         }
 3614:                     }
 3615:                 }
 3616:             }
 3617:         }
 3618:     } else if (setting == 'lcmenu') {
 3619:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3620:         var divid = 'lti_menufield_'+item;
 3621:         var setvis = '';
 3622:         for (var i=0; i<menus.length; i++) {
 3623:             var radioname = menus[i];  
 3624:             var num = form.elements[radioname].length;
 3625:             if (num) {
 3626:                 for (var j=0; j<num; j++) {
 3627:                     if (form.elements[radioname][j].checked) {
 3628:                         if (form.elements[radioname][j].value == '1') {
 3629:                             if (document.getElementById(divid)) {
 3630:                                 document.getElementById(divid).style.display = 'inline-block';
 3631:                             }
 3632:                             setvis = 1;
 3633:                             break;
 3634:                         }
 3635:                     }
 3636:                 }
 3637:             }
 3638:             if (setvis == 1) {
 3639:                 break;
 3640:             }
 3641:         }
 3642:         if (!setvis) {
 3643:             if (document.getElementById(divid)) {
 3644:                 document.getElementById(divid).style.display = 'none';
 3645:             }
 3646:         }
 3647:     }
 3648:     return;
 3649: }
 3650: // ]]>
 3651: </script>
 3652: 
 3653: ENDSCRIPT
 3654: }
 3655: 
 3656: sub autoupdate_javascript {
 3657:     return <<"ENDSCRIPT";
 3658: <script type="text/javascript">
 3659: // <![CDATA[
 3660: function toggleLastActiveDays(form) {
 3661:     var radioname = 'lastactive';
 3662:     var divid = 'lastactive_div';
 3663:     var num = form.elements[radioname].length;
 3664:     if (num) {
 3665:         var setvis = '';
 3666:         for (var i=0; i<num; i++) {
 3667:             if (form.elements[radioname][i].checked) {
 3668:                 if (form.elements[radioname][i].value == '1') {
 3669:                     if (document.getElementById(divid)) {
 3670:                         document.getElementById(divid).style.display = 'inline-block';
 3671:                     }
 3672:                     setvis = 1;
 3673:                 }
 3674:                 break;
 3675:             }
 3676:         }
 3677:         if (!setvis) {
 3678:             if (document.getElementById(divid)) {
 3679:                 document.getElementById(divid).style.display = 'none';
 3680:             }
 3681:         }
 3682:     }
 3683:     return;
 3684: }
 3685: // ]]>
 3686: </script>
 3687: 
 3688: ENDSCRIPT
 3689: }
 3690: 
 3691: sub saml_javascript {
 3692:     return <<"ENDSCRIPT";
 3693: <script type="text/javascript">
 3694: // <![CDATA[
 3695: function toggleSamlOptions(form,hostid) { 
 3696:     var radioname = 'saml_'+hostid;
 3697:     var tablecellon = 'samloptionson_'+hostid;
 3698:     var tablecelloff = 'samloptionsoff_'+hostid;
 3699:     var num = form.elements[radioname].length;
 3700:     if (num) {
 3701:         var setvis = ''; 
 3702:         for (var i=0; i<num; i++) {
 3703:             if (form.elements[radioname][i].checked) {
 3704:                 if (form.elements[radioname][i].value == '1') { 
 3705:                     if (document.getElementById(tablecellon)) {
 3706:                         document.getElementById(tablecellon).style.display='';
 3707:                     }
 3708:                     if (document.getElementById(tablecelloff)) {
 3709:                         document.getElementById(tablecelloff).style.display='none';
 3710:                     }
 3711:                     setvis = 1;
 3712:                 }
 3713:                 break;
 3714:             }
 3715:         }
 3716:         if (!setvis) {
 3717:             if (document.getElementById(tablecellon)) {
 3718:                 document.getElementById(tablecellon).style.display='none';
 3719:             }
 3720:             if (document.getElementById(tablecelloff)) {
 3721:                 document.getElementById(tablecelloff).style.display='';
 3722:             }
 3723:         }
 3724:     }
 3725:     return;
 3726: }
 3727: // ]]>
 3728: </script>
 3729: 
 3730: ENDSCRIPT
 3731: }
 3732: 
 3733: sub ipaccess_javascript {
 3734:     my ($settings) = @_;
 3735:     my (%ordered,$total,%jstext);
 3736:     $total = 0;
 3737:     if (ref($settings) eq 'HASH') {
 3738:         foreach my $item (keys(%{$settings})) {
 3739:             if (ref($settings->{$item}) eq 'HASH') {
 3740:                 my $num = $settings->{$item}{'order'};
 3741:                 $ordered{$num} = $item;
 3742:             }
 3743:         }
 3744:         $total = scalar(keys(%{$settings}));
 3745:     }
 3746:     my @jsarray = ();
 3747:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3748:         push(@jsarray,$ordered{$item});
 3749:     }
 3750:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3751:     return <<"ENDSCRIPT";
 3752: <script type="text/javascript">
 3753: // <![CDATA[
 3754: function reorderIPaccess(form,item) {
 3755:     var changedVal;
 3756: $jstext
 3757:     var newpos = 'ipaccess_pos_add';
 3758:     var maxh = 1 + $total;
 3759:     var current = new Array;
 3760:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3761:     if (item == newpos) {
 3762:         changedVal = newitemVal;
 3763:     } else {
 3764:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3765:         current[newitemVal] = newpos;
 3766:     }
 3767:     for (var i=0; i<ipaccess.length; i++) {
 3768:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3769:         if (elementName != item) {
 3770:             if (form.elements[elementName]) {
 3771:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3772:                 current[currVal] = elementName;
 3773:             }
 3774:         }
 3775:     }
 3776:     var oldVal;
 3777:     for (var j=0; j<maxh; j++) {
 3778:         if (current[j] == undefined) {
 3779:             oldVal = j;
 3780:         }
 3781:     }
 3782:     if (oldVal < changedVal) {
 3783:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3784:            var elementName = current[k];
 3785:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3786:         }
 3787:     } else {
 3788:         for (var k=changedVal; k<oldVal; k++) {
 3789:             var elementName = current[k];
 3790:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3791:         }
 3792:     }
 3793:     return;
 3794: }
 3795: // ]]>
 3796: </script>
 3797: 
 3798: ENDSCRIPT
 3799: }
 3800: 
 3801: sub print_autoenroll {
 3802:     my ($dom,$settings,$rowtotal) = @_;
 3803:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3804:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3805:     if (ref($settings) eq 'HASH') {
 3806:         if (exists($settings->{'run'})) {
 3807:             if ($settings->{'run'} eq '0') {
 3808:                 $runoff = ' checked="checked" ';
 3809:                 $runon = ' ';
 3810:             } else {
 3811:                 $runon = ' checked="checked" ';
 3812:                 $runoff = ' ';
 3813:             }
 3814:         } else {
 3815:             if ($autorun) {
 3816:                 $runon = ' checked="checked" ';
 3817:                 $runoff = ' ';
 3818:             } else {
 3819:                 $runoff = ' checked="checked" ';
 3820:                 $runon = ' ';
 3821:             }
 3822:         }
 3823:         if (exists($settings->{'co-owners'})) {
 3824:             if ($settings->{'co-owners'} eq '0') {
 3825:                 $coownersoff = ' checked="checked" ';
 3826:                 $coownerson = ' ';
 3827:             } else {
 3828:                 $coownerson = ' checked="checked" ';
 3829:                 $coownersoff = ' ';
 3830:             }
 3831:         } else {
 3832:             $coownersoff = ' checked="checked" ';
 3833:             $coownerson = ' ';
 3834:         }
 3835:         if (exists($settings->{'sender_domain'})) {
 3836:             $defdom = $settings->{'sender_domain'};
 3837:         }
 3838:         if (exists($settings->{'autofailsafe'})) {
 3839:             $failsafe = $settings->{'autofailsafe'};
 3840:         }
 3841:     } else {
 3842:         if ($autorun) {
 3843:             $runon = ' checked="checked" ';
 3844:             $runoff = ' ';
 3845:         } else {
 3846:             $runoff = ' checked="checked" ';
 3847:             $runon = ' ';
 3848:         }
 3849:     }
 3850:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3851:     my $notif_sender;
 3852:     if (ref($settings) eq 'HASH') {
 3853:         $notif_sender = $settings->{'sender_uname'};
 3854:     }
 3855:     my $datatable='<tr class="LC_odd_row">'.
 3856:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3857:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3858:                   '<input type="radio" name="autoenroll_run"'.
 3859:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3860:                   '<label><input type="radio" name="autoenroll_run"'.
 3861:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3862:                   '</tr><tr>'.
 3863:                   '<td>'.&mt('Notification messages - sender').
 3864:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3865:                   &mt('username').':&nbsp;'.
 3866:                   '<input type="text" name="sender_uname" value="'.
 3867:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3868:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3869:                   '<tr class="LC_odd_row">'.
 3870:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3871:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3872:                   '<input type="radio" name="autoassign_coowners"'.
 3873:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3874:                   '<label><input type="radio" name="autoassign_coowners"'.
 3875:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3876:                   '</tr><tr>'.
 3877:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3878:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3879:                   '<input type="text" name="autoenroll_failsafe"'.
 3880:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3881:     $$rowtotal += 4;
 3882:     return $datatable;
 3883: }
 3884: 
 3885: sub print_autoupdate {
 3886:     my ($position,$dom,$settings,$rowtotal) = @_;
 3887:     my ($enable,$datatable);
 3888:     if ($position eq 'top') {
 3889:         my %choices = &Apache::lonlocal::texthash (
 3890:                           run        => 'Auto-update active?',
 3891:                           classlists => 'Update information in classlists?',
 3892:                           unexpired  => 'Skip updates for users without active or future roles?',
 3893:                           lastactive => 'Skip updates for inactive users?',
 3894:         );
 3895:         my $itemcount = 0;
 3896:         my $updateon = ' ';
 3897:         my $updateoff = ' checked="checked" ';
 3898:         if (ref($settings) eq 'HASH') {
 3899:             if ($settings->{'run'} eq '1') {
 3900:                 $updateon = $updateoff;
 3901:                 $updateoff = ' ';
 3902:             }
 3903:         }
 3904:         $enable = '<tr class="LC_odd_row">'.
 3905:                   '<td>'.$choices{'run'}.'</td>'.
 3906:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3907:                   '<input type="radio" name="autoupdate_run"'.
 3908:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 3909:                   '<label><input type="radio" name="autoupdate_run"'.
 3910:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 3911:                   '</tr>';
 3912:         my @toggles = ('classlists','unexpired');
 3913:         my %defaultchecked = ('classlists' => 'off',
 3914:                               'unexpired'  => 'off'
 3915:                               );
 3916:         $$rowtotal ++;
 3917:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3918:                                                      \%choices,$itemcount,'','','left','no');
 3919:         $datatable = $enable.$datatable;
 3920:         $$rowtotal += $itemcount;
 3921:         my $lastactiveon = ' ';
 3922:         my $lastactiveoff = ' checked="checked" ';
 3923:         my $lastactivestyle = 'none';
 3924:         my $lastactivedays;
 3925:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 3926:         if (ref($settings) eq 'HASH') {
 3927:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 3928:                 $lastactiveon = $lastactiveoff;
 3929:                 $lastactiveoff = ' ';
 3930:                 $lastactivestyle = 'inline-block';
 3931:                 $lastactivedays = $settings->{'lastactive'};
 3932:             }
 3933:         }
 3934:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3935:         $datatable .= '<tr'.$css_class.'>'.
 3936:                       '<td>'.$choices{'lastactive'}.'</td>'.
 3937:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3938:                       '<input type="radio" name="lastactive"'.
 3939:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3940:                       '&nbsp;<label>'.
 3941:                       '<input type="radio" name="lastactive"'.
 3942:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 3943:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 3944:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 3945:                           '<input type="text" size="5" name="lastactivedays" value="'.
 3946:                           $lastactivedays.'" />').
 3947:                       '</span></td>'.
 3948:                       '</tr>';
 3949:         $$rowtotal ++;
 3950:     } elsif ($position eq 'middle') {
 3951:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3952:         my $numinrow = 3;
 3953:         my $locknamesettings;
 3954:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3955:                                      $dom,$numinrow,$othertitle,
 3956:                                     'lockablenames',$rowtotal);
 3957:         $$rowtotal ++;
 3958:     } else {
 3959:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3960:         my @fields = ('lastname','firstname','middlename','generation',
 3961:                       'permanentemail','id');
 3962:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3963:         my $numrows = 0;
 3964:         if (ref($types) eq 'ARRAY') {
 3965:             if (@{$types} > 0) {
 3966:                 $datatable = 
 3967:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3968:                                          \@fields,$types,\$numrows);
 3969:                     $$rowtotal += @{$types}; 
 3970:             }
 3971:         }
 3972:         $datatable .= 
 3973:             &usertype_update_row($settings,{'default' => $othertitle},
 3974:                                  \%fieldtitles,\@fields,['default'],
 3975:                                  \$numrows);
 3976:         $$rowtotal ++;     
 3977:     }
 3978:     return $datatable;
 3979: }
 3980: 
 3981: sub print_autocreate {
 3982:     my ($dom,$settings,$rowtotal) = @_;
 3983:     my (%createon,%createoff,%currhash);
 3984:     my @types = ('xml','req');
 3985:     if (ref($settings) eq 'HASH') {
 3986:         foreach my $item (@types) {
 3987:             $createoff{$item} = ' checked="checked" ';
 3988:             $createon{$item} = ' ';
 3989:             if (exists($settings->{$item})) {
 3990:                 if ($settings->{$item}) {
 3991:                     $createon{$item} = ' checked="checked" ';
 3992:                     $createoff{$item} = ' ';
 3993:                 }
 3994:             }
 3995:         }
 3996:         if ($settings->{'xmldc'} ne '') {
 3997:             $currhash{$settings->{'xmldc'}} = 1;
 3998:         }
 3999:     } else {
 4000:         foreach my $item (@types) {
 4001:             $createoff{$item} = ' checked="checked" ';
 4002:             $createon{$item} = ' ';
 4003:         }
 4004:     }
 4005:     $$rowtotal += 2;
 4006:     my $numinrow = 2;
 4007:     my $datatable='<tr class="LC_odd_row">'.
 4008:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4009:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4010:                   '<input type="radio" name="autocreate_xml"'.
 4011:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4012:                   '<label><input type="radio" name="autocreate_xml"'.
 4013:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4014:                   '</td></tr><tr>'.
 4015:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4016:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4017:                   '<input type="radio" name="autocreate_req"'.
 4018:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4019:                   '<label><input type="radio" name="autocreate_req"'.
 4020:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4021:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4022:                                                    'autocreate_xmldc',%currhash);
 4023:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4024:     if ($numdc > 1) {
 4025:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4026:                       '</td><td class="LC_left_item">';
 4027:     } else {
 4028:         $datatable .= &mt('Course creation processed as:').
 4029:                       '</td><td class="LC_right_item">';
 4030:     }
 4031:     $datatable .= $dctable.'</td></tr>';
 4032:     $$rowtotal += $rows;
 4033:     return $datatable;
 4034: }
 4035: 
 4036: sub print_directorysrch {
 4037:     my ($position,$dom,$settings,$rowtotal) = @_;
 4038:     my $datatable;
 4039:     if ($position eq 'top') {
 4040:         my $instsrchon = ' ';
 4041:         my $instsrchoff = ' checked="checked" ';
 4042:         my ($exacton,$containson,$beginson);
 4043:         my $instlocalon = ' ';
 4044:         my $instlocaloff = ' checked="checked" ';
 4045:         if (ref($settings) eq 'HASH') {
 4046:             if ($settings->{'available'} eq '1') {
 4047:                 $instsrchon = $instsrchoff;
 4048:                 $instsrchoff = ' ';
 4049:             }
 4050:             if ($settings->{'localonly'} eq '1') {
 4051:                 $instlocalon = $instlocaloff;
 4052:                 $instlocaloff = ' ';
 4053:             }
 4054:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4055:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4056:                     if ($type eq 'exact') {
 4057:                         $exacton = ' checked="checked" ';
 4058:                     } elsif ($type eq 'contains') {
 4059:                         $containson = ' checked="checked" ';
 4060:                     } elsif ($type eq 'begins') {
 4061:                         $beginson = ' checked="checked" ';
 4062:                     }
 4063:                 }
 4064:             } else {
 4065:                 if ($settings->{'searchtypes'} eq 'exact') {
 4066:                     $exacton = ' checked="checked" ';
 4067:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4068:                     $containson = ' checked="checked" ';
 4069:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4070:                     $exacton = ' checked="checked" ';
 4071:                     $containson = ' checked="checked" ';
 4072:                 }
 4073:             }
 4074:         }
 4075:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4076:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4077: 
 4078:         my $numinrow = 4;
 4079:         my $cansrchrow = 0;
 4080:         $datatable='<tr class="LC_odd_row">'.
 4081:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4082:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4083:                    '<input type="radio" name="dirsrch_available"'.
 4084:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4085:                    '<label><input type="radio" name="dirsrch_available"'.
 4086:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4087:                    '</tr><tr>'.
 4088:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4089:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4090:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4091:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4092:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4093:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4094:                    '</tr>';
 4095:         $$rowtotal += 2;
 4096:         if (ref($usertypes) eq 'HASH') {
 4097:             if (keys(%{$usertypes}) > 0) {
 4098:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4099:                                              $numinrow,$othertitle,'cansearch',
 4100:                                              $rowtotal);
 4101:                 $cansrchrow = 1;
 4102:             }
 4103:         }
 4104:         if ($cansrchrow) {
 4105:             $$rowtotal ++;
 4106:             $datatable .= '<tr>';
 4107:         } else {
 4108:             $datatable .= '<tr class="LC_odd_row">';
 4109:         }
 4110:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4111:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4112:         foreach my $title (@{$titleorder}) {
 4113:             if (defined($searchtitles->{$title})) {
 4114:                 my $check = ' ';
 4115:                 if (ref($settings) eq 'HASH') {
 4116:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4117:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4118:                             $check = ' checked="checked" ';
 4119:                         }
 4120:                     }
 4121:                 }
 4122:                 $datatable .= '<td class="LC_left_item">'.
 4123:                               '<span class="LC_nobreak"><label>'.
 4124:                               '<input type="checkbox" name="searchby" '.
 4125:                               'value="'.$title.'"'.$check.'/>'.
 4126:                               $searchtitles->{$title}.'</label></span></td>';
 4127:             }
 4128:         }
 4129:         $datatable .= '</tr></table></td></tr>';
 4130:         $$rowtotal ++;
 4131:         if ($cansrchrow) {
 4132:             $datatable .= '<tr class="LC_odd_row">';
 4133:         } else {
 4134:             $datatable .= '<tr>';
 4135:         }
 4136:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4137:                       '<td class="LC_left_item" colspan="2">'.
 4138:                       '<span class="LC_nobreak"><label>'.
 4139:                       '<input type="checkbox" name="searchtypes" '.
 4140:                       $exacton.' value="exact" />'.&mt('Exact match').
 4141:                       '</label>&nbsp;'.
 4142:                       '<label><input type="checkbox" name="searchtypes" '.
 4143:                       $beginson.' value="begins" />'.&mt('Begins with').
 4144:                       '</label>&nbsp;'.
 4145:                       '<label><input type="checkbox" name="searchtypes" '.
 4146:                       $containson.' value="contains" />'.&mt('Contains').
 4147:                       '</label></span></td></tr>';
 4148:         $$rowtotal ++;
 4149:     } else {
 4150:         my $domsrchon = ' checked="checked" ';
 4151:         my $domsrchoff = ' ';
 4152:         my $domlocalon = ' ';
 4153:         my $domlocaloff = ' checked="checked" ';
 4154:         if (ref($settings) eq 'HASH') {
 4155:             if ($settings->{'lclocalonly'} eq '1') {
 4156:                 $domlocalon = $domlocaloff;
 4157:                 $domlocaloff = ' ';
 4158:             }
 4159:             if ($settings->{'lcavailable'} eq '0') {
 4160:                 $domsrchoff = $domsrchon;
 4161:                 $domsrchon = ' ';
 4162:             }
 4163:         }
 4164:         $datatable='<tr class="LC_odd_row">'.
 4165:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4166:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4167:                       '<input type="radio" name="dirsrch_domavailable"'.
 4168:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4169:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4170:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4171:                       '</tr><tr>'.
 4172:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4173:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4174:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4175:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4176:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4177:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4178:                       '</tr>';
 4179:         $$rowtotal += 2;
 4180:     }
 4181:     return $datatable;
 4182: }
 4183: 
 4184: sub print_contacts {
 4185:     my ($position,$dom,$settings,$rowtotal) = @_;
 4186:     my $datatable;
 4187:     my @contacts = ('adminemail','supportemail');
 4188:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4189:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4190:     if ($position eq 'top') {
 4191:         if (ref($settings) eq 'HASH') {
 4192:             foreach my $item (@contacts) {
 4193:                 if (exists($settings->{$item})) {
 4194:                     $to{$item} = $settings->{$item};
 4195:                 }
 4196:             }
 4197:         }
 4198:     } elsif ($position eq 'middle') {
 4199:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4200:                      'updatesmail','idconflictsmail','hostipmail');
 4201:         foreach my $type (@mailings) {
 4202:             $otheremails{$type} = '';
 4203:         }
 4204:     } elsif ($position eq 'lower') {
 4205:         if (ref($settings) eq 'HASH') {
 4206:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4207:                 %lonstatus = %{$settings->{'lonstatus'}};
 4208:             }
 4209:         }
 4210:     } else {
 4211:         @mailings = ('helpdeskmail','otherdomsmail');
 4212:         foreach my $type (@mailings) {
 4213:             $otheremails{$type} = '';
 4214:         }
 4215:         $bccemails{'helpdeskmail'} = '';
 4216:         $bccemails{'otherdomsmail'} = '';
 4217:         $includestr{'helpdeskmail'} = '';
 4218:         $includestr{'otherdomsmail'} = '';
 4219:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4220:     }
 4221:     if (ref($settings) eq 'HASH') {
 4222:         unless (($position eq 'top') || ($position eq 'lower')) {
 4223:             foreach my $type (@mailings) {
 4224:                 if (exists($settings->{$type})) {
 4225:                     if (ref($settings->{$type}) eq 'HASH') {
 4226:                         foreach my $item (@contacts) {
 4227:                             if ($settings->{$type}{$item}) {
 4228:                                 $checked{$type}{$item} = ' checked="checked" ';
 4229:                             }
 4230:                         }
 4231:                         $otheremails{$type} = $settings->{$type}{'others'};
 4232:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4233:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4234:                             if ($settings->{$type}{'include'} ne '') {
 4235:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4236:                                 $includestr{$type} = &unescape($includestr{$type});
 4237:                             }
 4238:                         }
 4239:                     }
 4240:                 } elsif ($type eq 'lonstatusmail') {
 4241:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4242:                 }
 4243:             }
 4244:         }
 4245:         if ($position eq 'bottom') {
 4246:             foreach my $type (@mailings) {
 4247:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4248:                 if ($settings->{$type}{'include'} ne '') {
 4249:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4250:                     $includestr{$type} = &unescape($includestr{$type});
 4251:                 }
 4252:             }
 4253:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4254:                 if (ref($fields) eq 'ARRAY') {
 4255:                     foreach my $field (@{$fields}) {
 4256:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4257:                     }
 4258:                 }
 4259:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4260:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4261:                 } else {
 4262:                     $maxsize = '1.0';
 4263:                 }
 4264:             } else {
 4265:                 if (ref($fields) eq 'ARRAY') {
 4266:                     foreach my $field (@{$fields}) {
 4267:                         $currfield{$field} = 'yes';
 4268:                     }
 4269:                 }
 4270:                 $maxsize = '1.0';
 4271:             }
 4272:         }
 4273:     } else {
 4274:         if ($position eq 'top') {
 4275:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4276:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4277:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4278:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4279:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4280:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4281:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4282:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4283:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4284:         } elsif ($position eq 'bottom') {
 4285:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4286:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4287:             if (ref($fields) eq 'ARRAY') {
 4288:                 foreach my $field (@{$fields}) {
 4289:                     $currfield{$field} = 'yes';
 4290:                 }
 4291:             }
 4292:             $maxsize = '1.0';
 4293:         }
 4294:     }
 4295:     my ($titles,$short_titles) = &contact_titles();
 4296:     my $rownum = 0;
 4297:     my $css_class;
 4298:     if ($position eq 'top') {
 4299:         foreach my $item (@contacts) {
 4300:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4301:             $datatable .= '<tr'.$css_class.'>'. 
 4302:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4303:                           '</span></td><td class="LC_right_item">'.
 4304:                           '<input type="text" name="'.$item.'" value="'.
 4305:                           $to{$item}.'" /></td></tr>';
 4306:             $rownum ++;
 4307:         }
 4308:     } elsif ($position eq 'bottom') {
 4309:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4310:         $datatable .= '<tr'.$css_class.'>'.
 4311:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4312:                       &mt('(e-mail, subject, and description always shown)').
 4313:                       '</td><td class="LC_left_item">';
 4314:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4315:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4316:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4317:             foreach my $field (@{$fields}) {
 4318:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4319:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4320:                     $datatable .= ' '.&mt('(logged-in users)');
 4321:                 }
 4322:                 $datatable .='</td><td>';
 4323:                 my $clickaction;
 4324:                 if ($field eq 'screenshot') {
 4325:                     $clickaction = ' onclick="screenshotSize(this);"';
 4326:                 }
 4327:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4328:                     foreach my $option (@{$possoptions->{$field}}) {
 4329:                         my $checked;
 4330:                         if ($currfield{$field} eq $option) {
 4331:                             $checked = ' checked="checked"';
 4332:                         }
 4333:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4334:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4335:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4336:                                       '</label></span>'.('&nbsp;'x2);
 4337:                     }
 4338:                 }
 4339:                 if ($field eq 'screenshot') {
 4340:                     my $display;
 4341:                     if ($currfield{$field} eq 'no') {
 4342:                         $display = ' style="display:none"';
 4343:                     }
 4344:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4345:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4346:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4347:                 }
 4348:                 $datatable .= '</td></tr>';
 4349:             }
 4350:             $datatable .= '</table>';
 4351:         }
 4352:         $datatable .= '</td></tr>'."\n";
 4353:         $rownum ++;
 4354:     }
 4355:     unless (($position eq 'top') || ($position eq 'lower')) {
 4356:         foreach my $type (@mailings) {
 4357:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4358:             $datatable .= '<tr'.$css_class.'>'.
 4359:                           '<td><span class="LC_nobreak">'.
 4360:                           $titles->{$type}.': </span></td>'.
 4361:                           '<td class="LC_left_item">';
 4362:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4363:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4364:             }
 4365:             $datatable .= '<span class="LC_nobreak">';
 4366:             foreach my $item (@contacts) {
 4367:                 $datatable .= '<label>'.
 4368:                               '<input type="checkbox" name="'.$type.'"'.
 4369:                               $checked{$type}{$item}.
 4370:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4371:                               '</label>&nbsp;';
 4372:             }
 4373:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4374:                           '<input type="text" name="'.$type.'_others" '.
 4375:                           'value="'.$otheremails{$type}.'"  />';
 4376:             my %locchecked;
 4377:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4378:                 foreach my $loc ('s','b') {
 4379:                     if ($includeloc{$type} eq $loc) {
 4380:                         $locchecked{$loc} = ' checked="checked"';
 4381:                         last;
 4382:                     }
 4383:                 }
 4384:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4385:                               '<input type="text" name="'.$type.'_bcc" '.
 4386:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4387:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4388:                               &mt('Text automatically added to e-mail:').' '.
 4389:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4390:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4391:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4392:                               ('&nbsp;'x2).
 4393:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4394:                               '</span></fieldset>';
 4395:             }
 4396:             $datatable .= '</td></tr>'."\n";
 4397:             $rownum ++;
 4398:         }
 4399:     }
 4400:     if ($position eq 'middle') {
 4401:         my %choices;
 4402:         my $corelink = &core_link_msu();
 4403:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4404:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4405:                                         $corelink);
 4406:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4407:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4408:         my %defaultchecked = ('reporterrors'  => 'on',
 4409:                               'reportupdates' => 'on',
 4410:                               'reportstatus'  => 'on');
 4411:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4412:                                                    \%choices,$rownum);
 4413:         $datatable .= $reports;
 4414:     } elsif ($position eq 'lower') {
 4415:         my (%current,%excluded,%weights);
 4416:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4417:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4418:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4419:         } else {
 4420:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4421:         }
 4422:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4423:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4424:         } else {
 4425:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4426:         }
 4427:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4428:             foreach my $type ('E','W','N','U') {
 4429:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4430:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4431:                 } else {
 4432:                     $weights{$type} = $defaults->{$type};
 4433:                 }
 4434:             }
 4435:         } else {
 4436:             foreach my $type ('E','W','N','U') {
 4437:                 $weights{$type} = $defaults->{$type};
 4438:             }
 4439:         }
 4440:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4441:             if (@{$lonstatus{'excluded'}} > 0) {
 4442:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4443:             }
 4444:         }
 4445:         foreach my $item ('errorthreshold','errorsysmail') { 
 4446:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4447:             $datatable .= '<tr'.$css_class.'>'.
 4448:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4449:                           $titles->{$item}.
 4450:                           '</span></td><td class="LC_left_item">'.
 4451:                           '<input type="text" name="'.$item.'" value="'.
 4452:                           $current{$item}.'" size="5" /></td></tr>';
 4453:             $rownum ++;
 4454:         }
 4455:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4456:         $datatable .= '<tr'.$css_class.'>'.
 4457:                       '<td class="LC_left_item">'.
 4458:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4459:                       '</span></td><td class="LC_left_item"><table><tr>';
 4460:         foreach my $type ('E','W','N','U') {
 4461:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4462:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4463:                           $weights{$type}.'" size="5" /></td>';
 4464:         }
 4465:         $datatable .= '</tr></table></tr>';
 4466:         $rownum ++;
 4467:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4468:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4469:                       $titles->{'errorexcluded'}.'</td>'.
 4470:                       '<td class="LC_left_item"><table>';
 4471:         my $numinrow = 4;
 4472:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4473:         for (my $i=0; $i<@ids; $i++) {
 4474:             my $rem = $i%($numinrow);
 4475:             if ($rem == 0) {
 4476:                 if ($i > 0) {
 4477:                     $datatable .= '</tr>';
 4478:                 }
 4479:                 $datatable .= '<tr>';
 4480:             }
 4481:             my $check;
 4482:             if ($excluded{$ids[$i]}) {
 4483:                 $check = ' checked="checked" ';
 4484:             }
 4485:             $datatable .= '<td class="LC_left_item">'.
 4486:                           '<span class="LC_nobreak"><label>'.
 4487:                           '<input type="checkbox" name="errorexcluded" '.
 4488:                           'value="'.$ids[$i].'"'.$check.' />'.
 4489:                           $ids[$i].'</label></span></td>';
 4490:         }
 4491:         my $colsleft = $numinrow - @ids%($numinrow);
 4492:         if ($colsleft > 1 ) {
 4493:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4494:                           '&nbsp;</td>';
 4495:         } elsif ($colsleft == 1) {
 4496:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4497:         }
 4498:         $datatable .= '</tr></table></td></tr>';
 4499:         $rownum ++;
 4500:     } elsif ($position eq 'bottom') {
 4501:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4502:         my (@posstypes,%usertypeshash);
 4503:         if (ref($types) eq 'ARRAY') {
 4504:             @posstypes = @{$types};
 4505:         }
 4506:         if (@posstypes) {
 4507:             if (ref($usertypes) eq 'HASH') {
 4508:                 %usertypeshash = %{$usertypes};
 4509:             }
 4510:             my @overridden;
 4511:             my $numinrow = 4;
 4512:             if (ref($settings) eq 'HASH') {
 4513:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4514:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4515:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4516:                             push(@overridden,$key);
 4517:                             foreach my $item (@contacts) {
 4518:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4519:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4520:                                 }
 4521:                             }
 4522:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4523:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4524:                             $includeloc{'override_'.$key} = '';
 4525:                             $includestr{'override_'.$key} = '';
 4526:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4527:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 4528:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4529:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4530:                             }
 4531:                         }
 4532:                     }
 4533:                 }
 4534:             }
 4535:             my $customclass = 'LC_helpdesk_override';
 4536:             my $optionsprefix = 'LC_options_helpdesk_';
 4537: 
 4538:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4539:  
 4540:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4541:                                          $numinrow,$othertitle,'overrides',
 4542:                                          \$rownum,$onclicktypes,$customclass);
 4543:             $rownum ++;
 4544:             $usertypeshash{'default'} = $othertitle;
 4545:             foreach my $status (@posstypes) {
 4546:                 my $css_class;
 4547:                 if ($rownum%2) {
 4548:                     $css_class = 'LC_odd_row ';
 4549:                 }
 4550:                 $css_class .= $customclass;
 4551:                 my $rowid = $optionsprefix.$status;
 4552:                 my $hidden = 1;
 4553:                 my $currstyle = 'display:none';
 4554:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4555:                     $currstyle = 'display:table-row';
 4556:                     $hidden = 0;
 4557:                 }
 4558:                 my $key = 'override_'.$status;
 4559:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4560:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4561:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4562:                                                   \@contacts,$short_titles);
 4563:                 unless ($hidden) {
 4564:                     $rownum ++;
 4565:                 }
 4566:             }
 4567:         }
 4568:     }
 4569:     $$rowtotal += $rownum;
 4570:     return $datatable;
 4571: }
 4572: 
 4573: sub core_link_msu {
 4574:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4575:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4576: }
 4577: 
 4578: sub overridden_helpdesk {
 4579:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4580:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4581:     my $class = 'LC_left_item';
 4582:     if ($css_class) {
 4583:         $css_class = ' class="'.$css_class.'"';
 4584:     }
 4585:     if ($rowid) {
 4586:         $rowid = ' id="'.$rowid.'"';
 4587:     }
 4588:     if ($rowstyle) {
 4589:         $rowstyle = ' style="'.$rowstyle.'"';
 4590:     }
 4591:     my ($output,$description);
 4592:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4593:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4594:               "<td>$description</td>\n".
 4595:               '<td class="'.$class.'" colspan="2">'.
 4596:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4597:               '<span class="LC_nobreak">';
 4598:     if (ref($contacts) eq 'ARRAY') {
 4599:         foreach my $item (@{$contacts}) {
 4600:             my $check;
 4601:             if (ref($checked) eq 'HASH') {
 4602:                $check = $checked->{$item};
 4603:             }
 4604:             my $title;
 4605:             if (ref($short_titles) eq 'HASH') {
 4606:                 $title = $short_titles->{$item}; 
 4607:             }
 4608:             $output .= '<label>'.
 4609:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4610:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4611:         }
 4612:     }
 4613:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4614:                '<input type="text" name="override_'.$type.'_others" '.
 4615:                'value="'.$otheremails.'"  />';
 4616:     my %locchecked;
 4617:     foreach my $loc ('s','b') {
 4618:         if ($includeloc eq $loc) {
 4619:             $locchecked{$loc} = ' checked="checked"';
 4620:             last;
 4621:         }
 4622:     }
 4623:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4624:                '<input type="text" name="override_'.$type.'_bcc" '.
 4625:                'value="'.$bccemails.'"  /></fieldset>'.
 4626:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4627:                &mt('Text automatically added to e-mail:').' '.
 4628:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4629:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4630:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4631:                ('&nbsp;'x2).
 4632:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4633:                '</span></fieldset>'.
 4634:                '</td></tr>'."\n";
 4635:     return $output;
 4636: }
 4637: 
 4638: sub contacts_javascript {
 4639:     return <<"ENDSCRIPT";
 4640: 
 4641: <script type="text/javascript">
 4642: // <![CDATA[
 4643: 
 4644: function screenshotSize(field) {
 4645:     if (document.getElementById('help_screenshotsize')) {
 4646:         if (field.value == 'no') {
 4647:             document.getElementById('help_screenshotsize').style.display="none";
 4648:         } else {
 4649:             document.getElementById('help_screenshotsize').style.display="";
 4650:         }
 4651:     }
 4652:     return;
 4653: }
 4654: 
 4655: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4656:     if (form.elements[checkbox].length != undefined) {
 4657:         var count = 0;
 4658:         if (docount) {
 4659:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4660:                 if (form.elements[checkbox][i].checked) {
 4661:                     count ++;
 4662:                 }
 4663:             }
 4664:         }
 4665:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4666:             var type = form.elements[checkbox][i].value;
 4667:             if (document.getElementById(prefix+type)) {
 4668:                 if (form.elements[checkbox][i].checked) {
 4669:                     document.getElementById(prefix+type).style.display = 'table-row';
 4670:                     if (count % 2 == 1) {
 4671:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4672:                     } else {
 4673:                         document.getElementById(prefix+type).className = target;
 4674:                     }
 4675:                     count ++;
 4676:                 } else {
 4677:                     document.getElementById(prefix+type).style.display = 'none';
 4678:                 }
 4679:             }
 4680:         }
 4681:     }
 4682:     return;
 4683: }
 4684: 
 4685: 
 4686: // ]]>
 4687: </script>
 4688: 
 4689: ENDSCRIPT
 4690: }
 4691: 
 4692: sub print_helpsettings {
 4693:     my ($position,$dom,$settings,$rowtotal) = @_;
 4694:     my $confname = $dom.'-domainconfig';
 4695:     my $formname = 'display';
 4696:     my ($datatable,$itemcount);
 4697:     if ($position eq 'top') {
 4698:         $itemcount = 1;
 4699:         my (%choices,%defaultchecked,@toggles);
 4700:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4701:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4702:                                      &mt('LON-CAPA bug tracker'),600,500));
 4703:         %defaultchecked = ('submitbugs' => 'on');
 4704:         @toggles = ('submitbugs');
 4705:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4706:                                                      \%choices,$itemcount);
 4707:         $$rowtotal ++;
 4708:     } else {
 4709:         my $css_class;
 4710:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4711:         my (%customroles,%ordered,%current);
 4712:         if (ref($settings) eq 'HASH') {
 4713:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4714:                 %current = %{$settings->{'adhoc'}};
 4715:             }
 4716:         }
 4717:         my $count = 0;
 4718:         foreach my $key (sort(keys(%existing))) {
 4719:             if ($key=~/^rolesdef\_(\w+)$/) {
 4720:                 my $rolename = $1;
 4721:                 my (%privs,$order);
 4722:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4723:                 $customroles{$rolename} = \%privs;
 4724:                 if (ref($current{$rolename}) eq 'HASH') {
 4725:                     $order = $current{$rolename}{'order'};
 4726:                 }
 4727:                 if ($order eq '') {
 4728:                     $order = $count;
 4729:                 }
 4730:                 $ordered{$order} = $rolename;
 4731:                 $count++;
 4732:             }
 4733:         }
 4734:         my $maxnum = scalar(keys(%ordered));
 4735:         my @roles_by_num = ();
 4736:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4737:             push(@roles_by_num,$item);
 4738:         }
 4739:         my $context = 'domprefs';
 4740:         my $crstype = 'Course';
 4741:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4742:         my @accesstypes = ('all','dh','da','none');
 4743:         my ($numstatustypes,@jsarray);
 4744:         if (ref($types) eq 'ARRAY') {
 4745:             if (@{$types} > 0) {
 4746:                 $numstatustypes = scalar(@{$types});
 4747:                 push(@accesstypes,'status');
 4748:                 @jsarray = ('bystatus');
 4749:             }
 4750:         }
 4751:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4752:         if (keys(%domhelpdesk)) {
 4753:             push(@accesstypes,('inc','exc'));
 4754:             push(@jsarray,('notinc','notexc'));
 4755:         }
 4756:         my $hiddenstr = join("','",@jsarray);
 4757:         my $context = 'domprefs';
 4758:         my $crstype = 'Course';
 4759:         my $prefix = 'helproles_';
 4760:         my $add_class = 'LC_hidden';
 4761:         foreach my $num (@roles_by_num) {
 4762:             my $role = $ordered{$num};
 4763:             my ($desc,$access,@statuses);
 4764:             if (ref($current{$role}) eq 'HASH') {
 4765:                 $desc = $current{$role}{'desc'};
 4766:                 $access = $current{$role}{'access'};
 4767:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4768:                     @statuses = @{$current{$role}{'insttypes'}};
 4769:                 }
 4770:             }
 4771:             if ($desc eq '') {
 4772:                 $desc = $role;
 4773:             }
 4774:             my $identifier = 'custhelp'.$num;
 4775:             my %full=();
 4776:             my %levels= (
 4777:                          course => {},
 4778:                          domain => {},
 4779:                          system => {},
 4780:                         );
 4781:             my %levelscurrent=(
 4782:                                course => {},
 4783:                                domain => {},
 4784:                                system => {},
 4785:                               );
 4786:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4787:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4788:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4789:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4790:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4791:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4792:             for (my $k=0; $k<=$maxnum; $k++) {
 4793:                 my $vpos = $k+1;
 4794:                 my $selstr;
 4795:                 if ($k == $num) {
 4796:                     $selstr = ' selected="selected" ';
 4797:                 }
 4798:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4799:             }
 4800:             $datatable .= '</select>'.('&nbsp;'x2).
 4801:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4802:                           '</td>'.
 4803:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4804:                           &mt('Name shown to users:').
 4805:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4806:                           '</fieldset>'.
 4807:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4808:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4809:                           '<fieldset>'.
 4810:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4811:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4812:                                                                    \%levelscurrent,$identifier,
 4813:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4814:                           '</fieldset></td>';
 4815:             $itemcount ++;
 4816:         }
 4817:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4818:         my $newcust = 'custhelp'.$count;
 4819:         my (%privs,%levelscurrent);
 4820:         my %full=();
 4821:         my %levels= (
 4822:                      course => {},
 4823:                      domain => {},
 4824:                      system => {},
 4825:                     );
 4826:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4827:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4828:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4829:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4830:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4831:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4832:         for (my $k=0; $k<$maxnum+1; $k++) {
 4833:             my $vpos = $k+1;
 4834:             my $selstr;
 4835:             if ($k == $maxnum) {
 4836:                 $selstr = ' selected="selected" ';
 4837:             }
 4838:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4839:         }
 4840:         $datatable .= '</select>&nbsp;'."\n".
 4841:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4842:                       '</label></span></td>'.
 4843:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4844:                       '<span class="LC_nobreak">'.
 4845:                       &mt('Internal name:').
 4846:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4847:                       '</span>'.('&nbsp;'x4).
 4848:                       '<span class="LC_nobreak">'.
 4849:                       &mt('Name shown to users:').
 4850:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4851:                       '</span></fieldset>'.
 4852:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4853:                                              $usertypes,$types,\%domhelpdesk).
 4854:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4855:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4856:                                                                 \@templateroles,$newcust).
 4857:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4858:                                                                \%levelscurrent,$newcust).
 4859:                       '</fieldset>'.
 4860:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4861:                       '</td></tr>';
 4862:         $count ++;
 4863:         $$rowtotal += $count;
 4864:     }
 4865:     return $datatable;
 4866: }
 4867: 
 4868: sub adhocbutton {
 4869:     my ($prefix,$num,$field,$visibility) = @_;
 4870:     my %lt = &Apache::lonlocal::texthash(
 4871:                                           show => 'Show details',
 4872:                                           hide => 'Hide details',
 4873:                                         );
 4874:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4875:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4876:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4877:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4878: }
 4879: 
 4880: sub helpsettings_javascript {
 4881:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4882:     return unless(ref($roles_by_num) eq 'ARRAY');
 4883:     my %html_js_lt = &Apache::lonlocal::texthash(
 4884:                                           show => 'Show details',
 4885:                                           hide => 'Hide details',
 4886:                                         );
 4887:     &html_escape(\%html_js_lt);
 4888:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4889:     return <<"ENDSCRIPT";
 4890: <script type="text/javascript">
 4891: // <![CDATA[
 4892: 
 4893: function reorderHelpRoles(form,item) {
 4894:     var changedVal;
 4895: $jstext
 4896:     var newpos = 'helproles_${total}_pos';
 4897:     var maxh = 1 + $total;
 4898:     var current = new Array();
 4899:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4900:     if (item == newpos) {
 4901:         changedVal = newitemVal;
 4902:     } else {
 4903:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4904:         current[newitemVal] = newpos;
 4905:     }
 4906:     for (var i=0; i<helproles.length; i++) {
 4907:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4908:         if (elementName != item) {
 4909:             if (form.elements[elementName]) {
 4910:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4911:                 current[currVal] = elementName;
 4912:             }
 4913:         }
 4914:     }
 4915:     var oldVal;
 4916:     for (var j=0; j<maxh; j++) {
 4917:         if (current[j] == undefined) {
 4918:             oldVal = j;
 4919:         }
 4920:     }
 4921:     if (oldVal < changedVal) {
 4922:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4923:            var elementName = current[k];
 4924:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4925:         }
 4926:     } else {
 4927:         for (var k=changedVal; k<oldVal; k++) {
 4928:             var elementName = current[k];
 4929:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4930:         }
 4931:     }
 4932:     return;
 4933: }
 4934: 
 4935: function helpdeskAccess(num) {
 4936:     var curraccess = null;
 4937:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4938:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4939:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4940:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4941:             }
 4942:         }
 4943:     }
 4944:     var shown = Array();
 4945:     var hidden = Array();
 4946:     if (curraccess == 'none') {
 4947:         hidden = Array('$hiddenstr');
 4948:     } else {
 4949:         if (curraccess == 'status') {
 4950:             shown = Array('bystatus');
 4951:             hidden = Array('notinc','notexc');
 4952:         } else {
 4953:             if (curraccess == 'exc') {
 4954:                 shown = Array('notexc');
 4955:                 hidden = Array('notinc','bystatus');
 4956:             }
 4957:             if (curraccess == 'inc') {
 4958:                 shown = Array('notinc');
 4959:                 hidden = Array('notexc','bystatus');
 4960:             }
 4961:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4962:                 hidden = Array('notinc','notexc','bystatus');
 4963:             }
 4964:         }
 4965:     }
 4966:     if (hidden.length > 0) {
 4967:         for (var i=0; i<hidden.length; i++) {
 4968:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4969:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4970:             }
 4971:         }
 4972:     }
 4973:     if (shown.length > 0) {
 4974:         for (var i=0; i<shown.length; i++) {
 4975:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4976:                 if (shown[i] == 'privs') {
 4977:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4978:                 } else {
 4979:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4980:                 }
 4981:             }
 4982:         }
 4983:     }
 4984:     return;
 4985: }
 4986: 
 4987: function toggleHelpdeskItem(num,field) {
 4988:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4989:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4990:             document.getElementById('helproles_'+num+'_'+field).className =
 4991:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4992:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4993:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4994:             }
 4995:         } else {
 4996:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4997:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4998:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4999:             }
 5000:         }
 5001:     }
 5002:     return;
 5003: }
 5004: 
 5005: // ]]>
 5006: </script>
 5007: 
 5008: ENDSCRIPT
 5009: }
 5010: 
 5011: sub helpdeskroles_access {
 5012:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5013:         $usertypes,$types,$domhelpdesk) = @_;
 5014:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5015:     my %lt = &Apache::lonlocal::texthash(
 5016:                     'rou'    => 'Role usage',
 5017:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5018:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5019:                     'dh'     => 'All with domain helpdesk role',
 5020:                     'da'     => 'All with domain helpdesk assistant role',
 5021:                     'none'   => 'None',
 5022:                     'status' => 'Determined based on institutional status',
 5023:                     'inc'    => 'Include all, but exclude specific personnel',
 5024:                     'exc'    => 'Exclude all, but include specific personnel',
 5025:                   );
 5026:     my %usecheck = (
 5027:                      all => ' checked="checked"',
 5028:                    );
 5029:     my %displaydiv = (
 5030:                       status => 'none',
 5031:                       inc    => 'none',
 5032:                       exc    => 'none',
 5033:                       priv   => 'block',
 5034:                      );
 5035:     my $output;
 5036:     if (ref($current) eq 'HASH') {
 5037:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5038:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5039:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5040:                 delete($usecheck{'all'});
 5041:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5042:                     my $access = $1;
 5043:                     $displaydiv{$access} = 'inline';
 5044:                 } elsif ($current->{access} eq 'none') {
 5045:                     $displaydiv{'priv'} = 'none';
 5046:                 }
 5047:             }
 5048:         }
 5049:     }
 5050:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5051:               '<p>'.$lt{'whi'}.'</p>';
 5052:     foreach my $access (@{$accesstypes}) {
 5053:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5054:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5055:                    $lt{$access}.'</label>';
 5056:         if ($access eq 'status') {
 5057:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5058:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5059:                                                                  $othertitle,$usertypes,$types).
 5060:                        '</div>';
 5061:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5062:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5063:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5064:                        '</div>';
 5065:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5066:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5067:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5068:                        '</div>';
 5069:         }
 5070:         $output .= '</p>';
 5071:     }
 5072:     $output .= '</fieldset>';
 5073:     return $output;
 5074: }
 5075: 
 5076: sub radiobutton_prefs {
 5077:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5078:         $additional,$align,$firstval) = @_;
 5079:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5080:                    (ref($choices) eq 'HASH'));
 5081: 
 5082:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5083: 
 5084:     foreach my $item (@{$toggles}) {
 5085:         if ($defaultchecked->{$item} eq 'on') {
 5086:             $checkedon{$item} = ' checked="checked" ';
 5087:             $checkedoff{$item} = ' ';
 5088:         } elsif ($defaultchecked->{$item} eq 'off') {
 5089:             $checkedoff{$item} = ' checked="checked" ';
 5090:             $checkedon{$item} = ' ';
 5091:         }
 5092:     }
 5093:     if (ref($settings) eq 'HASH') {
 5094:         foreach my $item (@{$toggles}) {
 5095:             if ($settings->{$item} eq '1') {
 5096:                 $checkedon{$item} =  ' checked="checked" ';
 5097:                 $checkedoff{$item} = ' ';
 5098:             } elsif ($settings->{$item} eq '0') {
 5099:                 $checkedoff{$item} =  ' checked="checked" ';
 5100:                 $checkedon{$item} = ' ';
 5101:             }
 5102:         }
 5103:     }
 5104:     if ($onclick) {
 5105:         $onclick = ' onclick="'.$onclick.'"';
 5106:     }
 5107:     foreach my $item (@{$toggles}) {
 5108:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5109:         $datatable .=
 5110:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5111:             '<span class="LC_nobreak">'.$choices->{$item}.
 5112:             '</span></td>';
 5113:         if ($align eq 'left') {
 5114:             $datatable .= '<td class="LC_left_item">';
 5115:         } else {
 5116:             $datatable .= '<td class="LC_right_item">';
 5117:         }
 5118:         $datatable .= '<span class="LC_nobreak">';
 5119:         if ($firstval eq 'no') {
 5120:             $datatable .=
 5121:                 '<label><input type="radio" name="'.
 5122:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5123:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5124:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5125:         } else {
 5126:             $datatable .=
 5127:                 '<label><input type="radio" name="'.
 5128:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5129:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5130:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5131:         }
 5132:         $datatable .= '</span>'.$additional.'</td></tr>';
 5133:         $itemcount ++;
 5134:     }
 5135:     return ($datatable,$itemcount);
 5136: }
 5137: 
 5138: sub print_ltitools {
 5139:     my ($dom,$settings,$rowtotal) = @_;
 5140:     my $rownum = 0;
 5141:     my $css_class;
 5142:     my $itemcount = 1;
 5143:     my $maxnum = 0;
 5144:     my %ordered;
 5145:     if (ref($settings) eq 'HASH') {
 5146:         foreach my $item (keys(%{$settings})) {
 5147:             if (ref($settings->{$item}) eq 'HASH') {
 5148:                 my $num = $settings->{$item}{'order'};
 5149:                 $ordered{$num} = $item;
 5150:             }
 5151:         }
 5152:     }
 5153:     my $confname = $dom.'-domainconfig';
 5154:     my $switchserver = &check_switchserver($dom,$confname);
 5155:     my $maxnum = scalar(keys(%ordered));
 5156:     my $datatable;
 5157:     my %lt = &ltitools_names();
 5158:     my @courseroles = ('cc','in','ta','ep','st');
 5159:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 5160:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 5161:     if (keys(%ordered)) {
 5162:         my @items = sort { $a <=> $b } keys(%ordered);
 5163:         for (my $i=0; $i<@items; $i++) {
 5164:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5165:             my $item = $ordered{$items[$i]};
 5166:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 5167:             if (ref($settings->{$item}) eq 'HASH') {
 5168:                 $title = $settings->{$item}->{'title'};
 5169:                 $url = $settings->{$item}->{'url'};
 5170:                 $key = $settings->{$item}->{'key'};
 5171:                 $secret = $settings->{$item}->{'secret'};
 5172:                 $lifetime = $settings->{$item}->{'lifetime'};
 5173:                 my $image = $settings->{$item}->{'image'};
 5174:                 if ($image ne '') {
 5175:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 5176:                 }
 5177:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 5178:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 5179:                 } else {
 5180:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 5181:                 }
 5182:             }
 5183:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 5184:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5185:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 5186:             for (my $k=0; $k<=$maxnum; $k++) {
 5187:                 my $vpos = $k+1;
 5188:                 my $selstr;
 5189:                 if ($k == $i) {
 5190:                     $selstr = ' selected="selected" ';
 5191:                 }
 5192:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5193:             }
 5194:             $datatable .= '</select>'.('&nbsp;'x2).
 5195:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 5196:                 &mt('Delete?').'</label></span></td>'.
 5197:                 '<td colspan="2">'.
 5198:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5199:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 5200:                 ('&nbsp;'x2).
 5201:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 5202:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 5203:                 ('&nbsp;'x2).
 5204:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 5205:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 5206:                 ('&nbsp;'x2).
 5207:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 5208:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 5209:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 5210:                 '<br /><br />'.
 5211:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 5212:                 ' value="'.$url.'" /></span>'.
 5213:                 ('&nbsp;'x2).
 5214:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 5215:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 5216:                 ('&nbsp;'x2).
 5217:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 5218:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 5219:                 ('&nbsp;'x2).
 5220:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 5221:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 5222:                 '<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>'.
 5223:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 5224:                 '</fieldset>'.
 5225:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 5226:                 '<span class="LC_nobreak">'.&mt('Display target:');
 5227:             my %currdisp;
 5228:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 5229:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 5230:                     $currdisp{'window'} = ' checked="checked"';
 5231:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 5232:                     $currdisp{'tab'} = ' checked="checked"';
 5233:                 } else {
 5234:                     $currdisp{'iframe'} = ' checked="checked"';
 5235:                 }
 5236:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 5237:                     $currdisp{'width'} = $1;
 5238:                 }
 5239:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 5240:                      $currdisp{'height'} = $1;
 5241:                 }
 5242:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 5243:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 5244:             } else {
 5245:                 $currdisp{'iframe'} = ' checked="checked"';
 5246:             }
 5247:             foreach my $disp ('iframe','tab','window') {
 5248:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 5249:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 5250:             }
 5251:             $datatable .= ('&nbsp;'x4);
 5252:             foreach my $dimen ('width','height') {
 5253:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 5254:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 5255:                               ('&nbsp;'x2);
 5256:             }
 5257:             $datatable .= '</span><br />'.
 5258:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 5259:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 5260:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 5261:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 5262:                           '</textarea></div><div style=""></div><br />';
 5263:             my %units = (
 5264:                           'passback' => 'days',
 5265:                           'roster'   => 'seconds',
 5266:                         );
 5267:             foreach my $extra ('passback','roster') {
 5268:                 my $validsty = 'none';
 5269:                 my $currvalid;
 5270:                 my $checkedon = '';
 5271:                 my $checkedoff = ' checked="checked"';
 5272:                 if ($settings->{$item}->{$extra}) {
 5273:                     $checkedon = $checkedoff;
 5274:                     $checkedoff = '';
 5275:                     $validsty = 'inline-block';
 5276:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 5277:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 5278:                     }
 5279:                 }
 5280:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 5281:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 5282:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 5283:                               &mt('No').'</label>'.('&nbsp;'x2).
 5284:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 5285:                               &mt('Yes').'</label></span></div>'.
 5286:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 5287:                               '<span class="LC_nobreak">'.
 5288:                               &mt("at least [_1] $units{$extra} after launch",
 5289:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 5290:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5291:             }
 5292:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5293:             if ($imgsrc) {
 5294:                 $datatable .= $imgsrc.
 5295:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 5296:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 5297:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 5298:             } else {
 5299:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 5300:             }
 5301:             if ($switchserver) {
 5302:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5303:             } else {
 5304:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 5305:             }
 5306:             $datatable .= '</span></fieldset>';
 5307:             my (%checkedfields,%rolemaps,$userincdom);
 5308:             if (ref($settings->{$item}) eq 'HASH') {
 5309:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 5310:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 5311:                 }
 5312:                 $userincdom = $settings->{$item}->{'incdom'};
 5313:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 5314:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 5315:                     $checkedfields{'roles'} = 1;
 5316:                 }
 5317:             }
 5318:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 5319:                           '<span class="LC_nobreak">';
 5320:             my $userfieldstyle = 'display:none;';
 5321:             my $seluserdom = '';
 5322:             my $unseluserdom = ' selected="selected"';
 5323:             foreach my $field (@fields) {
 5324:                 my ($checked,$onclick,$id,$spacer);
 5325:                 if ($checkedfields{$field}) {
 5326:                     $checked = ' checked="checked"';
 5327:                 }
 5328:                 if ($field eq 'user') {
 5329:                     $id = ' id="ltitools_user_field_'.$i.'"';
 5330:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 5331:                     if ($checked) {
 5332:                         $userfieldstyle = 'display:inline-block';
 5333:                         if ($userincdom) {
 5334:                             $seluserdom = $unseluserdom;
 5335:                             $unseluserdom = '';
 5336:                         }
 5337:                     }
 5338:                 } else {
 5339:                     $spacer = ('&nbsp;' x2);
 5340:                 }
 5341:                 $datatable .= '<label>'.
 5342:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 5343:                               $lt{$field}.'</label>'.$spacer;
 5344:             }
 5345:             $datatable .= '</span>';
 5346:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 5347:                           '<span class="LC_nobreak"> : '.
 5348:                           '<select name="ltitools_userincdom_'.$i.'">'.
 5349:                           '<option value="">'.&mt('Select').'</option>'.
 5350:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 5351:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 5352:                           '</select></span></div>';
 5353:             $datatable .= '</fieldset>'.
 5354:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 5355:             foreach my $role (@courseroles) {
 5356:                 my ($selected,$selectnone);
 5357:                 if (!$rolemaps{$role}) {
 5358:                     $selectnone = ' selected="selected"';
 5359:                 }
 5360:                 $datatable .= '<td style="text-align: center">'. 
 5361:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5362:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 5363:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5364:                 foreach my $ltirole (@ltiroles) {
 5365:                     unless ($selectnone) {
 5366:                         if ($rolemaps{$role} eq $ltirole) {
 5367:                             $selected = ' selected="selected"';
 5368:                         } else {
 5369:                             $selected = '';
 5370:                         }
 5371:                     }
 5372:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5373:                 }
 5374:                 $datatable .= '</select></td>';
 5375:             }
 5376:             $datatable .= '</tr></table></fieldset>';
 5377:             my %courseconfig;
 5378:             if (ref($settings->{$item}) eq 'HASH') {
 5379:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 5380:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 5381:                 }
 5382:             }
 5383:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 5384:             foreach my $item ('label','title','target','linktext','explanation','append') {
 5385:                 my $checked;
 5386:                 if ($courseconfig{$item}) {
 5387:                     $checked = ' checked="checked"';
 5388:                 }
 5389:                 $datatable .= '<label>'.
 5390:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 5391:                        $lt{'crs'.$item}.'</label>&nbsp; '."\n";
 5392:             }
 5393:             $datatable .= '</span></fieldset>'.
 5394:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 5395:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 5396:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 5397:                 my %custom = %{$settings->{$item}->{'custom'}};
 5398:                 if (keys(%custom) > 0) {
 5399:                     foreach my $key (sort(keys(%custom))) {
 5400:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 5401:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 5402:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5403:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 5404:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 5405:                     }
 5406:                 }
 5407:             }
 5408:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 5409:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 5410:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 5411:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 5412:             $datatable .= '</table></fieldset></td></tr>'."\n";
 5413:             $itemcount ++;
 5414:         }
 5415:     }
 5416:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5417:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 5418:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5419:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 5420:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 5421:     for (my $k=0; $k<$maxnum+1; $k++) {
 5422:         my $vpos = $k+1;
 5423:         my $selstr;
 5424:         if ($k == $maxnum) {
 5425:             $selstr = ' selected="selected" ';
 5426:         }
 5427:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5428:     }
 5429:     $datatable .= '</select>&nbsp;'."\n".
 5430:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5431:                   '<td colspan="2">'.
 5432:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5433:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 5434:                   ('&nbsp;'x2).
 5435:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 5436:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5437:                   ('&nbsp;'x2).
 5438:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 5439:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 5440:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 5441:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5442:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5443:                   '<br />'.
 5444:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 5445:                   ('&nbsp;'x2).
 5446:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 5447:                   ('&nbsp;'x2).
 5448:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 5449:                   ('&nbsp;'x2).
 5450:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 5451:                   '<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".
 5452:                   '</fieldset>'.
 5453:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 5454:                   '<span class="LC_nobreak">'.&mt('Display target:');
 5455:     my %defaultdisp;
 5456:     $defaultdisp{'iframe'} = ' checked="checked"';
 5457:     foreach my $disp ('iframe','tab','window') {
 5458:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 5459:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 5460:     }
 5461:     $datatable .= ('&nbsp;'x4);
 5462:     foreach my $dimen ('width','height') {
 5463:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 5464:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 5465:                       ('&nbsp;'x2);
 5466:     }
 5467:     $datatable .= '</span><br />'.
 5468:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 5469:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 5470:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 5471:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 5472:                   '</div><div style=""></div><br />';
 5473:     my %units = (
 5474:                   'passback' => 'days',
 5475:                   'roster'   => 'seconds',
 5476:                 );
 5477:     my %defaulttimes = (
 5478:                      'passback' => '7',
 5479:                      'roster'   => '300',
 5480:                    );
 5481:     foreach my $extra ('passback','roster') {
 5482:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 5483:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 5484:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 5485:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 5486:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 5487:                       &mt('Yes').'</label></span></div>'.
 5488:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 5489:                       '<span class="LC_nobreak">'.
 5490:                       &mt("at least [_1] $units{$extra} after launch",
 5491:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 5492:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5493:     }
 5494:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 5495:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 5496:     if ($switchserver) {
 5497:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5498:     } else {
 5499:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 5500:     }
 5501:     $datatable .= '</span></fieldset>'.
 5502:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 5503:                   '<span class="LC_nobreak">';
 5504:     foreach my $field (@fields) {
 5505:         my ($id,$onclick,$spacer);
 5506:         if ($field eq 'user') {
 5507:             $id = ' id="ltitools_user_field_add"';
 5508:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 5509:         } else {
 5510:             $spacer = ('&nbsp;' x2);
 5511:         }
 5512:         $datatable .= '<label>'.
 5513:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 5514:                       $lt{$field}.'</label>'.$spacer;
 5515:     }
 5516:     $datatable .= '</span>'.
 5517:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 5518:                   '<span class="LC_nobreak"> : '.
 5519:                   '<select name="ltitools_userincdom_add">'.
 5520:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 5521:                   '<option value="0">'.&mt('username').'</option>'.
 5522:                   '<option value="1">'.&mt('username:domain').'</option>'.
 5523:                   '</select></span></div></fieldset>';
 5524:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 5525:     foreach my $role (@courseroles) {
 5526:         my ($checked,$checkednone);
 5527:         $datatable .= '<td style="text-align: center">'.
 5528:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5529:                       '<select name="ltitools_add_roles_'.$role.'">'.
 5530:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 5531:         foreach my $ltirole (@ltiroles) {
 5532:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 5533:         }
 5534:         $datatable .= '</select></td>';
 5535:     }
 5536:     $datatable .= '</tr></table></fieldset>'.
 5537:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 5538:     foreach my $item ('label','title','target','linktext','explanation','append') {
 5539:         $datatable .= '<label>'.
 5540:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 5541:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 5542:     }
 5543:     $datatable .= '</span></fieldset>'.
 5544:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 5545:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5546:                   '<tr><td><span class="LC_nobreak">'.
 5547:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 5548:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 5549:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 5550:                   '</table></fieldset>'."\n".
 5551:                   '</td>'."\n".
 5552:                   '</tr>'."\n";
 5553:     $itemcount ++;
 5554:     return $datatable;
 5555: }
 5556: 
 5557: sub ltitools_names {
 5558:     my %lt = &Apache::lonlocal::texthash(
 5559:                                           'title'          => 'Title',
 5560:                                           'version'        => 'Version',
 5561:                                           'msgtype'        => 'Message Type',
 5562:                                           'sigmethod'      => 'Signature Method',
 5563:                                           'url'            => 'URL',
 5564:                                           'key'            => 'Key',
 5565:                                           'lifetime'       => 'Nonce lifetime (s)',
 5566:                                           'secret'         => 'Secret',
 5567:                                           'icon'           => 'Icon',   
 5568:                                           'user'           => 'User',
 5569:                                           'fullname'       => 'Full Name',
 5570:                                           'firstname'      => 'First Name',
 5571:                                           'lastname'       => 'Last Name',
 5572:                                           'email'          => 'E-mail',
 5573:                                           'roles'          => 'Role',
 5574:                                           'window'         => 'Window',
 5575:                                           'tab'            => 'Tab',
 5576:                                           'iframe'         => 'iFrame',
 5577:                                           'height'         => 'Height',
 5578:                                           'width'          => 'Width',
 5579:                                           'linktext'       => 'Default Link Text',
 5580:                                           'explanation'    => 'Default Explanation',
 5581:                                           'passback'       => 'Tool can return grades:',
 5582:                                           'roster'         => 'Tool can retrieve roster:',
 5583:                                           'crstarget'      => 'Display target',
 5584:                                           'crslabel'       => 'Course label',
 5585:                                           'crstitle'       => 'Course title', 
 5586:                                           'crslinktext'    => 'Link Text',
 5587:                                           'crsexplanation' => 'Explanation',
 5588:                                           'crsappend'      => 'Provider URL',
 5589:                                         );
 5590:     return %lt;
 5591: }
 5592: 
 5593: sub print_proctoring {
 5594:     my ($dom,$settings,$rowtotal) = @_;
 5595:     my $itemcount = 1;
 5596:     my (%ordered,%providernames,%current,%currentdef);
 5597:     my $confname = $dom.'-domainconfig';
 5598:     my $switchserver = &check_switchserver($dom,$confname);
 5599:     if (ref($settings) eq 'HASH') {
 5600:         foreach my $item (keys(%{$settings})) {
 5601:             if (ref($settings->{$item}) eq 'HASH') {
 5602:                 my $num = $settings->{$item}{'order'};
 5603:                 $ordered{$num} = $item;
 5604:             }
 5605:         }
 5606:     } else {
 5607:         %ordered = (
 5608:                      1 => 'proctorio',
 5609:                      2 => 'examity',
 5610:                    );
 5611:     }
 5612:     %providernames = &proctoring_providernames();
 5613:     my $maxnum = scalar(keys(%ordered));
 5614:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5615:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5616:     if (ref($requref) eq 'HASH') {
 5617:         %requserfields = %{$requref};
 5618:     }
 5619:     if (ref($opturef) eq 'HASH') {
 5620:         %optuserfields = %{$opturef};
 5621:     }
 5622:     if (ref($defref) eq 'HASH') {
 5623:         %defaults = %{$defref};
 5624:     }
 5625:     if (ref($extref) eq 'HASH') {
 5626:         %extended = %{$extref};
 5627:     }
 5628:     if (ref($crsref) eq 'HASH') {
 5629:         %crsconf = %{$crsref};
 5630:     }
 5631:     if (ref($rolesref) eq 'ARRAY') {
 5632:         @courseroles = @{$rolesref};
 5633:     }
 5634:     if (ref($ltiref) eq 'ARRAY') {
 5635:         @ltiroles = @{$ltiref};
 5636:     }
 5637:     my $datatable;
 5638:     my $css_class;
 5639:     if (keys(%ordered)) {
 5640:         my @items = sort { $a <=> $b } keys(%ordered);
 5641:         for (my $i=0; $i<@items; $i++) {
 5642:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5643:             my $provider = $ordered{$items[$i]};
 5644:             my $optionsty = 'none';
 5645:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5646:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5647:             if (ref($settings) eq 'HASH') {
 5648:                 if (ref($settings->{$provider}) eq 'HASH') {
 5649:                     %current = %{$settings->{$provider}};
 5650:                     if ($current{'available'}) {
 5651:                         $optionsty = 'block';
 5652:                         $available = 1;
 5653:                     }
 5654:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5655:                         $lifetime = $current{'lifetime'};
 5656:                     }
 5657:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5658:                         $version = $current{'version'};
 5659:                     }
 5660:                     if ($current{'image'} ne '') {
 5661:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5662:                     }
 5663:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5664:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5665:                     }
 5666:                     $userincdom = $current{'incdom'};
 5667:                     if (ref($current{'roles'}) eq 'HASH') {
 5668:                         %rolemaps = %{$current{'roles'}};
 5669:                         $checkedfields{'roles'} = 1;
 5670:                     }
 5671:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5672:                         foreach my $val (@{$current{'defaults'}}) {
 5673:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5674:                                 $inuse{$val} = 1;
 5675:                             } else {
 5676:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5677:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5678:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5679:                                             $inuse{$poss} = $val;
 5680:                                             last;
 5681:                                         }
 5682:                                     }
 5683:                                 }
 5684:                             }
 5685:                         }
 5686:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5687:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5688:                             my $currval = $current{'defaults'}{$key}; 
 5689:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5690:                                 $inuse{$key} = 1;
 5691:                             } else {
 5692:                                 my $match;
 5693:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5694:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5695:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5696:                                             $inuse{$poss} = $key;
 5697:                                             last;
 5698:                                         }
 5699:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5700:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5701:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5702:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5703:                                                     $currentdef{$inner} = $currval;
 5704:                                                     $match = 1;
 5705:                                                     last;
 5706:                                                 }
 5707:                                             } elsif ($inner eq $key) {
 5708:                                                 $currentdef{$key} = $currval;
 5709:                                                 $match = 1;
 5710:                                                 last;
 5711:                                             }
 5712:                                         }
 5713:                                     }
 5714:                                     last if ($match);
 5715:                                 }
 5716:                             }
 5717:                         }
 5718:                     }
 5719:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5720:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5721:                     }
 5722:                 }
 5723:             }
 5724:             my %lt = &proctoring_titles($provider);
 5725:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5726:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5727:             my %checkedavailable = (
 5728:                                    yes => '',
 5729:                                    no  => ' checked="checked"',
 5730:                                  );
 5731:             if ($available) {
 5732:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5733:                 $checkedavailable{'no'} = '';
 5734:             }
 5735:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5736:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5737:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5738:             for (my $k=0; $k<$maxnum; $k++) {
 5739:                 my $vpos = $k+1;
 5740:                 my $selstr;
 5741:                 if ($k == $i) {
 5742:                     $selstr = ' selected="selected" ';
 5743:                 }
 5744:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5745:             }
 5746:             if ($version eq '') {
 5747:                 if ($provider eq 'proctorio') {
 5748:                     $version = '1.0';
 5749:                 } elsif ($provider eq 'examity') {
 5750:                     $version = '1.1';
 5751:                 }
 5752:             }
 5753:             if ($lifetime eq '') {
 5754:                 $lifetime = '300';
 5755:             }
 5756:             $datatable .=
 5757:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5758:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5759:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5760:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5761:                 '</td>'.
 5762:                 '<td colspan="2">'.
 5763:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5764:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5765:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5766:                 ('&nbsp;'x2).
 5767:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5768:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5769:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5770:                 ('&nbsp;'x2).
 5771:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5772:                 '<br />'.
 5773:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5774:                 '<br />'.
 5775:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5776:                 ('&nbsp;'x2).
 5777:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5778:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.proctoring_'.$provider.'_secret.type='."'text'".' } else { this.form.proctoring_'.$provider.'_secret.type='."'password'".' }" />'.$lt{'visible'}.'</label></span><br />'."\n";
 5779:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5780:             if ($imgsrc) {
 5781:                 $datatable .= $imgsrc.
 5782:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5783:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5784:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5785:             }
 5786:             $datatable .= '&nbsp;';
 5787:             if ($switchserver) {
 5788:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5789:             } else {
 5790:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5791:             }
 5792:             unless ($imgsrc) {
 5793:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5794:             }
 5795:             $datatable .= '</fieldset>'."\n";
 5796:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5797:                 if (@{$requserfields{$provider}} > 0) {
 5798:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5799:                     foreach my $field (@{$requserfields{$provider}}) {
 5800:                         $datatable .= '<span class="LC_nobreak">'.
 5801:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5802:                                       $lt{$field}.'</label>';
 5803:                         if ($field eq 'user') {
 5804:                             my $seluserdom = '';
 5805:                             my $unseluserdom = ' selected="selected"';
 5806:                             if ($userincdom) {
 5807:                                 $seluserdom = $unseluserdom;
 5808:                                 $unseluserdom = '';
 5809:                             }
 5810:                             $datatable .= ':&nbsp;'.
 5811:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5812:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5813:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5814:                                 '</select>&nbsp;';
 5815:                         } else {
 5816:                             $datatable .= '&nbsp;';
 5817:                             if ($field eq 'roles') {
 5818:                                 $showroles = 1; 
 5819:                             } 
 5820:                         }
 5821:                         $datatable .= '</span> ';
 5822:                     }
 5823:                 }
 5824:                 $datatable .= '</fieldset>'."\n";
 5825:             }
 5826:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5827:                 if (@{$optuserfields{$provider}} > 0) {
 5828:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5829:                     foreach my $field (@{$optuserfields{$provider}}) {
 5830:                         my $checked;
 5831:                         if ($checkedfields{$field}) {
 5832:                             $checked = ' checked="checked"';
 5833:                         }
 5834:                         $datatable .= '<span class="LC_nobreak">'.
 5835:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5836:                     }
 5837:                     $datatable .= '</fieldset>'."\n";
 5838:                 }
 5839:             }
 5840:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5841:                 if (@{$defaults{$provider}}) {
 5842:                     my (%options,@selectboxes);
 5843:                     if (ref($extended{$provider}) eq 'HASH') {
 5844:                         %options = %{$extended{$provider}};
 5845:                     }
 5846:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5847:                     my ($rem,$numinrow,$dropdowns);
 5848:                     if ($provider eq 'proctorio') { 
 5849:                         $datatable .= '<table>';
 5850:                         $numinrow = 4;
 5851:                     }
 5852:                     my $i = 0;
 5853:                     foreach my $field (@{$defaults{$provider}}) {
 5854:                         my $checked;
 5855:                         if ($inuse{$field}) {
 5856:                             $checked = ' checked="checked"';
 5857:                         }
 5858:                         if ($provider eq 'examity') {
 5859:                             if ($field eq 'display') {
 5860:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5861:                                 foreach my $option ('iframe','tab','window') {
 5862:                                     my $checkdisp;
 5863:                                     if ($currentdef{'target'} eq $option) {
 5864:                                         $checkdisp = ' checked="checked"';
 5865:                                     }
 5866:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5867:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5868:                                 }
 5869:                                 $datatable .= ('&nbsp;'x4);
 5870:                                 foreach my $dimen ('width','height') {
 5871:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5872:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5873:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5874:                                                   ('&nbsp;'x2);
 5875:                                 }
 5876:                                 $datatable .= '</span><br />'.
 5877:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5878:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5879:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5880:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5881:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5882:                                               $currentdef{'explanation'}.
 5883:                                               '</textarea></div><div style=""></div><br />';
 5884:                             }
 5885:                         } else {
 5886:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5887:                                 my ($output,$selnone);
 5888:                                 unless ($checked) {
 5889:                                     $selnone = ' selected="selected"';
 5890:                                 }
 5891:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5892:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5893:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5894:                                 foreach my $option (@{$options{$field}}) {
 5895:                                     my $sel; 
 5896:                                     if ($inuse{$field} eq $option) {
 5897:                                         $sel = ' selected="selected"';
 5898:                                     }
 5899:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5900:                                 }
 5901:                                 $output .= '</select></span>';
 5902:                                 push(@selectboxes,$output);
 5903:                             } else {
 5904:                                 $rem = $i%($numinrow);
 5905:                                 if ($rem == 0) {
 5906:                                     if ($i > 0) {
 5907:                                         $datatable .= '</tr>';
 5908:                                     }
 5909:                                     $datatable .= '<tr>';
 5910:                                 }
 5911:                                 $datatable .= '<td class="LC_left_item">'.
 5912:                                               '<span class="LC_nobreak">'.
 5913:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5914:                                               $fieldtitles{$field}.'</label></span></td>';
 5915:                                 $i++;
 5916:                             }
 5917:                         }
 5918:                     }
 5919:                     if ($provider eq 'proctorio') {
 5920:                         if ($numinrow) {
 5921:                             $rem = $i%$numinrow;
 5922:                         }
 5923:                         my $colsleft = $numinrow - $rem;
 5924:                         if ($colsleft > 1) {
 5925:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5926:                         } else {
 5927:                             $datatable .= '<td class="LC_left_item">';
 5928:                         }
 5929:                         $datatable .= '&nbsp;'.
 5930:                                       '</td></tr></table>';
 5931:                         if (@selectboxes) {
 5932:                             $datatable .= '<hr /><table>';
 5933:                             $numinrow = 2;
 5934:                             for (my $i=0; $i<@selectboxes; $i++) {
 5935:                                 $rem = $i%($numinrow);
 5936:                                 if ($rem == 0) {
 5937:                                     if ($i > 0) {
 5938:                                         $datatable .= '</tr>';
 5939:                                     }
 5940:                                     $datatable .= '<tr>';
 5941:                                 }
 5942:                                 $datatable .= '<td class="LC_left_item">'.
 5943:                                               $selectboxes[$i].'</td>';
 5944:                             }
 5945:                             if ($numinrow) {
 5946:                                 $rem = $i%$numinrow;
 5947:                             }
 5948:                             $colsleft = $numinrow - $rem;
 5949:                             if ($colsleft > 1) {
 5950:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5951:                             } else {
 5952:                                 $datatable .= '<td class="LC_left_item">';
 5953:                             }
 5954:                             $datatable .= '&nbsp;'.
 5955:                                           '</td></tr></table>';
 5956:                         }
 5957:                     }
 5958:                     $datatable .= '</fieldset>';
 5959:                 }
 5960:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5961:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5962:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5963:                     my ($rem,$numinrow);
 5964:                     if ($provider eq 'proctorio') {
 5965:                         $datatable .= '<table>';
 5966:                         $numinrow = 4;
 5967:                     }
 5968:                     my $i = 0;
 5969:                     foreach my $item (@{$crsconf{$provider}}) {
 5970:                         my $name;
 5971:                         if ($provider eq 'examity') {
 5972:                             $name = $lt{'crs'.$item};
 5973:                         } elsif ($provider eq 'proctorio') {
 5974:                             $name = $fieldtitles{$item};
 5975:                             $rem = $i%($numinrow);
 5976:                             if ($rem == 0) {
 5977:                                 if ($i > 0) {
 5978:                                     $datatable .= '</tr>';
 5979:                                 }
 5980:                                 $datatable .= '<tr>';
 5981:                             }
 5982:                             $datatable .= '<td class="LC_left_item>';
 5983:                         }
 5984:                         my $checked;
 5985:                         if ($crsconfig{$item}) {
 5986:                             $checked = ' checked="checked"';
 5987:                         }
 5988:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5989:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5990:                                       $name.'</label></span>';
 5991:                         if ($provider eq 'examity') {
 5992:                             $datatable .= '&nbsp; ';
 5993:                         }
 5994:                         $datatable .= "\n";
 5995:                         $i++;
 5996:                     }
 5997:                     if ($provider eq 'proctorio') {
 5998:                         if ($numinrow) {
 5999:                             $rem = $i%$numinrow;
 6000:                         }
 6001:                         my $colsleft = $numinrow - $rem;
 6002:                         if ($colsleft > 1) {
 6003:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 6004:                         } else {
 6005:                             $datatable .= '<td class="LC_left_item">';
 6006:                         }
 6007:                         $datatable .= '&nbsp;'.
 6008:                                       '</td></tr></table>';
 6009:                     }
 6010:                     $datatable .= '</fieldset>';
 6011:                 }
 6012:                 if ($showroles) {
 6013:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 6014:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 6015:                     foreach my $role (@courseroles) {
 6016:                         my ($selected,$selectnone);
 6017:                         if (!$rolemaps{$role}) {
 6018:                             $selectnone = ' selected="selected"';
 6019:                         }
 6020:                         $datatable .= '<td style="text-align: center">'.
 6021:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 6022:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 6023:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6024:                         foreach my $ltirole (@ltiroles) {
 6025:                             unless ($selectnone) {
 6026:                                 if ($rolemaps{$role} eq $ltirole) {
 6027:                                     $selected = ' selected="selected"';
 6028:                                 } else {
 6029:                                     $selected = '';
 6030:                                 }
 6031:                             }
 6032:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 6033:                         }
 6034:                         $datatable .= '</select></td>';
 6035:                     }
 6036:                     $datatable .= '</tr></table></fieldset>'.
 6037:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 6038:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 6039:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 6040:                                   '<tr><td></td><td>lms</td>'.
 6041:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 6042:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 6043:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 6044:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 6045:                         my %custom = %{$settings->{$provider}->{'custom'}};
 6046:                         if (keys(%custom) > 0) {
 6047:                             foreach my $key (sort(keys(%custom))) {
 6048:                                 next if ($key eq 'lms');
 6049:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 6050:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 6051:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 6052:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 6053:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 6054:                             }
 6055:                         }
 6056:                     }
 6057:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 6058:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 6059:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 6060:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 6061:                                   '</table></fieldset></td></tr>'."\n";
 6062:                 }
 6063:                 $datatable .= '</td></tr>';
 6064:             }
 6065:             $itemcount ++;
 6066:         }
 6067:     }
 6068:     return $datatable;
 6069: }
 6070: 
 6071: sub proctoring_data {
 6072:     my $requserfields = {
 6073:                       proctorio => ['user'],
 6074:                       examity   => ['roles','user'],
 6075:                      };
 6076:     my $optuserfields = {
 6077:                         proctorio => ['fullname'],
 6078:                         examity   => ['fullname','firstname','lastname','email'],
 6079:                      };
 6080:     my $defaults = {
 6081:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6082:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6083:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6084:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6085:                                 'reentry','calculator','whiteboard'],
 6086:                   examity   => ['display'],
 6087:                 };
 6088:     my $extended = { 
 6089:                   proctorio => {
 6090:                                  verifyid => ['verifyidauto','verifyidlive'],
 6091:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6092:                                  tabslinks => ['notabs','linksonly'],
 6093:                                  reentry => ['noreentry','agentreentry'],
 6094:                                  calculator => ['calculatorbasic','calculatorsci'],
 6095:                                },
 6096:                   examity => {
 6097:                                display => {
 6098:                                            target      => ['iframe','tab','window'],
 6099:                                            width       => '',
 6100:                                            height      => '',
 6101:                                            linktext    => '',
 6102:                                            explanation => '',
 6103:                                           },
 6104:                              },
 6105:                 };
 6106:     my $crsconf = {
 6107:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6108:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6109:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6110:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6111:                                'reentry','calculator','whiteboard'],
 6112:                  examity => ['label','title','target','linktext','explanation','append'],
 6113:                };
 6114:     my $courseroles = ['cc','in','ta','ep','st'];
 6115:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6116:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6117: }
 6118: 
 6119: sub proctoring_titles {
 6120:     my ($item) = @_;
 6121:     my (%common_lt,%custom_lt);
 6122:     %common_lt = &Apache::lonlocal::texthash (
 6123:                      'avai'      => 'Available?',
 6124:                      'base'      => 'Basic Settings',
 6125:                      'requ'      => 'User data required to be sent on launch',
 6126:                      'optu'      => 'User data optionally sent on launch',
 6127:                      'udsl'      => 'User data sent on launch',
 6128:                      'defa'      => 'Defaults for items configurable in course',
 6129:                      'sigmethod' => 'Signature Method',
 6130:                      'key'       => 'Key',
 6131:                      'lifetime'  => 'Nonce lifetime (s)',
 6132:                      'secret'    => 'Secret',
 6133:                      'icon'      => 'Icon',
 6134:                      'fullname'  => 'Full Name',
 6135:                      'visible'   => 'Visible input',
 6136:                      'username'  => 'username',
 6137:                      'user'      => 'User',
 6138:                  );
 6139:     if ($item eq 'proctorio') {
 6140:         %custom_lt = &Apache::lonlocal::texthash (
 6141:                          'version'        => 'OAuth version',
 6142:                          'url'            => 'API URL',
 6143:                          'uname:dom'      => 'username-domain',
 6144:         );
 6145:     } elsif ($item eq 'examity') {
 6146:         %custom_lt = &Apache::lonlocal::texthash (
 6147:                          'version'        => 'LTI Version',
 6148:                          'url'            => 'URL',
 6149:                          'uname:dom'      => 'username:domain',
 6150:                          'msgtype'        => 'Message Type',
 6151:                          'firstname'      => 'First Name',
 6152:                          'lastname'       => 'Last Name',
 6153:                          'email'          => 'E-mail',
 6154:                          'roles'          => 'Role',
 6155:                          'crstarget'      => 'Display target',
 6156:                          'crslabel'       => 'Course label',
 6157:                          'crstitle'       => 'Course title', 
 6158:                          'crslinktext'    => 'Link Text',
 6159:                          'crsexplanation' => 'Explanation',
 6160:                          'crsappend'      => 'Provider URL',
 6161:         );
 6162:     }
 6163:     my %lt = (%common_lt,%custom_lt);
 6164:     return %lt;
 6165: }
 6166: 
 6167: sub proctoring_fieldtitles {
 6168:     my ($item) = @_;
 6169:     if ($item eq 'proctorio') {
 6170:         return &Apache::lonlocal::texthash (
 6171:                   'recordvideo' => 'Record video',
 6172:                   'recordaudio' => 'Record audio',
 6173:                   'recordscreen' => 'Record screen',
 6174:                   'recordwebtraffic' => 'Record web traffic',
 6175:                   'recordroomstart' => 'Record room scan',
 6176:                   'verifyvideo' => 'Verify webcam',
 6177:                   'verifyaudio' => 'Verify microphone',
 6178:                   'verifydesktop' => 'Verify desktop recording',
 6179:                   'verifyid' => 'Photo ID verification',
 6180:                   'verifysignature' => 'Require signature',
 6181:                   'fullscreen' => 'Fullscreen',
 6182:                   'clipboard' => 'Disable copy/paste',
 6183:                   'tabslinks' => 'New tabs/windows',
 6184:                   'closetabs' => 'Close other tabs',
 6185:                   'onescreen' => 'Limit to single screen',
 6186:                   'print' => 'Disable Printing',
 6187:                   'downloads' => 'Disable Downloads',
 6188:                   'cache' => 'Empty cache after exam',
 6189:                   'rightclick' => 'Disable right click',
 6190:                   'reentry' => 'Re-entry to exam',
 6191:                   'calculator' => 'Onscreen calculator',
 6192:                   'whiteboard' => 'Onscreen whiteboard',
 6193:                   'verifyidauto' => 'Automated verification',
 6194:                   'verifyidlive' => 'Live agent verification',
 6195:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6196:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6197:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6198:                   'notabs' => 'Disaallowed',
 6199:                   'linksonly' => 'Allowed from links in exam',
 6200:                   'noreentry' => 'Disallowed',
 6201:                   'agentreentry' => 'Agent required for re-entry',
 6202:                   'calculatorbasic' => 'Basic',
 6203:                   'calculatorsci' => 'Scientific',
 6204:         );
 6205:     } elsif ($item eq 'examity') {
 6206:         return &Apache::lonlocal::texthash (
 6207:                 'target'         => 'Display target',   
 6208:                 'window'         => 'Window',
 6209:                 'tab'            => 'Tab',
 6210:                 'iframe'         => 'iFrame',
 6211:                 'height'         => 'Height (pixels)',
 6212:                 'width'          => 'Width (pixels)',
 6213:                 'linktext'       => 'Default Link Text',
 6214:                 'explanation'    => 'Default Explanation',
 6215:                 'append'         => 'Provider URL',
 6216:         );
 6217:     }
 6218: }
 6219: 
 6220: sub proctoring_providernames {
 6221:     return (
 6222:              proctorio => 'Proctorio',
 6223:              examity   => 'Examity',
 6224:            );
 6225: }
 6226: 
 6227: sub print_lti {
 6228:     my ($dom,$settings,$rowtotal) = @_;
 6229:     my $itemcount = 1;
 6230:     my $maxnum = 0;
 6231:     my $css_class;
 6232:     my %ordered;
 6233:     if (ref($settings) eq 'HASH') {
 6234:         foreach my $item (keys(%{$settings})) {
 6235:             if (ref($settings->{$item}) eq 'HASH') {
 6236:                 my $num = $settings->{$item}{'order'};
 6237:                 if ($num eq '') {
 6238:                     $num = scalar(keys(%{$settings}));
 6239:                 }
 6240:                 $ordered{$num} = $item;
 6241:             }
 6242:         }
 6243:     }
 6244:     my $maxnum = scalar(keys(%ordered));
 6245:     my $datatable;
 6246:     my %lt = &lti_names();
 6247:     if (keys(%ordered)) {
 6248:         my @items = sort { $a <=> $b } keys(%ordered);
 6249:         for (my $i=0; $i<@items; $i++) {
 6250:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6251:             my $item = $ordered{$items[$i]};
 6252:             my ($key,$secret,$lifetime,$consumer,$requser,$crsinc,$current);
 6253:             if (ref($settings->{$item}) eq 'HASH') {
 6254:                 $key = $settings->{$item}->{'key'};
 6255:                 $secret = $settings->{$item}->{'secret'};
 6256:                 $lifetime = $settings->{$item}->{'lifetime'};
 6257:                 $consumer = $settings->{$item}->{'consumer'};
 6258:                 $requser = $settings->{$item}->{'requser'};
 6259:                 $crsinc = $settings->{$item}->{'crsinc'};
 6260:                 $current = $settings->{$item};
 6261:             }
 6262:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6263:             my %checkedrequser = (
 6264:                                    yes => ' checked="checked"',
 6265:                                    no  => '',
 6266:                                  );
 6267:             if (!$requser) {
 6268:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 6269:                 $checkedrequser{'yes'} = '';
 6270:             }
 6271:             my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6272:             my %checkedcrsinc = (
 6273:                                       yes => ' checked="checked"',
 6274:                                       no  => '',
 6275:                                    );
 6276:             if (!$crsinc) {
 6277:                 $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6278:                 $checkedcrsinc{'yes'} = '';
 6279:             }
 6280:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6281:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6282:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6283:             for (my $k=0; $k<=$maxnum; $k++) {
 6284:                 my $vpos = $k+1;
 6285:                 my $selstr;
 6286:                 if ($k == $i) {
 6287:                     $selstr = ' selected="selected" ';
 6288:                 }
 6289:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6290:             }
 6291:             $datatable .= '</select>'.('&nbsp;'x2).
 6292:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6293:                 &mt('Delete?').'</label></span></td>'.
 6294:                 '<td colspan="2">'.
 6295:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6296:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 6297:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6298:                 ('&nbsp;'x2).
 6299:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6300:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6301:                 ('&nbsp;'x2).
 6302:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6303:                 'value="'.$lifetime.'" size="3" /></span>'.
 6304:                 ('&nbsp;'x2).
 6305:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6306:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6307:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6308:                 '<br /><br />'.
 6309:                 '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6310:                 '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6311:                 '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6312:                 ('&nbsp;'x4).
 6313:                 '<span class="LC_nobreak">'.$lt{'key'}.
 6314:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 6315:                 ('&nbsp;'x2).
 6316:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6317:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 6318:                 '<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>'.
 6319:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6320:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6321:             $itemcount ++;
 6322:         }
 6323:     }
 6324:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6325:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6326:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6327:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6328:                   '<select name="lti_pos_add"'.$chgstr.'>';
 6329:     for (my $k=0; $k<$maxnum+1; $k++) {
 6330:         my $vpos = $k+1;
 6331:         my $selstr;
 6332:         if ($k == $maxnum) {
 6333:             $selstr = ' selected="selected" ';
 6334:         }
 6335:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6336:     }
 6337:     $datatable .= '</select>&nbsp;'."\n".
 6338:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6339:                   '<td colspan="2">'.
 6340:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6341:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 6342:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6343:                   ('&nbsp;'x2).
 6344:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6345:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6346:                   ('&nbsp;'x2).
 6347:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 6348:                   ('&nbsp;'x2).
 6349:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6350:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6351:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6352:                   '<br /><br />'.
 6353:                   '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6354:                   '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6355:                   '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6356:                   ('&nbsp;'x4).
 6357:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 6358:                   ('&nbsp;'x2).
 6359:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 6360:                   '<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".
 6361:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6362:                   '</td>'."\n".
 6363:                   '</tr>'."\n";
 6364:     $$rowtotal ++;
 6365:     return $datatable;;
 6366: }
 6367: 
 6368: sub lti_names {
 6369:     my %lt = &Apache::lonlocal::texthash(
 6370:                                           'version'   => 'LTI Version',
 6371:                                           'url'       => 'URL',
 6372:                                           'key'       => 'Key',
 6373:                                           'lifetime'  => 'Nonce lifetime (s)',
 6374:                                           'consumer'  => 'Consumer',
 6375:                                           'secret'    => 'Secret',
 6376:                                           'requser'   => "User's identity sent",
 6377:                                           'crsinc'    => "Course's identity sent",
 6378:                                           'email'     => 'Email address',
 6379:                                           'sourcedid' => 'User ID',
 6380:                                           'other'     => 'Other',
 6381:                                           'passback'  => 'Can return grades to Consumer:',
 6382:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6383:                                           'topmenu'   => 'Display LON-CAPA page header',
 6384:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6385:                                         );
 6386:     return %lt;
 6387: }
 6388: 
 6389: sub lti_options {
 6390:     my ($num,$current,$itemcount,%lt) = @_;
 6391:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6392:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6393:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6394:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6395:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6396:     $checked{'mapcrstype'} = {};
 6397:     $checked{'makeuser'} = {};
 6398:     $checked{'selfenroll'} = {};
 6399:     $checked{'crssec'} = {};
 6400:     $checked{'crssecsrc'} = {};
 6401:     $checked{'lcauth'} = {};
 6402:     $checked{'menuitem'} = {};
 6403:     if ($num eq 'add') {
 6404:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6405:     }
 6406:     my $userfieldsty = 'none';
 6407:     my $crsfieldsty = 'none';
 6408:     my $crssecfieldsty = 'none';
 6409:     my $secsrcfieldsty = 'none';
 6410:     my $callbacksty = 'none';
 6411:     my $passbacksty = 'none';
 6412:     my $optionsty = 'block';
 6413:     my $crssty = 'block';
 6414:     my $lcauthparm;
 6415:     my $lcauthparmstyle = 'display:none';
 6416:     my $lcauthparmtext;
 6417:     my $menusty;
 6418:     my $numinrow = 4;
 6419:     my %menutitles = &ltimenu_titles();
 6420: 
 6421:     if (ref($current) eq 'HASH') {
 6422:         if (!$current->{'requser'}) {
 6423:             $optionsty = 'none';
 6424:             $crssty = 'none';
 6425:         } elsif (!$current->{'crsinc'}) {
 6426:             $crssty = 'none';
 6427:         }
 6428:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6429:             $checked{'mapuser'}{'sourcedid'} = '';
 6430:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6431:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 6432:             } else {
 6433:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6434:                 $userfield = $current->{'mapuser'};
 6435:                 $userfieldsty = 'inline-block';
 6436:             }
 6437:         }
 6438:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6439:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6440:             if ($current->{'mapcrs'} eq 'context_id') {
 6441:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 6442:             } else {
 6443:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6444:                 $cidfield = $current->{'mapcrs'};
 6445:                 $crsfieldsty = 'inline-block';
 6446:             }
 6447:         }
 6448:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6449:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6450:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6451:             }
 6452:         }
 6453:         if (!$current->{'storecrs'}) {
 6454:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6455:             $checked{'storecrs'}{'Y'} = '';
 6456:         }
 6457:         if ($current->{'makecrs'}) {
 6458:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6459:         }
 6460:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6461:             foreach my $role (@{$current->{'makeuser'}}) {
 6462:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6463:             }
 6464:         }
 6465:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6466:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6467:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6468:                 $lcauthparm = $current->{'lcauthparm'};
 6469:                 $lcauthparmstyle = 'display:table-row'; 
 6470:                 if ($current->{'lcauth'} eq 'localauth') {
 6471:                     $lcauthparmtext = &mt('Local auth argument');
 6472:                 } else {
 6473:                     $lcauthparmtext = &mt('Kerberos domain');
 6474:                 }
 6475:             }
 6476:         }
 6477:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6478:             foreach my $role (@{$current->{'selfenroll'}}) {
 6479:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6480:             }
 6481:         }
 6482:         if (ref($current->{'maproles'}) eq 'HASH') {
 6483:             %rolemaps = %{$current->{'maproles'}};
 6484:         }
 6485:         if ($current->{'section'} ne '') {
 6486:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 6487:             $crssecfieldsty = 'inline-block';
 6488:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6489:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6490:             } else {
 6491:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6492:                 $crssecsrc = $current->{'section'};
 6493:                 $secsrcfieldsty = 'inline-block';
 6494:             }
 6495:         } else {
 6496:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6497:         }
 6498:         if ($current->{'callback'} ne '') {
 6499:             $callback = $current->{'callback'};
 6500:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6501:             $callbacksty = 'inline-block';
 6502:         } else {
 6503:             $checked{'callback'}{'N'} = ' checked="checked"';
 6504:         }
 6505:         if ($current->{'topmenu'}) {
 6506:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6507:         } else {
 6508:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6509:         }
 6510:         if ($current->{'inlinemenu'}) {
 6511:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6512:         } else {
 6513:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6514:         }
 6515:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6516:             $menusty = 'inline-block';
 6517:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6518:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6519:                     if (exists($menutitles{$item})) {
 6520:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6521:                     }
 6522:                 }
 6523:             }
 6524:         } else {
 6525:             $menusty = 'none';
 6526:         }
 6527:     } else {
 6528:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6529:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6530:         $checked{'callback'}{'N'} = ' checked="checked"';
 6531:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6532:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 6533:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6534:         $menusty = 'inline-block'; 
 6535:     }
 6536:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6537:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6538:                                official   => 'Official',
 6539:                                unofficial => 'Unofficial',
 6540:                                community  => 'Community',
 6541:                                textbook   => 'Textbook',
 6542:                                placement  => 'Placement Test',
 6543:                                lti        => 'LTI Provider',
 6544:     );
 6545:     my @authtypes = ('internal','krb4','krb5','localauth');
 6546:     my %shortauth = (
 6547:                      internal => 'int',
 6548:                      krb4 => 'krb4',
 6549:                      krb5 => 'krb5',
 6550:                      localauth  => 'loc'
 6551:                     );
 6552:     my %authnames = &authtype_names();
 6553:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6554:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6555:     my @courseroles = ('cc','in','ta','ep','st');
 6556:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6557:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6558:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6559:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6560:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6561:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6562:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6563:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6564:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6565:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6566:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6567:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6568:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6569:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6570:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6571:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6572:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6573:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6574:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6575:     foreach my $option ('sourcedid','email','other') {
 6576:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6577:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6578:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6579:     }
 6580:     $output .= '</span></div>'.
 6581:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6582:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6583:                'value="'.$userfield.'" /></div></fieldset>'.
 6584:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6585:     foreach my $ltirole (@ltiroles) {
 6586:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6587:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 6588:     }
 6589:     $output .= '</fieldset>'.
 6590:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6591:                '<table>'.
 6592:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6593:                '</table>'.
 6594:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6595:                '<td class="LC_left_item">';
 6596:     foreach my $auth ('lti',@authtypes) {
 6597:         my $authtext;
 6598:         if ($auth eq 'lti') {
 6599:             $authtext = &mt('None');
 6600:         } else {
 6601:             $authtext = $authnames{$shortauth{$auth}};
 6602:         }
 6603:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6604:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6605:                    $authtext.'</label></span> &nbsp;';
 6606:     }
 6607:     $output .= '</td></tr>'.
 6608:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6609:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6610:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6611:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6612:                '</table></fieldset>'.
 6613:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6614:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6615:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6616:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6617:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6618:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6619:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6620:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6621:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6622:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6623:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6624:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6625:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6626:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6627:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6628:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6629:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6630:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6631:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6632:                    ('&nbsp;'x2);
 6633:     }
 6634:     $output .= '</span></div></fieldset>'.
 6635:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6636:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6637:                &mt('Unique course identifier').':&nbsp;';
 6638:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6639:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6640:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6641:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6642:     }
 6643:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6644:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6645:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6646:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6647:     foreach my $type (@coursetypes) {
 6648:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6649:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6650:                    ('&nbsp;'x2);
 6651:     }
 6652:     $output .= '</span><br /><br />'.
 6653:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6654:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6655:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6656:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6657:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6658:                '</fieldset>'.
 6659:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6660:     foreach my $ltirole (@lticourseroles) {
 6661:         my ($selected,$selectnone);
 6662:         if ($rolemaps{$ltirole} eq '') {
 6663:             $selectnone = ' selected="selected"';
 6664:         }
 6665:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6666:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6667:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6668:         foreach my $role (@courseroles) {
 6669:             unless ($selectnone) {
 6670:                 if ($rolemaps{$ltirole} eq $role) {
 6671:                     $selected = ' selected="selected"';
 6672:                 } else {
 6673:                     $selected = '';
 6674:                 }
 6675:             }
 6676:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6677:                        &Apache::lonnet::plaintext($role,'Course').
 6678:                        '</option>';
 6679:         }
 6680:         $output .= '</select></td>';
 6681:     }
 6682:     $output .= '</tr></table></fieldset>'.
 6683:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6684:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6685:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6686:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6687:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6688:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6689:                '</fieldset>'.
 6690:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6691:     foreach my $lticrsrole (@lticourseroles) {
 6692:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6693:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6694:     }
 6695:     $output .= '</fieldset>'.
 6696:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6697:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6698:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6699:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6700:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6701:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6702:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6703:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6704:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6705:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6706:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6707:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6708:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6709:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6710:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6711:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6712:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6713:     foreach my $extra ('roster','passback') {
 6714:         my $checkedon = '';
 6715:         my $checkedoff = ' checked="checked"';
 6716:         if ($extra eq 'passback') {
 6717:             $pb1p1chk = ' checked="checked"';
 6718:             $pb1p0chk = '';
 6719:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 6720:         } else {
 6721:             $onclickpb = ''; 
 6722:         }
 6723:         if (ref($current) eq 'HASH') {
 6724:             if (($current->{$extra})) {
 6725:                 $checkedon = $checkedoff;
 6726:                 $checkedoff = '';
 6727:                 if ($extra eq 'passback') {
 6728:                     $passbacksty = 'inline-block';
 6729:                 }
 6730:                 if ($current->{'passbackformat'} eq '1.0') {
 6731:                     $pb1p0chk =  ' checked="checked"';
 6732:                     $pb1p1chk = '';
 6733:                 }
 6734:             }
 6735:         }
 6736:         $output .= $lt{$extra}.'&nbsp;'.
 6737:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6738:                    &mt('No').'</label>'.('&nbsp;'x2).
 6739:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6740:                    &mt('Yes').'</label><br />';
 6741:     }
 6742:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6743:                '<span class="LC_nobreak">'.&mt('Grade format').
 6744:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6745:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6746:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6747:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6748:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6749:     $output .= '</span></div></fieldset>';
 6750: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6751: #
 6752: #    $output .= '</fieldset>'.
 6753: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6754:     return $output;
 6755: }
 6756: 
 6757: sub ltimenu_titles {
 6758:     return &Apache::lonlocal::texthash(
 6759:                                         fullname    => 'Full name',
 6760:                                         coursetitle => 'Course title',
 6761:                                         role        => 'Role',
 6762:                                         logout      => 'Logout',
 6763:                                         grades      => 'Grades',
 6764:     );
 6765: }
 6766: 
 6767: sub print_coursedefaults {
 6768:     my ($position,$dom,$settings,$rowtotal) = @_;
 6769:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6770:     my $itemcount = 1;
 6771:     my %choices =  &Apache::lonlocal::texthash (
 6772:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6773:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6774:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6775:         coursecredits        => 'Credits can be specified for courses',
 6776:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6777:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6778:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6779:         texengine            => 'Default method to display mathematics',
 6780:         postsubmit           => 'Disable submit button/keypress following student submission',
 6781:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6782:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6783:     );
 6784:     my %staticdefaults = (
 6785:                            anonsurvey_threshold => 10,
 6786:                            uploadquota          => 500,
 6787:                            postsubmit           => 60,
 6788:                            mysqltables          => 172800,
 6789:                          );
 6790:     if ($position eq 'top') {
 6791:         %defaultchecked = (
 6792:                             'canuse_pdfforms' => 'off',
 6793:                             'uselcmath'       => 'on',
 6794:                             'usejsme'         => 'on',
 6795:                             'inline_chem'     => 'on',
 6796:                             'canclone'        => 'none',
 6797:                           );
 6798:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6799:         my $deftex = $Apache::lonnet::deftex;
 6800:         if (ref($settings) eq 'HASH') {
 6801:             if ($settings->{'texengine'}) {
 6802:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6803:                     $deftex = $settings->{'texengine'};
 6804:                 }
 6805:             }
 6806:         }
 6807:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6808:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6809:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6810:                        '</span></td><td class="LC_right_item">'.
 6811:                        '<select name="texengine">'."\n";
 6812:         my %texoptions = (
 6813:                             MathJax  => 'MathJax',
 6814:                             mimetex  => &mt('Convert to Images'),
 6815:                             tth      => &mt('TeX to HTML'),
 6816:                          );
 6817:         foreach my $renderer ('MathJax','mimetex','tth') {
 6818:             my $selected = '';
 6819:             if ($renderer eq $deftex) {
 6820:                 $selected = ' selected="selected"';
 6821:             }
 6822:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6823:         }
 6824:         $mathdisp .= '</select></td></tr>'."\n";
 6825:         $itemcount ++;
 6826:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6827:                                                      \%choices,$itemcount);
 6828:         $datatable = $mathdisp.$datatable;
 6829:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6830:         $datatable .=
 6831:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6832:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6833:             '</span></td><td class="LC_left_item">';
 6834:         my $currcanclone = 'none';
 6835:         my $onclick;
 6836:         my @cloneoptions = ('none','domain');
 6837:         my %clonetitles = &Apache::lonlocal::texthash (
 6838:                              none     => 'No additional course requesters',
 6839:                              domain   => "Any course requester in course's domain",
 6840:                              instcode => 'Course requests for official courses ...',
 6841:                           );
 6842:         my (%codedefaults,@code_order,@posscodes);
 6843:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6844:                                                     \@code_order) eq 'ok') {
 6845:             if (@code_order > 0) {
 6846:                 push(@cloneoptions,'instcode');
 6847:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6848:             }
 6849:         }
 6850:         if (ref($settings) eq 'HASH') {
 6851:             if ($settings->{'canclone'}) {
 6852:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6853:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6854:                         if (@code_order > 0) {
 6855:                             $currcanclone = 'instcode';
 6856:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6857:                         }
 6858:                     }
 6859:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6860:                     $currcanclone = $settings->{'canclone'};
 6861:                 }
 6862:             }
 6863:         }
 6864:         foreach my $option (@cloneoptions) {
 6865:             my ($checked,$additional);
 6866:             if ($currcanclone eq $option) {
 6867:                 $checked = ' checked="checked"';
 6868:             }
 6869:             if ($option eq 'instcode') {
 6870:                 if (@code_order) {
 6871:                     my $show = 'none';
 6872:                     if ($checked) {
 6873:                         $show = 'block';
 6874:                     }
 6875:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6876:                                   &mt('Institutional codes for new and cloned course have identical:').
 6877:                                   '<br />';
 6878:                     foreach my $item (@code_order) {
 6879:                         my $codechk;
 6880:                         if ($checked) {
 6881:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6882:                                 $codechk = ' checked="checked"';
 6883:                             }
 6884:                         }
 6885:                         $additional .= '<label>'.
 6886:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6887:                                        $item.'</label>';
 6888:                     }
 6889:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6890:                 }
 6891:             }
 6892:             $datatable .=
 6893:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6894:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6895:                 '</label>&nbsp;'.$additional.'</span><br />';
 6896:         }
 6897:         $datatable .= '</td>'.
 6898:                       '</tr>';
 6899:         $itemcount ++;
 6900:     } else {
 6901:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6902:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6903:         my $currusecredits = 0;
 6904:         my $postsubmitclient = 1;
 6905:         my @types = ('official','unofficial','community','textbook','placement');
 6906:         if (ref($settings) eq 'HASH') {
 6907:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6908:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6909:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6910:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6911:                 }
 6912:             }
 6913:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6914:                 foreach my $type (@types) {
 6915:                     next if ($type eq 'community');
 6916:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6917:                     if ($defcredits{$type} ne '') {
 6918:                         $currusecredits = 1;
 6919:                     }
 6920:                 }
 6921:             }
 6922:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6923:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6924:                     $postsubmitclient = 0;
 6925:                     foreach my $type (@types) {
 6926:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6927:                     }
 6928:                 } else {
 6929:                     foreach my $type (@types) {
 6930:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6931:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6932:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6933:                             } else {
 6934:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6935:                             }
 6936:                         } else {
 6937:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6938:                         }
 6939:                     }
 6940:                 }
 6941:             } else {
 6942:                 foreach my $type (@types) {
 6943:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6944:                 }
 6945:             }
 6946:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6947:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6948:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6949:                 }
 6950:             } else {
 6951:                 foreach my $type (@types) {
 6952:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6953:                 }
 6954:             }
 6955:         } else {
 6956:             foreach my $type (@types) {
 6957:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6958:             }
 6959:         }
 6960:         if (!$currdefresponder) {
 6961:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6962:         } elsif ($currdefresponder < 1) {
 6963:             $currdefresponder = 1;
 6964:         }
 6965:         foreach my $type (@types) {
 6966:             if ($curruploadquota{$type} eq '') {
 6967:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6968:             }
 6969:         }
 6970:         $datatable .=
 6971:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6972:                 $choices{'anonsurvey_threshold'}.
 6973:                 '</span></td>'.
 6974:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6975:                 '<input type="text" name="anonsurvey_threshold"'.
 6976:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6977:                 '</td></tr>'."\n";
 6978:         $itemcount ++;
 6979:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6980:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6981:                       $choices{'uploadquota'}.
 6982:                       '</span></td>'.
 6983:                       '<td style="text-align: right" class="LC_right_item">'.
 6984:                       '<table><tr>';
 6985:         foreach my $type (@types) {
 6986:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6987:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6988:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6989:         }
 6990:         $datatable .= '</tr></table></td></tr>'."\n";
 6991:         $itemcount ++;
 6992:         my $onclick = "toggleDisplay(this.form,'credits');";
 6993:         my $display = 'none';
 6994:         if ($currusecredits) {
 6995:             $display = 'block';
 6996:         }
 6997:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6998:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6999:         foreach my $type (@types) {
 7000:             next if ($type eq 'community');
 7001:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7002:                            '<input type="text" name="'.$type.'_credits"'.
 7003:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7004:         }
 7005:         $additional .= '</tr></table></div>'."\n";
 7006:         %defaultchecked = ('coursecredits' => 'off');
 7007:         @toggles = ('coursecredits');
 7008:         my $current = {
 7009:                         'coursecredits' => $currusecredits,
 7010:                       };
 7011:         (my $table,$itemcount) =
 7012:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7013:                                \%choices,$itemcount,$onclick,$additional,'left');
 7014:         $datatable .= $table;
 7015:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7016:         my $display = 'none';
 7017:         if ($postsubmitclient) {
 7018:             $display = 'block';
 7019:         }
 7020:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7021:                       &mt('Number of seconds submit is disabled').'<br />'.
 7022:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7023:                       '<table><tr>';
 7024:         foreach my $type (@types) {
 7025:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7026:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7027:                            $deftimeout{$type}.'" size="5" /></td>';
 7028:         }
 7029:         $additional .= '</tr></table></div>'."\n";
 7030:         %defaultchecked = ('postsubmit' => 'on');
 7031:         @toggles = ('postsubmit');
 7032:         $current = {
 7033:                        'postsubmit' => $postsubmitclient,
 7034:                    };
 7035:         ($table,$itemcount) =
 7036:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7037:                                \%choices,$itemcount,$onclick,$additional,'left');
 7038:         $datatable .= $table;
 7039:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7040:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7041:                       $choices{'mysqltables'}.
 7042:                       '</span></td>'.
 7043:                       '<td style="text-align: right" class="LC_right_item">'.
 7044:                       '<table><tr>';
 7045:         foreach my $type (@types) {
 7046:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7047:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7048:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7049:         }
 7050:         $datatable .= '</tr></table></td></tr>'."\n";
 7051:         $itemcount ++;
 7052: 
 7053:     }
 7054:     $$rowtotal += $itemcount;
 7055:     return $datatable;
 7056: }
 7057: 
 7058: sub print_selfenrollment {
 7059:     my ($position,$dom,$settings,$rowtotal) = @_;
 7060:     my ($css_class,$datatable);
 7061:     my $itemcount = 1;
 7062:     my @types = ('official','unofficial','community','textbook','placement');
 7063:     if (($position eq 'top') || ($position eq 'middle')) {
 7064:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7065:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7066:         my @rows;
 7067:         my $key;
 7068:         if ($position eq 'top') {
 7069:             $key = 'admin'; 
 7070:             if (ref($rowsref) eq 'ARRAY') {
 7071:                 @rows = @{$rowsref};
 7072:             }
 7073:         } elsif ($position eq 'middle') {
 7074:             $key = 'default';
 7075:             @rows = ('types','registered','approval','limit');
 7076:         }
 7077:         foreach my $row (@rows) {
 7078:             if (defined($titlesref->{$row})) {
 7079:                 $itemcount ++;
 7080:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7081:                 $datatable .= '<tr'.$css_class.'>'.
 7082:                               '<td>'.$titlesref->{$row}.'</td>'.
 7083:                               '<td class="LC_left_item">'.
 7084:                               '<table><tr>';
 7085:                 my (%current,%currentcap);
 7086:                 if (ref($settings) eq 'HASH') {
 7087:                     if (ref($settings->{$key}) eq 'HASH') {
 7088:                         foreach my $type (@types) {
 7089:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7090:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7091:                             }
 7092:                             if (($row eq 'limit') && ($key eq 'default')) {
 7093:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7094:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7095:                                 }
 7096:                             }
 7097:                         }
 7098:                     }
 7099:                 }
 7100:                 my %roles = (
 7101:                              '0' => &Apache::lonnet::plaintext('dc'),
 7102:                             ); 
 7103:             
 7104:                 foreach my $type (@types) {
 7105:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7106:                         $datatable .= '<th>'.&mt($type).'</th>';
 7107:                     }
 7108:                 }
 7109:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7110:                     $datatable .= '</tr><tr>';
 7111:                 }
 7112:                 foreach my $type (@types) {
 7113:                     if ($type eq 'community') {
 7114:                         $roles{'1'} = &mt('Community personnel');
 7115:                     } else {
 7116:                         $roles{'1'} = &mt('Course personnel');
 7117:                     }
 7118:                     $datatable .= '<td style="vertical-align: top">';
 7119:                     if ($position eq 'top') {
 7120:                         my %checked;
 7121:                         if ($current{$type} eq '0') {
 7122:                             $checked{'0'} = ' checked="checked"';
 7123:                         } else {
 7124:                             $checked{'1'} = ' checked="checked"';
 7125:                         }
 7126:                         foreach my $role ('1','0') {
 7127:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7128:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7129:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7130:                                           $roles{$role}.'</label></span> ';
 7131:                         }
 7132:                     } else {
 7133:                         if ($row eq 'types') {
 7134:                             my %checked;
 7135:                             if ($current{$type} =~ /^(all|dom)$/) {
 7136:                                 $checked{$1} = ' checked="checked"';
 7137:                             } else {
 7138:                                 $checked{''} = ' checked="checked"';
 7139:                             }
 7140:                             foreach my $val ('','dom','all') {
 7141:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7142:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7143:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7144:                             }
 7145:                         } elsif ($row eq 'registered') {
 7146:                             my %checked;
 7147:                             if ($current{$type} eq '1') {
 7148:                                 $checked{'1'} = ' checked="checked"';
 7149:                             } else {
 7150:                                 $checked{'0'} = ' checked="checked"';
 7151:                             }
 7152:                             foreach my $val ('0','1') {
 7153:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7154:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7155:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7156:                             }
 7157:                         } elsif ($row eq 'approval') {
 7158:                             my %checked;
 7159:                             if ($current{$type} =~ /^([12])$/) {
 7160:                                 $checked{$1} = ' checked="checked"';
 7161:                             } else {
 7162:                                 $checked{'0'} = ' checked="checked"';
 7163:                             }
 7164:                             for my $val (0..2) {
 7165:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7166:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7167:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7168:                             }
 7169:                         } elsif ($row eq 'limit') {
 7170:                             my %checked;
 7171:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7172:                                 $checked{$1} = ' checked="checked"';
 7173:                             } else {
 7174:                                 $checked{'none'} = ' checked="checked"';
 7175:                             }
 7176:                             my $cap;
 7177:                             if ($currentcap{$type} =~ /^\d+$/) {
 7178:                                 $cap = $currentcap{$type};
 7179:                             }
 7180:                             foreach my $val ('none','allstudents','selfenrolled') {
 7181:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7182:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7183:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7184:                             }
 7185:                             $datatable .= '<br />'.
 7186:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7187:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7188:                                           '</span>'; 
 7189:                         }
 7190:                     }
 7191:                     $datatable .= '</td>';
 7192:                 }
 7193:                 $datatable .= '</tr>';
 7194:             }
 7195:             $datatable .= '</table></td></tr>';
 7196:         }
 7197:     } elsif ($position eq 'bottom') {
 7198:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7199:     }
 7200:     $$rowtotal += $itemcount;
 7201:     return $datatable;
 7202: }
 7203: 
 7204: sub print_validation_rows {
 7205:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7206:     my ($itemsref,$namesref,$fieldsref);
 7207:     if ($caller eq 'selfenroll') { 
 7208:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7209:     } elsif ($caller eq 'requestcourses') {
 7210:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7211:     }
 7212:     my %currvalidation;
 7213:     if (ref($settings) eq 'HASH') {
 7214:         if (ref($settings->{'validation'}) eq 'HASH') {
 7215:             %currvalidation = %{$settings->{'validation'}};
 7216:         }
 7217:     }
 7218:     my $datatable;
 7219:     my $itemcount = 0;
 7220:     foreach my $item (@{$itemsref}) {
 7221:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7222:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7223:                       $namesref->{$item}.
 7224:                       '</span></td>'.
 7225:                       '<td class="LC_left_item">';
 7226:         if (($item eq 'url') || ($item eq 'button')) {
 7227:             $datatable .= '<span class="LC_nobreak">'.
 7228:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7229:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7230:         } elsif ($item eq 'fields') {
 7231:             my @currfields;
 7232:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7233:                 @currfields = @{$currvalidation{$item}};
 7234:             }
 7235:             foreach my $field (@{$fieldsref}) {
 7236:                 my $check = '';
 7237:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7238:                     $check = ' checked="checked"';
 7239:                 }
 7240:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7241:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7242:                               ' value="'.$field.'"'.$check.' />'.$field.
 7243:                               '</label></span> ';
 7244:             }
 7245:         } elsif ($item eq 'markup') {
 7246:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7247:                            $currvalidation{$item}.
 7248:                               '</textarea>';
 7249:         }
 7250:         $datatable .= '</td></tr>'."\n";
 7251:         if (ref($rowtotal)) {
 7252:             $itemcount ++;
 7253:         }
 7254:     }
 7255:     if ($caller eq 'requestcourses') {
 7256:         my %currhash;
 7257:         if (ref($settings) eq 'HASH') {
 7258:             if (ref($settings->{'validation'}) eq 'HASH') {
 7259:                 if ($settings->{'validation'}{'dc'} ne '') {
 7260:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7261:                 }
 7262:             }
 7263:         }
 7264:         my $numinrow = 2;
 7265:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7266:                                                        'validationdc',%currhash);
 7267:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7268:         $datatable .= '<tr'.$css_class.'><td>';
 7269:         if ($numdc > 1) {
 7270:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7271:         } else {
 7272:             $datatable .=  &mt('Course creation processed as: ');
 7273:         }
 7274:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7275:         $itemcount ++;
 7276:     }
 7277:     if (ref($rowtotal)) {
 7278:         $$rowtotal += $itemcount;
 7279:     }
 7280:     return $datatable;
 7281: }
 7282: 
 7283: sub print_privacy {
 7284:     my ($position,$dom,$settings,$rowtotal) = @_;
 7285:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7286:     my $itemcount = 0;
 7287:     unless ($position eq 'top') {
 7288:         @items = ('domain','author','course','community');
 7289:         %names = &Apache::lonlocal::texthash (
 7290:                      domain => 'Assigned domain role(s)',
 7291:                      author => 'Assigned co-author role(s)',
 7292:                      course => 'Assigned course role(s)',
 7293:                      community => 'Assigned community role',
 7294:                  );
 7295:         $numinrow = 4;
 7296:         ($othertitle,$usertypes,$types) =
 7297:             &Apache::loncommon::sorted_inst_types($dom);
 7298:     }
 7299:     if (($position eq 'top') || ($position eq 'middle')) {
 7300:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7301:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7302:         if ($position eq 'top') {
 7303:             my %curr;
 7304:             my @options = ('none','user','domain','auto');
 7305:             my %titles = &Apache::lonlocal::texthash (
 7306:                 none   => 'Not allowed',
 7307:                 user   => 'User authorizes',
 7308:                 domain => 'DC authorizes',
 7309:                 auto   => 'Unrestricted',
 7310:                 instdom => 'Other domain shares institution/provider',
 7311:                 extdom => 'Other domain has different institution/provider',
 7312:             );
 7313:             my %names = &Apache::lonlocal::texthash (
 7314:                 domain => 'Domain role',
 7315:                 author => 'Co-author role',
 7316:                 course => 'Course role',
 7317:                 community => 'Community role',
 7318:             );
 7319:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7320:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7321:             foreach my $domtype ('instdom','extdom') {
 7322:                 my (%checked,$skip);
 7323:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7324:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7325:                               '<td class="LC_left_item">';
 7326:                 if ($domtype eq 'instdom') {
 7327:                     unless (@instdoms > 1) {
 7328:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7329:                         $skip = 1;
 7330:                     }
 7331:                 } elsif ($domtype eq 'extdom') {
 7332:                     if (keys(%by_location) == 0) {
 7333:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7334:                         $skip = 1;
 7335:                     }
 7336:                 }
 7337:                 unless ($skip) {
 7338:                     foreach my $roletype ('domain','author','course','community') {
 7339:                         $checked{'auto'} = ' checked="checked"';
 7340:                         if (ref($settings) eq 'HASH') {
 7341:                             if (ref($settings->{approval}) eq 'HASH') {
 7342:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7343:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7344:                                         $checked{$1} = ' checked="checked"';
 7345:                                         $checked{'auto'} = '';
 7346:                                     }
 7347:                                 }
 7348:                             }
 7349:                         }
 7350:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7351:                         foreach my $option (@options) {
 7352:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7353:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7354:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7355:                                           '</label></span>&nbsp; ';
 7356:                         }
 7357:                         $datatable .= '</fieldset>';
 7358:                     }
 7359:                 }
 7360:                 $datatable .= '</td></tr>';
 7361:                 $itemcount ++;
 7362:             }
 7363:         } elsif ($position eq 'middle') {
 7364:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7365:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7366:                     foreach my $item (@{$types}) {
 7367:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7368:                                                                $numinrow,$itemcount,'','','','','',
 7369:                                                                '',$usertypes->{$item});
 7370:                         $itemcount ++;
 7371:                     }
 7372:                 }
 7373:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7374:                                                        $numinrow,$itemcount,'','','','','',
 7375:                                                        '',$othertitle);
 7376:                 $itemcount ++;
 7377:             } else {
 7378:                 my (@insttypes,%insttitles);
 7379:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7380:                     @insttypes = @{$types};
 7381:                     %insttitles = %{$usertypes};
 7382:                 }
 7383:                 foreach my $item (@insttypes,'default') {
 7384:                     my $title;
 7385:                     if ($item eq 'default') {
 7386:                         $title = $othertitle;
 7387:                     } else {
 7388:                         $title = $insttitles{$item};
 7389:                     }
 7390:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7391:                     $datatable .= '<tr'.$css_class.'>'.
 7392:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7393:                                   '<td class="LC_left_item">'.
 7394:                                   &mt('Nothing to set here, as there are no other domains').
 7395:                                   '</td></tr>';
 7396:                     $itemcount ++;
 7397:                 }
 7398:             }
 7399:         }
 7400:     } else {
 7401:         my $prefix;
 7402:         if ($position eq 'lower') {
 7403:             $prefix = 'priv';
 7404:         } else {
 7405:             $prefix = 'unpriv';
 7406:         }
 7407:         foreach my $item (@items) {
 7408:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7409:                                                    $numinrow,$itemcount,'','','','','',
 7410:                                                    '',$names{$item});
 7411:             $itemcount ++;
 7412:         }
 7413:     }
 7414:     if (ref($rowtotal)) {
 7415:         $$rowtotal += $itemcount;
 7416:     }
 7417:     return $datatable;
 7418: }
 7419: 
 7420: sub print_passwords {
 7421:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7422:     my ($datatable,$css_class);
 7423:     my $itemcount = 0;
 7424:     my %titles = &Apache::lonlocal::texthash (
 7425:         captcha        => '"Forgot Password" CAPTCHA validation',
 7426:         link           => 'Reset link expiration (hours)',
 7427:         case           => 'Case-sensitive usernames/e-mail',
 7428:         prelink        => 'Information required (form 1)',
 7429:         postlink       => 'Information required (form 2)',
 7430:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7431:         customtext     => 'Domain specific text (HTML)',
 7432:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7433:         intauth_check  => 'Check bcrypt cost if authenticated',
 7434:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7435:         permanent      => 'Permanent e-mail address',
 7436:         critical       => 'Critical notification address',
 7437:         notify         => 'Notification address',
 7438:         min            => 'Minimum password length',
 7439:         max            => 'Maximum password length',
 7440:         chars          => 'Required characters',
 7441:         expire         => 'Password expiration (days)',
 7442:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7443:     );
 7444:     if ($position eq 'top') {
 7445:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7446:         my $shownlinklife = 2;
 7447:         my $prelink = 'both';
 7448:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7449:         if (ref($settings) eq 'HASH') {
 7450:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7451:                 $shownlinklife = $settings->{resetlink};
 7452:             }
 7453:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7454:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7455:             }
 7456:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7457:                 $prelink = $settings->{resetprelink};
 7458:             }
 7459:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7460:                 %postlink = %{$settings->{resetpostlink}};
 7461:             }
 7462:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7463:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 7464:             }
 7465:             if ($settings->{resetremove}) {
 7466:                 $nostdtext = 1;
 7467:             }
 7468:             if ($settings->{resetcustom}) {
 7469:                 $customurl = $settings->{resetcustom};
 7470:             }
 7471:         } else {
 7472:             if (ref($types) eq 'ARRAY') {
 7473:                 foreach my $item (@{$types}) {
 7474:                     $casesens{$item} = 1;
 7475:                     $postlink{$item} = ['username','email'];
 7476:                 }
 7477:             }
 7478:             $casesens{'default'} = 1;
 7479:             $postlink{'default'} = ['username','email'];
 7480:             $prelink = 'both';
 7481:             %emailsrc = (
 7482:                           permanent => 1,
 7483:                           critical  => 1,
 7484:                           notify    => 1,
 7485:             );
 7486:         }
 7487:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 7488:         $itemcount ++;
 7489:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7490:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 7491:                       '<td class="LC_left_item">'.
 7492:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7493:                       'name="passwords_link" size="3" /></td></tr>';
 7494:         $itemcount ++;
 7495:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7496:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 7497:                       '<td class="LC_left_item">';
 7498:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7499:             foreach my $item (@{$types}) {
 7500:                 my $checkedcase;
 7501:                 if ($casesens{$item}) {
 7502:                     $checkedcase = ' checked="checked"';
 7503:                 }
 7504:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7505:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 7506:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 7507:                               '</span>&nbsp;&nbsp; ';
 7508:             }
 7509:         }
 7510:         my $checkedcase;
 7511:         if ($casesens{'default'}) {
 7512:             $checkedcase = ' checked="checked"';
 7513:         }
 7514:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7515:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 7516:                       $othertitle.'</label></span></td>';
 7517:         $itemcount ++;
 7518:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7519:         my %checkedpre = (
 7520:                              both => ' checked="checked"',
 7521:                              either => '',
 7522:                          );
 7523:         if ($prelink eq 'either') {
 7524:             $checkedpre{either} = ' checked="checked"';
 7525:             $checkedpre{both} = '';
 7526:         }
 7527:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 7528:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7529:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 7530:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 7531:                       '<span class="LC_nobreak"><label>'.
 7532:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 7533:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 7534:         $itemcount ++;
 7535:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7536:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 7537:                       '<td class="LC_left_item">';
 7538:         my %postlinked;
 7539:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7540:             foreach my $item (@{$types}) {
 7541:                 undef(%postlinked);
 7542:                 $datatable .= '<fieldset style="display: inline-block;">'.
 7543:                               '<legend>'.$usertypes->{$item}.'</legend>';
 7544:                 if (ref($postlink{$item}) eq 'ARRAY') {
 7545:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 7546:                 }
 7547:                 foreach my $field ('email','username') {
 7548:                     my $checked;
 7549:                     if ($postlinked{$field}) {
 7550:                         $checked = ' checked="checked"';
 7551:                     }
 7552:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7553:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 7554:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 7555:                                   '<span>&nbsp;&nbsp; ';
 7556:                 }
 7557:                 $datatable .= '</fieldset>';
 7558:             }
 7559:         }
 7560:         if (ref($postlink{'default'}) eq 'ARRAY') {
 7561:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 7562:         }
 7563:         $datatable .= '<fieldset style="display: inline-block;">'.
 7564:                       '<legend>'.$othertitle.'</legend>';
 7565:         foreach my $field ('email','username') {
 7566:             my $checked;
 7567:             if ($postlinked{$field}) {
 7568:                 $checked = ' checked="checked"';
 7569:             }
 7570:             $datatable .= '<span class="LC_nobreak"><label>'.
 7571:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 7572:                           $field.'"'.$checked.' />'.$field.'</label>'.
 7573:                           '<span>&nbsp;&nbsp; ';
 7574:         }
 7575:         $datatable .= '</fieldset></td></tr>';
 7576:         $itemcount ++;
 7577:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7578:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7579:                       '<td class="LC_left_item">';
 7580:         foreach my $type ('permanent','critical','notify') {
 7581:             my $checkedemail;
 7582:             if ($emailsrc{$type}) {
 7583:                 $checkedemail = ' checked="checked"';
 7584:             }
 7585:             $datatable .= '<span class="LC_nobreak"><label>'.
 7586:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7587:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7588:                           '<span>&nbsp;&nbsp; ';
 7589:         }
 7590:         $datatable .= '</td></tr>';
 7591:         $itemcount ++;
 7592:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7593:         my $switchserver = &check_switchserver($dom,$confname);
 7594:         my ($showstd,$noshowstd);
 7595:         if ($nostdtext) {
 7596:             $noshowstd = ' checked="checked"';
 7597:         } else {
 7598:             $showstd = ' checked="checked"';
 7599:         }
 7600:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7601:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7602:                       &mt('Retain standard text:').
 7603:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7604:                       &mt('Yes').'</label>'.'&nbsp;'.
 7605:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7606:                       &mt('No').'</label></span><br />'.
 7607:                       '<span class="LC_fontsize_small">'.
 7608:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7609:                       &mt('Include custom text:');
 7610:         if ($customurl) {
 7611:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7612:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7613:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7614:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7615:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7616:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7617:         }
 7618:         if ($switchserver) {
 7619:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7620:         } else {
 7621:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7622:                          '<input type="file" name="passwords_customfile" /></span>';
 7623:         }
 7624:         $datatable .= '</td></tr>';
 7625:     } elsif ($position eq 'middle') {
 7626:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 7627:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7628:         my %defaults;
 7629:         if (ref($domconf{'defaults'}) eq 'HASH') {
 7630:             %defaults = %{$domconf{'defaults'}};
 7631:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7632:                 $defaults{'intauth_cost'} = 10;
 7633:             }
 7634:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7635:                 $defaults{'intauth_check'} = 0;
 7636:             }
 7637:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7638:                 $defaults{'intauth_switch'} = 0;
 7639:             }
 7640:         } else {
 7641:             %defaults = (
 7642:                           'intauth_cost'   => 10,
 7643:                           'intauth_check'  => 0,
 7644:                           'intauth_switch' => 0,
 7645:                         );
 7646:         }
 7647:         foreach my $item (@items) {
 7648:             if ($itemcount%2) {
 7649:                 $css_class = '';
 7650:             } else {
 7651:                 $css_class = ' class="LC_odd_row" ';
 7652:             }
 7653:             $datatable .= '<tr'.$css_class.'>'.
 7654:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 7655:                           '</span></td><td class="LC_left_item" colspan="3">';
 7656:             if ($item eq 'intauth_switch') {
 7657:                 my @options = (0,1,2);
 7658:                 my %optiondesc = &Apache::lonlocal::texthash (
 7659:                                    0 => 'No',
 7660:                                    1 => 'Yes',
 7661:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7662:                                  );
 7663:                 $datatable .= '<table width="100%">';
 7664:                 foreach my $option (@options) {
 7665:                     my $checked = ' ';
 7666:                     if ($defaults{$item} eq $option) {
 7667:                         $checked = ' checked="checked"';
 7668:                     }
 7669:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7670:                                   '<label><input type="radio" name="'.$item.
 7671:                                   '" value="'.$option.'"'.$checked.' />'.
 7672:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7673:                 }
 7674:                 $datatable .= '</table>';
 7675:             } elsif ($item eq 'intauth_check') {
 7676:                 my @options = (0,1,2);
 7677:                 my %optiondesc = &Apache::lonlocal::texthash (
 7678:                                    0 => 'No',
 7679:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7680:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7681:                                  );
 7682:                 $datatable .= '<table width="100%">';
 7683:                 foreach my $option (@options) {
 7684:                     my $checked = ' ';
 7685:                     my $onclick;
 7686:                     if ($defaults{$item} eq $option) {
 7687:                         $checked = ' checked="checked"';
 7688:                     }
 7689:                     if ($option == 2) {
 7690:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7691:                     }
 7692:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7693:                                   '<label><input type="radio" name="'.$item.
 7694:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7695:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7696:                 }
 7697:                 $datatable .= '</table>';
 7698:             } else {
 7699:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7700:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7701:             }
 7702:             $datatable .= '</td></tr>';
 7703:             $itemcount ++;
 7704:         }
 7705:     } elsif ($position eq 'lower') {
 7706:         my ($min,$max,%chars,$expire,$numsaved);
 7707:         $min = $Apache::lonnet::passwdmin;
 7708:         if (ref($settings) eq 'HASH') {
 7709:             if ($settings->{min}) {
 7710:                 $min = $settings->{min};
 7711:             }
 7712:             if ($settings->{max}) {
 7713:                 $max = $settings->{max};
 7714:             }
 7715:             if (ref($settings->{chars}) eq 'ARRAY') {
 7716:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 7717:             }
 7718:             if ($settings->{expire}) {
 7719:                 $expire = $settings->{expire};
 7720:             }
 7721:             if ($settings->{numsaved}) {
 7722:                 $numsaved = $settings->{numsaved};
 7723:             }
 7724:         }
 7725:         my %rulenames = &Apache::lonlocal::texthash(
 7726:                                                      uc => 'At least one upper case letter',
 7727:                                                      lc => 'At least one lower case letter',
 7728:                                                      num => 'At least one number',
 7729:                                                      spec => 'At least one non-alphanumeric',
 7730:                                                    );
 7731:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7732:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7733:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7734:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 7735:                       'onblur="javascript:warnIntPass(this);" />'.
 7736:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7737:                       '</span></td></tr>';
 7738:         $itemcount ++;
 7739:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7740:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7741:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7742:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 7743:                       'onblur="javascript:warnIntPass(this);" />'.
 7744:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7745:                       '</span></td></tr>';
 7746:         $itemcount ++;
 7747:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7748:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7749:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7750:                       '</span></td>';
 7751:         my $numinrow = 2;
 7752:         my @possrules = ('uc','lc','num','spec');
 7753:         $datatable .= '<td class="LC_left_item"><table>';
 7754:         for (my $i=0; $i<@possrules; $i++) {
 7755:             my ($rem,$checked);
 7756:             if ($chars{$possrules[$i]}) {
 7757:                 $checked = ' checked="checked"';
 7758:             }
 7759:             $rem = $i%($numinrow);
 7760:             if ($rem == 0) {
 7761:                 if ($i > 0) {
 7762:                     $datatable .= '</tr>';
 7763:                 }
 7764:                 $datatable .= '<tr>';
 7765:             }
 7766:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 7767:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7768:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 7769:         }
 7770:         my $rem = @possrules%($numinrow);
 7771:         my $colsleft = $numinrow - $rem;
 7772:         if ($colsleft > 1 ) {
 7773:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7774:                           '&nbsp;</td>';
 7775:         } elsif ($colsleft == 1) {
 7776:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7777:         }
 7778:         $datatable .='</table></td></tr>';
 7779:         $itemcount ++;
 7780:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7781:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7782:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7783:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" '.
 7784:                       'onblur="javascript:warnIntPass(this);" />'.
 7785:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7786:                       '</span></td></tr>';
 7787:         $itemcount ++;
 7788:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7789:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7790:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7791:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 7792:                       'onblur="javascript:warnIntPass(this);" />'. 
 7793:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7794:                       '</span></td></tr>';
 7795:     } else {
 7796:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7797:         my %ownerchg = (
 7798:                           by  => {},
 7799:                           for => {},
 7800:                        );
 7801:         my %ownertitles = &Apache::lonlocal::texthash (
 7802:                             by  => 'Course owner status(es) allowed',
 7803:                             for => 'Student status(es) allowed',
 7804:                           );
 7805:         if (ref($settings) eq 'HASH') {
 7806:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7807:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7808:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7809:                 }
 7810:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7811:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7812:                 }
 7813:             }
 7814:         }
 7815:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7816:         $datatable .= '<tr '.$css_class.'>'.
 7817:                       '<td>'.
 7818:                       &mt('Requirements').'<ul>'.
 7819:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7820:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7821:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7822:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7823:                       '</ul>'.
 7824:                       '</td>'.
 7825:                       '<td class="LC_left_item">';
 7826:         foreach my $item ('by','for') {
 7827:             $datatable .= '<fieldset style="display: inline-block;">'.
 7828:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7829:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7830:                 foreach my $type (@{$types}) {
 7831:                     my $checked;
 7832:                     if ($ownerchg{$item}{$type}) {
 7833:                         $checked = ' checked="checked"';
 7834:                     }
 7835:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7836:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7837:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7838:                                   '</span>&nbsp;&nbsp; ';
 7839:                 }
 7840:             }
 7841:             my $checked;
 7842:             if ($ownerchg{$item}{'default'}) {
 7843:                 $checked = ' checked="checked"';
 7844:             }
 7845:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7846:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7847:                           $othertitle.'</label></span></fieldset>';
 7848:         }
 7849:         $datatable .= '</td></tr>';
 7850:     }
 7851:     return $datatable;
 7852: }
 7853: 
 7854: sub print_wafproxy {
 7855:     my ($position,$dom,$settings,$rowtotal) = @_;
 7856:     my $css_class;
 7857:     my $itemcount = 0;
 7858:     my $datatable;
 7859:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7860:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 7861:     my %lt = &wafproxy_titles();
 7862:     foreach my $server (sort(keys(%servers))) {
 7863:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7864:         next if ($serverhome eq '');
 7865:         my $serverdom;
 7866:         if ($serverhome ne $server) {
 7867:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7868:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 7869:                 $othercontrol{$server} = $serverdom;
 7870:             }
 7871:         } else {
 7872:             $serverdom = &Apache::lonnet::host_domain($server);
 7873:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 7874:             if ($serverdom ne $dom) {
 7875:                 $othercontrol{$server} = $serverdom;
 7876:             } else {
 7877:                 $setdom = 1;
 7878:                 if (ref($settings) eq 'HASH') {
 7879:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7880:                         $aliases{$dom} = $settings->{'alias'};
 7881:                         if ($aliases{$dom} ne '') {
 7882:                             $showdom = 1;
 7883:                         }
 7884:                     }
 7885:                     if (ref($settings->{'saml'}) eq 'HASH') {
 7886:                         $saml{$dom} = $settings->{'saml'};
 7887:                     }
 7888:                 }
 7889:             }
 7890:         }
 7891:     }
 7892:     if ($setdom) {
 7893:         %{$values{$dom}} = ();
 7894:         if (ref($settings) eq 'HASH') {
 7895:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7896:                 $values{$dom}{$item} = $settings->{$item};
 7897:             }
 7898:         }
 7899:     }
 7900:     if (keys(%othercontrol)) {
 7901:         %otherdoms = reverse(%othercontrol);
 7902:         foreach my $domain (keys(%otherdoms)) {
 7903:             %{$values{$domain}} = ();
 7904:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 7905:             if (ref($config{'wafproxy'}) eq 'HASH') {
 7906:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 7907:                 if (exists($config{'wafproxy'}{'saml'})) { 
 7908:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 7909:                 }
 7910:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7911:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 7912:                 }
 7913:             }
 7914:         }
 7915:     }
 7916:     if ($position eq 'top') {
 7917:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7918:         my %aliasinfo;
 7919:         foreach my $server (sort(keys(%servers))) {
 7920:             $itemcount ++;
 7921:             my $dom_in_effect;
 7922:             my $aliasrows = '<tr>'.
 7923:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7924:                             &mt('Hostname').':&nbsp;'.
 7925:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 7926:             if ($othercontrol{$server}) {
 7927:                 $dom_in_effect = $othercontrol{$server};
 7928:                 my ($current,$forsaml);
 7929:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 7930:                     $current = $aliases{$dom_in_effect}{$server};
 7931:                 }
 7932:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 7933:                     if ($saml{$dom_in_effect}{$server}) {
 7934:                         $forsaml = 1;
 7935:                     }
 7936:                 }
 7937:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7938:                               &mt('Alias').':&nbsp';
 7939:                 if ($current) {
 7940:                     $aliasrows .= $current;
 7941:                     if ($forsaml) {
 7942:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 7943:                     }
 7944:                 } else {
 7945:                     $aliasrows .= &mt('None');
 7946:                 }
 7947:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 7948:                               &mt('controlled by domain: [_1]',
 7949:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 7950:             } else {
 7951:                 $dom_in_effect = $dom;
 7952:                 my ($current,$samlon,$samloff);
 7953:                 $samloff = ' checked="checked"';
 7954:                 if (ref($aliases{$dom}) eq 'HASH') {
 7955:                     if ($aliases{$dom}{$server}) {
 7956:                         $current = $aliases{$dom}{$server};
 7957:                     }
 7958:                 }
 7959:                 if (ref($saml{$dom}) eq 'HASH') {
 7960:                     if ($saml{$dom}{$server}) {
 7961:                         $samlon = $samloff;
 7962:                         undef($samloff);
 7963:                     }
 7964:                 }
 7965:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7966:                               &mt('Alias').':&nbsp;'.
 7967:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 7968:                               'value="'.$current.'" size="30" />'.
 7969:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 7970:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 7971:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 7972:                               &mt('No').'</label>&nbsp;<label>'. 
 7973:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 7974:                               &mt('Yes').'</label></span>'.
 7975:                               '</td>'; 
 7976:             }
 7977:             $aliasrows .= '</tr>';
 7978:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 7979:         }
 7980:         if ($aliasinfo{$dom}) {
 7981:             my ($onclick,$wafon,$wafoff,$showtable);
 7982:             $onclick = ' onclick="javascript:toggleWAF();"';
 7983:             $wafoff = ' checked="checked"';
 7984:             $showtable = ' style="display:none";';
 7985:             if ($showdom) {
 7986:                 $wafon = $wafoff;
 7987:                 $wafoff = '';
 7988:                 $showtable = ' style="display:inline;"';
 7989:             }
 7990:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7991:             $datatable = '<tr'.$css_class.'>'.
 7992:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 7993:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 7994:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 7995:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 7996:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 7997:                          &mt('No').'</label></span></td>'.
 7998:                          '<td class="LC_left_item">'.
 7999:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8000:                          '</table></td></tr>';
 8001:             $itemcount++;
 8002:         }
 8003:         if (keys(%otherdoms)) {
 8004:             foreach my $key (sort(keys(%otherdoms))) {
 8005:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8006:                 $datatable .= '<tr'.$css_class.'>'.
 8007:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8008:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8009:                               '</table></td></tr>';
 8010:                 $itemcount++;
 8011:             }
 8012:         }
 8013:     } else {
 8014:         my %ip_methods = &remoteip_methods();
 8015:         if ($setdom) {
 8016:             $itemcount ++;
 8017:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8018:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8019:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8020:             $wafstyle = ' style="display:none;"';
 8021:             $nowafstyle = ' style="display:table-row;"';
 8022:             $currwafdisplay = ' style="display: none"';
 8023:             $wafrangestyle = ' style="display: none"';
 8024:             $curr_remotip = 'n';
 8025:             $ssltossl = ' checked="checked"';
 8026:             if ($showdom) {
 8027:                 $wafstyle = ' style="display:table-row;"';
 8028:                 $nowafstyle =  ' style="display:none;"';
 8029:                 if (keys(%{$values{$dom}})) {
 8030:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8031:                         $curr_remotip = $values{$dom}{remoteip};
 8032:                     }
 8033:                     if ($curr_remotip eq 'h') {
 8034:                         $currwafdisplay = ' style="display:table-row"';
 8035:                         $wafrangestyle = ' style="display:inline-block;"';
 8036:                     }
 8037:                     if ($values{$dom}{'sslopt'}) {
 8038:                         $alltossl = ' checked="checked"';
 8039:                         $ssltossl = '';
 8040:                     }
 8041:                 }
 8042:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8043:                     $vpndircheck = ' checked="checked"';
 8044:                     $currwafvpn = ' style="display:table-row;"';
 8045:                     $wafrangestyle = ' style="display:inline-block;"';
 8046:                 } else {
 8047:                     $vpnaliascheck = ' checked="checked"';
 8048:                     $currwafvpn = ' style="display:none;"';
 8049:                 }
 8050:             }
 8051:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8052:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8053:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8054:                           '</tr>'.
 8055:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8056:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8057:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8058:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8059:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8060:                           '<td class="LC_left_item"><table>'.
 8061:                           '<tr>'.
 8062:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8063:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8064:             foreach my $option ('m','h','n') {
 8065:                 my $sel;
 8066:                 if ($option eq $curr_remotip) {
 8067:                    $sel = ' selected="selected"';
 8068:                 }
 8069:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8070:                               $ip_methods{$option}.'</option>';
 8071:             }
 8072:             $datatable .= '</select></td></tr>'."\n".
 8073:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8074:                           $lt{'ipheader'}.':&nbsp;'.
 8075:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8076:                           'name="wafproxy_ipheader" />'.
 8077:                           '</td></tr>'."\n".
 8078:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8079:                           $lt{'trusted'}.':<br />'.
 8080:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8081:                           $values{$dom}{'trusted'}.'</textarea>'.
 8082:                           '</td></tr>'."\n".
 8083:                           '<tr><td><hr /></td></tr>'."\n".
 8084:                           '<tr>'.
 8085:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8086:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8087:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8088:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8089:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8090:             foreach my $item ('vpnint','vpnext') {
 8091:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8092:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8093:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8094:                               $values{$dom}{$item}.'</textarea>'.
 8095:                               '</td></tr>'."\n";
 8096:             }
 8097:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8098:                           '<tr>'.
 8099:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8100:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8101:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8102:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8103:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8104:                           '</table></td></tr>';
 8105:         }
 8106:         if (keys(%otherdoms)) {
 8107:             foreach my $domain (sort(keys(%otherdoms))) {
 8108:                 $itemcount ++;
 8109:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8110:                 $datatable .= '<tr'.$css_class.'>'.
 8111:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8112:                               '<td class="LC_left_item"><table>';
 8113:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8114:                     my $showval = &mt('None');
 8115:                     if ($item eq 'ssl') {
 8116:                         $showval = $lt{'ssltossl'};
 8117:                     }
 8118:                     if ($values{$domain}{$item}) {
 8119:                         $showval = $values{$domain}{$item};
 8120:                         if ($item eq 'ssl') {
 8121:                             $showval = $lt{'alltossl'};
 8122:                         } elsif ($item eq 'remoteip') {
 8123:                             $showval = $ip_methods{$values{$domain}{$item}};
 8124:                         }
 8125:                     }
 8126:                     $datatable .= '<tr>'.
 8127:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8128:                 }
 8129:                 $datatable .= '</table></td></tr>';
 8130:             }
 8131:         }
 8132:     }
 8133:     $$rowtotal += $itemcount;
 8134:     return $datatable;
 8135: }
 8136: 
 8137: sub wafproxy_titles {
 8138:     return &Apache::lonlocal::texthash(
 8139:                remoteip   => "Method for determining user's IP",
 8140:                ipheader   => 'Request header containing remote IP',
 8141:                trusted    => 'Trusted IP range(s)',
 8142:                vpnaccess  => 'Access from institutional VPN',
 8143:                vpndirect  => 'via regular hostname (no WAF)',
 8144:                vpnaliased => 'via aliased hostname (WAF)',
 8145:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8146:                vpnext     => 'IP Range(s) for backend WAF connections',
 8147:                sslopt     => 'Forwarding http/https',
 8148:                alltossl   => 'WAF forwards both http and https requests to https',
 8149:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8150:            );
 8151: }
 8152: 
 8153: sub remoteip_methods {
 8154:     return &Apache::lonlocal::texthash(
 8155:               m => 'Use Apache mod_remoteip',
 8156:               h => 'Use headers parsed by LON-CAPA',
 8157:               n => 'Not in use',
 8158:            );
 8159: }
 8160: 
 8161: sub print_usersessions {
 8162:     my ($position,$dom,$settings,$rowtotal) = @_;
 8163:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8164:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8165:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8166: 
 8167:     my @alldoms = &Apache::lonnet::all_domains();
 8168:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8169:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8170:     my %altids = &id_for_thisdom(%servers);
 8171:     if ($position eq 'top') {
 8172:         if (keys(%serverhomes) > 1) {
 8173:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8174:             my ($curroffloadnow,$curroffloadoth);
 8175:             if (ref($settings) eq 'HASH') {
 8176:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8177:                     $curroffloadnow = $settings->{'offloadnow'};
 8178:                 }
 8179:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8180:                     $curroffloadoth = $settings->{'offloadoth'};
 8181:                 }
 8182:             }
 8183:             my $other_insts = scalar(keys(%by_location));
 8184:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8185:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8186:         } else {
 8187:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8188:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8189:                           '</td></tr>';
 8190:         }
 8191:     } else {
 8192:         my %titles = &usersession_titles();
 8193:         my ($prefix,@types);
 8194:         if ($position eq 'bottom') {
 8195:             $prefix = 'remote';
 8196:             @types = ('version','excludedomain','includedomain');
 8197:         } else {
 8198:             $prefix = 'hosted';
 8199:             @types = ('excludedomain','includedomain');
 8200:         }
 8201:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8202:     }
 8203:     $$rowtotal += $itemcount;
 8204:     return $datatable;
 8205: }
 8206: 
 8207: sub rules_by_location {
 8208:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8209:     my ($datatable,$itemcount,$css_class);
 8210:     if (keys(%{$by_location}) == 0) {
 8211:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8212:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8213:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8214:                      '</td></tr>';
 8215:         $itemcount = 1;
 8216:     } else {
 8217:         $itemcount = 0;
 8218:         my $numinrow = 5;
 8219:         my (%current,%checkedon,%checkedoff);
 8220:         my @locations = sort(keys(%{$by_location}));
 8221:         foreach my $type (@{$types}) {
 8222:             $checkedon{$type} = '';
 8223:             $checkedoff{$type} = ' checked="checked"';
 8224:         }
 8225:         if (ref($settings) eq 'HASH') {
 8226:             if (ref($settings->{$prefix}) eq 'HASH') {
 8227:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8228:                     $current{$key} = $settings->{$prefix}{$key};
 8229:                     if ($key eq 'version') {
 8230:                         if ($current{$key} ne '') {
 8231:                             $checkedon{$key} = ' checked="checked"';
 8232:                             $checkedoff{$key} = '';
 8233:                         }
 8234:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8235:                         $checkedon{$key} = ' checked="checked"';
 8236:                         $checkedoff{$key} = '';
 8237:                     }
 8238:                 }
 8239:             }
 8240:         }
 8241:         foreach my $type (@{$types}) {
 8242:             next if ($type ne 'version' && !@locations);
 8243:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8244:             $datatable .= '<tr'.$css_class.'>
 8245:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8246:                            <span class="LC_nobreak">&nbsp;
 8247:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8248:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8249:             if ($type eq 'version') {
 8250:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8251:                 my $selector = '<select name="'.$prefix.'_version">';
 8252:                 foreach my $version (@lcversions) {
 8253:                     my $selected = '';
 8254:                     if ($current{'version'} eq $version) {
 8255:                         $selected = ' selected="selected"';
 8256:                     }
 8257:                     $selector .= ' <option value="'.$version.'"'.
 8258:                                  $selected.'>'.$version.'</option>';
 8259:                 }
 8260:                 $selector .= '</select> ';
 8261:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8262:             } else {
 8263:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8264:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8265:                              ' />'.('&nbsp;'x2).
 8266:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8267:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8268:                              "\n".
 8269:                              '</div><div><table>';
 8270:                 my $rem;
 8271:                 for (my $i=0; $i<@locations; $i++) {
 8272:                     my ($showloc,$value,$checkedtype);
 8273:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8274:                         my $ip = $by_location->{$locations[$i]}->[0];
 8275:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8276:                             $value = join(':',@{$by_ip->{$ip}});
 8277:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8278:                             if (ref($current{$type}) eq 'ARRAY') {
 8279:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8280:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8281:                                         $checkedtype = ' checked="checked"';
 8282:                                         last;
 8283:                                     }
 8284:                                 }
 8285:                             }
 8286:                         }
 8287:                     }
 8288:                     $rem = $i%($numinrow);
 8289:                     if ($rem == 0) {
 8290:                         if ($i > 0) {
 8291:                             $datatable .= '</tr>';
 8292:                         }
 8293:                         $datatable .= '<tr>';
 8294:                     }
 8295:                     $datatable .= '<td class="LC_left_item">'.
 8296:                                   '<span class="LC_nobreak"><label>'.
 8297:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8298:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8299:                                   '</label></span></td>';
 8300:                 }
 8301:                 $rem = @locations%($numinrow);
 8302:                 my $colsleft = $numinrow - $rem;
 8303:                 if ($colsleft > 1 ) {
 8304:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8305:                                   '&nbsp;</td>';
 8306:                 } elsif ($colsleft == 1) {
 8307:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8308:                 }
 8309:                 $datatable .= '</tr></table>';
 8310:             }
 8311:             $datatable .= '</td></tr>';
 8312:             $itemcount ++;
 8313:         }
 8314:     }
 8315:     return ($datatable,$itemcount);
 8316: }
 8317: 
 8318: sub print_ssl {
 8319:     my ($position,$dom,$settings,$rowtotal) = @_;
 8320:     my ($css_class,$datatable);
 8321:     my $itemcount = 1;
 8322:     if ($position eq 'top') {
 8323:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8324:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8325:         my $same_institution;
 8326:         if ($intdom ne '') {
 8327:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8328:             if (ref($internet_names) eq 'ARRAY') {
 8329:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8330:                     $same_institution = 1;
 8331:                 }
 8332:             }
 8333:         }
 8334:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8335:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8336:         if ($same_institution) {
 8337:             my %domservers = &Apache::lonnet::get_servers($dom);
 8338:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8339:         } else {
 8340:             $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.");
 8341:         }
 8342:         $datatable .= '</td></tr>';
 8343:         $itemcount ++;
 8344:     } else {
 8345:         my %titles = &ssl_titles();
 8346:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8347:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8348:         my @alldoms = &Apache::lonnet::all_domains();
 8349:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8350:         my @domservers = &Apache::lonnet::get_servers($dom);
 8351:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8352:         my %altids = &id_for_thisdom(%servers);
 8353:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8354:             my $legacy;
 8355:             unless (ref($settings) eq 'HASH') {
 8356:                 my $name;
 8357:                 if ($position eq 'connto') {
 8358:                     $name = 'loncAllowInsecure';
 8359:                 } else {
 8360:                     $name = 'londAllowInsecure';
 8361:                 }
 8362:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8363:                 my @ids=&Apache::lonnet::current_machine_ids();
 8364:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8365:                     my %what = (
 8366:                                    $name => 1,
 8367:                                );
 8368:                     my ($result,$returnhash) =
 8369:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8370:                     if ($result eq 'ok') {
 8371:                         if (ref($returnhash) eq 'HASH') {
 8372:                             $legacy = $returnhash->{$name};
 8373:                         }
 8374:                     }
 8375:                 } else {
 8376:                     $legacy = $Apache::lonnet::perlvar{$name};
 8377:                 }
 8378:             }
 8379:             foreach my $type ('dom','intdom','other') {
 8380:                 my %checked;
 8381:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8382:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8383:                               '<td class="LC_right_item">';
 8384:                 my $skip; 
 8385:                 if ($type eq 'dom') {
 8386:                     unless (keys(%servers) > 1) {
 8387:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8388:                         $skip = 1;
 8389:                     }
 8390:                 }
 8391:                 if ($type eq 'intdom') {
 8392:                     unless (@instdoms > 1) {
 8393:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8394:                         $skip = 1;
 8395:                     } 
 8396:                 } elsif ($type eq 'other') {
 8397:                     if (keys(%by_location) == 0) {
 8398:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8399:                         $skip = 1;
 8400:                     }
 8401:                 }
 8402:                 unless ($skip) {
 8403:                     $checked{'yes'} = ' checked="checked"'; 
 8404:                     if (ref($settings) eq 'HASH') {
 8405:                         if (ref($settings->{$position}) eq 'HASH') {
 8406:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8407:                                 $checked{$1} = $checked{'yes'};
 8408:                                 delete($checked{'yes'}); 
 8409:                             }
 8410:                         }
 8411:                     } else {
 8412:                         if ($legacy == 0) {
 8413:                             $checked{'req'} = $checked{'yes'};
 8414:                             delete($checked{'yes'});    
 8415:                         }
 8416:                     }
 8417:                     foreach my $option ('no','yes','req') {
 8418:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8419:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8420:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8421:                                       '</label></span>'.('&nbsp;'x2);
 8422:                     }
 8423:                 }
 8424:                 $datatable .= '</td></tr>';
 8425:                 $itemcount ++; 
 8426:             }
 8427:         } else {
 8428:             my $prefix = 'replication';
 8429:             my @types = ('certreq','nocertreq');
 8430:             if (keys(%by_location) == 0) {
 8431:                 $datatable .= '<tr'.$css_class.'><td>'.
 8432:                               &mt('Nothing to set here, as there are no other institutions').
 8433:                               '</td></tr>';
 8434:                 $itemcount ++;
 8435:             } else {
 8436:                 ($datatable,$itemcount) = 
 8437:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8438:             }
 8439:         }
 8440:     }
 8441:     $$rowtotal += $itemcount;
 8442:     return $datatable;
 8443: }
 8444: 
 8445: sub ssl_titles {
 8446:     return &Apache::lonlocal::texthash (
 8447:                dom           => 'LON-CAPA servers/VMs from same domain',
 8448:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 8449:                other         => 'External LON-CAPA servers/VMs',
 8450:                connto        => 'Connections to other servers',
 8451:                connfrom      => 'Connections from other servers',
 8452:                replication   => 'Replicating content to other institutions',
 8453:                certreq       => 'Client certificate required, but specific domains exempt',
 8454:                nocertreq     => 'No client certificate required, except for specific domains',
 8455:                no            => 'SSL not used',
 8456:                yes           => 'SSL Optional (used if available)',
 8457:                req           => 'SSL Required',
 8458:     );
 8459: }
 8460: 
 8461: sub print_trust {
 8462:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 8463:     my ($css_class,$datatable,%checked,%choices);
 8464:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8465:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8466:     my $itemcount = 1;
 8467:     my %titles = &trust_titles();
 8468:     my @types = ('exc','inc');
 8469:     if ($prefix eq 'top') {
 8470:         $prefix = 'content';
 8471:     } elsif ($prefix eq 'bottom') {
 8472:         $prefix = 'msg';
 8473:     }
 8474:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8475:     $$rowtotal += $itemcount;
 8476:     return $datatable;
 8477: }
 8478: 
 8479: sub trust_titles {
 8480:     return &Apache::lonlocal::texthash(
 8481:                content  => "Access to this domain's content by others",
 8482:                shared   => "Access to other domain's content by this domain",
 8483:                enroll   => "Enrollment in this domain's courses by others", 
 8484:                othcoau  => "Co-author roles in this domain for others",
 8485:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 8486:                domroles => "Domain roles in this domain assignable to others",
 8487:                catalog  => "Course Catalog for this domain displayed elsewhere",
 8488:                reqcrs   => "Requests for creation of courses in this domain by others",
 8489:                msg      => "Users in other domains can send messages to this domain",
 8490:                exc      => "Allow all, but exclude specific domains",
 8491:                inc      => "Deny all, but include specific domains",
 8492:            );
 8493: } 
 8494: 
 8495: sub build_location_hashes {
 8496:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 8497:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 8498:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 8499:     my %iphost = &Apache::lonnet::get_iphost();
 8500:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 8501:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 8502:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 8503:         foreach my $id (@{$iphost{$primary_ip}}) {
 8504:             my $intdom = &Apache::lonnet::internet_dom($id);
 8505:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 8506:                 push(@{$intdoms},$intdom);
 8507:             }
 8508:         }
 8509:     }
 8510:     foreach my $ip (keys(%iphost)) {
 8511:         if (ref($iphost{$ip}) eq 'ARRAY') {
 8512:             foreach my $id (@{$iphost{$ip}}) {
 8513:                 my $location = &Apache::lonnet::internet_dom($id);
 8514:                 if ($location) {
 8515:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 8516:                         my $dom = &Apache::lonnet::host_domain($id);
 8517:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 8518:                             push(@{$instdoms},$dom);
 8519:                         }
 8520:                         next;
 8521:                     }
 8522:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8523:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 8524:                             push(@{$by_ip->{$ip}},$location);
 8525:                         }
 8526:                     } else {
 8527:                         $by_ip->{$ip} = [$location];
 8528:                     }
 8529:                 }
 8530:             }
 8531:         }
 8532:     }
 8533:     foreach my $ip (sort(keys(%{$by_ip}))) {
 8534:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8535:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 8536:             my $first = $by_ip->{$ip}->[0];
 8537:             if (ref($by_location->{$first}) eq 'ARRAY') {
 8538:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 8539:                     push(@{$by_location->{$first}},$ip);
 8540:                 }
 8541:             } else {
 8542:                 $by_location->{$first} = [$ip];
 8543:             }
 8544:         }
 8545:     }
 8546:     return;
 8547: }
 8548: 
 8549: sub current_offloads_to {
 8550:     my ($dom,$settings,$servers) = @_;
 8551:     my (%spareid,%otherdomconfigs);
 8552:     if (ref($servers) eq 'HASH') {
 8553:         foreach my $lonhost (sort(keys(%{$servers}))) {
 8554:             my $gotspares;
 8555:             if (ref($settings) eq 'HASH') {
 8556:                 if (ref($settings->{'spares'}) eq 'HASH') {
 8557:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 8558:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 8559:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 8560:                         $gotspares = 1;
 8561:                     }
 8562:                 }
 8563:             }
 8564:             unless ($gotspares) {
 8565:                 my $gotspares;
 8566:                 my $serverhomeID =
 8567:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 8568:                 my $serverhomedom =
 8569:                     &Apache::lonnet::host_domain($serverhomeID);
 8570:                 if ($serverhomedom ne $dom) {
 8571:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 8572:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8573:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8574:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8575:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8576:                                 $gotspares = 1;
 8577:                             }
 8578:                         }
 8579:                     } else {
 8580:                         $otherdomconfigs{$serverhomedom} =
 8581:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 8582:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 8583:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8584:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8585:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 8586:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8587:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8588:                                         $gotspares = 1;
 8589:                                     }
 8590:                                 }
 8591:                             }
 8592:                         }
 8593:                     }
 8594:                 }
 8595:             }
 8596:             unless ($gotspares) {
 8597:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8598:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8599:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8600:                } else {
 8601:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 8602:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 8603:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8604:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8605:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8606:                     } else {
 8607:                         my %what = (
 8608:                              spareid => 1,
 8609:                         );
 8610:                         my ($result,$returnhash) = 
 8611:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 8612:                         if ($result eq 'ok') { 
 8613:                             if (ref($returnhash) eq 'HASH') {
 8614:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 8615:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 8616:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 8617:                                 }
 8618:                             }
 8619:                         }
 8620:                     }
 8621:                 }
 8622:             }
 8623:         }
 8624:     }
 8625:     return %spareid;
 8626: }
 8627: 
 8628: sub spares_row {
 8629:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 8630:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 8631:     my $css_class;
 8632:     my $numinrow = 4;
 8633:     my $itemcount = 1;
 8634:     my $datatable;
 8635:     my %typetitles = &sparestype_titles();
 8636:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8637:         foreach my $server (sort(keys(%{$servers}))) {
 8638:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 8639:             my ($othercontrol,$serverdom);
 8640:             if ($serverhome ne $server) {
 8641:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 8642:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8643:             } else {
 8644:                 $serverdom = &Apache::lonnet::host_domain($server);
 8645:                 if ($serverdom ne $dom) {
 8646:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8647:                 }
 8648:             }
 8649:             next unless (ref($spareid->{$server}) eq 'HASH');
 8650:             my ($checkednow,$checkedoth);
 8651:             if (ref($curroffloadnow) eq 'HASH') {
 8652:                 if ($curroffloadnow->{$server}) {
 8653:                     $checkednow = ' checked="checked"';
 8654:                 }
 8655:             }
 8656:             if (ref($curroffloadoth) eq 'HASH') {
 8657:                 if ($curroffloadoth->{$server}) {
 8658:                     $checkedoth = ' checked="checked"';
 8659:                 }
 8660:             }
 8661:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8662:             $datatable .= '<tr'.$css_class.'>
 8663:                            <td rowspan="2">
 8664:                             <span class="LC_nobreak">'.
 8665:                           &mt('[_1] when busy, offloads to:'
 8666:                               ,'<b>'.$server.'</b>').'</span><br />'.
 8667:                           '<span class="LC_nobreak">'."\n".
 8668:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 8669:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 8670:                           "\n";
 8671:             if ($other_insts) {
 8672:                 $datatable .= '<br />'.
 8673:                               '<span class="LC_nobreak">'."\n".
 8674:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 8675:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 8676:                           "\n";
 8677:             }
 8678:             my (%current,%canselect);
 8679:             my @choices = 
 8680:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 8681:             foreach my $type ('primary','default') {
 8682:                 if (ref($spareid->{$server}) eq 'HASH') {
 8683:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 8684:                         my @spares = @{$spareid->{$server}{$type}};
 8685:                         if (@spares > 0) {
 8686:                             if ($othercontrol) {
 8687:                                 $current{$type} = join(', ',@spares);
 8688:                             } else {
 8689:                                 $current{$type} .= '<table>';
 8690:                                 my $numspares = scalar(@spares);
 8691:                                 for (my $i=0;  $i<@spares; $i++) {
 8692:                                     my $rem = $i%($numinrow);
 8693:                                     if ($rem == 0) {
 8694:                                         if ($i > 0) {
 8695:                                             $current{$type} .= '</tr>';
 8696:                                         }
 8697:                                         $current{$type} .= '<tr>';
 8698:                                     }
 8699:                                     $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;'.
 8700:                                                        $spareid->{$server}{$type}[$i].
 8701:                                                        '</label></td>'."\n";
 8702:                                 }
 8703:                                 my $rem = @spares%($numinrow);
 8704:                                 my $colsleft = $numinrow - $rem;
 8705:                                 if ($colsleft > 1 ) {
 8706:                                     $current{$type} .= '<td colspan="'.$colsleft.
 8707:                                                        '" class="LC_left_item">'.
 8708:                                                        '&nbsp;</td>';
 8709:                                 } elsif ($colsleft == 1) {
 8710:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 8711:                                 }
 8712:                                 $current{$type} .= '</tr></table>';
 8713:                             }
 8714:                         }
 8715:                     }
 8716:                     if ($current{$type} eq '') {
 8717:                         $current{$type} = &mt('None specified');
 8718:                     }
 8719:                     if ($othercontrol) {
 8720:                         if ($type eq 'primary') {
 8721:                             $canselect{$type} = $othercontrol;
 8722:                         }
 8723:                     } else {
 8724:                         $canselect{$type} = 
 8725:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 8726:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 8727:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 8728:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 8729:                         if (@choices > 0) {
 8730:                             foreach my $lonhost (@choices) {
 8731:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 8732:                             }
 8733:                         }
 8734:                         $canselect{$type} .= '</select>'."\n";
 8735:                     }
 8736:                 } else {
 8737:                     $current{$type} = &mt('Could not be determined');
 8738:                     if ($type eq 'primary') {
 8739:                         $canselect{$type} =  $othercontrol;
 8740:                     }
 8741:                 }
 8742:                 if ($type eq 'default') {
 8743:                     $datatable .= '<tr'.$css_class.'>';
 8744:                 }
 8745:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 8746:                               '<td>'.$current{$type}.'</td>'."\n".
 8747:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 8748:             }
 8749:             $itemcount ++;
 8750:         }
 8751:     }
 8752:     $$rowtotal += $itemcount;
 8753:     return $datatable;
 8754: }
 8755: 
 8756: sub possible_newspares {
 8757:     my ($server,$currspares,$serverhomes,$altids) = @_;
 8758:     my $serverhostname = &Apache::lonnet::hostname($server);
 8759:     my %excluded;
 8760:     if ($serverhostname ne '') {
 8761:         %excluded = (
 8762:                        $serverhostname => 1,
 8763:                     );
 8764:     }
 8765:     if (ref($currspares) eq 'HASH') {
 8766:         foreach my $type (keys(%{$currspares})) {
 8767:             if (ref($currspares->{$type}) eq 'ARRAY') {
 8768:                 if (@{$currspares->{$type}} > 0) {
 8769:                     foreach my $curr (@{$currspares->{$type}}) {
 8770:                         my $hostname = &Apache::lonnet::hostname($curr);
 8771:                         $excluded{$hostname} = 1;
 8772:                     }
 8773:                 }
 8774:             }
 8775:         }
 8776:     }
 8777:     my @choices;
 8778:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8779:         if (keys(%{$serverhomes}) > 1) {
 8780:             foreach my $name (sort(keys(%{$serverhomes}))) {
 8781:                 unless ($excluded{$name}) {
 8782:                     if (exists($altids->{$serverhomes->{$name}})) {
 8783:                         push(@choices,$altids->{$serverhomes->{$name}});
 8784:                     } else {
 8785:                         push(@choices,$serverhomes->{$name});
 8786:                     }
 8787:                 }
 8788:             }
 8789:         }
 8790:     }
 8791:     return sort(@choices);
 8792: }
 8793: 
 8794: sub print_loadbalancing {
 8795:     my ($dom,$settings,$rowtotal) = @_;
 8796:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8797:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8798:     my $numinrow = 1;
 8799:     my $datatable;
 8800:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8801:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 8802:     if (ref($settings) eq 'HASH') {
 8803:         %existing = %{$settings};
 8804:     }
 8805:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 8806:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8807:                                   \%currtargets,\%currrules,\%currcookies);
 8808:     } else {
 8809:         return;
 8810:     }
 8811:     my ($othertitle,$usertypes,$types) =
 8812:         &Apache::loncommon::sorted_inst_types($dom);
 8813:     my $rownum = 8;
 8814:     if (ref($types) eq 'ARRAY') {
 8815:         $rownum += scalar(@{$types});
 8816:     }
 8817:     my @css_class = ('LC_odd_row','LC_even_row');
 8818:     my $balnum = 0;
 8819:     my $islast;
 8820:     my (@toshow,$disabledtext);
 8821:     if (keys(%currbalancer) > 0) {
 8822:         @toshow = sort(keys(%currbalancer));
 8823:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 8824:             push(@toshow,'');
 8825:         }
 8826:     } else {
 8827:         @toshow = ('');
 8828:         $disabledtext = &mt('No existing load balancer');
 8829:     }
 8830:     foreach my $lonhost (@toshow) {
 8831:         if ($balnum == scalar(@toshow)-1) {
 8832:             $islast = 1;
 8833:         } else {
 8834:             $islast = 0;
 8835:         }
 8836:         my $cssidx = $balnum%2;
 8837:         my $targets_div_style = 'display: none';
 8838:         my $disabled_div_style = 'display: block';
 8839:         my $homedom_div_style = 'display: none';
 8840:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 8841:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8842:                       '<p>';
 8843:         if ($lonhost eq '') {
 8844:             $datatable .= '<span class="LC_nobreak">';
 8845:             if (keys(%currbalancer) > 0) {
 8846:                 $datatable .= &mt('Add balancer:');
 8847:             } else {
 8848:                 $datatable .= &mt('Enable balancer:');
 8849:             }
 8850:             $datatable .= '&nbsp;'.
 8851:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 8852:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 8853:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 8854:                           '<option value="" selected="selected">'.&mt('None').
 8855:                           '</option>'."\n";
 8856:             foreach my $server (sort(keys(%servers))) {
 8857:                 next if ($currbalancer{$server});
 8858:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 8859:             }
 8860:             $datatable .=
 8861:                 '</select>'."\n".
 8862:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 8863:         } else {
 8864:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 8865:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 8866:                            &mt('Stop balancing').'</label>'.
 8867:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 8868:             $targets_div_style = 'display: block';
 8869:             $disabled_div_style = 'display: none';
 8870:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 8871:                 $homedom_div_style = 'display: block';
 8872:             }
 8873:         }
 8874:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8875:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 8876:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 8877:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 8878:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 8879:         my @sparestypes = ('primary','default');
 8880:         my %typetitles = &sparestype_titles();
 8881:         my %hostherechecked = (
 8882:                                   no => ' checked="checked"',
 8883:                               );
 8884:         my %balcookiechecked = (
 8885:                                   no => ' checked="checked"', 
 8886:                                );
 8887:         foreach my $sparetype (@sparestypes) {
 8888:             my $targettable;
 8889:             for (my $i=0; $i<$numspares; $i++) {
 8890:                 my $checked;
 8891:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 8892:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8893:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8894:                             $checked = ' checked="checked"';
 8895:                         }
 8896:                     }
 8897:                 }
 8898:                 my ($chkboxval,$disabled);
 8899:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 8900:                     $chkboxval = $spares[$i];
 8901:                 }
 8902:                 if (exists($currbalancer{$spares[$i]})) {
 8903:                     $disabled = ' disabled="disabled"';
 8904:                 }
 8905:                 $targettable .=
 8906:                     '<td><span class="LC_nobreak"><label>'.
 8907:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 8908:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 8909:                     '</span></label></span></td>';
 8910:                 my $rem = $i%($numinrow);
 8911:                 if ($rem == 0) {
 8912:                     if (($i > 0) && ($i < $numspares-1)) {
 8913:                         $targettable .= '</tr>';
 8914:                     }
 8915:                     if ($i < $numspares-1) {
 8916:                         $targettable .= '<tr>';
 8917:                     }
 8918:                 }
 8919:             }
 8920:             if ($targettable ne '') {
 8921:                 my $rem = $numspares%($numinrow);
 8922:                 my $colsleft = $numinrow - $rem;
 8923:                 if ($colsleft > 1 ) {
 8924:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8925:                                     '&nbsp;</td>';
 8926:                 } elsif ($colsleft == 1) {
 8927:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 8928:                 }
 8929:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 8930:                                '<table><tr>'.$targettable.'</tr></table><br />';
 8931:             }
 8932:             $hostherechecked{$sparetype} = '';
 8933:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 8934:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8935:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8936:                         $hostherechecked{$sparetype} = ' checked="checked"';
 8937:                         $hostherechecked{'no'} = '';
 8938:                     }
 8939:                 }
 8940:             }
 8941:         }
 8942:         if ($currcookies{$lonhost}) {
 8943:             %balcookiechecked = (
 8944:                                     yes => ' checked="checked"',
 8945:                                 );
 8946:         }
 8947:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 8948:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 8949:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 8950:         foreach my $sparetype (@sparestypes) {
 8951:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 8952:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 8953:                           '</i></label><br />';
 8954:         }
 8955:         $datatable .= &mt('Use balancer cookie').'<br />'.
 8956:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 8957:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 8958:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 8959:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 8960:                       '</div></td></tr>'.
 8961:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 8962:                                            $othertitle,$usertypes,$types,\%servers,
 8963:                                            \%currbalancer,$lonhost,
 8964:                                            $targets_div_style,$homedom_div_style,
 8965:                                            $css_class[$cssidx],$balnum,$islast);
 8966:         $$rowtotal += $rownum;
 8967:         $balnum ++;
 8968:     }
 8969:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 8970:     return $datatable;
 8971: }
 8972: 
 8973: sub get_loadbalancers_config {
 8974:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 8975:     return unless ((ref($servers) eq 'HASH') &&
 8976:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 8977:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 8978:                    (ref($currcookies) eq 'HASH'));
 8979:     if (keys(%{$existing}) > 0) {
 8980:         my $oldlonhost;
 8981:         foreach my $key (sort(keys(%{$existing}))) {
 8982:             if ($key eq 'lonhost') {
 8983:                 $oldlonhost = $existing->{'lonhost'};
 8984:                 $currbalancer->{$oldlonhost} = 1;
 8985:             } elsif ($key eq 'targets') {
 8986:                 if ($oldlonhost) {
 8987:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 8988:                 }
 8989:             } elsif ($key eq 'rules') {
 8990:                 if ($oldlonhost) {
 8991:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 8992:                 }
 8993:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8994:                 $currbalancer->{$key} = 1;
 8995:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8996:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8997:                 if ($existing->{$key}{'cookie'}) {
 8998:                     $currcookies->{$key} = 1;
 8999:                 }
 9000:             }
 9001:         }
 9002:     } else {
 9003:         my ($balancerref,$targetsref) =
 9004:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9005:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9006:             foreach my $server (sort(keys(%{$balancerref}))) {
 9007:                 $currbalancer->{$server} = 1;
 9008:                 $currtargets->{$server} = $targetsref->{$server};
 9009:             }
 9010:         }
 9011:     }
 9012:     return;
 9013: }
 9014: 
 9015: sub loadbalancing_rules {
 9016:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9017:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9018:         $css_class,$balnum,$islast) = @_;
 9019:     my $output;
 9020:     my $num = 0;
 9021:     my ($alltypes,$othertypes,$titles) =
 9022:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9023:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9024:         foreach my $type (@{$alltypes}) {
 9025:             $num ++;
 9026:             my $current;
 9027:             if (ref($currrules) eq 'HASH') {
 9028:                 $current = $currrules->{$type};
 9029:             }
 9030:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9031:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9032:                     $current = '';
 9033:                 }
 9034:             }
 9035:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9036:                                              $servers,$currbalancer,$lonhost,$dom,
 9037:                                              $targets_div_style,$homedom_div_style,
 9038:                                              $css_class,$balnum,$num,$islast);
 9039:         }
 9040:     }
 9041:     return $output;
 9042: }
 9043: 
 9044: sub loadbalancing_titles {
 9045:     my ($dom,$intdom,$usertypes,$types) = @_;
 9046:     my %othertypes = (
 9047:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9048:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9049:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9050:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9051:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9052:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9053:                      );
 9054:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9055:     my @available;
 9056:     if (ref($types) eq 'ARRAY') {
 9057:         @available = @{$types};
 9058:     }
 9059:     unless (grep(/^default$/,@available)) {
 9060:         push(@available,'default');
 9061:     }
 9062:     unshift(@alltypes,@available);
 9063:     my %titles;
 9064:     foreach my $type (@alltypes) {
 9065:         if ($type =~ /^_LC_/) {
 9066:             $titles{$type} = $othertypes{$type};
 9067:         } elsif ($type eq 'default') {
 9068:             $titles{$type} = &mt('All users from [_1]',$dom);
 9069:             if (ref($types) eq 'ARRAY') {
 9070:                 if (@{$types} > 0) {
 9071:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9072:                 }
 9073:             }
 9074:         } elsif (ref($usertypes) eq 'HASH') {
 9075:             $titles{$type} = $usertypes->{$type};
 9076:         }
 9077:     }
 9078:     return (\@alltypes,\%othertypes,\%titles);
 9079: }
 9080: 
 9081: sub loadbalance_rule_row {
 9082:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9083:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9084:     my @rulenames;
 9085:     my %ruletitles = &offloadtype_text();
 9086:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9087:         @rulenames = ('balancer','offloadedto','specific');
 9088:     } else {
 9089:         @rulenames = ('default','homeserver');
 9090:         if ($type eq '_LC_external') {
 9091:             push(@rulenames,'externalbalancer');
 9092:         } else {
 9093:             push(@rulenames,'specific');
 9094:         }
 9095:         push(@rulenames,'none');
 9096:     }
 9097:     my $style = $targets_div_style;
 9098:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9099:         $style = $homedom_div_style;
 9100:     }
 9101:     my $space;
 9102:     if ($islast && $num == 1) {
 9103:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9104:     }
 9105:     my $output =
 9106:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9107:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9108:         '<td valaign="top">'.$space.
 9109:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9110:     for (my $i=0; $i<@rulenames; $i++) {
 9111:         my $rule = $rulenames[$i];
 9112:         my ($checked,$extra);
 9113:         if ($rulenames[$i] eq 'default') {
 9114:             $rule = '';
 9115:         }
 9116:         if ($rulenames[$i] eq 'specific') {
 9117:             if (ref($servers) eq 'HASH') {
 9118:                 my $default;
 9119:                 if (($current ne '') && (exists($servers->{$current}))) {
 9120:                     $checked = ' checked="checked"';
 9121:                 }
 9122:                 unless ($checked) {
 9123:                     $default = ' selected="selected"';
 9124:                 }
 9125:                 $extra =
 9126:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9127:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9128:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9129:                     '<option value=""'.$default.'></option>'."\n";
 9130:                 foreach my $server (sort(keys(%{$servers}))) {
 9131:                     if (ref($currbalancer) eq 'HASH') {
 9132:                         next if (exists($currbalancer->{$server}));
 9133:                     }
 9134:                     my $selected;
 9135:                     if ($server eq $current) {
 9136:                         $selected = ' selected="selected"';
 9137:                     }
 9138:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9139:                 }
 9140:                 $extra .= '</select>';
 9141:             }
 9142:         } elsif ($rule eq $current) {
 9143:             $checked = ' checked="checked"';
 9144:         }
 9145:         $output .= '<span class="LC_nobreak"><label>'.
 9146:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9147:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9148:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9149:                    ')"'.$checked.' />&nbsp;';
 9150:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9151:             $output .= $ruletitles{'particular'};
 9152:         } else {
 9153:             $output .= $ruletitles{$rulenames[$i]};
 9154:         }
 9155:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9156:     }
 9157:     $output .= '</div></td></tr>'."\n";
 9158:     return $output;
 9159: }
 9160: 
 9161: sub offloadtype_text {
 9162:     my %ruletitles = &Apache::lonlocal::texthash (
 9163:            'default'          => 'Offloads to default destinations',
 9164:            'homeserver'       => "Offloads to user's home server",
 9165:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9166:            'specific'         => 'Offloads to specific server',
 9167:            'none'             => 'No offload',
 9168:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9169:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9170:            'particular'       => 'Session hosted (after re-auth) on server:',
 9171:     );
 9172:     return %ruletitles;
 9173: }
 9174: 
 9175: sub sparestype_titles {
 9176:     my %typestitles = &Apache::lonlocal::texthash (
 9177:                           'primary' => 'primary',
 9178:                           'default' => 'default',
 9179:                       );
 9180:     return %typestitles;
 9181: }
 9182: 
 9183: sub contact_titles {
 9184:     my %titles = &Apache::lonlocal::texthash (
 9185:                    'supportemail'    => 'Support E-mail address',
 9186:                    'adminemail'      => 'Default Server Admin E-mail address',
 9187:                    'errormail'       => 'Error reports to be e-mailed to',
 9188:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9189:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9190:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9191:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9192:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9193:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9194:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9195:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9196:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9197:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9198:                    'errorweights'    => 'Weights used to compute error count',
 9199:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9200:                  );
 9201:     my %short_titles = &Apache::lonlocal::texthash (
 9202:                            adminemail   => 'Admin E-mail address',
 9203:                            supportemail => 'Support E-mail',
 9204:                        );   
 9205:     return (\%titles,\%short_titles);
 9206: }
 9207: 
 9208: sub helpform_fields {
 9209:     my %titles =  &Apache::lonlocal::texthash (
 9210:                        'username'   => 'Name',
 9211:                        'user'       => 'Username/domain',
 9212:                        'phone'      => 'Phone',
 9213:                        'cc'         => 'Cc e-mail',
 9214:                        'course'     => 'Course Details',
 9215:                        'section'    => 'Sections',
 9216:                        'screenshot' => 'File upload',
 9217:     );
 9218:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9219:     my %possoptions = (
 9220:                         username     => ['yes','no','req'],
 9221:                         phone        => ['yes','no','req'],
 9222:                         user         => ['yes','no'],
 9223:                         cc           => ['yes','no'],
 9224:                         course       => ['yes','no'],
 9225:                         section      => ['yes','no'],
 9226:                         screenshot   => ['yes','no'],
 9227:                       );
 9228:     my %fieldoptions = &Apache::lonlocal::texthash (
 9229:                          'yes'  => 'Optional',
 9230:                          'req'  => 'Required',
 9231:                          'no'   => "Not shown",
 9232:     );
 9233:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9234: }
 9235: 
 9236: sub tool_titles {
 9237:     my %titles = &Apache::lonlocal::texthash (
 9238:                      aboutme    => 'Personal web page',
 9239:                      blog       => 'Blog',
 9240:                      webdav     => 'WebDAV',
 9241:                      portfolio  => 'Portfolio',
 9242:                      official   => 'Official courses (with institutional codes)',
 9243:                      unofficial => 'Unofficial courses',
 9244:                      community  => 'Communities',
 9245:                      textbook   => 'Textbook courses',
 9246:                      placement  => 'Placement tests',
 9247:                  );
 9248:     return %titles;
 9249: }
 9250: 
 9251: sub courserequest_titles {
 9252:     my %titles = &Apache::lonlocal::texthash (
 9253:                                    official   => 'Official',
 9254:                                    unofficial => 'Unofficial',
 9255:                                    community  => 'Communities',
 9256:                                    textbook   => 'Textbook',
 9257:                                    placement  => 'Placement tests',
 9258:                                    lti        => 'LTI Provider',
 9259:                                    norequest  => 'Not allowed',
 9260:                                    approval   => 'Approval by DC',
 9261:                                    validate   => 'With validation',
 9262:                                    autolimit  => 'Numerical limit',
 9263:                                    unlimited  => '(blank for unlimited)',
 9264:                  );
 9265:     return %titles;
 9266: }
 9267: 
 9268: sub authorrequest_titles {
 9269:     my %titles = &Apache::lonlocal::texthash (
 9270:                                    norequest  => 'Not allowed',
 9271:                                    approval   => 'Approval by Dom. Coord.',
 9272:                                    automatic  => 'Automatic approval',
 9273:                  );
 9274:     return %titles;
 9275: }
 9276: 
 9277: sub courserequest_conditions {
 9278:     my %conditions = &Apache::lonlocal::texthash (
 9279:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9280:        validate   => '(Processing of request subject to institutional validation).',
 9281:                  );
 9282:     return %conditions;
 9283: }
 9284: 
 9285: 
 9286: sub print_usercreation {
 9287:     my ($position,$dom,$settings,$rowtotal) = @_;
 9288:     my $numinrow = 4;
 9289:     my $datatable;
 9290:     if ($position eq 'top') {
 9291:         $$rowtotal ++;
 9292:         my $rowcount = 0;
 9293:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9294:         if (ref($rules) eq 'HASH') {
 9295:             if (keys(%{$rules}) > 0) {
 9296:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9297:                                                 $ruleorder,$numinrow,$rowcount);
 9298:                 $$rowtotal ++;
 9299:                 $rowcount ++;
 9300:             }
 9301:         }
 9302:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9303:         if (ref($idrules) eq 'HASH') {
 9304:             if (keys(%{$idrules}) > 0) {
 9305:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9306:                                                 $idruleorder,$numinrow,$rowcount);
 9307:                 $$rowtotal ++;
 9308:                 $rowcount ++;
 9309:             }
 9310:         }
 9311:         if ($rowcount == 0) {
 9312:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9313:             $$rowtotal ++;
 9314:             $rowcount ++;
 9315:         }
 9316:     } elsif ($position eq 'middle') {
 9317:         my @creators = ('author','course','requestcrs');
 9318:         my ($rules,$ruleorder) =
 9319:             &Apache::lonnet::inst_userrules($dom,'username');
 9320:         my %lt = &usercreation_types();
 9321:         my %checked;
 9322:         if (ref($settings) eq 'HASH') {
 9323:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9324:                 foreach my $item (@creators) {
 9325:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9326:                 }
 9327:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9328:                 foreach my $item (@creators) {
 9329:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9330:                         $checked{$item} = 'none';
 9331:                     }
 9332:                 }
 9333:             }
 9334:         }
 9335:         my $rownum = 0;
 9336:         foreach my $item (@creators) {
 9337:             $rownum ++;
 9338:             if ($checked{$item} eq '') {
 9339:                 $checked{$item} = 'any';
 9340:             }
 9341:             my $css_class;
 9342:             if ($rownum%2) {
 9343:                 $css_class = '';
 9344:             } else {
 9345:                 $css_class = ' class="LC_odd_row" ';
 9346:             }
 9347:             $datatable .= '<tr'.$css_class.'>'.
 9348:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9349:                          '</span></td><td style="text-align: right">';
 9350:             my @options = ('any');
 9351:             if (ref($rules) eq 'HASH') {
 9352:                 if (keys(%{$rules}) > 0) {
 9353:                     push(@options,('official','unofficial'));
 9354:                 }
 9355:             }
 9356:             push(@options,'none');
 9357:             foreach my $option (@options) {
 9358:                 my $type = 'radio';
 9359:                 my $check = ' ';
 9360:                 if ($checked{$item} eq $option) {
 9361:                     $check = ' checked="checked" ';
 9362:                 } 
 9363:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9364:                               '<input type="'.$type.'" name="can_createuser_'.
 9365:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9366:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9367:             }
 9368:             $datatable .= '</td></tr>';
 9369:         }
 9370:     } else {
 9371:         my @contexts = ('author','course','domain');
 9372:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9373:         my %checked;
 9374:         if (ref($settings) eq 'HASH') {
 9375:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9376:                 foreach my $item (@contexts) {
 9377:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9378:                         foreach my $auth (@authtypes) {
 9379:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9380:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9381:                             }
 9382:                         }
 9383:                     }
 9384:                 }
 9385:             }
 9386:         } else {
 9387:             foreach my $item (@contexts) {
 9388:                 foreach my $auth (@authtypes) {
 9389:                     $checked{$item}{$auth} = ' checked="checked" ';
 9390:                 }
 9391:             }
 9392:         }
 9393:         my %title = &context_names();
 9394:         my %authname = &authtype_names();
 9395:         my $rownum = 0;
 9396:         my $css_class; 
 9397:         foreach my $item (@contexts) {
 9398:             if ($rownum%2) {
 9399:                 $css_class = '';
 9400:             } else {
 9401:                 $css_class = ' class="LC_odd_row" ';
 9402:             }
 9403:             $datatable .=   '<tr'.$css_class.'>'.
 9404:                             '<td>'.$title{$item}.
 9405:                             '</td><td class="LC_left_item">'.
 9406:                             '<span class="LC_nobreak">';
 9407:             foreach my $auth (@authtypes) {
 9408:                 $datatable .= '<label>'. 
 9409:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9410:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9411:                               $authname{$auth}.'</label>&nbsp;';
 9412:             }
 9413:             $datatable .= '</span></td></tr>';
 9414:             $rownum ++;
 9415:         }
 9416:         $$rowtotal += $rownum;
 9417:     }
 9418:     return $datatable;
 9419: }
 9420: 
 9421: sub print_selfcreation {
 9422:     my ($position,$dom,$settings,$rowtotal) = @_;
 9423:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9424:         $emaildomain,$datatable);
 9425:     if (ref($settings) eq 'HASH') {
 9426:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 9427:             $createsettings = $settings->{'cancreate'};
 9428:             if (ref($createsettings) eq 'HASH') {
 9429:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 9430:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 9431:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 9432:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 9433:                         @selfcreate = ('email','login','sso');
 9434:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 9435:                         @selfcreate = ($createsettings->{'selfcreate'});
 9436:                     }
 9437:                 }
 9438:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 9439:                     $processing = $createsettings->{'selfcreateprocessing'};
 9440:                 }
 9441:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 9442:                     $emailoptions = $createsettings->{'emailoptions'};
 9443:                 }
 9444:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 9445:                     $emailverified = $createsettings->{'emailverified'};
 9446:                 }
 9447:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 9448:                     $emaildomain = $createsettings->{'emaildomain'};
 9449:                 }
 9450:             }
 9451:         }
 9452:     }
 9453:     my %radiohash;
 9454:     my $numinrow = 4;
 9455:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 9456:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9457:     if ($position eq 'top') {
 9458:         my %choices = &Apache::lonlocal::texthash (
 9459:                                                       cancreate_login      => 'Institutional Login',
 9460:                                                       cancreate_sso        => 'Institutional Single Sign On',
 9461:                                                   );
 9462:         my @toggles = sort(keys(%choices));
 9463:         my %defaultchecked = (
 9464:                                'cancreate_login' => 'off',
 9465:                                'cancreate_sso'   => 'off',
 9466:                              );
 9467:         my ($onclick,$itemcount);
 9468:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9469:                                                      \%choices,$itemcount,$onclick);
 9470:         $$rowtotal += $itemcount;
 9471:         
 9472:         if (ref($usertypes) eq 'HASH') {
 9473:             if (keys(%{$usertypes}) > 0) {
 9474:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 9475:                                              $dom,$numinrow,$othertitle,
 9476:                                              'statustocreate',$rowtotal);
 9477:                 $$rowtotal ++;
 9478:             }
 9479:         }
 9480:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 9481:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9482:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 9483:         my $rem;
 9484:         my $numperrow = 2;
 9485:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 9486:         $datatable .= '<tr'.$css_class.'>'.
 9487:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 9488:                      '<td class="LC_left_item">'."\n".
 9489:                      '<table>'."\n";
 9490:         for (my $i=0; $i<@fields; $i++) {
 9491:             $rem = $i%($numperrow);
 9492:             if ($rem == 0) {
 9493:                 if ($i > 0) {
 9494:                     $datatable .= '</tr>';
 9495:                 }
 9496:                 $datatable .= '<tr>';
 9497:             }
 9498:             my $currval;
 9499:             if (ref($createsettings) eq 'HASH') {
 9500:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 9501:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 9502:                 }
 9503:             }
 9504:             $datatable .= '<td class="LC_left_item">'.
 9505:                           '<span class="LC_nobreak">'.
 9506:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 9507:                           'value="'.$currval.'" size="10" />&nbsp;'.
 9508:                           $fieldtitles{$fields[$i]}.'</span></td>';
 9509:         }
 9510:         my $colsleft = $numperrow - $rem;
 9511:         if ($colsleft > 1 ) {
 9512:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9513:                          '&nbsp;</td>';
 9514:         } elsif ($colsleft == 1) {
 9515:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 9516:         }
 9517:         $datatable .= '</tr></table></td></tr>';
 9518:         $$rowtotal ++;
 9519:     } elsif ($position eq 'middle') {
 9520:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 9521:         my @posstypes;
 9522:         if (ref($types) eq 'ARRAY') {
 9523:             @posstypes = @{$types};
 9524:         }
 9525:         unless (grep(/^default$/,@posstypes)) {
 9526:             push(@posstypes,'default');
 9527:         }
 9528:         my %usertypeshash;
 9529:         if (ref($usertypes) eq 'HASH') {
 9530:             %usertypeshash = %{$usertypes};
 9531:         }
 9532:         $usertypeshash{'default'} = $othertitle;
 9533:         foreach my $status (@posstypes) {
 9534:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 9535:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 9536:             $$rowtotal ++;
 9537:         }
 9538:     } else {
 9539:         my %choices = &Apache::lonlocal::texthash (
 9540:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 9541:                                                   );
 9542:         my @toggles = sort(keys(%choices));
 9543:         my %defaultchecked = (
 9544:                                'cancreate_email' => 'off',
 9545:                              );
 9546:         my $customclass = 'LC_selfcreate_email';
 9547:         my $classprefix = 'LC_canmodify_emailusername_';
 9548:         my $optionsprefix = 'LC_options_emailusername_';
 9549:         my $display = 'none';
 9550:         my $rowstyle = 'display:none';
 9551:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 9552:             $display = 'block';
 9553:             $rowstyle = 'display:table-row';
 9554:         }
 9555:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 9556:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9557:                                                      \%choices,$$rowtotal,$onclick);
 9558:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 9559:                                          $rowstyle);
 9560:         $$rowtotal ++;
 9561:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 9562:                                       $rowstyle);
 9563:         $$rowtotal ++;
 9564:         my (@ordered,@posstypes,%usertypeshash);
 9565:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9566:         my ($emailrules,$emailruleorder) =
 9567:             &Apache::lonnet::inst_userrules($dom,'email');
 9568:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9569:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9570:         if (ref($types) eq 'ARRAY') {
 9571:             @posstypes = @{$types};
 9572:         }
 9573:         if (@posstypes) {
 9574:             unless (grep(/^default$/,@posstypes)) {
 9575:                 push(@posstypes,'default');
 9576:             }
 9577:             if (ref($usertypes) eq 'HASH') {
 9578:                 %usertypeshash = %{$usertypes};
 9579:             }
 9580:             my $currassign;
 9581:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 9582:                 $currassign = {
 9583:                                   selfassign => $domdefaults{'inststatusguest'},
 9584:                               };
 9585:                 @ordered = @{$domdefaults{'inststatusguest'}};
 9586:             } else {
 9587:                 $currassign = { selfassign => [] };
 9588:             }
 9589:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 9590:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 9591:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 9592:                                          $numinrow,$othertitle,'selfassign',
 9593:                                          $rowtotal,$onclicktypes,$customclass,
 9594:                                          $rowstyle);
 9595:             $$rowtotal ++;
 9596:             $usertypeshash{'default'} = $othertitle;
 9597:             foreach my $status (@posstypes) {
 9598:                 my $css_class;
 9599:                 if ($$rowtotal%2) {
 9600:                     $css_class = 'LC_odd_row ';
 9601:                 }
 9602:                 $css_class .= $customclass;
 9603:                 my $rowid = $optionsprefix.$status;
 9604:                 my $hidden = 1;
 9605:                 my $currstyle = 'display:none';
 9606:                 if (grep(/^\Q$status\E$/,@ordered)) {
 9607:                     $currstyle = $rowstyle;
 9608:                     $hidden = 0; 
 9609:                 }
 9610:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9611:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 9612:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 9613:                 unless ($hidden) {
 9614:                     $$rowtotal ++;
 9615:                 }
 9616:             }
 9617:         } else {
 9618:             my $css_class;
 9619:             if ($$rowtotal%2) {
 9620:                 $css_class = 'LC_odd_row ';
 9621:             }
 9622:             $css_class .= $customclass;
 9623:             $usertypeshash{'default'} = $othertitle;
 9624:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9625:                                          $emailrules,$emailruleorder,$settings,'default','',
 9626:                                          $othertitle,$css_class,$rowstyle,$intdom);
 9627:             $$rowtotal ++;
 9628:         }
 9629:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9630:         $numinrow = 1;
 9631:         if (@posstypes) {
 9632:             foreach my $status (@posstypes) {
 9633:                 my $rowid = $classprefix.$status;
 9634:                 my $datarowstyle = 'display:none';
 9635:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 9636:                     $datarowstyle = $rowstyle; 
 9637:                 }
 9638:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 9639:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9640:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 9641:                 unless ($datarowstyle eq 'display:none') {
 9642:                     $$rowtotal ++;
 9643:                 }
 9644:             }
 9645:         } else {
 9646:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 9647:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9648:                                                    $infotitles,'',$customclass,$rowstyle);
 9649:         }
 9650:     }
 9651:     return $datatable;
 9652: }
 9653: 
 9654: sub selfcreate_javascript {
 9655:     return <<"ENDSCRIPT";
 9656: 
 9657: <script type="text/javascript">
 9658: // <![CDATA[
 9659: 
 9660: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 9661:     var x = document.getElementsByClassName(target);
 9662:     var insttypes = 0;
 9663:     var insttypeRegExp = new RegExp(prefix);
 9664:     if ((x.length != undefined) && (x.length > 0)) {
 9665:         if (form.elements[radio].length != undefined) {
 9666:             for (var i=0; i<form.elements[radio].length; i++) {
 9667:                 if (form.elements[radio][i].checked) {
 9668:                     if (form.elements[radio][i].value == 1) {
 9669:                         for (var j=0; j<x.length; j++) {
 9670:                             if (x[j].id == 'undefined') {
 9671:                                 x[j].style.display = 'table-row';
 9672:                             } else if (insttypeRegExp.test(x[j].id)) {
 9673:                                 insttypes ++;
 9674:                             } else {
 9675:                                 x[j].style.display = 'table-row';
 9676:                             }
 9677:                         }
 9678:                     } else {
 9679:                         for (var j=0; j<x.length; j++) {
 9680:                             x[j].style.display = 'none';
 9681:                         }
 9682:                     }
 9683:                     break;
 9684:                 }
 9685:             }
 9686:             if (insttypes > 0) {
 9687:                 toggleDataRow(form,checkbox,target,altprefix);
 9688:                 toggleDataRow(form,checkbox,target,prefix,1);
 9689:             }
 9690:         }
 9691:     }
 9692:     return;
 9693: }
 9694: 
 9695: function toggleDataRow(form,checkbox,target,prefix,docount) {
 9696:     if (form.elements[checkbox].length != undefined) {
 9697:         var count = 0;
 9698:         if (docount) {
 9699:             for (var i=0; i<form.elements[checkbox].length; i++) {
 9700:                 if (form.elements[checkbox][i].checked) {
 9701:                     count ++;
 9702:                 }
 9703:             }
 9704:         }
 9705:         for (var i=0; i<form.elements[checkbox].length; i++) {
 9706:             var type = form.elements[checkbox][i].value;
 9707:             if (document.getElementById(prefix+type)) {
 9708:                 if (form.elements[checkbox][i].checked) {
 9709:                     document.getElementById(prefix+type).style.display = 'table-row';
 9710:                     if (count % 2 == 1) {
 9711:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 9712:                     } else {
 9713:                         document.getElementById(prefix+type).className = target;
 9714:                     }
 9715:                     count ++;
 9716:                 } else {
 9717:                     document.getElementById(prefix+type).style.display = 'none';
 9718:                 }
 9719:             }
 9720:         }
 9721:     }
 9722:     return;
 9723: }
 9724: 
 9725: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 9726:     var caller = radio+'_'+status;
 9727:     if (form.elements[caller].length != undefined) {
 9728:         for (var i=0; i<form.elements[caller].length; i++) {
 9729:             if (form.elements[caller][i].checked) {
 9730:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 9731:                     var curr = form.elements[caller][i].value;
 9732:                     if (prefix) {
 9733:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 9734:                     }
 9735:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 9736:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 9737:                     if (curr == 'custom') {
 9738:                         if (prefix) { 
 9739:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 9740:                         }
 9741:                     } else if (curr == 'inst') {
 9742:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 9743:                     } else if (curr == 'noninst') {
 9744:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 9745:                     }
 9746:                     break;
 9747:                 }
 9748:             }
 9749:         }
 9750:     }
 9751: }
 9752: 
 9753: // ]]>
 9754: </script>
 9755: 
 9756: ENDSCRIPT
 9757: }
 9758: 
 9759: sub noninst_users {
 9760:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 9761:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 9762:     my $class = 'LC_left_item';
 9763:     if ($css_class) {
 9764:         $css_class = ' class="'.$css_class.'"'; 
 9765:     }
 9766:     if ($rowid) {
 9767:         $rowid = ' id="'.$rowid.'"';
 9768:     }
 9769:     if ($rowstyle) {
 9770:         $rowstyle = ' style="'.$rowstyle.'"';
 9771:     }
 9772:     my ($output,$description);
 9773:     if ($type eq 'default') {
 9774:         $description = &mt('Requests for: [_1]',$typetitle);
 9775:     } else {
 9776:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 9777:     }
 9778:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 9779:               "<td>$description</td>\n".           
 9780:               '<td class="'.$class.'" colspan="2">'.
 9781:               '<table><tr>';
 9782:     my %headers = &Apache::lonlocal::texthash( 
 9783:               approve  => 'Processing',
 9784:               email    => 'E-mail',
 9785:               username => 'Username',
 9786:     );
 9787:     foreach my $item ('approve','email','username') {
 9788:         $output .= '<th>'.$headers{$item}.'</th>';
 9789:     }
 9790:     $output .= '</tr><tr>';
 9791:     foreach my $item ('approve','email','username') {
 9792:         $output .= '<td style="vertical-align: top">';
 9793:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 9794:         if ($item eq 'approve') {
 9795:             %choices = &Apache::lonlocal::texthash (
 9796:                                                      automatic => 'Automatically approved',
 9797:                                                      approval  => 'Queued for approval',
 9798:                                                    );
 9799:             @options = ('automatic','approval');
 9800:             $hashref = $processing;
 9801:             $defoption = 'automatic';
 9802:             $name = 'cancreate_emailprocess_'.$type;
 9803:         } elsif ($item eq 'email') {
 9804:             %choices = &Apache::lonlocal::texthash (
 9805:                                                      any     => 'Any e-mail',
 9806:                                                      inst    => 'Institutional only',
 9807:                                                      noninst => 'Non-institutional only',
 9808:                                                      custom  => 'Custom restrictions',
 9809:                                                    );
 9810:             @options = ('any','inst','noninst');
 9811:             my $showcustom;
 9812:             if (ref($emailrules) eq 'HASH') {
 9813:                 if (keys(%{$emailrules}) > 0) {
 9814:                     push(@options,'custom');
 9815:                     $showcustom = 'cancreate_emailrule';
 9816:                     if (ref($settings) eq 'HASH') {
 9817:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 9818:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 9819:                                 if (exists($emailrules->{$rule})) {
 9820:                                     $hascustom ++;
 9821:                                 }
 9822:                             }
 9823:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 9824:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 9825:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 9826:                                     if (exists($emailrules->{$rule})) {
 9827:                                         $hascustom ++;
 9828:                                     }
 9829:                                 }
 9830:                             }
 9831:                         }
 9832:                     }
 9833:                 }
 9834:             }
 9835:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 9836:                                                      "'cancreate_emaildomain','$type'".');"';
 9837:             $hashref = $emailoptions;
 9838:             $defoption = 'any';
 9839:             $name = 'cancreate_emailoptions_'.$type;
 9840:         } elsif ($item eq 'username') {
 9841:             %choices = &Apache::lonlocal::texthash (
 9842:                                                      all    => 'Same as e-mail',
 9843:                                                      first  => 'Omit @domain',
 9844:                                                      free   => 'Free to choose',
 9845:                                                    );
 9846:             @options = ('all','first','free');
 9847:             $hashref = $emailverified;
 9848:             $defoption = 'all';
 9849:             $name = 'cancreate_usernameoptions_'.$type;
 9850:         }
 9851:         foreach my $option (@options) {
 9852:             my $checked;
 9853:             if (ref($hashref) eq 'HASH') {
 9854:                 if ($type eq '') {
 9855:                     if (!exists($hashref->{'default'})) {
 9856:                         if ($option eq $defoption) {
 9857:                             $checked = ' checked="checked"';
 9858:                         }
 9859:                     } else {
 9860:                         if ($hashref->{'default'} eq $option) {
 9861:                             $checked = ' checked="checked"';
 9862:                         }
 9863:                     }
 9864:                 } else {
 9865:                     if (!exists($hashref->{$type})) {
 9866:                         if ($option eq $defoption) {
 9867:                             $checked = ' checked="checked"';
 9868:                         }
 9869:                     } else {
 9870:                         if ($hashref->{$type} eq $option) {
 9871:                             $checked = ' checked="checked"';
 9872:                         }
 9873:                     }
 9874:                 }
 9875:             } elsif (($item eq 'email') && ($hascustom)) {
 9876:                 if ($option eq 'custom') {
 9877:                     $checked = ' checked="checked"';
 9878:                 }
 9879:             } elsif ($option eq $defoption) {
 9880:                 $checked = ' checked="checked"';
 9881:             }
 9882:             $output .= '<span class="LC_nobreak"><label>'.
 9883:                        '<input type="radio" name="'.$name.'"'.
 9884:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 9885:                        $choices{$option}.'</label></span><br />';
 9886:             if ($item eq 'email') {
 9887:                 if ($option eq 'custom') {
 9888:                     my $id = 'cancreate_emailrule_'.$type;
 9889:                     my $display = 'none';
 9890:                     if ($checked) {
 9891:                         $display = 'inline';
 9892:                     }
 9893:                     my $numinrow = 2;
 9894:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 9895:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 9896:                                &user_formats_row('email',$settings,$emailrules,
 9897:                                                  $emailruleorder,$numinrow,'',$type);
 9898:                               '</table></fieldset>';
 9899:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 9900:                     my %text = &Apache::lonlocal::texthash (
 9901:                                                              inst    => 'must end:',
 9902:                                                              noninst => 'cannot end:',
 9903:                                                            );
 9904:                     my $value;
 9905:                     if (ref($emaildomain) eq 'HASH') {
 9906:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 9907:                             $value = $emaildomain->{$type}->{$option}; 
 9908:                         }
 9909:                     }
 9910:                     if ($value eq '') {
 9911:                         $value = '@'.$intdom;
 9912:                     }
 9913:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 9914:                     my $display = 'none';
 9915:                     if ($checked) {
 9916:                         $display = 'inline';
 9917:                     }
 9918:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 9919:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 9920:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 9921:                                '</div>';
 9922:                 }
 9923:             }
 9924:         }
 9925:         $output .= '</td>'."\n";
 9926:     }
 9927:     $output .= "</tr></table></td></tr>\n";
 9928:     return $output;
 9929: }
 9930: 
 9931: sub captcha_choice {
 9932:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 9933:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 9934:         $vertext,$currver);
 9935:     my %lt = &captcha_phrases();
 9936:     $keyentry = 'hidden';
 9937:     my $colspan=2;
 9938:     if ($context eq 'cancreate') {
 9939:         $rowname = &mt('CAPTCHA validation');
 9940:     } elsif ($context eq 'login') {
 9941:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 9942:     } elsif ($context eq 'passwords') {
 9943:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 9944:         $colspan=1;
 9945:     }
 9946:     if (ref($settings) eq 'HASH') {
 9947:         if ($settings->{'captcha'}) {
 9948:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 9949:         } else {
 9950:             $checked{'original'} = ' checked="checked"';
 9951:         }
 9952:         if ($settings->{'captcha'} eq 'recaptcha') {
 9953:             $pubtext = $lt{'pub'};
 9954:             $privtext = $lt{'priv'};
 9955:             $keyentry = 'text';
 9956:             $vertext = $lt{'ver'};
 9957:             $currver = $settings->{'recaptchaversion'};
 9958:             if ($currver ne '2') {
 9959:                 $currver = 1;
 9960:             }
 9961:         }
 9962:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 9963:             $currpub = $settings->{'recaptchakeys'}{'public'};
 9964:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 9965:         }
 9966:     } else {
 9967:         $checked{'original'} = ' checked="checked"';
 9968:     }
 9969:     my $css_class;
 9970:     if ($itemcount%2) {
 9971:         $css_class = 'LC_odd_row';
 9972:     }
 9973:     if ($customcss) {
 9974:         $css_class .= " $customcss";
 9975:     }
 9976:     $css_class =~ s/^\s+//;
 9977:     if ($css_class) {
 9978:         $css_class = ' class="'.$css_class.'"';
 9979:     }
 9980:     if ($rowstyle) {
 9981:         $css_class .= ' style="'.$rowstyle.'"';
 9982:     }
 9983:     my $output = '<tr'.$css_class.'>'.
 9984:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 9985:                  '<table><tr><td>'."\n";
 9986:     foreach my $option ('original','recaptcha','notused') {
 9987:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 9988:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 9989:                    $lt{$option}.'</label></span>';
 9990:         unless ($option eq 'notused') {
 9991:             $output .= ('&nbsp;'x2)."\n";
 9992:         }
 9993:     }
 9994: #
 9995: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9996: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9997: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9998: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9999: #
10000:     $output .= '</td></tr>'."\n".
10001:                '<tr><td class="LC_zero_height">'."\n".
10002:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10003:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10004:                $currpub.'" size="40" /></span><br />'."\n".
10005:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10006:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10007:                $currpriv.'" size="40" /></span><br />'.
10008:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10009:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10010:                $currver.'" size="3" /></span><br />'.
10011:                '</td></tr></table>'."\n".
10012:                '</td></tr>';
10013:     return $output;
10014: }
10015: 
10016: sub user_formats_row {
10017:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10018:     my $output;
10019:     my %text = (
10020:                    'username' => 'new usernames',
10021:                    'id'       => 'IDs',
10022:                );
10023:     unless ($type eq 'email') {
10024:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10025:         $output = '<tr '.$css_class.'>'.
10026:                   '<td><span class="LC_nobreak">'.
10027:                   &mt("Format rules to check for $text{$type}: ").
10028:                   '</td><td class="LC_left_item" colspan="2"><table>';
10029:     }
10030:     my $rem;
10031:     if (ref($ruleorder) eq 'ARRAY') {
10032:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10033:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10034:                 my $rem = $i%($numinrow);
10035:                 if ($rem == 0) {
10036:                     if ($i > 0) {
10037:                         $output .= '</tr>';
10038:                     }
10039:                     $output .= '<tr>';
10040:                 }
10041:                 my $check = ' ';
10042:                 if (ref($settings) eq 'HASH') {
10043:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10044:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10045:                             $check = ' checked="checked" ';
10046:                         }
10047:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10048:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10049:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10050:                                 $check = ' checked="checked" ';
10051:                             }
10052:                         }
10053:                     }
10054:                 }
10055:                 my $name = $type.'_rule';
10056:                 if ($type eq 'email') {
10057:                     $name .= '_'.$status;
10058:                 }
10059:                 $output .= '<td class="LC_left_item">'.
10060:                            '<span class="LC_nobreak"><label>'.
10061:                            '<input type="checkbox" name="'.$name.'" '.
10062:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10063:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10064:             }
10065:         }
10066:         $rem = @{$ruleorder}%($numinrow);
10067:     }
10068:     my $colsleft;
10069:     if ($rem) {
10070:         $colsleft = $numinrow - $rem;
10071:     }
10072:     if ($colsleft > 1 ) {
10073:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10074:                    '&nbsp;</td>';
10075:     } elsif ($colsleft == 1) {
10076:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10077:     }
10078:     $output .= '</tr></table>';
10079:     unless ($type eq 'email') {
10080:         $output .= '</td></tr>';
10081:     }
10082:     return $output;
10083: }
10084: 
10085: sub usercreation_types {
10086:     my %lt = &Apache::lonlocal::texthash (
10087:                     author     => 'When adding a co-author',
10088:                     course     => 'When adding a user to a course',
10089:                     requestcrs => 'When requesting a course',
10090:                     any        => 'Any',
10091:                     official   => 'Institutional only ',
10092:                     unofficial => 'Non-institutional only',
10093:                     none       => 'None',
10094:     );
10095:     return %lt;
10096: }
10097: 
10098: sub selfcreation_types {
10099:     my %lt = &Apache::lonlocal::texthash (
10100:                     selfcreate => 'User creates own account',
10101:                     any        => 'Any',
10102:                     official   => 'Institutional only ',
10103:                     unofficial => 'Non-institutional only',
10104:                     email      => 'E-mail address',
10105:                     login      => 'Institutional Login',
10106:                     sso        => 'SSO',
10107:              );
10108: }
10109: 
10110: sub authtype_names {
10111:     my %lt = &Apache::lonlocal::texthash(
10112:                       int    => 'Internal',
10113:                       krb4   => 'Kerberos 4',
10114:                       krb5   => 'Kerberos 5',
10115:                       loc    => 'Local',
10116:                       lti    => 'LTI',
10117:                   );
10118:     return %lt;
10119: }
10120: 
10121: sub context_names {
10122:     my %context_title = &Apache::lonlocal::texthash(
10123:        author => 'Creating users when an Author',
10124:        course => 'Creating users when in a course',
10125:        domain => 'Creating users when a Domain Coordinator',
10126:     );
10127:     return %context_title;
10128: }
10129: 
10130: sub print_usermodification {
10131:     my ($position,$dom,$settings,$rowtotal) = @_;
10132:     my $numinrow = 4;
10133:     my ($context,$datatable,$rowcount);
10134:     if ($position eq 'top') {
10135:         $rowcount = 0;
10136:         $context = 'author'; 
10137:         foreach my $role ('ca','aa') {
10138:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10139:                                                    $numinrow,$rowcount);
10140:             $$rowtotal ++;
10141:             $rowcount ++;
10142:         }
10143:     } elsif ($position eq 'bottom') {
10144:         $context = 'course';
10145:         $rowcount = 0;
10146:         foreach my $role ('st','ep','ta','in','cr') {
10147:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10148:                                                    $numinrow,$rowcount);
10149:             $$rowtotal ++;
10150:             $rowcount ++;
10151:         }
10152:     }
10153:     return $datatable;
10154: }
10155: 
10156: sub print_defaults {
10157:     my ($position,$dom,$settings,$rowtotal) = @_;
10158:     my $rownum = 0;
10159:     my ($datatable,$css_class,$titles);
10160:     unless ($position eq 'bottom') {
10161:         $titles = &defaults_titles($dom);
10162:     }
10163:     if ($position eq 'top') {
10164:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10165:                      'datelocale_def','portal_def');
10166:         my %defaults;
10167:         if (ref($settings) eq 'HASH') {
10168:             %defaults = %{$settings};
10169:         } else {
10170:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10171:             foreach my $item (@items) {
10172:                 $defaults{$item} = $domdefaults{$item};
10173:             }
10174:         }
10175:         foreach my $item (@items) {
10176:             if ($rownum%2) {
10177:                 $css_class = '';
10178:             } else {
10179:                 $css_class = ' class="LC_odd_row" ';
10180:             }
10181:             $datatable .= '<tr'.$css_class.'>'.
10182:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10183:                           '</span></td><td class="LC_right_item" colspan="3">';
10184:             if ($item eq 'auth_def') {
10185:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10186:                 my %shortauth = (
10187:                                  internal => 'int',
10188:                                  krb4 => 'krb4',
10189:                                  krb5 => 'krb5',
10190:                                  localauth  => 'loc',
10191:                                  lti => 'lti',
10192:                                 );
10193:                 my %authnames = &authtype_names();
10194:                 foreach my $auth (@authtypes) {
10195:                     my $checked = ' ';
10196:                     if ($defaults{$item} eq $auth) {
10197:                         $checked = ' checked="checked" ';
10198:                     }
10199:                     $datatable .= '<label><input type="radio" name="'.$item.
10200:                                   '" value="'.$auth.'"'.$checked.'/>'.
10201:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10202:                 }
10203:             } elsif ($item eq 'timezone_def') {
10204:                 my $includeempty = 1;
10205:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10206:             } elsif ($item eq 'datelocale_def') {
10207:                 my $includeempty = 1;
10208:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10209:             } elsif ($item eq 'lang_def') {
10210:                 my $includeempty = 1;
10211:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10212:             } else {
10213:                 my $size;
10214:                 if ($item eq 'portal_def') {
10215:                     $size = ' size="25"';
10216:                 }
10217:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10218:                               $defaults{$item}.'"'.$size.' />';
10219:             }
10220:             $datatable .= '</td></tr>';
10221:             $rownum ++;
10222:         }
10223:     } else {
10224:         my %defaults;
10225:         if (ref($settings) eq 'HASH') {
10226:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10227:                 my $maxnum = @{$settings->{'inststatusorder'}};
10228:                 for (my $i=0; $i<$maxnum; $i++) {
10229:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10230:                     my $item = $settings->{'inststatusorder'}->[$i];
10231:                     my $title = $settings->{'inststatustypes'}->{$item};
10232:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10233:                     $datatable .= '<tr'.$css_class.'>'.
10234:                                   '<td><span class="LC_nobreak">'.
10235:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10236:                     for (my $k=0; $k<=$maxnum; $k++) {
10237:                         my $vpos = $k+1;
10238:                         my $selstr;
10239:                         if ($k == $i) {
10240:                             $selstr = ' selected="selected" ';
10241:                         }
10242:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10243:                     }
10244:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10245:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10246:                                   &mt('delete').'</span></td>'.
10247:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10248:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10249:                                   '</span></td></tr>';
10250:                 }
10251:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10252:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10253:                 $datatable .= '<tr '.$css_class.'>'.
10254:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10255:                 for (my $k=0; $k<=$maxnum; $k++) {
10256:                     my $vpos = $k+1;
10257:                     my $selstr;
10258:                     if ($k == $maxnum) {
10259:                         $selstr = ' selected="selected" ';
10260:                     }
10261:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10262:                 }
10263:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10264:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10265:                               '&nbsp;'.&mt('(new)').
10266:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10267:                               &mt('Name displayed').':'.
10268:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10269:                               '</tr>'."\n";
10270:                 $rownum ++;
10271:             }
10272:         }
10273:     }
10274:     $$rowtotal += $rownum;
10275:     return $datatable;
10276: }
10277: 
10278: sub get_languages_hash {
10279:     my %langchoices;
10280:     foreach my $id (&Apache::loncommon::languageids()) {
10281:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10282:         if ($code ne '') {
10283:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10284:         }
10285:     }
10286:     return %langchoices;
10287: }
10288: 
10289: sub defaults_titles {
10290:     my ($dom) = @_;
10291:     my %titles = &Apache::lonlocal::texthash (
10292:                    'auth_def'      => 'Default authentication type',
10293:                    'auth_arg_def'  => 'Default authentication argument',
10294:                    'lang_def'      => 'Default language',
10295:                    'timezone_def'  => 'Default timezone',
10296:                    'datelocale_def' => 'Default locale for dates',
10297:                    'portal_def'     => 'Portal/Default URL',
10298:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10299:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10300:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10301:                  );
10302:     if ($dom) {
10303:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10304:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10305:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10306:         $protocol = 'http' if ($protocol ne 'https');
10307:         if ($uint_dom) {
10308:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10309:                                          $uint_dom);
10310:         }
10311:     }
10312:     return (\%titles);
10313: }
10314: 
10315: sub print_scantron {
10316:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10317:     if ($position eq 'top') {
10318:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10319:     } else {
10320:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10321:     }
10322: }
10323: 
10324: sub scantron_javascript {
10325:     return <<"ENDSCRIPT";
10326: 
10327: <script type="text/javascript">
10328: // <![CDATA[
10329: 
10330: function toggleScantron(form) {
10331:     var csvfieldset = new Array();
10332:     if (document.getElementById('scantroncsv_cols')) {
10333:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10334:     }
10335:     if (document.getElementById('scantroncsv_options')) {
10336:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10337:     }
10338:     if (csvfieldset.length) {
10339:         if (document.getElementById('scantronconfcsv')) {
10340:             var scantroncsv = document.getElementById('scantronconfcsv');
10341:             if (scantroncsv.checked) {
10342:                 for (var i=0; i<csvfieldset.length; i++) {
10343:                     csvfieldset[i].style.display = 'block';
10344:                 }
10345:             } else {
10346:                 for (var i=0; i<csvfieldset.length; i++) {
10347:                     csvfieldset[i].style.display = 'none';
10348:                 }
10349:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10350:                 if (csvselects.length) {
10351:                     for (var j=0; j<csvselects.length; j++) {
10352:                         csvselects[j].selectedIndex = 0;
10353:                     }
10354:                 }
10355:             }
10356:         }
10357:     }
10358:     return;
10359: }
10360: // ]]>
10361: </script>
10362: 
10363: ENDSCRIPT
10364: 
10365: }
10366: 
10367: sub print_scantronformat {
10368:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10369:     my $itemcount = 1;
10370:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10371:         %confhash);
10372:     my $switchserver = &check_switchserver($dom,$confname);
10373:     my %lt = &Apache::lonlocal::texthash (
10374:                 default => 'Default bubblesheet format file error',
10375:                 custom  => 'Custom bubblesheet format file error',
10376:              );
10377:     my %scantronfiles = (
10378:         default => 'default.tab',
10379:         custom => 'custom.tab',
10380:     );
10381:     foreach my $key (keys(%scantronfiles)) {
10382:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10383:                               .$scantronfiles{$key};
10384:     }
10385:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10386:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10387:         if (!$switchserver) {
10388:             my $servadm = $r->dir_config('lonAdmEMail');
10389:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10390:             if ($configuserok eq 'ok') {
10391:                 if ($author_ok eq 'ok') {
10392:                     my %legacyfile = (
10393:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
10394:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
10395:                     );
10396:                     my %md5chk;
10397:                     foreach my $type (keys(%legacyfile)) {
10398:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
10399:                         chomp($md5chk{$type});
10400:                     }
10401:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
10402:                         foreach my $type (keys(%legacyfile)) {
10403:                             ($scantronurls{$type},my $error) =
10404:                                 &legacy_scantronformat($r,$dom,$confname,
10405:                                                  $type,$legacyfile{$type},
10406:                                                  $scantronurls{$type},
10407:                                                  $scantronfiles{$type});
10408:                             if ($error ne '') {
10409:                                 $error{$type} = $error;
10410:                             }
10411:                         }
10412:                         if (keys(%error) == 0) {
10413:                             $is_custom = 1;
10414:                             $confhash{'scantron'}{'scantronformat'} =
10415:                                 $scantronurls{'custom'};
10416:                             my $putresult =
10417:                                 &Apache::lonnet::put_dom('configuration',
10418:                                                          \%confhash,$dom);
10419:                             if ($putresult ne 'ok') {
10420:                                 $error{'custom'} =
10421:                                     '<span class="LC_error">'.
10422:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10423:                             }
10424:                         }
10425:                     } else {
10426:                         ($scantronurls{'default'},my $error) =
10427:                             &legacy_scantronformat($r,$dom,$confname,
10428:                                           'default',$legacyfile{'default'},
10429:                                           $scantronurls{'default'},
10430:                                           $scantronfiles{'default'});
10431:                         if ($error eq '') {
10432:                             $confhash{'scantron'}{'scantronformat'} = ''; 
10433:                             my $putresult =
10434:                                 &Apache::lonnet::put_dom('configuration',
10435:                                                          \%confhash,$dom);
10436:                             if ($putresult ne 'ok') {
10437:                                 $error{'default'} =
10438:                                     '<span class="LC_error">'.
10439:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10440:                             }
10441:                         } else {
10442:                             $error{'default'} = $error;
10443:                         }
10444:                     }
10445:                 }
10446:             }
10447:         } else {
10448:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
10449:         }
10450:     }
10451:     if (ref($settings) eq 'HASH') {
10452:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
10453:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
10454:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
10455:                 $scantronurl = '';
10456:             } else {
10457:                 $scantronurl = $settings->{'scantronformat'};
10458:             }
10459:             $is_custom = 1;
10460:         } else {
10461:             $scantronurl = $scantronurls{'default'};
10462:         }
10463:     } else {
10464:         if ($is_custom) {
10465:             $scantronurl = $scantronurls{'custom'};
10466:         } else {
10467:             $scantronurl = $scantronurls{'default'};
10468:         }
10469:     }
10470:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10471:     $datatable .= '<tr'.$css_class.'>';
10472:     if (!$is_custom) {
10473:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
10474:                       '<span class="LC_nobreak">';
10475:         if ($scantronurl) {
10476:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
10477:                                                          undef,undef,undef,undef,'background-color:#ffffff');
10478:         } else {
10479:             $datatable = &mt('File unavailable for display');
10480:         }
10481:         $datatable .= '</span></td>';
10482:         if (keys(%error) == 0) { 
10483:             $datatable .= '<td style="vertical-align: bottom">';
10484:             if (!$switchserver) {
10485:                 $datatable .= &mt('Upload:').'<br />';
10486:             }
10487:         } else {
10488:             my $errorstr;
10489:             foreach my $key (sort(keys(%error))) {
10490:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10491:             }
10492:             $datatable .= '<td>'.$errorstr;
10493:         }
10494:     } else {
10495:         if (keys(%error) > 0) {
10496:             my $errorstr;
10497:             foreach my $key (sort(keys(%error))) {
10498:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10499:             } 
10500:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
10501:         } elsif ($scantronurl) {
10502:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
10503:                                                        undef,undef,undef,undef,'background-color:#ffffff');
10504:             $datatable .= '<td><span class="LC_nobreak">'.
10505:                           $link.
10506:                           '<label><input type="checkbox" name="scantronformat_del"'.
10507:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
10508:                           '<td><span class="LC_nobreak">&nbsp;'.
10509:                           &mt('Replace:').'</span><br />';
10510:         }
10511:     }
10512:     if (keys(%error) == 0) {
10513:         if ($switchserver) {
10514:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
10515:         } else {
10516:             $datatable .='<span class="LC_nobreak">&nbsp;'.
10517:                          '<input type="file" name="scantronformat" /></span>';
10518:         }
10519:     }
10520:     $datatable .= '</td></tr>';
10521:     $$rowtotal ++;
10522:     return $datatable;
10523: }
10524: 
10525: sub legacy_scantronformat {
10526:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
10527:     my ($url,$error);
10528:     my @statinfo = &Apache::lonnet::stat_file($newurl);
10529:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
10530:         (my $result,$url) =
10531:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
10532:                          '','',$newfile);
10533:         if ($result ne 'ok') {
10534:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
10535:         }
10536:     }
10537:     return ($url,$error);
10538: }
10539: 
10540: sub print_scantronconfig {
10541:     my ($dom,$settings,$rowtotal) = @_;
10542:     my $itemcount = 2;
10543:     my $is_checked = ' checked="checked"';
10544:     my %optionson = (
10545:                      hdr => ' checked="checked"',
10546:                      pad => ' checked="checked"',
10547:                      rem => ' checked="checked"',
10548:                     );
10549:     my %optionsoff = (
10550:                       hdr => '',
10551:                       pad => '',
10552:                       rem => '',
10553:                      );
10554:     my $currcsvsty = 'none';
10555:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
10556:     my @fields = &scantroncsv_fields();
10557:     my %titles = &scantronconfig_titles();
10558:     if (ref($settings) eq 'HASH') {
10559:         if (ref($settings->{config}) eq 'HASH') {
10560:             if ($settings->{config}->{dat}) {
10561:                 $checked{'dat'} = $is_checked;
10562:             }
10563:             if (ref($settings->{config}->{csv}) eq 'HASH') {
10564:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
10565:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
10566:                     if (keys(%csvfields) > 0) {
10567:                         $checked{'csv'} = $is_checked;
10568:                         $currcsvsty = 'block';
10569:                     }
10570:                 }
10571:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
10572:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
10573:                     foreach my $option (keys(%optionson)) {
10574:                         unless ($csvoptions{$option}) {
10575:                             $optionsoff{$option} = $optionson{$option};
10576:                             $optionson{$option} = '';
10577:                         }
10578:                     }
10579:                 }
10580:             }
10581:         } else {
10582:             $checked{'dat'} = $is_checked;
10583:         }
10584:     } else {
10585:         $checked{'dat'} = $is_checked;
10586:     }
10587:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
10588:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10589:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
10590:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
10591:     foreach my $item ('dat','csv') {
10592:         my $id;
10593:         if ($item eq 'csv') {
10594:             $id = 'id="scantronconfcsv" ';
10595:         }
10596:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
10597:                       $titles{$item}.'</label>'.('&nbsp;'x3);
10598:         if ($item eq 'csv') {
10599:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
10600:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
10601:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
10602:             foreach my $col (@fields) {
10603:                 my $selnone;
10604:                 if ($csvfields{$col} eq '') {
10605:                     $selnone = ' selected="selected"';
10606:                 }
10607:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
10608:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
10609:                               '<option value=""'.$selnone.'></option>';
10610:                 for (my $i=0; $i<20; $i++) {
10611:                     my $shown = $i+1;
10612:                     my $sel;
10613:                     unless ($selnone) {
10614:                         if (exists($csvfields{$col})) {
10615:                             if ($csvfields{$col} == $i) {
10616:                                 $sel = ' selected="selected"';
10617:                             }
10618:                         }
10619:                     }
10620:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
10621:                 }
10622:                 $datatable .= '</select></td></tr>';
10623:            }
10624:            $datatable .= '</table></fieldset>'.
10625:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
10626:                          '<legend>'.&mt('CSV Options').'</legend>';
10627:            foreach my $option ('hdr','pad','rem') {
10628:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
10629:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
10630:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
10631:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
10632:            }
10633:            $datatable .= '</fieldset>';
10634:            $itemcount ++;
10635:         }
10636:     }
10637:     $datatable .= '</td></tr>';
10638:     $$rowtotal ++;
10639:     return $datatable;
10640: }
10641: 
10642: sub scantronconfig_titles {
10643:     return &Apache::lonlocal::texthash(
10644:                                           dat => 'Standard format (.dat)',
10645:                                           csv => 'Comma separated values (.csv)',
10646:                                           hdr => 'Remove first line in file (contains column titles)',
10647:                                           pad => 'Prepend 0s to PaperID',
10648:                                           rem => 'Remove leading spaces (except Question Response columns)',
10649:                                           CODE => 'CODE',
10650:                                           ID   => 'Student ID',
10651:                                           PaperID => 'Paper ID',
10652:                                           FirstName => 'First Name',
10653:                                           LastName => 'Last Name',
10654:                                           FirstQuestion => 'First Question Response',
10655:                                           Section => 'Section',
10656:     );
10657: }
10658: 
10659: sub scantroncsv_fields {
10660:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
10661: }
10662: 
10663: sub print_coursecategories {
10664:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
10665:     my $datatable;
10666:     if ($position eq 'top') {
10667:         my (%checked);
10668:         my @catitems = ('unauth','auth');
10669:         my @cattypes = ('std','domonly','codesrch','none');
10670:         $checked{'unauth'} = 'std';
10671:         $checked{'auth'} = 'std';
10672:         if (ref($settings) eq 'HASH') {
10673:             foreach my $type (@cattypes) {
10674:                 if ($type eq $settings->{'unauth'}) {
10675:                     $checked{'unauth'} = $type;
10676:                 }
10677:                 if ($type eq $settings->{'auth'}) {
10678:                     $checked{'auth'} = $type;
10679:                 }
10680:             }
10681:         }
10682:         my %lt = &Apache::lonlocal::texthash (
10683:                                                unauth   => 'Catalog type for unauthenticated users',
10684:                                                auth     => 'Catalog type for authenticated users',
10685:                                                none     => 'No catalog',
10686:                                                std      => 'Standard catalog',
10687:                                                domonly  => 'Domain-only catalog',
10688:                                                codesrch => "Code search form",
10689:                                              );
10690:        my $itemcount = 0;
10691:        foreach my $item (@catitems) {
10692:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10693:            $datatable .= '<tr '.$css_class.'>'.
10694:                          '<td>'.$lt{$item}.'</td>'.
10695:                          '<td class="LC_right_item"><span class="LC_nobreak">';
10696:            foreach my $type (@cattypes) {
10697:                my $ischecked;
10698:                if ($checked{$item} eq $type) {
10699:                    $ischecked=' checked="checked"';
10700:                }
10701:                $datatable .= '<label>'.
10702:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
10703:                              ' />'.$lt{$type}.'</label>&nbsp;';
10704:            }
10705:            $datatable .= '</span></td></tr>';
10706:            $itemcount ++;
10707:         }
10708:         $$rowtotal += $itemcount;
10709:     } elsif ($position eq 'middle') {
10710:         my $toggle_cats_crs = ' ';
10711:         my $toggle_cats_dom = ' checked="checked" ';
10712:         my $can_cat_crs = ' ';
10713:         my $can_cat_dom = ' checked="checked" ';
10714:         my $toggle_catscomm_comm = ' ';
10715:         my $toggle_catscomm_dom = ' checked="checked" ';
10716:         my $can_catcomm_comm = ' ';
10717:         my $can_catcomm_dom = ' checked="checked" ';
10718:         my $toggle_catsplace_place = ' ';
10719:         my $toggle_catsplace_dom = ' checked="checked" ';
10720:         my $can_catplace_place = ' ';
10721:         my $can_catplace_dom = ' checked="checked" ';
10722: 
10723:         if (ref($settings) eq 'HASH') {
10724:             if ($settings->{'togglecats'} eq 'crs') {
10725:                 $toggle_cats_crs = $toggle_cats_dom;
10726:                 $toggle_cats_dom = ' ';
10727:             }
10728:             if ($settings->{'categorize'} eq 'crs') {
10729:                 $can_cat_crs = $can_cat_dom;
10730:                 $can_cat_dom = ' ';
10731:             }
10732:             if ($settings->{'togglecatscomm'} eq 'comm') {
10733:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
10734:                 $toggle_catscomm_dom = ' ';
10735:             }
10736:             if ($settings->{'categorizecomm'} eq 'comm') {
10737:                 $can_catcomm_comm = $can_catcomm_dom;
10738:                 $can_catcomm_dom = ' ';
10739:             }
10740:             if ($settings->{'togglecatsplace'} eq 'place') {
10741:                 $toggle_catsplace_place = $toggle_catsplace_dom;
10742:                 $toggle_catsplace_dom = ' ';
10743:             }
10744:             if ($settings->{'categorizeplace'} eq 'place') {
10745:                 $can_catplace_place = $can_catplace_dom;
10746:                 $can_catplace_dom = ' ';
10747:             }
10748:         }
10749:         my %title = &Apache::lonlocal::texthash (
10750:                      togglecats      => 'Show/Hide a course in catalog',
10751:                      togglecatscomm  => 'Show/Hide a community in catalog',
10752:                      togglecatsplace => 'Show/Hide a placement test in catalog',
10753:                      categorize      => 'Assign a category to a course',
10754:                      categorizecomm  => 'Assign a category to a community',
10755:                      categorizeplace => 'Assign a category to a placement test',
10756:                     );
10757:         my %level = &Apache::lonlocal::texthash (
10758:                      dom   => 'Set in Domain',
10759:                      crs   => 'Set in Course',
10760:                      comm  => 'Set in Community',
10761:                      place => 'Set in Placement Test',
10762:                     );
10763:         $datatable = '<tr class="LC_odd_row">'.
10764:                   '<td>'.$title{'togglecats'}.'</td>'.
10765:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10766:                   '<input type="radio" name="togglecats"'.
10767:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10768:                   '<label><input type="radio" name="togglecats"'.
10769:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
10770:                   '</tr><tr>'.
10771:                   '<td>'.$title{'categorize'}.'</td>'.
10772:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10773:                   '<label><input type="radio" name="categorize"'.
10774:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10775:                   '<label><input type="radio" name="categorize"'.
10776:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
10777:                   '</tr><tr class="LC_odd_row">'.
10778:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
10779:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10780:                   '<input type="radio" name="togglecatscomm"'.
10781:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10782:                   '<label><input type="radio" name="togglecatscomm"'.
10783:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
10784:                   '</tr><tr>'.
10785:                   '<td>'.$title{'categorizecomm'}.'</td>'.
10786:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10787:                   '<label><input type="radio" name="categorizecomm"'.
10788:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10789:                   '<label><input type="radio" name="categorizecomm"'.
10790:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
10791:                   '</tr><tr class="LC_odd_row">'.
10792:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
10793:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10794:                   '<input type="radio" name="togglecatsplace"'.
10795:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10796:                   '<label><input type="radio" name="togglecatscomm"'.
10797:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
10798:                   '</tr><tr>'.
10799:                   '<td>'.$title{'categorizeplace'}.'</td>'.
10800:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10801:                   '<label><input type="radio" name="categorizeplace"'.
10802:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10803:                   '<label><input type="radio" name="categorizeplace"'.
10804:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
10805:                   '</tr>';
10806:         $$rowtotal += 6;
10807:     } else {
10808:         my $css_class;
10809:         my $itemcount = 1;
10810:         my $cathash; 
10811:         if (ref($settings) eq 'HASH') {
10812:             $cathash = $settings->{'cats'};
10813:         }
10814:         if (ref($cathash) eq 'HASH') {
10815:             my (@cats,@trails,%allitems,%idx,@jsarray);
10816:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
10817:                                                    \%allitems,\%idx,\@jsarray);
10818:             my $maxdepth = scalar(@cats);
10819:             my $colattrib = '';
10820:             if ($maxdepth > 2) {
10821:                 $colattrib = ' colspan="2" ';
10822:             }
10823:             my @path;
10824:             if (@cats > 0) {
10825:                 if (ref($cats[0]) eq 'ARRAY') {
10826:                     my $numtop = @{$cats[0]};
10827:                     my $maxnum = $numtop;
10828:                     my %default_names = (
10829:                           instcode    => &mt('Official courses'),
10830:                           communities => &mt('Communities'),
10831:                           placement   => &mt('Placement Tests'),
10832:                     );
10833: 
10834:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
10835:                         ($cathash->{'instcode::0'} eq '') ||
10836:                         (!grep(/^communities$/,@{$cats[0]})) || 
10837:                         ($cathash->{'communities::0'} eq '') ||
10838:                         (!grep(/^placement$/,@{$cats[0]})) ||
10839:                         ($cathash->{'placement::0'} eq '')) {
10840:                         $maxnum ++;
10841:                     }
10842:                     my $lastidx;
10843:                     for (my $i=0; $i<$numtop; $i++) {
10844:                         my $parent = $cats[0][$i];
10845:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10846:                         my $item = &escape($parent).'::0';
10847:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
10848:                         $lastidx = $idx{$item};
10849:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10850:                                       .'<select name="'.$item.'"'.$chgstr.'>';
10851:                         for (my $k=0; $k<=$maxnum; $k++) {
10852:                             my $vpos = $k+1;
10853:                             my $selstr;
10854:                             if ($k == $i) {
10855:                                 $selstr = ' selected="selected" ';
10856:                             }
10857:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10858:                         }
10859:                         $datatable .= '</select></span></td><td>';
10860:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
10861:                             $datatable .=  '<span class="LC_nobreak">'
10862:                                            .$default_names{$parent}.'</span>';
10863:                             if ($parent eq 'instcode') {
10864:                                 $datatable .= '<br /><span class="LC_nobreak">('
10865:                                               .&mt('with institutional codes')
10866:                                               .')</span></td><td'.$colattrib.'>';
10867:                             } else {
10868:                                 $datatable .= '<table><tr><td>';
10869:                             }
10870:                             $datatable .= '<span class="LC_nobreak">'
10871:                                           .'<label><input type="radio" name="'
10872:                                           .$parent.'" value="1" checked="checked" />'
10873:                                           .&mt('Display').'</label>';
10874:                             if ($parent eq 'instcode') {
10875:                                 $datatable .= '&nbsp;';
10876:                             } else {
10877:                                 $datatable .= '</span></td></tr><tr><td>'
10878:                                               .'<span class="LC_nobreak">';
10879:                             }
10880:                             $datatable .= '<label><input type="radio" name="'
10881:                                           .$parent.'" value="0" />'
10882:                                           .&mt('Do not display').'</label></span>';
10883:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
10884:                                 $datatable .= '</td></tr></table>';
10885:                             }
10886:                             $datatable .= '</td>';
10887:                         } else {
10888:                             $datatable .= $parent
10889:                                           .'&nbsp;<span class="LC_nobreak"><label>'
10890:                                           .'<input type="checkbox" name="deletecategory" '
10891:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
10892:                         }
10893:                         my $depth = 1;
10894:                         push(@path,$parent);
10895:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
10896:                         pop(@path);
10897:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
10898:                         $itemcount ++;
10899:                     }
10900:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10901:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
10902:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
10903:                     for (my $k=0; $k<=$maxnum; $k++) {
10904:                         my $vpos = $k+1;
10905:                         my $selstr;
10906:                         if ($k == $numtop) {
10907:                             $selstr = ' selected="selected" ';
10908:                         }
10909:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10910:                     }
10911:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
10912:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
10913:                                   .'</tr>'."\n";
10914:                     $itemcount ++;
10915:                     foreach my $default ('instcode','communities','placement') {
10916:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
10917:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
10918:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
10919:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
10920:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
10921:                             for (my $k=0; $k<=$maxnum; $k++) {
10922:                                 my $vpos = $k+1;
10923:                                 my $selstr;
10924:                                 if ($k == $maxnum) {
10925:                                     $selstr = ' selected="selected" ';
10926:                                 }
10927:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10928:                             }
10929:                             $datatable .= '</select></span></td>'.
10930:                                           '<td><span class="LC_nobreak">'.
10931:                                           $default_names{$default}.'</span>';
10932:                             if ($default eq 'instcode') {
10933:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
10934:                                               .&mt('with institutional codes').')</span>';
10935:                             }
10936:                             $datatable .= '</td>'
10937:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
10938:                                           .&mt('Display').'</label>&nbsp;'
10939:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
10940:                                           .&mt('Do not display').'</label></span></td></tr>';
10941:                         }
10942:                     }
10943:                 }
10944:             } else {
10945:                 $datatable .= &initialize_categories($itemcount);
10946:             }
10947:         } else {
10948:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
10949:                           .&initialize_categories($itemcount);
10950:         }
10951:         $$rowtotal += $itemcount;
10952:     }
10953:     return $datatable;
10954: }
10955: 
10956: sub print_serverstatuses {
10957:     my ($dom,$settings,$rowtotal) = @_;
10958:     my $datatable;
10959:     my @pages = &serverstatus_pages();
10960:     my (%namedaccess,%machineaccess);
10961:     foreach my $type (@pages) {
10962:         $namedaccess{$type} = '';
10963:         $machineaccess{$type}= '';
10964:     }
10965:     if (ref($settings) eq 'HASH') {
10966:         foreach my $type (@pages) {
10967:             if (exists($settings->{$type})) {
10968:                 if (ref($settings->{$type}) eq 'HASH') {
10969:                     foreach my $key (keys(%{$settings->{$type}})) {
10970:                         if ($key eq 'namedusers') {
10971:                             $namedaccess{$type} = $settings->{$type}->{$key};
10972:                         } elsif ($key eq 'machines') {
10973:                             $machineaccess{$type} = $settings->{$type}->{$key};
10974:                         }
10975:                     }
10976:                 }
10977:             }
10978:         }
10979:     }
10980:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10981:     my $rownum = 0;
10982:     my $css_class;
10983:     foreach my $type (@pages) {
10984:         $rownum ++;
10985:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10986:         $datatable .= '<tr'.$css_class.'>'.
10987:                       '<td><span class="LC_nobreak">'.
10988:                       $titles->{$type}.'</span></td>'.
10989:                       '<td class="LC_left_item">'.
10990:                       '<input type="text" name="'.$type.'_namedusers" '.
10991:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
10992:                       '<td class="LC_right_item">'.
10993:                       '<span class="LC_nobreak">'.
10994:                       '<input type="text" name="'.$type.'_machines" '.
10995:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10996:                       '</span></td></tr>'."\n";
10997:     }
10998:     $$rowtotal += $rownum;
10999:     return $datatable;
11000: }
11001: 
11002: sub serverstatus_pages {
11003:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11004:             'checksums','clusterstatus','certstatus','metadata_keywords',
11005:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11006:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11007: }
11008: 
11009: sub defaults_javascript {
11010:     my ($settings) = @_;
11011:     return unless (ref($settings) eq 'HASH');
11012:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11013:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11014:         if ($maxnum eq '') {
11015:             $maxnum = 0;
11016:         }
11017:         $maxnum ++;
11018:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11019:         return <<"ENDSCRIPT";
11020: <script type="text/javascript">
11021: // <![CDATA[
11022: function reorderTypes(form,caller) {
11023:     var changedVal;
11024: $jstext 
11025:     var newpos = 'addinststatus_pos';
11026:     var current = new Array;
11027:     var maxh = $maxnum;
11028:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11029:     var oldVal;
11030:     if (caller == newpos) {
11031:         changedVal = newitemVal;
11032:     } else {
11033:         var curritem = 'inststatus_pos_'+caller;
11034:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11035:         current[newitemVal] = newpos;
11036:     }
11037:     for (var i=0; i<inststatuses.length; i++) {
11038:         if (inststatuses[i] != caller) {
11039:             var elementName = 'inststatus_pos_'+inststatuses[i];
11040:             if (form.elements[elementName]) {
11041:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11042:                 current[currVal] = elementName;
11043:             }
11044:         }
11045:     }
11046:     for (var j=0; j<maxh; j++) {
11047:         if (current[j] == undefined) {
11048:             oldVal = j;
11049:         }
11050:     }
11051:     if (oldVal < changedVal) {
11052:         for (var k=oldVal+1; k<=changedVal ; k++) {
11053:            var elementName = current[k];
11054:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11055:         }
11056:     } else {
11057:         for (var k=changedVal; k<oldVal; k++) {
11058:             var elementName = current[k];
11059:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11060:         }
11061:     }
11062:     return;
11063: }
11064: 
11065: // ]]>
11066: </script>
11067: 
11068: ENDSCRIPT
11069:     }
11070:     return;
11071: }
11072: 
11073: sub passwords_javascript {
11074:     my %intalert = &Apache::lonlocal::texthash (
11075:         authcheck => '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.',
11076:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11077:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11078:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11079:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11080:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11081:     );
11082:     &js_escape(\%intalert);
11083:     my $defmin = $Apache::lonnet::passwdmin;
11084:     my $intauthjs = <<"ENDSCRIPT";
11085: 
11086: function warnIntAuth(field) {
11087:     if (field.name == 'intauth_check') {
11088:         if (field.value == '2') {
11089:             alert('$intalert{authcheck}');
11090:         }
11091:     }
11092:     if (field.name == 'intauth_cost') {
11093:         field.value.replace(/\s/g,'');
11094:         if (field.value != '') {
11095:             var regexdigit=/^\\d+\$/;
11096:             if (!regexdigit.test(field.value)) {
11097:                 alert('$intalert{authcost}');
11098:             }
11099:         }
11100:     }
11101:     return;
11102: }
11103: 
11104: function warnIntPass(field) {
11105:     field.value.replace(/^\s+/,'');
11106:     field.value.replace(/\s+\$/,'');
11107:     var regexdigit=/^\\d+\$/;
11108:     if (field.name == 'passwords_min') {
11109:         if (field.value == '') {
11110:             alert('$intalert{passmin}');
11111:             field.value = '$defmin';
11112:         } else {
11113:             if (!regexdigit.test(field.value)) {
11114:                 alert('$intalert{passmin}');
11115:                 field.value = '$defmin';
11116:             }
11117:             var minval = parseInt(field.value,10);
11118:             if (minval < $defmin) {
11119:                 alert('$intalert{passmin}');
11120:                 field.value = '$defmin';
11121:             }
11122:         }
11123:     } else {
11124:         if (field.value == '0') {
11125:             field.value = '';
11126:         }
11127:         if (field.value != '') {
11128:             if (field.name == 'passwords_expire') {
11129:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11130:                 if (!regexpposnum.test(field.value)) {
11131:                     alert('$intalert{passexp}');
11132:                     field.value = '';
11133:                 } else {
11134:                     var expval = parseFloat(field.value);
11135:                     if (expval == 0) {
11136:                         alert('$intalert{passexp}');
11137:                         field.value = '';
11138:                     }
11139:                 }
11140:             } else {
11141:                 if (!regexdigit.test(field.value)) {
11142:                     if (field.name == 'passwords_max') {
11143:                         alert('$intalert{passmax}');
11144:                     } else {
11145:                         if (field.name == 'passwords_numsaved') {
11146:                             alert('$intalert{passnum}');
11147:                         }
11148:                     }
11149:                     field.value = '';
11150:                 }
11151:             }
11152:         }
11153:     }
11154:     return;
11155: }
11156: 
11157: ENDSCRIPT
11158:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11159: }
11160: 
11161: sub coursecategories_javascript {
11162:     my ($settings) = @_;
11163:     my ($output,$jstext,$cathash);
11164:     if (ref($settings) eq 'HASH') {
11165:         $cathash = $settings->{'cats'};
11166:     }
11167:     if (ref($cathash) eq 'HASH') {
11168:         my (@cats,@jsarray,%idx);
11169:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11170:         if (@jsarray > 0) {
11171:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11172:             for (my $i=0; $i<@jsarray; $i++) {
11173:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11174:                     my $catstr = join('","',@{$jsarray[$i]});
11175:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11176:                 }
11177:             }
11178:         }
11179:     } else {
11180:         $jstext  = '    var categories = Array(1);'."\n".
11181:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11182:     }
11183:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11184:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11185:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11186:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11187:     &js_escape(\$instcode_reserved);
11188:     &js_escape(\$communities_reserved);
11189:     &js_escape(\$placement_reserved);
11190:     &js_escape(\$choose_again);
11191:     $output = <<"ENDSCRIPT";
11192: <script type="text/javascript">
11193: // <![CDATA[
11194: function reorderCats(form,parent,item,idx) {
11195:     var changedVal;
11196: $jstext
11197:     var newpos = 'addcategory_pos';
11198:     if (parent == '') {
11199:         var has_instcode = 0;
11200:         var maxtop = categories[idx].length;
11201:         for (var j=0; j<maxtop; j++) {
11202:             if (categories[idx][j] == 'instcode::0') {
11203:                 has_instcode == 1;
11204:             }
11205:         }
11206:         if (has_instcode == 0) {
11207:             categories[idx][maxtop] = 'instcode_pos';
11208:         }
11209:     } else {
11210:         newpos += '_'+parent;
11211:     }
11212:     var maxh = 1 + categories[idx].length;
11213:     var current = new Array;
11214:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11215:     if (item == newpos) {
11216:         changedVal = newitemVal;
11217:     } else {
11218:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11219:         current[newitemVal] = newpos;
11220:     }
11221:     for (var i=0; i<categories[idx].length; i++) {
11222:         var elementName = categories[idx][i];
11223:         if (elementName != item) {
11224:             if (form.elements[elementName]) {
11225:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11226:                 current[currVal] = elementName;
11227:             }
11228:         }
11229:     }
11230:     var oldVal;
11231:     for (var j=0; j<maxh; j++) {
11232:         if (current[j] == undefined) {
11233:             oldVal = j;
11234:         }
11235:     }
11236:     if (oldVal < changedVal) {
11237:         for (var k=oldVal+1; k<=changedVal ; k++) {
11238:            var elementName = current[k];
11239:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11240:         }
11241:     } else {
11242:         for (var k=changedVal; k<oldVal; k++) {
11243:             var elementName = current[k];
11244:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11245:         }
11246:     }
11247:     return;
11248: }
11249: 
11250: function categoryCheck(form) {
11251:     if (form.elements['addcategory_name'].value == 'instcode') {
11252:         alert('$instcode_reserved\\n$choose_again');
11253:         return false;
11254:     }
11255:     if (form.elements['addcategory_name'].value == 'communities') {
11256:         alert('$communities_reserved\\n$choose_again');
11257:         return false;
11258:     }
11259:     if (form.elements['addcategory_name'].value == 'placement') {
11260:         alert('$placement_reserved\\n$choose_again');
11261:         return false;
11262:     }
11263:     return true;
11264: }
11265: 
11266: // ]]>
11267: </script>
11268: 
11269: ENDSCRIPT
11270:     return $output;
11271: }
11272: 
11273: sub initialize_categories {
11274:     my ($itemcount) = @_;
11275:     my ($datatable,$css_class,$chgstr);
11276:     my %default_names = &Apache::lonlocal::texthash (
11277:                       instcode    => 'Official courses (with institutional codes)',
11278:                       communities => 'Communities',
11279:                       placement   => 'Placement Tests',
11280:                         );
11281:     my %selnum = (
11282:                    instcode    => '0',
11283:                    communities => '1',
11284:                    placement   => '2',
11285:                  );
11286:     my %selected;
11287:     foreach my $default ('instcode','communities','placement') {
11288:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11289:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11290:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11291:         $selected{$selnum{$default}} = ' selected="selected"';
11292:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11293:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11294:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11295:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11296:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11297:                      .'<option value="3">4</option></select>&nbsp;'
11298:                      .$default_names{$default}
11299:                      .'</span></td><td><span class="LC_nobreak">'
11300:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11301:                      .&mt('Display').'</label>&nbsp;<label>'
11302:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11303:                  .'</label></span></td></tr>';
11304:         $itemcount ++;
11305:     }
11306:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11307:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11308:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11309:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11310:                   .'<option value="0">1</option>'
11311:                   .'<option value="1">2</option>'
11312:                   .'<option value="2">3</option>'
11313:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11314:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11315:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11316:                   .'</td></tr>';
11317:     return $datatable;
11318: }
11319: 
11320: sub build_category_rows {
11321:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11322:     my ($text,$name,$item,$chgstr);
11323:     if (ref($cats) eq 'ARRAY') {
11324:         my $maxdepth = scalar(@{$cats});
11325:         if (ref($cats->[$depth]) eq 'HASH') {
11326:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11327:                 my $numchildren = @{$cats->[$depth]{$parent}};
11328:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11329:                 $text .= '<td><table class="LC_data_table">';
11330:                 my ($idxnum,$parent_name,$parent_item);
11331:                 my $higher = $depth - 1;
11332:                 if ($higher == 0) {
11333:                     $parent_name = &escape($parent).'::'.$higher;
11334:                 } else {
11335:                     if (ref($path) eq 'ARRAY') {
11336:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11337:                     }
11338:                 }
11339:                 $parent_item = 'addcategory_pos_'.$parent_name;
11340:                 for (my $j=0; $j<=$numchildren; $j++) {
11341:                     if ($j < $numchildren) {
11342:                         $name = $cats->[$depth]{$parent}[$j];
11343:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11344:                         $idxnum = $idx->{$item};
11345:                     } else {
11346:                         $name = $parent_name;
11347:                         $item = $parent_item;
11348:                     }
11349:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
11350:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
11351:                     for (my $i=0; $i<=$numchildren; $i++) {
11352:                         my $vpos = $i+1;
11353:                         my $selstr;
11354:                         if ($j == $i) {
11355:                             $selstr = ' selected="selected" ';
11356:                         }
11357:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
11358:                     }
11359:                     $text .= '</select>&nbsp;';
11360:                     if ($j < $numchildren) {
11361:                         my $deeper = $depth+1;
11362:                         $text .= $name.'&nbsp;'
11363:                                  .'<label><input type="checkbox" name="deletecategory" value="'
11364:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
11365:                         if(ref($path) eq 'ARRAY') {
11366:                             push(@{$path},$name);
11367:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
11368:                             pop(@{$path});
11369:                         }
11370:                     } else {
11371:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
11372:                         if ($j == $numchildren) {
11373:                             $text .= $name;
11374:                         } else {
11375:                             $text .= $item;
11376:                         }
11377:                         $text .= '" value="" />';
11378:                     }
11379:                     $text .= '</td></tr>';
11380:                 }
11381:                 $text .= '</table></td>';
11382:             } else {
11383:                 my $higher = $depth-1;
11384:                 if ($higher == 0) {
11385:                     $name = &escape($parent).'::'.$higher;
11386:                 } else {
11387:                     if (ref($path) eq 'ARRAY') {
11388:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11389:                     }
11390:                 }
11391:                 my $colspan;
11392:                 if ($parent ne 'instcode') {
11393:                     $colspan = $maxdepth - $depth - 1;
11394:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
11395:                 }
11396:             }
11397:         }
11398:     }
11399:     return $text;
11400: }
11401: 
11402: sub modifiable_userdata_row {
11403:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
11404:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
11405:     my ($role,$rolename,$statustype);
11406:     $role = $item;
11407:     if ($context eq 'cancreate') {
11408:         if ($item =~ /^(emailusername)_(.+)$/) {
11409:             $role = $1;
11410:             $statustype = $2;
11411:             if (ref($usertypes) eq 'HASH') {
11412:                 if ($usertypes->{$statustype}) {
11413:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
11414:                 } else {
11415:                     $rolename = &mt('Data provided by user');
11416:                 }
11417:             }
11418:         }
11419:     } elsif ($context eq 'selfcreate') {
11420:         if (ref($usertypes) eq 'HASH') {
11421:             $rolename = $usertypes->{$role};
11422:         } else {
11423:             $rolename = $role;
11424:         }
11425:     } elsif ($context eq 'lti') {
11426:         $rolename = &mt('Institutional data used (if available)');
11427:     } elsif ($context eq 'privacy') {
11428:         $rolename = $itemdesc;
11429:     } else {
11430:         if ($role eq 'cr') {
11431:             $rolename = &mt('Custom role');
11432:         } else {
11433:             $rolename = &Apache::lonnet::plaintext($role);
11434:         }
11435:     }
11436:     my (@fields,%fieldtitles);
11437:     if (ref($fieldsref) eq 'ARRAY') {
11438:         @fields = @{$fieldsref};
11439:     } else {
11440:         @fields = ('lastname','firstname','middlename','generation',
11441:                    'permanentemail','id');
11442:     }
11443:     if ((ref($titlesref) eq 'HASH')) {
11444:         %fieldtitles = %{$titlesref};
11445:     } else {
11446:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11447:     }
11448:     my $output;
11449:     my $css_class;
11450:     if ($rowcount%2) {
11451:         $css_class = 'LC_odd_row';
11452:     }
11453:     if ($customcss) {
11454:         $css_class .= " $customcss";
11455:     }
11456:     $css_class =~ s/^\s+//;
11457:     if ($css_class) {
11458:         $css_class = ' class="'.$css_class.'"';
11459:     }
11460:     if ($rowstyle) {
11461:         $css_class .= ' style="'.$rowstyle.'"';
11462:     }
11463:     if ($rowid) {
11464:         $rowid = ' id="'.$rowid.'"';
11465:     }
11466:     $output = '<tr '.$css_class.$rowid.'>'.
11467:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
11468:               '<td class="LC_left_item" colspan="2"><table>';
11469:     my $rem;
11470:     my %checks;
11471:     my %current;
11472:     if (ref($settings) eq 'HASH') {
11473:         my $hashref;
11474:         if ($context eq 'lti') {
11475:             if (ref($settings) eq 'HASH') {
11476:                 $hashref = $settings->{'instdata'};
11477:             }
11478:         } elsif ($context eq 'privacy') {
11479:             my ($key,$inner) = split(/_/,$role);
11480:             if (ref($settings) eq 'HASH') {
11481:                 if (ref($settings->{$key}) eq 'HASH') {
11482:                     $hashref = $settings->{$key}->{$inner};
11483:                 }
11484:             }
11485:         } elsif (ref($settings->{$context}) eq 'HASH') {
11486:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
11487:                 $hashref = $settings->{'lti_instdata'};
11488:             }
11489:             if ($role eq 'emailusername') {
11490:                 if ($statustype) {
11491:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
11492:                         $hashref = $settings->{$context}->{$role}->{$statustype};
11493:                     }
11494:                 }
11495:             }
11496:         }
11497:         if (ref($hashref) eq 'HASH') { 
11498:             foreach my $field (@fields) {
11499:                 if ($hashref->{$field}) {
11500:                     if ($role eq 'emailusername') {
11501:                         $checks{$field} = $hashref->{$field};
11502:                     } else {
11503:                         $checks{$field} = ' checked="checked" ';
11504:                     }
11505:                 }
11506:             }
11507:         }
11508:     }
11509:  
11510:     my $total = scalar(@fields);
11511:     for (my $i=0; $i<$total; $i++) {
11512:         $rem = $i%($numinrow);
11513:         if ($rem == 0) {
11514:             if ($i > 0) {
11515:                 $output .= '</tr>';
11516:             }
11517:             $output .= '<tr>';
11518:         }
11519:         my $check = ' ';
11520:         unless ($role eq 'emailusername') {
11521:             if (exists($checks{$fields[$i]})) {
11522:                 $check = $checks{$fields[$i]};
11523:             } elsif ($context eq 'privacy') {
11524:                 if ($role =~ /^priv_(domain|course)$/) {
11525:                     if (ref($settings) ne 'HASH') {
11526:                         $check = ' checked="checked" ';
11527:                     }
11528:                 } elsif ($role =~ /^priv_(author|community)$/) {
11529:                     if (ref($settings) ne 'HASH') {
11530:                         unless ($fields[$i] eq 'id') {
11531:                             $check = ' checked="checked" ';
11532:                         }
11533:                     }
11534:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
11535:                     if (ref($settings) ne 'HASH') {
11536:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
11537:                             $check = ' checked="checked" ';
11538:                         }
11539:                     }
11540:                 }
11541:             } elsif ($context ne 'lti') {
11542:                 if ($role eq 'st') {
11543:                     if (ref($settings) ne 'HASH') {
11544:                         $check = ' checked="checked" '; 
11545:                     }
11546:                 }
11547:             }
11548:         }
11549:         $output .= '<td class="LC_left_item">'.
11550:                    '<span class="LC_nobreak">';
11551:         my $prefix = 'canmodify';
11552:         if ($role eq 'emailusername') {
11553:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
11554:                 $checks{$fields[$i]} = 'omit';
11555:             }
11556:             foreach my $option ('required','optional','omit') {
11557:                 my $checked='';
11558:                 if ($checks{$fields[$i]} eq $option) {
11559:                     $checked='checked="checked" ';
11560:                 }
11561:                 $output .= '<label>'.
11562:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
11563:                            &mt($option).'</label>'.('&nbsp;' x2);
11564:             }
11565:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
11566:         } else {
11567:             if ($context eq 'lti') {
11568:                 $prefix = 'lti';
11569:             } elsif ($context eq 'privacy') {
11570:                 $prefix = 'privacy';
11571:             }
11572:             $output .= '<label>'.
11573:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
11574:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
11575:                        '</label>';
11576:         }
11577:         $output .= '</span></td>';
11578:     }
11579:     $rem = $total%$numinrow;
11580:     my $colsleft;
11581:     if ($rem) {
11582:         $colsleft = $numinrow - $rem;
11583:     }
11584:     if ($colsleft > 1) {
11585:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
11586:                    '&nbsp;</td>';
11587:     } elsif ($colsleft == 1) {
11588:         $output .= '<td class="LC_left_item">&nbsp;</td>';
11589:     }
11590:     $output .= '</tr></table></td></tr>';
11591:     return $output;
11592: }
11593: 
11594: sub insttypes_row {
11595:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
11596:         $customcss,$rowstyle) = @_;
11597:     my %lt = &Apache::lonlocal::texthash (
11598:                       cansearch => 'Users allowed to search',
11599:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
11600:                       lockablenames => 'User preference to lock name',
11601:                       selfassign    => 'Self-reportable affiliations',
11602:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
11603:              );
11604:     my $showdom;
11605:     if ($context eq 'cansearch') {
11606:         $showdom = ' ('.$dom.')';
11607:     }
11608:     my $class = 'LC_left_item';
11609:     if ($context eq 'statustocreate') {
11610:         $class = 'LC_right_item';
11611:     }
11612:     my $css_class;
11613:     if ($$rowtotal%2) {
11614:         $css_class = 'LC_odd_row';
11615:     }
11616:     if ($customcss) {
11617:         $css_class .= ' '.$customcss;
11618:     }
11619:     $css_class =~ s/^\s+//;
11620:     if ($css_class) {
11621:         $css_class = ' class="'.$css_class.'"';
11622:     }
11623:     if ($rowstyle) {
11624:         $css_class .= ' style="'.$rowstyle.'"';
11625:     }
11626:     if ($onclick) {
11627:         $onclick = 'onclick="'.$onclick.'" ';
11628:     }
11629:     my $output = '<tr'.$css_class.'>'.
11630:                  '<td>'.$lt{$context}.$showdom.
11631:                  '</td><td class="'.$class.'" colspan="2"><table>';
11632:     my $rem;
11633:     if (ref($types) eq 'ARRAY') {
11634:         for (my $i=0; $i<@{$types}; $i++) {
11635:             if (defined($usertypes->{$types->[$i]})) {
11636:                 my $rem = $i%($numinrow);
11637:                 if ($rem == 0) {
11638:                     if ($i > 0) {
11639:                         $output .= '</tr>';
11640:                     }
11641:                     $output .= '<tr>';
11642:                 }
11643:                 my $check = ' ';
11644:                 if (ref($settings) eq 'HASH') {
11645:                     if (ref($settings->{$context}) eq 'ARRAY') {
11646:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
11647:                             $check = ' checked="checked" ';
11648:                         }
11649:                     } elsif (ref($settings->{$context}) eq 'HASH') {
11650:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
11651:                             $check = ' checked="checked" ';
11652:                         }
11653:                     } elsif ($context eq 'statustocreate') {
11654:                         $check = ' checked="checked" ';
11655:                     }
11656:                 }
11657:                 $output .= '<td class="LC_left_item">'.
11658:                            '<span class="LC_nobreak"><label>'.
11659:                            '<input type="checkbox" name="'.$context.'" '.
11660:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
11661:                            $usertypes->{$types->[$i]}.'</label></span></td>';
11662:             }
11663:         }
11664:         $rem = @{$types}%($numinrow);
11665:     }
11666:     my $colsleft = $numinrow - $rem;
11667:     if ($context eq 'overrides') {
11668:         if ($colsleft > 1) {
11669:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11670:         } else {
11671:             $output .= '<td class="LC_left_item">';
11672:         }
11673:         $output .= '&nbsp;';  
11674:     } else {
11675:         if ($rem == 0) {
11676:             $output .= '<tr>';
11677:         }
11678:         if ($colsleft > 1) {
11679:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11680:         } else {
11681:             $output .= '<td class="LC_left_item">';
11682:         }
11683:         my $defcheck = ' ';
11684:         if (ref($settings) eq 'HASH') {  
11685:             if (ref($settings->{$context}) eq 'ARRAY') {
11686:                 if (grep(/^default$/,@{$settings->{$context}})) {
11687:                     $defcheck = ' checked="checked" ';
11688:                 }
11689:             } elsif ($context eq 'statustocreate') {
11690:                 $defcheck = ' checked="checked" ';
11691:             }
11692:         }
11693:         $output .= '<span class="LC_nobreak"><label>'.
11694:                    '<input type="checkbox" name="'.$context.'" '.
11695:                    'value="default"'.$defcheck.$onclick.' />'.
11696:                    $othertitle.'</label></span>';
11697:     }
11698:     $output .= '</td></tr></table></td></tr>';
11699:     return $output;
11700: }
11701: 
11702: sub sorted_searchtitles {
11703:     my %searchtitles = &Apache::lonlocal::texthash(
11704:                          'uname' => 'username',
11705:                          'lastname' => 'last name',
11706:                          'lastfirst' => 'last name, first name',
11707:                      );
11708:     my @titleorder = ('uname','lastname','lastfirst');
11709:     return (\%searchtitles,\@titleorder);
11710: }
11711: 
11712: sub sorted_searchtypes {
11713:     my %srchtypes_desc = (
11714:                            exact    => 'is exact match',
11715:                            contains => 'contains ..',
11716:                            begins   => 'begins with ..',
11717:                          );
11718:     my @srchtypeorder = ('exact','begins','contains');
11719:     return (\%srchtypes_desc,\@srchtypeorder);
11720: }
11721: 
11722: sub usertype_update_row {
11723:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
11724:     my $datatable;
11725:     my $numinrow = 4;
11726:     foreach my $type (@{$types}) {
11727:         if (defined($usertypes->{$type})) {
11728:             $$rownums ++;
11729:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
11730:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
11731:                           '</td><td class="LC_left_item"><table>';
11732:             for (my $i=0; $i<@{$fields}; $i++) {
11733:                 my $rem = $i%($numinrow);
11734:                 if ($rem == 0) {
11735:                     if ($i > 0) {
11736:                         $datatable .= '</tr>';
11737:                     }
11738:                     $datatable .= '<tr>';
11739:                 }
11740:                 my $check = ' ';
11741:                 if (ref($settings) eq 'HASH') {
11742:                     if (ref($settings->{'fields'}) eq 'HASH') {
11743:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
11744:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
11745:                                 $check = ' checked="checked" ';
11746:                             }
11747:                         }
11748:                     }
11749:                 }
11750: 
11751:                 if ($i == @{$fields}-1) {
11752:                     my $colsleft = $numinrow - $rem;
11753:                     if ($colsleft > 1) {
11754:                         $datatable .= '<td colspan="'.$colsleft.'">';
11755:                     } else {
11756:                         $datatable .= '<td>';
11757:                     }
11758:                 } else {
11759:                     $datatable .= '<td>';
11760:                 }
11761:                 $datatable .= '<span class="LC_nobreak"><label>'.
11762:                               '<input type="checkbox" name="updateable_'.$type.
11763:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
11764:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
11765:             }
11766:             $datatable .= '</tr></table></td></tr>';
11767:         }
11768:     }
11769:     return $datatable;
11770: }
11771: 
11772: sub modify_login {
11773:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11774:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
11775:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
11776:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso);
11777:     %title = ( coursecatalog => 'Display course catalog',
11778:                adminmail => 'Display administrator E-mail address',
11779:                helpdesk  => 'Display "Contact Helpdesk" link',
11780:                newuser => 'Link for visitors to create a user account',
11781:                loginheader => 'Log-in box header',
11782:                saml => 'Dual SSO and non-SSO login');
11783:     @offon = ('off','on');
11784:     if (ref($domconfig{login}) eq 'HASH') {
11785:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
11786:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
11787:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
11788:             }
11789:         }
11790:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
11791:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
11792:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
11793:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
11794:                     $saml{$lonhost} = 1;
11795:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
11796:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
11797:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
11798:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
11799:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
11800:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
11801:                 }
11802:             }
11803:         }
11804:     }
11805:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
11806:                                            \%domconfig,\%loginhash);
11807:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11808:     foreach my $item (@toggles) {
11809:         $loginhash{login}{$item} = $env{'form.'.$item};
11810:     }
11811:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
11812:     if (ref($colchanges{'login'}) eq 'HASH') {  
11813:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
11814:                                          \%loginhash);
11815:     }
11816: 
11817:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11818:     my %domservers = &Apache::lonnet::get_servers($dom);
11819:     my @loginvia_attribs = ('serverpath','custompath','exempt');
11820:     if (keys(%servers) > 1) {
11821:         foreach my $lonhost (keys(%servers)) {
11822:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
11823:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
11824:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
11825:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
11826:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
11827:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11828:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11829:                         $changes{'loginvia'}{$lonhost} = 1;
11830:                     } else {
11831:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
11832:                         $changes{'loginvia'}{$lonhost} = 1;
11833:                     }
11834:                 } else {
11835:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11836:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11837:                         $changes{'loginvia'}{$lonhost} = 1;
11838:                     }
11839:                 }
11840:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
11841:                     foreach my $item (@loginvia_attribs) {
11842:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
11843:                     }
11844:                 } else {
11845:                     foreach my $item (@loginvia_attribs) {
11846:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11847:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11848:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
11849:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11850:                                 $new = '/';
11851:                             }
11852:                         }
11853:                         if (($item eq 'custompath') && 
11854:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11855:                             $new = '';
11856:                         }
11857:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
11858:                             $changes{'loginvia'}{$lonhost} = 1;
11859:                         }
11860:                         if ($item eq 'exempt') {
11861:                             $new = &check_exempt_addresses($new);
11862:                         }
11863:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11864:                     }
11865:                 }
11866:             } else {
11867:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11868:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11869:                     $changes{'loginvia'}{$lonhost} = 1;
11870:                     foreach my $item (@loginvia_attribs) {
11871:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11872:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11873:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11874:                                 $new = '/';
11875:                             }
11876:                         }
11877:                         if (($item eq 'custompath') && 
11878:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11879:                             $new = '';
11880:                         }
11881:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11882:                     }
11883:                 }
11884:             }
11885:         }
11886:     }
11887: 
11888:     my $servadm = $r->dir_config('lonAdmEMail');
11889:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
11890:     if (ref($domconfig{'login'}) eq 'HASH') {
11891:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
11892:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
11893:                 if ($lang eq 'nolang') {
11894:                     push(@currlangs,$lang);
11895:                 } elsif (defined($langchoices{$lang})) {
11896:                     push(@currlangs,$lang);
11897:                 } else {
11898:                     next;
11899:                 }
11900:             }
11901:         }
11902:     }
11903:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
11904:     if (@currlangs > 0) {
11905:         foreach my $lang (@currlangs) {
11906:             if (grep(/^\Q$lang\E$/,@delurls)) {
11907:                 $changes{'helpurl'}{$lang} = 1;
11908:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
11909:                 $changes{'helpurl'}{$lang} = 1;
11910:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
11911:                 push(@newlangs,$lang);
11912:             } else {
11913:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11914:             }
11915:         }
11916:     }
11917:     unless (grep(/^nolang$/,@currlangs)) {
11918:         if ($env{'form.loginhelpurl_nolang.filename'}) {
11919:             $changes{'helpurl'}{'nolang'} = 1;
11920:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
11921:             push(@newlangs,'nolang');
11922:         }
11923:     }
11924:     if ($env{'form.loginhelpurl_add_lang'}) {
11925:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
11926:             ($env{'form.loginhelpurl_add_file.filename'})) {
11927:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
11928:             $addedfile = $env{'form.loginhelpurl_add_lang'};
11929:         }
11930:     }
11931:     if ((@newlangs > 0) || ($addedfile)) {
11932:         my $error;
11933:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11934:         if ($configuserok eq 'ok') {
11935:             if ($switchserver) {
11936:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
11937:             } elsif ($author_ok eq 'ok') {
11938:                 my @allnew = @newlangs;
11939:                 if ($addedfile ne '') {
11940:                     push(@allnew,$addedfile);
11941:                 }
11942:                 foreach my $lang (@allnew) {
11943:                     my $formelem = 'loginhelpurl_'.$lang;
11944:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
11945:                         $formelem = 'loginhelpurl_add_file';
11946:                     }
11947:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11948:                                                                "help/$lang",'','',$newfile{$lang});
11949:                     if ($result eq 'ok') {
11950:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
11951:                         $changes{'helpurl'}{$lang} = 1;
11952:                     } else {
11953:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
11954:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11955:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
11956:                             (!grep(/^\Q$lang\E$/,@delurls))) {
11957:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11958:                         }
11959:                     }
11960:                 }
11961:             } else {
11962:                 $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);
11963:             }
11964:         } else {
11965:             $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);
11966:         }
11967:         if ($error) {
11968:             &Apache::lonnet::logthis($error);
11969:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11970:         }
11971:     }
11972: 
11973:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
11974:     if (ref($domconfig{'login'}) eq 'HASH') {
11975:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
11976:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
11977:                 if ($domservers{$lonhost}) {
11978:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11979:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
11980:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
11981:                     }
11982:                 }
11983:             }
11984:         }
11985:     }
11986:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
11987:     foreach my $lonhost (sort(keys(%domservers))) {
11988:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11989:             $changes{'headtag'}{$lonhost} = 1;
11990:         } else {
11991:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
11992:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
11993:             }
11994:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
11995:                 push(@newhosts,$lonhost);
11996:             } elsif ($currheadtagurls{$lonhost}) {
11997:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
11998:                 if ($currexempt{$lonhost}) {
11999:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12000:                         $changes{'headtag'}{$lonhost} = 1;
12001:                     }
12002:                 } elsif ($possexempt{$lonhost}) {
12003:                     $changes{'headtag'}{$lonhost} = 1;
12004:                 }
12005:                 if ($possexempt{$lonhost}) {
12006:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12007:                 }
12008:             }
12009:         }
12010:     }
12011:     if (@newhosts) {
12012:         my $error;
12013:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12014:         if ($configuserok eq 'ok') {
12015:             if ($switchserver) {
12016:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12017:             } elsif ($author_ok eq 'ok') {
12018:                 foreach my $lonhost (@newhosts) {
12019:                     my $formelem = 'loginheadtag_'.$lonhost;
12020:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
12021:                                                                           "login/headtag/$lonhost",'','',
12022:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
12023:                     if ($result eq 'ok') {
12024:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12025:                         $changes{'headtag'}{$lonhost} = 1;
12026:                         if ($possexempt{$lonhost}) {
12027:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12028:                         }
12029:                     } else {
12030:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12031:                                            $newheadtagurls{$lonhost},$result);
12032:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12033:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12034:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12035:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12036:                         }
12037:                     }
12038:                 }
12039:             } else {
12040:                 $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);
12041:             }
12042:         } else {
12043:             $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);
12044:         }
12045:         if ($error) {
12046:             &Apache::lonnet::logthis($error);
12047:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12048:         }
12049:     }
12050:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12051:     my @newsamlimgs;
12052:     foreach my $lonhost (keys(%domservers)) {
12053:         if ($env{'form.saml_'.$lonhost}) {
12054:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12055:                 push(@newsamlimgs,$lonhost);
12056:             }
12057:             foreach my $item ('text','alt','url','title','notsso') {
12058:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12059:             }
12060:             if ($saml{$lonhost}) {
12061:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12062: #FIXME Need to obsolete published image
12063:                     delete($currsaml{$lonhost}{'img'});
12064:                     $changes{'saml'}{$lonhost} = 1;
12065:                 }
12066:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12067:                     $changes{'saml'}{$lonhost} = 1;
12068:                 }
12069:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12070:                     $changes{'saml'}{$lonhost} = 1;
12071:                 }
12072:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12073:                     $changes{'saml'}{$lonhost} = 1;
12074:                 }
12075:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12076:                     $changes{'saml'}{$lonhost} = 1;
12077:                 }
12078:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12079:                     $changes{'saml'}{$lonhost} = 1;
12080:                 }
12081:             } else {
12082:                 $changes{'saml'}{$lonhost} = 1;
12083:             }
12084:             foreach my $item ('text','alt','url','title','notsso') {
12085:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12086:             }
12087:         } else {
12088:             if ($saml{$lonhost}) {  
12089:                 $changes{'saml'}{$lonhost} = 1;
12090:                 delete($currsaml{$lonhost});
12091:             }
12092:         }
12093:     }
12094:     foreach my $posshost (keys(%currsaml)) {
12095:         unless (exists($domservers{$posshost})) { 
12096:             delete($currsaml{$posshost}); 
12097:         }
12098:     }
12099:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12100:     if (@newsamlimgs) {
12101:         my $error;
12102:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12103:         if ($configuserok eq 'ok') {
12104:             if ($switchserver) {
12105:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12106:             } elsif ($author_ok eq 'ok') {
12107:                 foreach my $lonhost (@newsamlimgs) {
12108:                     my $formelem = 'saml_img_'.$lonhost;
12109:                     my ($result,$imgurl) = &publishlogo($r,'upload',$formelem,$dom,$confname,
12110:                                                         "login/saml/$lonhost",'','',
12111:                                                         $env{'form.saml_img_'.$lonhost.'.filename'});
12112:                     if ($result eq 'ok') {
12113:                         $currsaml{$lonhost}{'img'} = $imgurl;
12114:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12115:                         $changes{'saml'}{$lonhost} = 1;
12116:                     } else {
12117:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12118:                                            $lonhost,$result);
12119:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12120:                     }
12121:                 }
12122:             } else {
12123:                 $error = &mt("Upload of SSO button image 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);
12124:             }
12125:         } else {
12126:             $error = &mt("Upload of SSO button image file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
12127:         }
12128:         if ($error) {
12129:             &Apache::lonnet::logthis($error);
12130:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12131:         }
12132:     }
12133:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12134: 
12135:     my $defaulthelpfile = '/adm/loginproblems.html';
12136:     my $defaulttext = &mt('Default in use');
12137: 
12138:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12139:                                              $dom);
12140:     if ($putresult eq 'ok') {
12141:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12142:         my %defaultchecked = (
12143:                     'coursecatalog' => 'on',
12144:                     'helpdesk'      => 'on',
12145:                     'adminmail'     => 'off',
12146:                     'newuser'       => 'off',
12147:         );
12148:         if (ref($domconfig{'login'}) eq 'HASH') {
12149:             foreach my $item (@toggles) {
12150:                 if ($defaultchecked{$item} eq 'on') { 
12151:                     if (($domconfig{'login'}{$item} eq '0') &&
12152:                         ($env{'form.'.$item} eq '1')) {
12153:                         $changes{$item} = 1;
12154:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12155:                               $domconfig{'login'}{$item} eq '1') &&
12156:                              ($env{'form.'.$item} eq '0')) {
12157:                         $changes{$item} = 1;
12158:                     }
12159:                 } elsif ($defaultchecked{$item} eq 'off') {
12160:                     if (($domconfig{'login'}{$item} eq '1') &&
12161:                         ($env{'form.'.$item} eq '0')) {
12162:                         $changes{$item} = 1;
12163:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12164:                               $domconfig{'login'}{$item} eq '0') &&
12165:                              ($env{'form.'.$item} eq '1')) {
12166:                         $changes{$item} = 1;
12167:                     }
12168:                 }
12169:             }
12170:         }
12171:         if (keys(%changes) > 0 || $colchgtext) {
12172:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12173:             if (exists($changes{'saml'})) {
12174:                 my $hostid_in_use;
12175:                 my @hosts = &Apache::lonnet::current_machine_ids();
12176:                 if (@hosts > 1) {
12177:                     foreach my $hostid (@hosts) {
12178:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12179:                             $hostid_in_use = $hostid;
12180:                             last;
12181:                         }
12182:                     }
12183:                 } else {
12184:                     $hostid_in_use = $r->dir_config('lonHostID');
12185:                 }
12186:                 if (($hostid_in_use) &&
12187:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12188:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12189:                 }
12190:                 if (ref($lastactref) eq 'HASH') {
12191:                     if (ref($changes{'saml'}) eq 'HASH') {
12192:                         my %updates;
12193:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12194:                         $lastactref->{'samllanding'} = \%updates;
12195:                     }
12196:                 }
12197:             }
12198:             if (ref($lastactref) eq 'HASH') {
12199:                 $lastactref->{'domainconfig'} = 1;
12200:             }
12201:             $resulttext = &mt('Changes made:').'<ul>';
12202:             foreach my $item (sort(keys(%changes))) {
12203:                 if ($item eq 'loginvia') {
12204:                     if (ref($changes{$item}) eq 'HASH') {
12205:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12206:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12207:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12208:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12209:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12210:                                     $protocol = 'http' if ($protocol ne 'https');
12211:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12212: 
12213:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12214:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12215:                                     } else {
12216:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12217:                                     }
12218:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12219:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12220:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12221:                                     }
12222:                                     $resulttext .= '</li>';
12223:                                 } else {
12224:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12225:                                 }
12226:                             } else {
12227:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12228:                             }
12229:                         }
12230:                         $resulttext .= '</ul></li>';
12231:                     }
12232:                 } elsif ($item eq 'helpurl') {
12233:                     if (ref($changes{$item}) eq 'HASH') {
12234:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12235:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12236:                                 my ($chg,$link);
12237:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12238:                                 if ($lang eq 'nolang') {
12239:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12240:                                 } else {
12241:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12242:                                 }
12243:                                 $resulttext .= '<li>'.$chg.'</li>';
12244:                             } else {
12245:                                 my $chg;
12246:                                 if ($lang eq 'nolang') {
12247:                                     $chg = &mt('custom log-in help file for no preferred language');
12248:                                 } else {
12249:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12250:                                 }
12251:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12252:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12253:                                                       '?inhibitmenu=yes',$chg,600,500).
12254:                                                '</li>';
12255:                             }
12256:                         }
12257:                     }
12258:                 } elsif ($item eq 'headtag') {
12259:                     if (ref($changes{$item}) eq 'HASH') {
12260:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12261:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12262:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12263:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12264:                                 $resulttext .= '<li><a href="'.
12265:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12266:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12267:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12268:                                 if ($possexempt{$lonhost}) {
12269:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12270:                                 } else {
12271:                                     $resulttext .= &mt('included for any client IP');
12272:                                 }
12273:                                 $resulttext .= '</li>';
12274:                             }
12275:                         }
12276:                     }
12277:                 } elsif ($item eq 'saml') {
12278:                     if (ref($changes{$item}) eq 'HASH') {
12279:                         my %notlt = (
12280:                                        text   => 'Text for log-in by SSO',
12281:                                        img    => 'SSO button image',
12282:                                        alt    => 'Alt text for button image',
12283:                                        url    => 'SSO URL',
12284:                                        title  => 'Tooltip for SSO link',
12285:                                        notsso => 'Text for non-SSO log-in',
12286:                                     );
12287:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12288:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
12289:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
12290:                                                '<ul>';
12291:                                 foreach my $key ('text','img','alt','url','title','notsso') {
12292:                                     if ($currsaml{$lonhost}{$key} eq '') {
12293:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
12294:                                     } else {
12295:                                         my $value = "'$currsaml{$lonhost}{$key}'";
12296:                                         if ($key eq 'img') {
12297:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
12298:                                         }
12299:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
12300:                                                                   $value).'</li>';
12301:                                     }
12302:                                 }
12303:                                 $resulttext .= '</ul></li>';
12304:                             } else {
12305:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
12306:                             }
12307:                         }
12308:                     }
12309:                 } elsif ($item eq 'captcha') {
12310:                     if (ref($loginhash{'login'}) eq 'HASH') {
12311:                         my $chgtxt;
12312:                         if ($loginhash{'login'}{$item} eq 'notused') {
12313:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
12314:                         } else {
12315:                             my %captchas = &captcha_phrases();
12316:                             if ($captchas{$loginhash{'login'}{$item}}) {
12317:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
12318:                             } else {
12319:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
12320:                             }
12321:                         }
12322:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12323:                     }
12324:                 } elsif ($item eq 'recaptchakeys') {
12325:                     if (ref($loginhash{'login'}) eq 'HASH') {
12326:                         my ($privkey,$pubkey);
12327:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
12328:                             $pubkey = $loginhash{'login'}{$item}{'public'};
12329:                             $privkey = $loginhash{'login'}{$item}{'private'};
12330:                         }
12331:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
12332:                         if (!$pubkey) {
12333:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
12334:                         } else {
12335:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12336:                         }
12337:                         if (!$privkey) {
12338:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
12339:                         } else {
12340:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
12341:                         }
12342:                         $chgtxt .= '</ul>';
12343:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12344:                     }
12345:                 } elsif ($item eq 'recaptchaversion') {
12346:                     if (ref($loginhash{'login'}) eq 'HASH') {
12347:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
12348:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
12349:                                            '</li>';
12350:                         }
12351:                     }
12352:                 } else {
12353:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
12354:                 }
12355:             }
12356:             $resulttext .= $colchgtext.'</ul>';
12357:         } else {
12358:             $resulttext = &mt('No changes made to log-in page settings');
12359:         }
12360:     } else {
12361:         $resulttext = '<span class="LC_error">'.
12362: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12363:     }
12364:     if ($errors) {
12365:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12366:                        $errors.'</ul>';
12367:     }
12368:     return $resulttext;
12369: }
12370: 
12371: sub check_exempt_addresses {
12372:     my ($iplist) = @_;
12373:     $iplist =~ s/^\s+//;
12374:     $iplist =~ s/\s+$//;
12375:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
12376:     my (@okips,$new);
12377:     foreach my $ip (@poss_ips) {
12378:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
12379:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
12380:                 push(@okips,$ip);
12381:             }
12382:         }
12383:     }
12384:     if (@okips > 0) {
12385:         $new = join(',',@okips);
12386:     } else {
12387:         $new = '';
12388:     }
12389:     return $new;
12390: }
12391: 
12392: sub color_font_choices {
12393:     my %choices =
12394:         &Apache::lonlocal::texthash (
12395:             img => "Header",
12396:             bgs => "Background colors",
12397:             links => "Link colors",
12398:             images => "Images",
12399:             font => "Font color",
12400:             fontmenu => "Font menu",
12401:             pgbg => "Page",
12402:             tabbg => "Header",
12403:             sidebg => "Border",
12404:             link => "Link",
12405:             alink => "Active link",
12406:             vlink => "Visited link",
12407:         );
12408:     return %choices;
12409: }
12410: 
12411: sub modify_ipaccess {
12412:     my ($dom,$lastactref,%domconfig) = @_;
12413:     my (@allpos,%changes,%confhash,$errors,$resulttext);
12414:     my (@items,%deletions,%itemids,@warnings);
12415:     my ($typeorder,$types) = &commblocktype_text();
12416:     if ($env{'form.ipaccess_add'}) {
12417:         my $name = $env{'form.ipaccess_name_add'};
12418:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
12419:         if ($newid) {
12420:             $itemids{'add'} = $newid;
12421:             push(@items,'add');
12422:             $changes{$newid} = 1;
12423:         } else {
12424:             $error = &mt('Failed to acquire unique ID for new IP access control item');
12425:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12426:         }
12427:     }
12428:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12429:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
12430:         if (@todelete) {
12431:             map { $deletions{$_} = 1; } @todelete;
12432:         }
12433:         my $maxnum = $env{'form.ipaccess_maxnum'};
12434:         for (my $i=0; $i<$maxnum; $i++) {
12435:             my $itemid = $env{'form.ipaccess_id_'.$i};
12436:             $itemid =~ s/\D+//g;
12437:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12438:                 if ($deletions{$itemid}) {
12439:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
12440:                 } else {
12441:                     push(@items,$i);
12442:                     $itemids{$i} = $itemid;
12443:                 }
12444:             }
12445:         }
12446:     }
12447:     foreach my $idx (@items) {
12448:         my $itemid = $itemids{$idx};
12449:         next unless ($itemid);
12450:         my %current;
12451:         unless ($idx eq 'add') {
12452:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12453:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
12454:             }
12455:         }
12456:         my $position = $env{'form.ipaccess_pos_'.$itemid};
12457:         $position =~ s/\D+//g;
12458:         if ($position ne '') {
12459:             $allpos[$position] = $itemid;
12460:         }
12461:         my $name = $env{'form.ipaccess_name_'.$idx};
12462:         $name =~ s/^\s+|\s+$//g;
12463:         $confhash{$itemid}{'name'} = $name;
12464:         my $possrange = $env{'form.ipaccess_range_'.$idx};
12465:         $possrange =~ s/^\s+|\s+$//g;
12466:         unless ($possrange eq '') {
12467:             $possrange =~ s/[\r\n]+/\s/g;
12468:             $possrange =~ s/\s*-\s*/-/g;
12469:             $possrange =~ s/\s+/,/g;
12470:             $possrange =~ s/,+/,/g;
12471:             if ($possrange ne '') {
12472:                 my (@ok,$count);
12473:                 $count = 0; 
12474:                 foreach my $poss (split(/\,/,$possrange)) {
12475:                     $count ++;
12476:                     $poss = &validate_ip_pattern($poss);
12477:                     if ($poss ne '') {
12478:                         push(@ok,$poss);
12479:                     }
12480:                 }
12481:                 my $diff = $count - scalar(@ok);
12482:                 if ($diff) {
12483:                     $errors .= '<li><span class="LC_error">'.
12484:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
12485:                                    $diff,$name).
12486:                                '</span></li>';
12487:                 }
12488:                 if (@ok) {
12489:                     my @cidr_list;
12490:                     foreach my $item (@ok) {
12491:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
12492:                     }
12493:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
12494:                 }
12495:             }
12496:         }
12497:         foreach my $field ('name','ip') {
12498:             unless (($idx eq 'add') || ($changes{$itemid})) {
12499:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
12500:                     $changes{$itemid} = 1;
12501:                     last;
12502:                 }
12503:             }
12504:         }
12505:         $confhash{$itemid}{'commblocks'} = {};
12506:          
12507:         my %commblocks;
12508:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);  
12509:         foreach my $type (@{$typeorder}) {
12510:             if ($commblocks{$type}) {
12511:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
12512:             }
12513:             unless (($idx eq 'add') || ($changes{$itemid})) {
12514:                 if (ref($current{'commblocks'}) eq 'HASH') {
12515:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
12516:                         $changes{$itemid} = 1;
12517:                     }
12518:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
12519:                     $changes{$itemid} = 1;
12520:                 }
12521:             }
12522:         }
12523:         $confhash{$itemid}{'courses'} = {};
12524:         my %crsdeletions;
12525:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
12526:         if (@delcrs) {
12527:             map { $crsdeletions{$_} = 1; } @delcrs;
12528:         }
12529:         if (ref($current{'courses'}) eq 'HASH') {
12530:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
12531:                 if ($crsdeletions{$cid}) {
12532:                     $changes{$itemid} = 1;
12533:                 } else {
12534:                     $confhash{$itemid}{'courses'}{$cid} = 1;
12535:                 }
12536:             }
12537:         }
12538:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
12539:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
12540:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) && 
12541:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
12542:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
12543:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
12544:                 $errors .= '<li><span class="LC_error">'.
12545:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
12546:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
12547:                            '</span></li>';
12548:             } else {
12549:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
12550:                 $changes{$itemid} = 1;
12551:             }
12552:         }
12553:     }
12554:     if (@allpos > 0) {
12555:         my $idx = 0;
12556:         foreach my $itemid (@allpos) {
12557:             if ($itemid ne '') {
12558:                 $confhash{$itemid}{'order'} = $idx;
12559:                 unless ($changes{$itemid}) {
12560:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12561:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12562:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
12563:                                 $changes{$itemid} = 1;
12564:                             }
12565:                         }
12566:                     }
12567:                 }
12568:                 $idx ++;
12569:             }
12570:         }
12571:     }
12572:     if (keys(%changes)) {
12573:         my %defaultshash = (
12574:                               ipaccess => \%confhash,
12575:                            );
12576:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12577:                                                  $dom);
12578:         if ($putresult eq 'ok') {
12579:             my $cachetime = 1800;
12580:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
12581:             if (ref($lastactref) eq 'HASH') {
12582:                 $lastactref->{'ipaccess'} = 1;
12583:             }
12584:             $resulttext = &mt('Changes made:').'<ul>';
12585:             my %bynum;
12586:             foreach my $itemid (sort(keys(%changes))) {
12587:                 if (ref($confhash{$itemid}) eq 'HASH') {
12588:                     my $position = $confhash{$itemid}{'order'};
12589:                     if ($position =~ /^\d+$/) {
12590:                         $bynum{$position} = $itemid;
12591:                     }
12592:                 }
12593:             }
12594:             if (keys(%deletions)) {
12595:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
12596:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12597:                 }
12598:             }
12599:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12600:                 my $itemid = $bynum{$pos};
12601:                 if (ref($confhash{$itemid}) eq 'HASH') {
12602:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
12603:                     my $position = $pos + 1;
12604:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12605:                     if ($confhash{$itemid}{'ip'} eq '') {
12606:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
12607:                     } else {
12608:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
12609:                     }
12610:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
12611:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
12612:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
12613:                                        '</li>';
12614:                     } else {
12615:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
12616:                     }
12617:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
12618:                         my @courses;
12619:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
12620:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1}); 
12621:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
12622:                         }
12623:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
12624:                                              join('</li><li>',@courses).'</li></ul>';
12625:                     } else {
12626:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
12627:                     }
12628:                     $resulttext .= '</ul></li>';
12629:                 }
12630:             }
12631:             $resulttext .= '</ul>';
12632:         } else {
12633:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12634:         }
12635:     } else {
12636:         $resulttext = &mt('No changes made');
12637:     }
12638:     if ($errors) {
12639:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
12640:                        $errors.'</ul></p>';
12641:     }
12642:     return $resulttext;
12643: }
12644: 
12645: sub get_ipaccess_id {
12646:     my ($domain,$location) = @_;
12647:     # get lock on ipaccess db
12648:     my $lockhash = {
12649:                       lock => $env{'user.name'}.
12650:                               ':'.$env{'user.domain'},
12651:                    };
12652:     my $tries = 0;
12653:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12654:     my ($id,$error);
12655: 
12656:     while (($gotlock ne 'ok') && ($tries<10)) {
12657:         $tries ++;
12658:         sleep (0.1);
12659:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12660:     }
12661:     if ($gotlock eq 'ok') {
12662:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
12663:         if ($currids{'lock'}) {
12664:             delete($currids{'lock'});
12665:             if (keys(%currids)) {
12666:                 my @curr = sort { $a <=> $b } keys(%currids);
12667:                 if ($curr[-1] =~ /^\d+$/) {
12668:                     $id = 1 + $curr[-1];
12669:                 }
12670:             } else {
12671:                 $id = 1;
12672:             }
12673:             if ($id) {
12674:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
12675:                     $error = 'nostore';
12676:                 }
12677:             } else {
12678:                 $error = 'nonumber';
12679:             }
12680:         }
12681:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
12682:     } else {
12683:         $error = 'nolock';
12684:     }
12685:     return ($id,$error);
12686: }
12687: 
12688: sub modify_rolecolors {
12689:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
12690:     my ($resulttext,%rolehash);
12691:     $rolehash{'rolecolors'} = {};
12692:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
12693:         if ($domconfig{'rolecolors'} eq '') {
12694:             $domconfig{'rolecolors'} = {};
12695:         }
12696:     }
12697:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
12698:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
12699:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
12700:                                              $dom);
12701:     if ($putresult eq 'ok') {
12702:         if (keys(%changes) > 0) {
12703:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12704:             if (ref($lastactref) eq 'HASH') {
12705:                 $lastactref->{'domainconfig'} = 1;
12706:             }
12707:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
12708:                                              $rolehash{'rolecolors'});
12709:         } else {
12710:             $resulttext = &mt('No changes made to default color schemes');
12711:         }
12712:     } else {
12713:         $resulttext = '<span class="LC_error">'.
12714: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12715:     }
12716:     if ($errors) {
12717:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12718:                        $errors.'</ul>';
12719:     }
12720:     return $resulttext;
12721: }
12722: 
12723: sub modify_colors {
12724:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
12725:     my (%changes,%choices);
12726:     my @bgs;
12727:     my @links = ('link','alink','vlink');
12728:     my @logintext;
12729:     my @images;
12730:     my $servadm = $r->dir_config('lonAdmEMail');
12731:     my $errors;
12732:     my %defaults;
12733:     foreach my $role (@{$roles}) {
12734:         if ($role eq 'login') {
12735:             %choices = &login_choices();
12736:             @logintext = ('textcol','bgcol');
12737:         } else {
12738:             %choices = &color_font_choices();
12739:         }
12740:         if ($role eq 'login') {
12741:             @images = ('img','logo','domlogo','login');
12742:             @bgs = ('pgbg','mainbg','sidebg');
12743:         } else {
12744:             @images = ('img');
12745:             @bgs = ('pgbg','tabbg','sidebg');
12746:         }
12747:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
12748:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
12749:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
12750:         }
12751:         if ($role eq 'login') {
12752:             foreach my $item (@logintext) {
12753:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12754:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12755:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12756:                 }
12757:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
12758:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12759:                 }
12760:             }
12761:         } else {
12762:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
12763:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
12764:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
12765:             }
12766:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
12767:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
12768:             }
12769:         }
12770:         foreach my $item (@bgs) {
12771:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12772:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12773:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12774:             }
12775:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
12776:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12777:             }
12778:         }
12779:         foreach my $item (@links) {
12780:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12781:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12782:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12783:             }
12784:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
12785:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12786:             }
12787:         }
12788:         my ($configuserok,$author_ok,$switchserver) = 
12789:             &config_check($dom,$confname,$servadm);
12790:         my ($width,$height) = &thumb_dimensions();
12791:         if (ref($domconfig->{$role}) ne 'HASH') {
12792:             $domconfig->{$role} = {};
12793:         }
12794:         foreach my $img (@images) {
12795:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
12796:                 if (defined($env{'form.login_showlogo_'.$img})) {
12797:                     $confhash->{$role}{'showlogo'}{$img} = 1;
12798:                 } else { 
12799:                     $confhash->{$role}{'showlogo'}{$img} = 0;
12800:                 }
12801:             } 
12802: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
12803: 		 && !defined($domconfig->{$role}{$img})
12804: 		 && !$env{'form.'.$role.'_del_'.$img}
12805: 		 && $env{'form.'.$role.'_import_'.$img}) {
12806: 		# import the old configured image from the .tab setting
12807: 		# if they haven't provided a new one 
12808: 		$domconfig->{$role}{$img} = 
12809: 		    $env{'form.'.$role.'_import_'.$img};
12810: 	    }
12811:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
12812:                 my $error;
12813:                 if ($configuserok eq 'ok') {
12814:                     if ($switchserver) {
12815:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
12816:                     } else {
12817:                         if ($author_ok eq 'ok') {
12818:                             my ($result,$logourl) = 
12819:                                 &publishlogo($r,'upload',$role.'_'.$img,
12820:                                            $dom,$confname,$img,$width,$height);
12821:                             if ($result eq 'ok') {
12822:                                 $confhash->{$role}{$img} = $logourl;
12823:                                 $changes{$role}{'images'}{$img} = 1;
12824:                             } else {
12825:                                 $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);
12826:                             }
12827:                         } else {
12828:                             $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);
12829:                         }
12830:                     }
12831:                 } else {
12832:                     $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);
12833:                 }
12834:                 if ($error) {
12835:                     &Apache::lonnet::logthis($error);
12836:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12837:                 }
12838:             } elsif ($domconfig->{$role}{$img} ne '') {
12839:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
12840:                     my $error;
12841:                     if ($configuserok eq 'ok') {
12842: # is confname an author?
12843:                         if ($switchserver eq '') {
12844:                             if ($author_ok eq 'ok') {
12845:                                 my ($result,$logourl) = 
12846:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
12847:                                             $dom,$confname,$img,$width,$height);
12848:                                 if ($result eq 'ok') {
12849:                                     $confhash->{$role}{$img} = $logourl;
12850: 				    $changes{$role}{'images'}{$img} = 1;
12851:                                 }
12852:                             }
12853:                         }
12854:                     }
12855:                 }
12856:             }
12857:         }
12858:         if (ref($domconfig) eq 'HASH') {
12859:             if (ref($domconfig->{$role}) eq 'HASH') {
12860:                 foreach my $img (@images) {
12861:                     if ($domconfig->{$role}{$img} ne '') {
12862:                         if ($env{'form.'.$role.'_del_'.$img}) {
12863:                             $confhash->{$role}{$img} = '';
12864:                             $changes{$role}{'images'}{$img} = 1;
12865:                         } else {
12866:                             if ($confhash->{$role}{$img} eq '') {
12867:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
12868:                             }
12869:                         }
12870:                     } else {
12871:                         if ($env{'form.'.$role.'_del_'.$img}) {
12872:                             $confhash->{$role}{$img} = '';
12873:                             $changes{$role}{'images'}{$img} = 1;
12874:                         } 
12875:                     }
12876:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
12877:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
12878:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
12879:                                 $domconfig->{$role}{'showlogo'}{$img}) {
12880:                                 $changes{$role}{'showlogo'}{$img} = 1; 
12881:                             }
12882:                         } else {
12883:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12884:                                 $changes{$role}{'showlogo'}{$img} = 1;
12885:                             }
12886:                         }
12887:                     }
12888:                 }
12889:                 if ($domconfig->{$role}{'font'} ne '') {
12890:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
12891:                         $changes{$role}{'font'} = 1;
12892:                     }
12893:                 } else {
12894:                     if ($confhash->{$role}{'font'}) {
12895:                         $changes{$role}{'font'} = 1;
12896:                     }
12897:                 }
12898:                 if ($role ne 'login') {
12899:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
12900:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
12901:                             $changes{$role}{'fontmenu'} = 1;
12902:                         }
12903:                     } else {
12904:                         if ($confhash->{$role}{'fontmenu'}) {
12905:                             $changes{$role}{'fontmenu'} = 1;
12906:                         }
12907:                     }
12908:                 }
12909:                 foreach my $item (@bgs) {
12910:                     if ($domconfig->{$role}{$item} ne '') {
12911:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12912:                             $changes{$role}{'bgs'}{$item} = 1;
12913:                         } 
12914:                     } else {
12915:                         if ($confhash->{$role}{$item}) {
12916:                             $changes{$role}{'bgs'}{$item} = 1;
12917:                         }
12918:                     }
12919:                 }
12920:                 foreach my $item (@links) {
12921:                     if ($domconfig->{$role}{$item} ne '') {
12922:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12923:                             $changes{$role}{'links'}{$item} = 1;
12924:                         }
12925:                     } else {
12926:                         if ($confhash->{$role}{$item}) {
12927:                             $changes{$role}{'links'}{$item} = 1;
12928:                         }
12929:                     }
12930:                 }
12931:                 foreach my $item (@logintext) {
12932:                     if ($domconfig->{$role}{$item} ne '') {
12933:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12934:                             $changes{$role}{'logintext'}{$item} = 1;
12935:                         }
12936:                     } else {
12937:                         if ($confhash->{$role}{$item}) {
12938:                             $changes{$role}{'logintext'}{$item} = 1;
12939:                         }
12940:                     }
12941:                 }
12942:             } else {
12943:                 &default_change_checker($role,\@images,\@links,\@bgs,
12944:                                         \@logintext,$confhash,\%changes); 
12945:             }
12946:         } else {
12947:             &default_change_checker($role,\@images,\@links,\@bgs,
12948:                                     \@logintext,$confhash,\%changes); 
12949:         }
12950:     }
12951:     return ($errors,%changes);
12952: }
12953: 
12954: sub config_check {
12955:     my ($dom,$confname,$servadm) = @_;
12956:     my ($configuserok,$author_ok,$switchserver,%currroles);
12957:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
12958:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
12959:                                                    $confname,$servadm);
12960:     if ($configuserok eq 'ok') {
12961:         $switchserver = &check_switchserver($dom,$confname);
12962:         if ($switchserver eq '') {
12963:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
12964:         }
12965:     }
12966:     return ($configuserok,$author_ok,$switchserver);
12967: }
12968: 
12969: sub default_change_checker {
12970:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
12971:     foreach my $item (@{$links}) {
12972:         if ($confhash->{$role}{$item}) {
12973:             $changes->{$role}{'links'}{$item} = 1;
12974:         }
12975:     }
12976:     foreach my $item (@{$bgs}) {
12977:         if ($confhash->{$role}{$item}) {
12978:             $changes->{$role}{'bgs'}{$item} = 1;
12979:         }
12980:     }
12981:     foreach my $item (@{$logintext}) {
12982:         if ($confhash->{$role}{$item}) {
12983:             $changes->{$role}{'logintext'}{$item} = 1;
12984:         }
12985:     }
12986:     foreach my $img (@{$images}) {
12987:         if ($env{'form.'.$role.'_del_'.$img}) {
12988:             $confhash->{$role}{$img} = '';
12989:             $changes->{$role}{'images'}{$img} = 1;
12990:         }
12991:         if ($role eq 'login') {
12992:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12993:                 $changes->{$role}{'showlogo'}{$img} = 1;
12994:             }
12995:         }
12996:     }
12997:     if ($confhash->{$role}{'font'}) {
12998:         $changes->{$role}{'font'} = 1;
12999:     }
13000: }
13001: 
13002: sub display_colorchgs {
13003:     my ($dom,$changes,$roles,$confhash) = @_;
13004:     my (%choices,$resulttext);
13005:     if (!grep(/^login$/,@{$roles})) {
13006:         $resulttext = &mt('Changes made:').'<br />';
13007:     }
13008:     foreach my $role (@{$roles}) {
13009:         if ($role eq 'login') {
13010:             %choices = &login_choices();
13011:         } else {
13012:             %choices = &color_font_choices();
13013:         }
13014:         if (ref($changes->{$role}) eq 'HASH') {
13015:             if ($role ne 'login') {
13016:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
13017:             }
13018:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
13019:                 if ($role ne 'login') {
13020:                     $resulttext .= '<ul>';
13021:                 }
13022:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
13023:                     if ($role ne 'login') {
13024:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
13025:                     }
13026:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
13027:                         if (($role eq 'login') && ($key eq 'showlogo')) {
13028:                             if ($confhash->{$role}{$key}{$item}) {
13029:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
13030:                             } else {
13031:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
13032:                             }
13033:                         } elsif ($confhash->{$role}{$item} eq '') {
13034:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
13035:                         } else {
13036:                             my $newitem = $confhash->{$role}{$item};
13037:                             if ($key eq 'images') {
13038:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
13039:                             }
13040:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
13041:                         }
13042:                     }
13043:                     if ($role ne 'login') {
13044:                         $resulttext .= '</ul></li>';
13045:                     }
13046:                 } else {
13047:                     if ($confhash->{$role}{$key} eq '') {
13048:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
13049:                     } else {
13050:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
13051:                     }
13052:                 }
13053:                 if ($role ne 'login') {
13054:                     $resulttext .= '</ul>';
13055:                 }
13056:             }
13057:         }
13058:     }
13059:     return $resulttext;
13060: }
13061: 
13062: sub thumb_dimensions {
13063:     return ('200','50');
13064: }
13065: 
13066: sub check_dimensions {
13067:     my ($inputfile) = @_;
13068:     my ($fullwidth,$fullheight);
13069:     if ($inputfile =~ m|^[/\w.\-]+$|) {
13070:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
13071:             my $imageinfo = <PIPE>;
13072:             if (!close(PIPE)) {
13073:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
13074:             }
13075:             chomp($imageinfo);
13076:             my ($fullsize) = 
13077:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
13078:             if ($fullsize) {
13079:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
13080:             }
13081:         }
13082:     }
13083:     return ($fullwidth,$fullheight);
13084: }
13085: 
13086: sub check_configuser {
13087:     my ($uhome,$dom,$confname,$servadm) = @_;
13088:     my ($configuserok,%currroles);
13089:     if ($uhome eq 'no_host') {
13090:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
13091:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
13092:         $configuserok = 
13093:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
13094:                              $configpass,'','','','','',undef,$servadm);
13095:     } else {
13096:         $configuserok = 'ok';
13097:         %currroles = 
13098:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
13099:     }
13100:     return ($configuserok,%currroles);
13101: }
13102: 
13103: sub check_authorstatus {
13104:     my ($dom,$confname,%currroles) = @_;
13105:     my $author_ok;
13106:     if (!$currroles{':'.$dom.':au'}) {
13107:         my $start = time;
13108:         my $end = 0;
13109:         $author_ok = 
13110:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
13111:                                         'au',$end,$start,'','','domconfig');
13112:     } else {
13113:         $author_ok = 'ok';
13114:     }
13115:     return $author_ok;
13116: }
13117: 
13118: sub publishlogo {
13119:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
13120:     my ($output,$fname,$logourl,$madethumb);
13121:     if ($action eq 'upload') {
13122:         $fname=$env{'form.'.$formname.'.filename'};
13123:         chop($env{'form.'.$formname});
13124:     } else {
13125:         ($fname) = ($formname =~ /([^\/]+)$/);
13126:     }
13127:     if ($savefileas ne '') {
13128:         $fname = $savefileas;
13129:     }
13130:     $fname=&Apache::lonnet::clean_filename($fname);
13131: # See if there is anything left
13132:     unless ($fname) { return ('error: no uploaded file'); }
13133:     $fname="$subdir/$fname";
13134:     my $docroot=$r->dir_config('lonDocRoot');
13135:     my $filepath="$docroot/priv";
13136:     my $relpath = "$dom/$confname";
13137:     my ($fnamepath,$file,$fetchthumb);
13138:     $file=$fname;
13139:     if ($fname=~m|/|) {
13140:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
13141:     }
13142:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
13143:     my $count;
13144:     for ($count=5;$count<=$#parts;$count++) {
13145:         $filepath.="/$parts[$count]";
13146:         if ((-e $filepath)!=1) {
13147:             mkdir($filepath,02770);
13148:         }
13149:     }
13150:     # Check for bad extension and disallow upload
13151:     if ($file=~/\.(\w+)$/ &&
13152:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
13153:         $output = 
13154:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
13155:     } elsif ($file=~/\.(\w+)$/ &&
13156:         !defined(&Apache::loncommon::fileembstyle($1))) {
13157:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
13158:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
13159:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
13160:     } elsif (-d "$filepath/$file") {
13161:         $output = &mt('Filename is a directory name - rename the file and re-upload');
13162:     } else {
13163:         my $source = $filepath.'/'.$file;
13164:         my $logfile;
13165:         if (!open($logfile,">>",$source.'.log')) {
13166:             return (&mt('No write permission to Authoring Space'));
13167:         }
13168:         print $logfile
13169: "\n================= Publish ".localtime()." ================\n".
13170: $env{'user.name'}.':'.$env{'user.domain'}."\n";
13171: # Save the file
13172:         if (!open(FH,">",$source)) {
13173:             &Apache::lonnet::logthis('Failed to create '.$source);
13174:             return (&mt('Failed to create file'));
13175:         }
13176:         if ($action eq 'upload') {
13177:             if (!print FH ($env{'form.'.$formname})) {
13178:                 &Apache::lonnet::logthis('Failed to write to '.$source);
13179:                 return (&mt('Failed to write file'));
13180:             }
13181:         } else {
13182:             my $original = &Apache::lonnet::filelocation('',$formname);
13183:             if(!copy($original,$source)) {
13184:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
13185:                 return (&mt('Failed to write file'));
13186:             }
13187:         }
13188:         close(FH);
13189:         chmod(0660, $source); # Permissions to rw-rw---.
13190: 
13191:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
13192:         my $copyfile=$targetdir.'/'.$file;
13193: 
13194:         my @parts=split(/\//,$targetdir);
13195:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
13196:         for (my $count=5;$count<=$#parts;$count++) {
13197:             $path.="/$parts[$count]";
13198:             if (!-e $path) {
13199:                 print $logfile "\nCreating directory ".$path;
13200:                 mkdir($path,02770);
13201:             }
13202:         }
13203:         my $versionresult;
13204:         if (-e $copyfile) {
13205:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
13206:         } else {
13207:             $versionresult = 'ok';
13208:         }
13209:         if ($versionresult eq 'ok') {
13210:             if (copy($source,$copyfile)) {
13211:                 print $logfile "\nCopied original source to ".$copyfile."\n";
13212:                 $output = 'ok';
13213:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
13214:                 push(@{$modified_urls},[$copyfile,$source]);
13215:                 my $metaoutput = 
13216:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
13217:                 unless ($registered_cleanup) {
13218:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
13219:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
13220:                     $registered_cleanup=1;
13221:                 }
13222:             } else {
13223:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
13224:                 $output = &mt('Failed to copy file to RES space').", $!";
13225:             }
13226:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
13227:                 my $inputfile = $filepath.'/'.$file;
13228:                 my $outfile = $filepath.'/'.'tn-'.$file;
13229:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
13230:                 if ($fullwidth ne '' && $fullheight ne '') { 
13231:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
13232:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
13233:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
13234:                         system({$args[0]} @args);
13235:                         chmod(0660, $filepath.'/tn-'.$file);
13236:                         if (-e $outfile) {
13237:                             my $copyfile=$targetdir.'/tn-'.$file;
13238:                             if (copy($outfile,$copyfile)) {
13239:                                 print $logfile "\nCopied source to ".$copyfile."\n";
13240:                                 my $thumb_metaoutput = 
13241:                                     &write_metadata($dom,$confname,$formname,
13242:                                                     $targetdir,'tn-'.$file,$logfile);
13243:                                 push(@{$modified_urls},[$copyfile,$outfile]);
13244:                                 unless ($registered_cleanup) {
13245:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
13246:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
13247:                                     $registered_cleanup=1;
13248:                                 }
13249:                                 $madethumb = 1;
13250:                             } else {
13251:                                 print $logfile "\nUnable to write ".$copyfile.
13252:                                                ':'.$!."\n";
13253:                             }
13254:                         }
13255:                     }
13256:                 }
13257:             }
13258:         } else {
13259:             $output = $versionresult;
13260:         }
13261:     }
13262:     return ($output,$logourl,$madethumb);
13263: }
13264: 
13265: sub logo_versioning {
13266:     my ($targetdir,$file,$logfile) = @_;
13267:     my $target = $targetdir.'/'.$file;
13268:     my ($maxversion,$fn,$extn,$output);
13269:     $maxversion = 0;
13270:     if ($file =~ /^(.+)\.(\w+)$/) {
13271:         $fn=$1;
13272:         $extn=$2;
13273:     }
13274:     opendir(DIR,$targetdir);
13275:     while (my $filename=readdir(DIR)) {
13276:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
13277:             $maxversion=($1>$maxversion)?$1:$maxversion;
13278:         }
13279:     }
13280:     $maxversion++;
13281:     print $logfile "\nCreating old version ".$maxversion."\n";
13282:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
13283:     if (copy($target,$copyfile)) {
13284:         print $logfile "Copied old target to ".$copyfile."\n";
13285:         $copyfile=$copyfile.'.meta';
13286:         if (copy($target.'.meta',$copyfile)) {
13287:             print $logfile "Copied old target metadata to ".$copyfile."\n";
13288:             $output = 'ok';
13289:         } else {
13290:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
13291:             $output = &mt('Failed to copy old meta').", $!, ";
13292:         }
13293:     } else {
13294:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
13295:         $output = &mt('Failed to copy old target').", $!, ";
13296:     }
13297:     return $output;
13298: }
13299: 
13300: sub write_metadata {
13301:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
13302:     my (%metadatafields,%metadatakeys,$output);
13303:     $metadatafields{'title'}=$formname;
13304:     $metadatafields{'creationdate'}=time;
13305:     $metadatafields{'lastrevisiondate'}=time;
13306:     $metadatafields{'copyright'}='public';
13307:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
13308:                                          $env{'user.domain'};
13309:     $metadatafields{'authorspace'}=$confname.':'.$dom;
13310:     $metadatafields{'domain'}=$dom;
13311:     {
13312:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
13313:         my $mfh;
13314:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
13315:             foreach (sort(keys(%metadatafields))) {
13316:                 unless ($_=~/\./) {
13317:                     my $unikey=$_;
13318:                     $unikey=~/^([A-Za-z]+)/;
13319:                     my $tag=$1;
13320:                     $tag=~tr/A-Z/a-z/;
13321:                     print $mfh "\n\<$tag";
13322:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
13323:                         my $value=$metadatafields{$unikey.'.'.$_};
13324:                         $value=~s/\"/\'\'/g;
13325:                         print $mfh ' '.$_.'="'.$value.'"';
13326:                     }
13327:                     print $mfh '>'.
13328:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
13329:                             .'</'.$tag.'>';
13330:                 }
13331:             }
13332:             $output = 'ok';
13333:             print $logfile "\nWrote metadata";
13334:             close($mfh);
13335:         } else {
13336:             print $logfile "\nFailed to open metadata file";
13337:             $output = &mt('Could not write metadata');
13338:         }
13339:     }
13340:     return $output;
13341: }
13342: 
13343: sub notifysubscribed {
13344:     foreach my $targetsource (@{$modified_urls}){
13345:         next unless (ref($targetsource) eq 'ARRAY');
13346:         my ($target,$source)=@{$targetsource};
13347:         if ($source ne '') {
13348:             if (open(my $logfh,">>",$source.'.log')) {
13349:                 print $logfh "\nCleanup phase: Notifications\n";
13350:                 my @subscribed=&subscribed_hosts($target);
13351:                 foreach my $subhost (@subscribed) {
13352:                     print $logfh "\nNotifying host ".$subhost.':';
13353:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
13354:                     print $logfh $reply;
13355:                 }
13356:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
13357:                 foreach my $subhost (@subscribedmeta) {
13358:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
13359:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
13360:                                                         $subhost);
13361:                     print $logfh $reply;
13362:                 }
13363:                 print $logfh "\n============ Done ============\n";
13364:                 close($logfh);
13365:             }
13366:         }
13367:     }
13368:     return OK;
13369: }
13370: 
13371: sub subscribed_hosts {
13372:     my ($target) = @_;
13373:     my @subscribed;
13374:     if (open(my $fh,"<","$target.subscription")) {
13375:         while (my $subline=<$fh>) {
13376:             if ($subline =~ /^($match_lonid):/) {
13377:                 my $host = $1;
13378:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
13379:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
13380:                         push(@subscribed,$host);
13381:                     }
13382:                 }
13383:             }
13384:         }
13385:     }
13386:     return @subscribed;
13387: }
13388: 
13389: sub check_switchserver {
13390:     my ($dom,$confname) = @_;
13391:     my ($allowed,$switchserver);
13392:     my $home = &Apache::lonnet::homeserver($confname,$dom);
13393:     if ($home eq 'no_host') {
13394:         $home = &Apache::lonnet::domain($dom,'primary');
13395:     }
13396:     my @ids=&Apache::lonnet::current_machine_ids();
13397:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
13398:     if (!$allowed) {
13399: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
13400:     }
13401:     return $switchserver;
13402: }
13403: 
13404: sub modify_quotas {
13405:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13406:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
13407:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
13408:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
13409:         $validationfieldsref);
13410:     if ($action eq 'quotas') {
13411:         $context = 'tools'; 
13412:     } else {
13413:         $context = $action;
13414:     }
13415:     if ($context eq 'requestcourses') {
13416:         @usertools = ('official','unofficial','community','textbook','placement','lti');
13417:         @options =('norequest','approval','validate','autolimit');
13418:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
13419:         %titles = &courserequest_titles();
13420:         $toolregexp = join('|',@usertools);
13421:         %conditions = &courserequest_conditions();
13422:         $confname = $dom.'-domainconfig';
13423:         my $servadm = $r->dir_config('lonAdmEMail');
13424:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13425:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
13426:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
13427:     } elsif ($context eq 'requestauthor') {
13428:         @usertools = ('author');
13429:         %titles = &authorrequest_titles();
13430:     } else {
13431:         @usertools = ('aboutme','blog','webdav','portfolio');
13432:         %titles = &tool_titles();
13433:     }
13434:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13435:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13436:     foreach my $key (keys(%env)) {
13437:         if ($context eq 'requestcourses') {
13438:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
13439:                 my $item = $1;
13440:                 my $type = $2;
13441:                 if ($type =~ /^limit_(.+)/) {
13442:                     $limithash{$item}{$1} = $env{$key};
13443:                 } else {
13444:                     $confhash{$item}{$type} = $env{$key};
13445:                 }
13446:             }
13447:         } elsif ($context eq 'requestauthor') {
13448:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
13449:                 $confhash{$1} = $env{$key};
13450:             }
13451:         } else {
13452:             if ($key =~ /^form\.quota_(.+)$/) {
13453:                 $confhash{'defaultquota'}{$1} = $env{$key};
13454:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
13455:                 $confhash{'authorquota'}{$1} = $env{$key};
13456:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
13457:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
13458:             }
13459:         }
13460:     }
13461:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13462:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
13463:         @approvalnotify = sort(@approvalnotify);
13464:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
13465:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
13466:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
13467:         foreach my $type (@hasuniquecode) {
13468:             if (grep(/^\Q$type\E$/,@crstypes)) {
13469:                 $confhash{'uniquecode'}{$type} = 1;
13470:             }
13471:         }
13472:         my (%newbook,%allpos);
13473:         if ($context eq 'requestcourses') {
13474:             foreach my $type ('textbooks','templates') {
13475:                 @{$allpos{$type}} = (); 
13476:                 my $invalid;
13477:                 if ($type eq 'textbooks') {
13478:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
13479:                 } else {
13480:                     $invalid = &mt('Invalid LON-CAPA course for template');
13481:                 }
13482:                 if ($env{'form.'.$type.'_addbook'}) {
13483:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
13484:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
13485:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
13486:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
13487:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13488:                         } else {
13489:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
13490:                             my $position = $env{'form.'.$type.'_addbook_pos'};
13491:                             $position =~ s/\D+//g;
13492:                             if ($position ne '') {
13493:                                 $allpos{$type}[$position] = $newbook{$type};
13494:                             }
13495:                         }
13496:                     } else {
13497:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13498:                     }
13499:                 }
13500:             } 
13501:         }
13502:         if (ref($domconfig{$action}) eq 'HASH') {
13503:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
13504:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
13505:                     $changes{'notify'}{'approval'} = 1;
13506:                 }
13507:             } else {
13508:                 if ($confhash{'notify'}{'approval'}) {
13509:                     $changes{'notify'}{'approval'} = 1;
13510:                 }
13511:             }
13512:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
13513:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
13514:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
13515:                         unless ($confhash{'uniquecode'}{$crstype}) {
13516:                             $changes{'uniquecode'} = 1;
13517:                         }
13518:                     }
13519:                     unless ($changes{'uniquecode'}) {
13520:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
13521:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
13522:                                 $changes{'uniquecode'} = 1;
13523:                             }
13524:                         }
13525:                     }
13526:                } else {
13527:                    $changes{'uniquecode'} = 1;
13528:                }
13529:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
13530:                 $changes{'uniquecode'} = 1;
13531:             }
13532:             if ($context eq 'requestcourses') {
13533:                 foreach my $type ('textbooks','templates') {
13534:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
13535:                         my %deletions;
13536:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
13537:                         if (@todelete) {
13538:                             map { $deletions{$_} = 1; } @todelete;
13539:                         }
13540:                         my %imgdeletions;
13541:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
13542:                         if (@todeleteimages) {
13543:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
13544:                         }
13545:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
13546:                         for (my $i=0; $i<=$maxnum; $i++) {
13547:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
13548:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
13549:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
13550:                                 if ($deletions{$key}) {
13551:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
13552:                                         #FIXME need to obsolete item in RES space
13553:                                     }
13554:                                     next;
13555:                                 } else {
13556:                                     my $newpos = $env{'form.'.$itemid};
13557:                                     $newpos =~ s/\D+//g;
13558:                                     foreach my $item ('subject','title','publisher','author') {
13559:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13560:                                                  ($type eq 'templates'));
13561:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
13562:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
13563:                                             $changes{$type}{$key} = 1;
13564:                                         }
13565:                                     }
13566:                                     $allpos{$type}[$newpos] = $key;
13567:                                 }
13568:                                 if ($imgdeletions{$key}) {
13569:                                     $changes{$type}{$key} = 1;
13570:                                     #FIXME need to obsolete item in RES space
13571:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
13572:                                     my ($cdom,$cnum) = split(/_/,$key);
13573:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13574:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13575:                                     } else {
13576:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
13577:                                                                                       $cdom,$cnum,$type,$configuserok,
13578:                                                                                       $switchserver,$author_ok);
13579:                                         if ($imgurl) {
13580:                                             $confhash{$type}{$key}{'image'} = $imgurl;
13581:                                             $changes{$type}{$key} = 1; 
13582:                                         }
13583:                                         if ($error) {
13584:                                             &Apache::lonnet::logthis($error);
13585:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13586:                                         }
13587:                                     }
13588:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
13589:                                     $confhash{$type}{$key}{'image'} = 
13590:                                         $domconfig{$action}{$type}{$key}{'image'};
13591:                                 }
13592:                             }
13593:                         }
13594:                     }
13595:                 }
13596:             }
13597:         } else {
13598:             if ($confhash{'notify'}{'approval'}) {
13599:                 $changes{'notify'}{'approval'} = 1;
13600:             }
13601:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
13602:                 $changes{'uniquecode'} = 1;
13603:             }
13604:         }
13605:         if ($context eq 'requestcourses') {
13606:             foreach my $type ('textbooks','templates') {
13607:                 if ($newbook{$type}) {
13608:                     $changes{$type}{$newbook{$type}} = 1;
13609:                     foreach my $item ('subject','title','publisher','author') {
13610:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13611:                                  ($type eq 'template'));
13612:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
13613:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
13614:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
13615:                         }
13616:                     }
13617:                     if ($type eq 'textbooks') {
13618:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
13619:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
13620:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13621:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13622:                             } else {
13623:                                 my ($imageurl,$error) =
13624:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
13625:                                                             $configuserok,$switchserver,$author_ok);
13626:                                 if ($imageurl) {
13627:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
13628:                                 }
13629:                                 if ($error) {
13630:                                     &Apache::lonnet::logthis($error);
13631:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13632:                                 }
13633:                             }
13634:                         }
13635:                     }
13636:                 }
13637:                 if (@{$allpos{$type}} > 0) {
13638:                     my $idx = 0;
13639:                     foreach my $item (@{$allpos{$type}}) {
13640:                         if ($item ne '') {
13641:                             $confhash{$type}{$item}{'order'} = $idx;
13642:                             if (ref($domconfig{$action}) eq 'HASH') {
13643:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
13644:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
13645:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
13646:                                             $changes{$type}{$item} = 1;
13647:                                         }
13648:                                     }
13649:                                 }
13650:                             }
13651:                             $idx ++;
13652:                         }
13653:                     }
13654:                 }
13655:             }
13656:             if (ref($validationitemsref) eq 'ARRAY') {
13657:                 foreach my $item (@{$validationitemsref}) {
13658:                     if ($item eq 'fields') {
13659:                         my @changed;
13660:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
13661:                         if (@{$confhash{'validation'}{$item}} > 0) {
13662:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
13663:                         }
13664:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13665:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13666:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
13667:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
13668:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
13669:                                 } else {
13670:                                     @changed = @{$confhash{'validation'}{$item}};
13671:                                 }
13672:                             } else {
13673:                                 @changed = @{$confhash{'validation'}{$item}};
13674:                             }
13675:                         } else {
13676:                             @changed = @{$confhash{'validation'}{$item}};
13677:                         }
13678:                         if (@changed) {
13679:                             if ($confhash{'validation'}{$item}) {
13680:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
13681:                             } else {
13682:                                 $changes{'validation'}{$item} = &mt('None');
13683:                             }
13684:                         }
13685:                     } else {
13686:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
13687:                         if ($item eq 'markup') {
13688:                             if ($env{'form.requestcourses_validation_'.$item}) {
13689:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13690:                             }
13691:                         }
13692:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13693:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13694:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
13695:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13696:                                 }
13697:                             } else {
13698:                                 if ($confhash{'validation'}{$item} ne '') {
13699:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13700:                                 }
13701:                             }
13702:                         } else {
13703:                             if ($confhash{'validation'}{$item} ne '') {
13704:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13705:                             }
13706:                         }
13707:                     }
13708:                 }
13709:             }
13710:             if ($env{'form.validationdc'}) {
13711:                 my $newval = $env{'form.validationdc'};
13712:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13713:                 if (exists($domcoords{$newval})) {
13714:                     $confhash{'validation'}{'dc'} = $newval;
13715:                 }
13716:             }
13717:             if (ref($confhash{'validation'}) eq 'HASH') {
13718:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13719:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13720:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13721:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
13722:                                 if ($confhash{'validation'}{'dc'} eq '') {
13723:                                     $changes{'validation'}{'dc'} = &mt('None');
13724:                                 } else {
13725:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13726:                                 }
13727:                             }
13728:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
13729:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13730:                         }
13731:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
13732:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13733:                     }
13734:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
13735:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13736:                 }
13737:             } else {
13738:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13739:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13740:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13741:                             $changes{'validation'}{'dc'} = &mt('None');
13742:                         }
13743:                     }
13744:                 }
13745:             }
13746:         }
13747:     } else {
13748:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
13749:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
13750:     }
13751:     foreach my $item (@usertools) {
13752:         foreach my $type (@{$types},'default','_LC_adv') {
13753:             my $unset; 
13754:             if ($context eq 'requestcourses') {
13755:                 $unset = '0';
13756:                 if ($type eq '_LC_adv') {
13757:                     $unset = '';
13758:                 }
13759:                 if ($confhash{$item}{$type} eq 'autolimit') {
13760:                     $confhash{$item}{$type} .= '=';
13761:                     unless ($limithash{$item}{$type} =~ /\D/) {
13762:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
13763:                     }
13764:                 }
13765:             } elsif ($context eq 'requestauthor') {
13766:                 $unset = '0';
13767:                 if ($type eq '_LC_adv') {
13768:                     $unset = '';
13769:                 }
13770:             } else {
13771:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
13772:                     $confhash{$item}{$type} = 1;
13773:                 } else {
13774:                     $confhash{$item}{$type} = 0;
13775:                 }
13776:             }
13777:             if (ref($domconfig{$action}) eq 'HASH') {
13778:                 if ($action eq 'requestauthor') {
13779:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
13780:                         $changes{$type} = 1;
13781:                     }
13782:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
13783:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
13784:                         $changes{$item}{$type} = 1;
13785:                     }
13786:                 } else {
13787:                     if ($context eq 'requestcourses') {
13788:                         if ($confhash{$item}{$type} ne $unset) {
13789:                             $changes{$item}{$type} = 1;
13790:                         }
13791:                     } else {
13792:                         if (!$confhash{$item}{$type}) {
13793:                             $changes{$item}{$type} = 1;
13794:                         }
13795:                     }
13796:                 }
13797:             } else {
13798:                 if ($context eq 'requestcourses') {
13799:                     if ($confhash{$item}{$type} ne $unset) {
13800:                         $changes{$item}{$type} = 1;
13801:                     }
13802:                 } elsif ($context eq 'requestauthor') {
13803:                     if ($confhash{$type} ne $unset) {
13804:                         $changes{$type} = 1;
13805:                     }
13806:                 } else {
13807:                     if (!$confhash{$item}{$type}) {
13808:                         $changes{$item}{$type} = 1;
13809:                     }
13810:                 }
13811:             }
13812:         }
13813:     }
13814:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13815:         if (ref($domconfig{'quotas'}) eq 'HASH') {
13816:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13817:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
13818:                     if (exists($confhash{'defaultquota'}{$key})) {
13819:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
13820:                             $changes{'defaultquota'}{$key} = 1;
13821:                         }
13822:                     } else {
13823:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
13824:                     }
13825:                 }
13826:             } else {
13827:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
13828:                     if (exists($confhash{'defaultquota'}{$key})) {
13829:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
13830:                             $changes{'defaultquota'}{$key} = 1;
13831:                         }
13832:                     } else {
13833:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
13834:                     }
13835:                 }
13836:             }
13837:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13838:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
13839:                     if (exists($confhash{'authorquota'}{$key})) {
13840:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
13841:                             $changes{'authorquota'}{$key} = 1;
13842:                         }
13843:                     } else {
13844:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
13845:                     }
13846:                 }
13847:             }
13848:         }
13849:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
13850:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
13851:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13852:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13853:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
13854:                             $changes{'defaultquota'}{$key} = 1;
13855:                         }
13856:                     } else {
13857:                         if (!exists($domconfig{'quotas'}{$key})) {
13858:                             $changes{'defaultquota'}{$key} = 1;
13859:                         }
13860:                     }
13861:                 } else {
13862:                     $changes{'defaultquota'}{$key} = 1;
13863:                 }
13864:             }
13865:         }
13866:         if (ref($confhash{'authorquota'}) eq 'HASH') {
13867:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
13868:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13869:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13870:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
13871:                             $changes{'authorquota'}{$key} = 1;
13872:                         }
13873:                     } else {
13874:                         $changes{'authorquota'}{$key} = 1;
13875:                     }
13876:                 } else {
13877:                     $changes{'authorquota'}{$key} = 1;
13878:                 }
13879:             }
13880:         }
13881:     }
13882: 
13883:     if ($context eq 'requestauthor') {
13884:         $domdefaults{'requestauthor'} = \%confhash;
13885:     } else {
13886:         foreach my $key (keys(%confhash)) {
13887:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
13888:                 $domdefaults{$key} = $confhash{$key};
13889:             }
13890:         }
13891:     }
13892: 
13893:     my %quotahash = (
13894:                       $action => { %confhash }
13895:                     );
13896:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
13897:                                              $dom);
13898:     if ($putresult eq 'ok') {
13899:         if (keys(%changes) > 0) {
13900:             my $cachetime = 24*60*60;
13901:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13902:             if (ref($lastactref) eq 'HASH') {
13903:                 $lastactref->{'domdefaults'} = 1;
13904:             }
13905:             $resulttext = &mt('Changes made:').'<ul>';
13906:             unless (($context eq 'requestcourses') ||
13907:                     ($context eq 'requestauthor')) {
13908:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
13909:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
13910:                     foreach my $type (@{$types},'default') {
13911:                         if (defined($changes{'defaultquota'}{$type})) {
13912:                             my $typetitle = $usertypes->{$type};
13913:                             if ($type eq 'default') {
13914:                                 $typetitle = $othertitle;
13915:                             }
13916:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
13917:                         }
13918:                     }
13919:                     $resulttext .= '</ul></li>';
13920:                 }
13921:                 if (ref($changes{'authorquota'}) eq 'HASH') {
13922:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
13923:                     foreach my $type (@{$types},'default') {
13924:                         if (defined($changes{'authorquota'}{$type})) {
13925:                             my $typetitle = $usertypes->{$type};
13926:                             if ($type eq 'default') {
13927:                                 $typetitle = $othertitle;
13928:                             }
13929:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
13930:                         }
13931:                     }
13932:                     $resulttext .= '</ul></li>';
13933:                 }
13934:             }
13935:             my %newenv;
13936:             foreach my $item (@usertools) {
13937:                 my (%haschgs,%inconf);
13938:                 if ($context eq 'requestauthor') {
13939:                     %haschgs = %changes;
13940:                     %inconf = %confhash;
13941:                 } else {
13942:                     if (ref($changes{$item}) eq 'HASH') {
13943:                         %haschgs = %{$changes{$item}};
13944:                     }
13945:                     if (ref($confhash{$item}) eq 'HASH') {
13946:                         %inconf = %{$confhash{$item}};
13947:                     }
13948:                 }
13949:                 if (keys(%haschgs) > 0) {
13950:                     my $newacc = 
13951:                         &Apache::lonnet::usertools_access($env{'user.name'},
13952:                                                           $env{'user.domain'},
13953:                                                           $item,'reload',$context);
13954:                     if (($context eq 'requestcourses') ||
13955:                         ($context eq 'requestauthor')) {
13956:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
13957:                             $newenv{'environment.canrequest.'.$item} = $newacc;
13958:                         }
13959:                     } else {
13960:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
13961:                             $newenv{'environment.availabletools.'.$item} = $newacc;
13962:                         }
13963:                     }
13964:                     unless ($context eq 'requestauthor') {
13965:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
13966:                     }
13967:                     foreach my $type (@{$types},'default','_LC_adv') {
13968:                         if ($haschgs{$type}) {
13969:                             my $typetitle = $usertypes->{$type};
13970:                             if ($type eq 'default') {
13971:                                 $typetitle = $othertitle;
13972:                             } elsif ($type eq '_LC_adv') {
13973:                                 $typetitle = 'LON-CAPA Advanced Users'; 
13974:                             }
13975:                             if ($inconf{$type}) {
13976:                                 if ($context eq 'requestcourses') {
13977:                                     my $cond;
13978:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
13979:                                         if ($1 eq '') {
13980:                                             $cond = &mt('(Automatic processing of any request).');
13981:                                         } else {
13982:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
13983:                                         }
13984:                                     } else { 
13985:                                         $cond = $conditions{$inconf{$type}};
13986:                                     }
13987:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
13988:                                 } elsif ($context eq 'requestauthor') {
13989:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
13990:                                                              $titles{$inconf{$type}},$typetitle);
13991: 
13992:                                 } else {
13993:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
13994:                                 }
13995:                             } else {
13996:                                 if ($type eq '_LC_adv') {
13997:                                     if ($inconf{$type} eq '0') {
13998:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13999:                                     } else { 
14000:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14001:                                     }
14002:                                 } else {
14003:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14004:                                 }
14005:                             }
14006:                         }
14007:                     }
14008:                     unless ($context eq 'requestauthor') {
14009:                         $resulttext .= '</ul></li>';
14010:                     }
14011:                 }
14012:             }
14013:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14014:                 if (ref($changes{'notify'}) eq 'HASH') {
14015:                     if ($changes{'notify'}{'approval'}) {
14016:                         if (ref($confhash{'notify'}) eq 'HASH') {
14017:                             if ($confhash{'notify'}{'approval'}) {
14018:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14019:                             } else {
14020:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14021:                             }
14022:                         }
14023:                     }
14024:                 }
14025:             }
14026:             if ($action eq 'requestcourses') {
14027:                 my @offon = ('off','on');
14028:                 if ($changes{'uniquecode'}) {
14029:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14030:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14031:                         $resulttext .= '<li>'.
14032:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14033:                                        '</li>';
14034:                     } else {
14035:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14036:                                        '</li>';
14037:                     }
14038:                 }
14039:                 foreach my $type ('textbooks','templates') {
14040:                     if (ref($changes{$type}) eq 'HASH') {
14041:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14042:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14043:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14044:                             my $coursetitle = $coursehash{'description'};
14045:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14046:                             $resulttext .= '<li>';
14047:                             foreach my $item ('subject','title','publisher','author') {
14048:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14049:                                          ($type eq 'templates'));
14050:                                 my $name = $item.':';
14051:                                 $name =~ s/^(\w)/\U$1/;
14052:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14053:                             }
14054:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14055:                             if ($type eq 'textbooks') {
14056:                                 if ($confhash{$type}{$key}{'image'}) {
14057:                                     $resulttext .= ' '.&mt('Image: [_1]',
14058:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14059:                                                    ' alt="Textbook cover" />').'<br />';
14060:                                 }
14061:                             }
14062:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14063:                         }
14064:                         $resulttext .= '</ul></li>';
14065:                     }
14066:                 }
14067:                 if (ref($changes{'validation'}) eq 'HASH') {
14068:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14069:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14070:                         foreach my $item (@{$validationitemsref}) {
14071:                             if (exists($changes{'validation'}{$item})) {
14072:                                 if ($item eq 'markup') {
14073:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14074:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14075:                                 } else {
14076:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14077:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14078:                                 }
14079:                             }
14080:                         }
14081:                         if (exists($changes{'validation'}{'dc'})) {
14082:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14083:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14084:                         }
14085:                     }
14086:                 }
14087:             }
14088:             $resulttext .= '</ul>';
14089:             if (keys(%newenv)) {
14090:                 &Apache::lonnet::appenv(\%newenv);
14091:             }
14092:         } else {
14093:             if ($context eq 'requestcourses') {
14094:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14095:             } elsif ($context eq 'requestauthor') {
14096:                 $resulttext = &mt('No changes made to rights to request author space.');
14097:             } else {
14098:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14099:             }
14100:         }
14101:     } else {
14102:         $resulttext = '<span class="LC_error">'.
14103: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14104:     }
14105:     if ($errors) {
14106:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14107:                        '<ul>'.$errors.'</ul></p>';
14108:     }
14109:     return $resulttext;
14110: }
14111: 
14112: sub process_textbook_image {
14113:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14114:     my $filename = $env{'form.'.$caller.'.filename'};
14115:     my ($error,$url);
14116:     my ($width,$height) = (50,50);
14117:     if ($configuserok eq 'ok') {
14118:         if ($switchserver) {
14119:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14120:                          $switchserver);
14121:         } elsif ($author_ok eq 'ok') {
14122:             my ($result,$imageurl) =
14123:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14124:                              "$type/$cdom/$cnum/cover",$width,$height);
14125:             if ($result eq 'ok') {
14126:                 $url = $imageurl;
14127:             } else {
14128:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14129:             }
14130:         } else {
14131:             $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);
14132:         }
14133:     } else {
14134:         $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);
14135:     }
14136:     return ($url,$error);
14137: }
14138: 
14139: sub modify_ltitools {
14140:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14141:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14142:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
14143:     my $confname = $dom.'-domainconfig';
14144:     my $servadm = $r->dir_config('lonAdmEMail');
14145:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14146:     my (%posslti,%possfield);
14147:     my @courseroles = ('cc','in','ta','ep','st');
14148:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
14149:     map { $posslti{$_} = 1; } @ltiroles;
14150:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
14151:     map { $possfield{$_} = 1; } @allfields;
14152:     my %lt = &ltitools_names(); 
14153:     if ($env{'form.ltitools_add'}) {
14154:         my $title = $env{'form.ltitools_add_title'};
14155:         $title =~ s/(`)/'/g;
14156:         ($newid,my $error) = &get_ltitools_id($dom,$title);
14157:         if ($newid) {
14158:             my $position = $env{'form.ltitools_add_pos'};
14159:             $position =~ s/\D+//g;
14160:             if ($position ne '') {
14161:                 $allpos[$position] = $newid;
14162:             }
14163:             $changes{$newid} = 1;
14164:             foreach my $item ('title','url','key','secret','lifetime') {
14165:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
14166:                 if ($item eq 'lifetime') {
14167:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
14168:                 }
14169:                 if ($env{'form.ltitools_add_'.$item}) {
14170:                     if (($item eq 'key') || ($item eq 'secret')) {
14171:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
14172:                     } else {
14173:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
14174:                     }
14175:                 }
14176:             }
14177:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
14178:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
14179:             }
14180:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
14181:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
14182:             }
14183:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
14184:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
14185:             } else {
14186:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
14187:             }
14188:             foreach my $item ('width','height','linktext','explanation') {
14189:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
14190:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
14191:                 if (($item eq 'width') || ($item eq 'height')) {
14192:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
14193:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
14194:                     }
14195:                 } else {
14196:                     if ($env{'form.ltitools_add_'.$item} ne '') {
14197:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
14198:                     }
14199:                 }
14200:             }
14201:             if ($env{'form.ltitools_add_target'} eq 'window') {
14202:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
14203:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
14204:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
14205:             } else {
14206:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
14207:             }
14208:             foreach my $item ('passback','roster') {
14209:                 if ($env{'form.ltitools_'.$item.'_add'}) {
14210:                     $confhash{$newid}{$item} = 1;
14211:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
14212:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
14213:                         $lifetime =~ s/^\s+|\s+$//g;
14214:                         if ($lifetime =~ /^\d+\.?\d*$/) {
14215:                             $confhash{$newid}{$item.'valid'} = $lifetime;
14216:                         }
14217:                     }
14218:                 }
14219:             }
14220:             if ($env{'form.ltitools_add_image.filename'} ne '') {
14221:                 my ($imageurl,$error) =
14222:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
14223:                                             $configuserok,$switchserver,$author_ok);
14224:                 if ($imageurl) {
14225:                     $confhash{$newid}{'image'} = $imageurl;
14226:                 }
14227:                 if ($error) {
14228:                     &Apache::lonnet::logthis($error);
14229:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14230:                 }
14231:             }
14232:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
14233:             foreach my $field (@fields) {
14234:                 if ($possfield{$field}) {
14235:                     if ($field eq 'roles') {
14236:                         foreach my $role (@courseroles) {
14237:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
14238:                             if (($choice ne '') && ($posslti{$choice})) {
14239:                                 $confhash{$newid}{'roles'}{$role} = $choice;
14240:                                 if ($role eq 'cc') {
14241:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
14242:                                 }
14243:                             }
14244:                         }
14245:                     } else {
14246:                         $confhash{$newid}{'fields'}{$field} = 1;
14247:                     }
14248:                 }
14249:             }
14250:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
14251:                 if ($confhash{$newid}{'fields'}{'user'}) {
14252:                     if ($env{'form.ltitools_userincdom_add'}) {
14253:                         $confhash{$newid}{'incdom'} = 1;
14254:                     }
14255:                 }
14256:             }
14257:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
14258:             foreach my $item (@courseconfig) {
14259:                 $confhash{$newid}{'crsconf'}{$item} = 1;
14260:             }
14261:             if ($env{'form.ltitools_add_custom'}) {
14262:                 my $name = $env{'form.ltitools_add_custom_name'};
14263:                 my $value = $env{'form.ltitools_add_custom_value'};
14264:                 $value =~ s/(`)/'/g;
14265:                 $name =~ s/(`)/'/g;
14266:                 $confhash{$newid}{'custom'}{$name} = $value;
14267:             }
14268:         } else {
14269:             my $error = &mt('Failed to acquire unique ID for new external tool');   
14270:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14271:         }
14272:     }
14273:     if (ref($domconfig{$action}) eq 'HASH') {
14274:         my %deletions;
14275:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
14276:         if (@todelete) {
14277:             map { $deletions{$_} = 1; } @todelete;
14278:         }
14279:         my %customadds;
14280:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
14281:         if (@newcustom) {
14282:             map { $customadds{$_} = 1; } @newcustom;
14283:         } 
14284:         my %imgdeletions;
14285:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
14286:         if (@todeleteimages) {
14287:             map { $imgdeletions{$_} = 1; } @todeleteimages;
14288:         }
14289:         my $maxnum = $env{'form.ltitools_maxnum'};
14290:         for (my $i=0; $i<=$maxnum; $i++) {
14291:             my $itemid = $env{'form.ltitools_id_'.$i};
14292:             $itemid =~ s/\D+//g;
14293:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14294:                 if ($deletions{$itemid}) {
14295:                     if ($domconfig{$action}{$itemid}{'image'}) {
14296:                         #FIXME need to obsolete item in RES space
14297:                     }
14298:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
14299:                     next;
14300:                 } else {
14301:                     my $newpos = $env{'form.ltitools_'.$itemid};
14302:                     $newpos =~ s/\D+//g;
14303:                     foreach my $item ('title','url','lifetime') {
14304:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
14305:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
14306:                             $changes{$itemid} = 1;
14307:                         }
14308:                     }
14309:                     foreach my $item ('key','secret') {
14310:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
14311:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
14312:                             $changes{$itemid} = 1;
14313:                         }
14314:                     }
14315:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
14316:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
14317:                     }
14318:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
14319:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
14320:                     }
14321:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
14322:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
14323:                     } else {
14324:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
14325:                     }
14326:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
14327:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
14328:                             $changes{$itemid} = 1;
14329:                         }
14330:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
14331:                         $changes{$itemid} = 1;
14332:                     }
14333:                     foreach my $size ('width','height') {
14334:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
14335:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
14336:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
14337:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
14338:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14339:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
14340:                                     $changes{$itemid} = 1;
14341:                                 }
14342:                             } else {
14343:                                 $changes{$itemid} = 1;
14344:                             }
14345:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14346:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
14347:                                 $changes{$itemid} = 1;
14348:                             }
14349:                         }
14350:                     }
14351:                     foreach my $item ('linktext','explanation') {
14352:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
14353:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
14354:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
14355:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
14356:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14357:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
14358:                                     $changes{$itemid} = 1;
14359:                                 }
14360:                             } else {
14361:                                 $changes{$itemid} = 1;
14362:                             }
14363:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14364:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
14365:                                 $changes{$itemid} = 1;
14366:                             }
14367:                         }
14368:                     }
14369:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
14370:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
14371:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
14372:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
14373:                     } else {
14374:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
14375:                     }
14376:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14377:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
14378:                             $changes{$itemid} = 1;
14379:                         }
14380:                     } else {
14381:                         $changes{$itemid} = 1;
14382:                     }
14383:                     foreach my $extra ('passback','roster') {
14384:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
14385:                             $confhash{$itemid}{$extra} = 1;
14386:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
14387:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
14388:                                 $lifetime =~ s/^\s+|\s+$//g;
14389:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
14390:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
14391:                                 }
14392:                             }
14393:                         }
14394:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
14395:                             $changes{$itemid} = 1;
14396:                         }
14397:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
14398:                             $changes{$itemid} = 1;
14399:                         }
14400:                     }
14401:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
14402:                     foreach my $item ('label','title','target','linktext','explanation','append') {
14403:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
14404:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
14405:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
14406:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
14407:                                     $changes{$itemid} = 1;
14408:                                 }
14409:                             } else {
14410:                                 $changes{$itemid} = 1;
14411:                             }
14412:                         }
14413:                     }
14414:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
14415:                     foreach my $field (@fields) {
14416:                         if ($possfield{$field}) {
14417:                             if ($field eq 'roles') {
14418:                                 foreach my $role (@courseroles) {
14419:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
14420:                                     if (($choice ne '') && ($posslti{$choice})) {
14421:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
14422:                                         if ($role eq 'cc') {
14423:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
14424:                                         }
14425:                                     }
14426:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
14427:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
14428:                                             $changes{$itemid} = 1;
14429:                                         }
14430:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
14431:                                         $changes{$itemid} = 1;
14432:                                     }
14433:                                 }
14434:                             } else {
14435:                                 $confhash{$itemid}{'fields'}{$field} = 1;
14436:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
14437:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
14438:                                         $changes{$itemid} = 1;
14439:                                     }
14440:                                 } else {
14441:                                     $changes{$itemid} = 1;
14442:                                 }
14443:                             }
14444:                         }
14445:                     }
14446:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
14447:                         if ($confhash{$itemid}{'fields'}{'user'}) {
14448:                             if ($env{'form.ltitools_userincdom_'.$i}) {
14449:                                 $confhash{$itemid}{'incdom'} = 1;
14450:                             }
14451:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
14452:                                 $changes{$itemid} = 1;
14453:                             }
14454:                         }
14455:                     }
14456:                     $allpos[$newpos] = $itemid;
14457:                 }
14458:                 if ($imgdeletions{$itemid}) {
14459:                     $changes{$itemid} = 1;
14460:                     #FIXME need to obsolete item in RES space
14461:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
14462:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
14463:                                                                  $itemid,$configuserok,$switchserver,
14464:                                                                  $author_ok);
14465:                     if ($imgurl) {
14466:                         $confhash{$itemid}{'image'} = $imgurl;
14467:                         $changes{$itemid} = 1;
14468:                     }
14469:                     if ($error) {
14470:                         &Apache::lonnet::logthis($error);
14471:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14472:                     }
14473:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
14474:                     $confhash{$itemid}{'image'} =
14475:                        $domconfig{$action}{$itemid}{'image'};
14476:                 }
14477:                 if ($customadds{$i}) {
14478:                     my $name = $env{'form.ltitools_custom_name_'.$i};
14479:                     $name =~ s/(`)/'/g;
14480:                     $name =~ s/^\s+//;
14481:                     $name =~ s/\s+$//;
14482:                     my $value = $env{'form.ltitools_custom_value_'.$i};
14483:                     $value =~ s/(`)/'/g;
14484:                     $value =~ s/^\s+//;
14485:                     $value =~ s/\s+$//;
14486:                     if ($name ne '') {
14487:                         $confhash{$itemid}{'custom'}{$name} = $value;
14488:                         $changes{$itemid} = 1;
14489:                     }
14490:                 }
14491:                 my %customdels;
14492:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
14493:                 if (@customdeletions) {
14494:                     $changes{$itemid} = 1;
14495:                 }
14496:                 map { $customdels{$_} = 1; } @customdeletions;
14497:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
14498:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
14499:                         unless ($customdels{$key}) {
14500:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
14501:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
14502:                             }
14503:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
14504:                                 $changes{$itemid} = 1;
14505:                             }
14506:                         }
14507:                     }
14508:                 }
14509:                 unless ($changes{$itemid}) {
14510:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
14511:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
14512:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
14513:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
14514:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
14515:                                         $changes{$itemid} = 1;
14516:                                         last;
14517:                                     }
14518:                                 }
14519:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
14520:                                 $changes{$itemid} = 1;
14521:                             }
14522:                         }
14523:                         last if ($changes{$itemid});
14524:                     }
14525:                 }
14526:             }
14527:         }
14528:     }
14529:     if (@allpos > 0) {
14530:         my $idx = 0;
14531:         foreach my $itemid (@allpos) {
14532:             if ($itemid ne '') {
14533:                 $confhash{$itemid}{'order'} = $idx;
14534:                 if (ref($domconfig{$action}) eq 'HASH') {
14535:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14536:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
14537:                             $changes{$itemid} = 1;
14538:                         }
14539:                     }
14540:                 }
14541:                 $idx ++;
14542:             }
14543:         }
14544:     }
14545:     my %ltitoolshash = (
14546:                           $action => { %confhash }
14547:                        );
14548:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
14549:                                              $dom);
14550:     if ($putresult eq 'ok') {
14551:         my %ltienchash = (
14552:                              $action => { %encconfig }
14553:                          );
14554:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
14555:         if (keys(%changes) > 0) {
14556:             my $cachetime = 24*60*60;
14557:             my %ltiall = %confhash;
14558:             foreach my $id (keys(%ltiall)) {
14559:                 if (ref($encconfig{$id}) eq 'HASH') {
14560:                     foreach my $item ('key','secret') {
14561:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
14562:                     }
14563:                 }
14564:             }
14565:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
14566:             if (ref($lastactref) eq 'HASH') {
14567:                 $lastactref->{'ltitools'} = 1;
14568:             }
14569:             $resulttext = &mt('Changes made:').'<ul>';
14570:             my %bynum;
14571:             foreach my $itemid (sort(keys(%changes))) {
14572:                 my $position = $confhash{$itemid}{'order'};
14573:                 $bynum{$position} = $itemid;
14574:             }
14575:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14576:                 my $itemid = $bynum{$pos}; 
14577:                 if (ref($confhash{$itemid}) ne 'HASH') {
14578:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14579:                 } else {
14580:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
14581:                     if ($confhash{$itemid}{'image'}) {
14582:                         $resulttext .= '&nbsp;'.
14583:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
14584:                                        ' alt="'.&mt('Tool Provider icon').'" />';
14585:                     }
14586:                     $resulttext .= '</li><ul>';
14587:                     my $position = $pos + 1;
14588:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14589:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
14590:                         if ($confhash{$itemid}{$item} ne '') {
14591:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
14592:                         }
14593:                     }
14594:                     if ($encconfig{$itemid}{'key'} ne '') {
14595:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
14596:                     }
14597:                     if ($encconfig{$itemid}{'secret'} ne '') {
14598:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14599:                         my $num = length($encconfig{$itemid}{'secret'});
14600:                         $resulttext .= ('*'x$num).'</li>';
14601:                     }
14602:                     $resulttext .= '<li>'.&mt('Configurable in course:');
14603:                     my @possconfig = ('label','title','target','linktext','explanation','append');
14604:                     my $numconfig = 0; 
14605:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
14606:                         foreach my $item (@possconfig) {
14607:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
14608:                                 $numconfig ++;
14609:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
14610:                             }
14611:                         }
14612:                     }
14613:                     if (!$numconfig) {
14614:                         $resulttext .= '&nbsp;'.&mt('None');
14615:                     }
14616:                     $resulttext .= '</li>';
14617:                     foreach my $item ('passback','roster') {
14618:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
14619:                         if ($confhash{$itemid}{$item}) {
14620:                             $resulttext .= &mt('Yes');
14621:                             if ($confhash{$itemid}{$item.'valid'}) {
14622:                                 if ($item eq 'passback') {
14623:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
14624:                                                            $confhash{$itemid}{$item.'valid'});
14625:                                 } else {
14626:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
14627:                                                            $confhash{$itemid}{$item.'valid'});
14628:                                 }
14629:                             }
14630:                         } else {
14631:                             $resulttext .= &mt('No');
14632:                         }
14633:                         $resulttext .= '</li>';
14634:                     }
14635:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
14636:                         my $displaylist;
14637:                         if ($confhash{$itemid}{'display'}{'target'}) {
14638:                             $displaylist = &mt('Display target').':&nbsp;'.
14639:                                            $confhash{$itemid}{'display'}{'target'}.',';
14640:                         }
14641:                         foreach my $size ('width','height') { 
14642:                             if ($confhash{$itemid}{'display'}{$size}) {
14643:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
14644:                                                 $confhash{$itemid}{'display'}{$size}.',';
14645:                             }
14646:                         }
14647:                         if ($displaylist) {
14648:                             $displaylist =~ s/,$//;
14649:                             $resulttext .= '<li>'.$displaylist.'</li>';
14650:                         }
14651:                         foreach my $item ('linktext','explanation') {
14652:                             if ($confhash{$itemid}{'display'}{$item}) {
14653:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
14654:                             }
14655:                         }
14656:                     }
14657:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
14658:                         my $fieldlist;
14659:                         foreach my $field (@allfields) {
14660:                             if ($confhash{$itemid}{'fields'}{$field}) {
14661:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
14662:                             }
14663:                         }
14664:                         if ($fieldlist) {
14665:                             $fieldlist =~ s/,$//;
14666:                             if ($confhash{$itemid}{'fields'}{'user'}) {
14667:                                 if ($confhash{$itemid}{'incdom'}) {
14668:                                     $fieldlist .= ' ('.&mt('username:domain').')';
14669:                                 } else {
14670:                                     $fieldlist .= ' ('.&mt('username').')';
14671:                                 }
14672:                             }
14673:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
14674:                         }
14675:                     }
14676:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
14677:                         my $rolemaps;
14678:                         foreach my $role (@courseroles) {
14679:                             if ($confhash{$itemid}{'roles'}{$role}) {
14680:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
14681:                                              $confhash{$itemid}{'roles'}{$role}.',';
14682:                             }
14683:                         }
14684:                         if ($rolemaps) {
14685:                             $rolemaps =~ s/,$//; 
14686:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14687:                         }
14688:                     }
14689:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
14690:                         my $customlist;
14691:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
14692:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
14693:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
14694:                             } 
14695:                         }
14696:                         if ($customlist) {
14697:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
14698:                         }
14699:                     } 
14700:                     $resulttext .= '</ul></li>';
14701:                 }
14702:             }
14703:             $resulttext .= '</ul>';
14704:         } else {
14705:             $resulttext = &mt('No changes made.');
14706:         }
14707:     } else {
14708:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14709:     }
14710:     if ($errors) {
14711:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14712:                        $errors.'</ul>';
14713:     }
14714:     return $resulttext;
14715: }
14716: 
14717: sub process_ltitools_image {
14718:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
14719:     my $filename = $env{'form.'.$caller.'.filename'};
14720:     my ($error,$url);
14721:     my ($width,$height) = (21,21);
14722:     if ($configuserok eq 'ok') {
14723:         if ($switchserver) {
14724:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
14725:                          $switchserver);
14726:         } elsif ($author_ok eq 'ok') {
14727:             my ($result,$imageurl,$madethumb) =
14728:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14729:                              "ltitools/$itemid/icon",$width,$height);
14730:             if ($result eq 'ok') {
14731:                 if ($madethumb) {
14732:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
14733:                     my $imagethumb = "$path/tn-".$imagefile;
14734:                     $url = $imagethumb;
14735:                 } else {
14736:                     $url = $imageurl;
14737:                 }
14738:             } else {
14739:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14740:             }
14741:         } else {
14742:             $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);
14743:         }
14744:     } else {
14745:         $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);
14746:     }
14747:     return ($url,$error);
14748: }
14749: 
14750: sub get_ltitools_id {
14751:     my ($cdom,$title) = @_;
14752:     # get lock on ltitools db
14753:     my $lockhash = {
14754:                       lock => $env{'user.name'}.
14755:                               ':'.$env{'user.domain'},
14756:                    };
14757:     my $tries = 0;
14758:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
14759:     my ($id,$error);
14760:  
14761:     while (($gotlock ne 'ok') && ($tries<10)) {
14762:         $tries ++;
14763:         sleep (0.1);
14764:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
14765:     }
14766:     if ($gotlock eq 'ok') {
14767:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
14768:         if ($currids{'lock'}) {
14769:             delete($currids{'lock'});
14770:             if (keys(%currids)) {
14771:                 my @curr = sort { $a <=> $b } keys(%currids);
14772:                 if ($curr[-1] =~ /^\d+$/) {
14773:                     $id = 1 + $curr[-1];
14774:                 }
14775:             } else {
14776:                 $id = 1;
14777:             }
14778:             if ($id) {
14779:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
14780:                     $error = 'nostore';
14781:                 }
14782:             } else {
14783:                 $error = 'nonumber';
14784:             }
14785:         }
14786:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
14787:     } else {
14788:         $error = 'nolock';
14789:     }
14790:     return ($id,$error);
14791: }
14792: 
14793: sub modify_proctoring {
14794:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14795:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14796:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
14797:     my $confname = $dom.'-domainconfig';
14798:     my $servadm = $r->dir_config('lonAdmEMail');
14799:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14800:     my %providernames = &proctoring_providernames();
14801:     my $maxnum = scalar(keys(%providernames));
14802: 
14803:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
14804:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
14805:     if (ref($requref) eq 'HASH') {
14806:         %requserfields = %{$requref};
14807:     }
14808:     if (ref($opturef) eq 'HASH') {
14809:         %optuserfields = %{$opturef};
14810:     }
14811:     if (ref($defref) eq 'HASH') {
14812:         %defaults = %{$defref};
14813:     }
14814:     if (ref($extref) eq 'HASH') {
14815:         %extended = %{$extref};
14816:     }
14817:     if (ref($crsref) eq 'HASH') {
14818:         %crsconf = %{$crsref};
14819:     }
14820:     if (ref($rolesref) eq 'ARRAY') {
14821:         @courseroles = @{$rolesref};
14822:     }
14823:     if (ref($ltiref) eq 'ARRAY') {
14824:         @ltiroles = @{$ltiref};
14825:     }
14826: 
14827:     if (ref($domconfig{$action}) eq 'HASH') {
14828:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
14829:         if (@todeleteimages) {
14830:             map { $imgdeletions{$_} = 1; } @todeleteimages;
14831:         }
14832:     }
14833:     my %customadds;
14834:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
14835:     if (@newcustom) {
14836:         map { $customadds{$_} = 1; } @newcustom;
14837:     }
14838:     foreach my $provider (sort(keys(%providernames))) {
14839:         $confhash{$provider} = {};
14840:         my $pos = $env{'form.proctoring_pos_'.$provider};
14841:         $pos =~ s/\D+//g;
14842:         $allpos[$pos] = $provider;
14843:         my (%current,%currentenc);
14844:         my $showroles = 0;
14845:         if (ref($domconfig{$action}) eq 'HASH') {
14846:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14847:                 %current = %{$domconfig{$action}{$provider}};
14848:                 foreach my $item ('key','secret') { 
14849:                     $currentenc{$item} = $current{$item};
14850:                     delete($current{$item});
14851:                 }
14852:             }
14853:         }
14854:         if ($env{'form.proctoring_available_'.$provider}) {
14855:             $confhash{$provider}{'available'} = 1;
14856:             unless ($current{'available'}) {
14857:                 $changes{$provider} = 1;
14858:             }
14859:         } else {
14860:             %{$confhash{$provider}} = %current;
14861:             %{$encconfhash{$provider}} = %currentenc;
14862:             $confhash{$provider}{'available'} = 0;
14863:             if ($current{'available'}) {
14864:                 $changes{$provider} = 1;
14865:             }
14866:         }
14867:         if ($confhash{$provider}{'available'}) {
14868:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
14869:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
14870:                 if ($field eq 'lifetime') {
14871:                     if ($possval =~ /^\d+$/) {
14872:                         $confhash{$provider}{$field} = $possval;
14873:                     }
14874:                 } elsif ($field eq 'version') {
14875:                     if ($possval =~ /^\d+\.\d+$/) {
14876:                         $confhash{$provider}{$field} = $possval;
14877:                     }
14878:                 } elsif ($field eq 'sigmethod') {
14879:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
14880:                         $confhash{$provider}{$field} = $possval;
14881:                     }
14882:                 } elsif ($field eq 'url') {
14883:                     $confhash{$provider}{$field} = $possval;
14884:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
14885:                     $encconfhash{$provider}{$field} = $possval;
14886:                     unless ($currentenc{$field} eq $possval) {
14887:                         $changes{$provider} = 1;
14888:                     }
14889:                 }
14890:                 unless (($field eq 'key') || ($field eq 'secret')) {
14891:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
14892:                         $changes{$provider} = 1;
14893:                     }
14894:                 }
14895:             }
14896:             if ($imgdeletions{$provider}) {
14897:                 $changes{$provider} = 1;
14898:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
14899:                 my ($imageurl,$error) =
14900:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
14901:                                               $configuserok,$switchserver,$author_ok);
14902:                 if ($imageurl) {
14903:                     $confhash{$provider}{'image'} = $imageurl;
14904:                     $changes{$provider} = 1; 
14905:                 }
14906:                 if ($error) {
14907:                     &Apache::lonnet::logthis($error);
14908:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14909:                 }
14910:             } elsif (exists($current{'image'})) {
14911:                 $confhash{$provider}{'image'} = $current{'image'};
14912:             }
14913:             if (ref($requserfields{$provider}) eq 'ARRAY') {
14914:                 if (@{$requserfields{$provider}} > 0) {
14915:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
14916:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
14917:                             $confhash{$provider}{'incdom'} = 1;
14918:                         }
14919:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
14920:                             $changes{$provider} = 1;
14921:                         }
14922:                     }
14923:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
14924:                         $showroles = 1;
14925:                     }
14926:                 }
14927:             }
14928:             $confhash{$provider}{'fields'} = [];
14929:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
14930:                 if (@{$optuserfields{$provider}} > 0) {
14931:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
14932:                     foreach my $field (@{$optuserfields{$provider}}) {
14933:                         if (grep(/^\Q$field\E$/,@optfields)) {
14934:                             push(@{$confhash{$provider}{'fields'}},$field);
14935:                         }
14936:                     }
14937:                 }
14938:                 if (ref($current{'fields'}) eq 'ARRAY') {
14939:                     unless ($changes{$provider}) {
14940:                         my @new = sort(@{$confhash{$provider}{'fields'}});
14941:                         my @old = sort(@{$current{'fields'}}); 
14942:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14943:                         if (@diffs) {
14944:                             $changes{$provider} = 1;
14945:                         }
14946:                     }
14947:                 } elsif (@{$confhash{$provider}{'fields'}}) {
14948:                     $changes{$provider} = 1;
14949:                 }
14950:             }
14951:             if (ref($defaults{$provider}) eq 'ARRAY') {  
14952:                 if (@{$defaults{$provider}} > 0) {
14953:                     my %options;
14954:                     if (ref($extended{$provider}) eq 'HASH') {
14955:                         %options = %{$extended{$provider}};
14956:                     }
14957:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
14958:                     foreach my $field (@{$defaults{$provider}}) {
14959:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
14960:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
14961:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
14962:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
14963:                             }
14964:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
14965:                             foreach my $inner (keys(%{$options{$field}})) {
14966:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
14967:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
14968:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
14969:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
14970:                                     }
14971:                                 } else {
14972:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
14973:                                 }
14974:                             }
14975:                         } else {
14976:                             if (grep(/^\Q$field\E$/,@checked)) {
14977:                                 push(@{$confhash{$provider}{'defaults'}},$field);
14978:                             }
14979:                         }
14980:                     }
14981:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14982:                         if (ref($current{'defaults'}) eq 'ARRAY') {
14983:                             unless ($changes{$provider}) {
14984:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
14985:                                 my @old = sort(@{$current{'defaults'}});
14986:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14987:                                 if (@diffs) {
14988:                                     $changes{$provider} = 1; 
14989:                                 }
14990:                             }
14991:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
14992:                             if (@{$current{'defaults'}}) {
14993:                                 $changes{$provider} = 1;
14994:                             }
14995:                         }
14996:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14997:                         if (ref($current{'defaults'}) eq 'HASH') {
14998:                             unless ($changes{$provider}) {
14999:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
15000:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
15001:                                         $changes{$provider} = 1;
15002:                                         last;
15003:                                     }
15004:                                 }
15005:                             }
15006:                             unless ($changes{$provider}) {
15007:                                 foreach my $key (keys(%{$current{'defaults'}})) {
15008:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
15009:                                         $changes{$provider} = 1;
15010:                                         last;
15011:                                     }
15012:                                 }
15013:                             }
15014:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
15015:                             $changes{$provider} = 1;
15016:                         }
15017:                     }
15018:                 }
15019:             }
15020:             if (ref($crsconf{$provider}) eq 'ARRAY') {
15021:                 if (@{$crsconf{$provider}} > 0) {
15022:                     $confhash{$provider}{'crsconf'} = [];
15023:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
15024:                     foreach my $crsfield (@{$crsconf{$provider}}) {
15025:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
15026:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
15027:                         }
15028:                     }
15029:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
15030:                         unless ($changes{$provider}) {  
15031:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
15032:                             my @old = sort(@{$current{'crsconf'}});
15033:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15034:                             if (@diffs) {
15035:                                 $changes{$provider} = 1;
15036:                             }
15037:                         }
15038:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
15039:                         $changes{$provider} = 1;
15040:                     }
15041:                 }
15042:             }
15043:             if ($showroles) {
15044:                 $confhash{$provider}{'roles'} = {};
15045:                 foreach my $role (@courseroles) {
15046:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
15047:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
15048:                         $confhash{$provider}{'roles'}{$role} = $poss;
15049:                     }
15050:                 }
15051:                 unless ($changes{$provider}) {
15052:                     if (ref($current{'roles'}) eq 'HASH') {
15053:                         foreach my $role (keys(%{$current{'roles'}})) {
15054:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
15055:                                 $changes{$provider} = 1;
15056:                                 last
15057:                             }
15058:                         }
15059:                         unless ($changes{$provider}) {
15060:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
15061:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
15062:                                     $changes{$provider} = 1;
15063:                                     last;
15064:                                 }
15065:                             }
15066:                         }
15067:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
15068:                         $changes{$provider} = 1;
15069:                     }
15070:                 }
15071:             }
15072:             if (ref($current{'custom'}) eq 'HASH') {
15073:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
15074:                 foreach my $key (keys(%{$current{'custom'}})) {
15075:                     if (grep(/^\Q$key\E$/,@customdels)) {
15076:                         $changes{$provider} = 1;
15077:                     } else {
15078:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
15079:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
15080:                             $changes{$provider} = 1;
15081:                         }
15082:                     }
15083:                 }
15084:             }
15085:             if ($customadds{$provider}) {
15086:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
15087:                 $name =~ s/(`)/'/g;
15088:                 $name =~ s/^\s+//;
15089:                 $name =~ s/\s+$//;
15090:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
15091:                 $value =~ s/(`)/'/g;
15092:                 $value =~ s/^\s+//;
15093:                 $value =~ s/\s+$//;
15094:                 if ($name ne '') {
15095:                     $confhash{$provider}{'custom'}{$name} = $value;
15096:                     $changes{$provider} = 1;
15097:                 }
15098:             }
15099:         }
15100:     }
15101:     if (@allpos > 0) {
15102:         my $idx = 0;
15103:         foreach my $provider (@allpos) {
15104:             if ($provider ne '') {
15105:                 $confhash{$provider}{'order'} = $idx;
15106:                 unless ($changes{$provider}) {
15107:                     if (ref($domconfig{$action}) eq 'HASH') {
15108:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15109:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
15110:                                 $changes{$provider} = 1;
15111:                             }
15112:                         }
15113:                     }
15114:                 }
15115:                 $idx ++;
15116:             }
15117:         }
15118:     }
15119:     my %proc_hash = (
15120:                           $action => { %confhash }
15121:                        );
15122:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
15123:                                              $dom);
15124:     if ($putresult eq 'ok') {
15125:         my %proc_enchash = (
15126:                              $action => { %encconfhash }
15127:                          );
15128:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
15129:         if (keys(%changes) > 0) {
15130:             my $cachetime = 24*60*60;
15131:             my %procall = %confhash;
15132:             foreach my $provider (keys(%procall)) {
15133:                 if (ref($encconfhash{$provider}) eq 'HASH') {
15134:                     foreach my $key ('key','secret') {
15135:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
15136:                     }
15137:                 }
15138:             }
15139:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
15140:             if (ref($lastactref) eq 'HASH') {
15141:                 $lastactref->{'proctoring'} = 1;
15142:             }
15143:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
15144:             my %bynum;
15145:             foreach my $provider (sort(keys(%changes))) {
15146:                 my $position = $confhash{$provider}{'order'};
15147:                 $bynum{$position} = $provider;
15148:             }
15149:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15150:                 my $provider = $bynum{$pos};
15151:                 my %lt = &proctoring_titles($provider);
15152:                 my %fieldtitles = &proctoring_fieldtitles($provider);
15153:                 if (!$confhash{$provider}{'available'}) {
15154:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
15155:                 } else {
15156:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
15157:                     if ($confhash{$provider}{'image'}) {
15158:                         $resulttext .= '&nbsp;'.
15159:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
15160:                                        ' alt="'.&mt('Proctoring icon').'" />';
15161:                     }
15162:                     $resulttext .= '<ul>';
15163:                     my $position = $pos + 1;
15164:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15165:                     foreach my $key ('version','sigmethod','url','lifetime') {
15166:                         if ($confhash{$provider}{$key} ne '') {
15167:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
15168:                         }
15169:                     }
15170:                     if ($encconfhash{$provider}{'key'} ne '') {
15171:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
15172:                     }
15173:                     if ($encconfhash{$provider}{'secret'} ne '') {
15174:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15175:                         my $num = length($encconfhash{$provider}{'secret'});
15176:                         $resulttext .= ('*'x$num).'</li>';
15177:                     }
15178:                     my (@fields,$showroles);
15179:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15180:                         push(@fields,@{$requserfields{$provider}});
15181:                     }
15182:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
15183:                         push(@fields,@{$confhash{$provider}{'fields'}});
15184:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
15185:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
15186:                     }
15187:                     if (@fields) {
15188:                         if (grep(/^roles$/,@fields)) {
15189:                             $showroles = 1;
15190:                         }
15191:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
15192:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
15193:                     }
15194:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15195:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
15196:                             if ($confhash{$provider}{'incdom'}) {
15197:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
15198:                             } else { 
15199:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
15200:                             }
15201:                         }
15202:                     }
15203:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15204:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
15205:                             $resulttext .= '<li>'.$lt{'defa'};
15206:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
15207:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
15208:                             }
15209:                             $resulttext =~ s/,$//;
15210:                             $resulttext .= '</li>';
15211:                         }
15212:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15213:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15214:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15215:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15216:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15217:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15218:                                 }
15219:                             }
15220:                             $resulttext .= '</ul></li>';
15221:                         }
15222:                     }
15223:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15224:                         if (@{$crsconf{$provider}} > 0) {
15225:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15226:                             my $numconfig = 0;
15227:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15228:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15229:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15230:                                         $numconfig ++;
15231:                                         if ($provider eq 'examity') {
15232:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15233:                                         } else {
15234:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15235:                                         }
15236:                                     }
15237:                                     $resulttext =~ s/,$//;
15238:                                 }
15239:                             }
15240:                             if (!$numconfig) {
15241:                                 $resulttext .= '&nbsp;'.&mt('None');
15242:                             }
15243:                             $resulttext .= '</li>';
15244:                         }
15245:                     }
15246:                     if ($showroles) {
15247:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15248:                             my $rolemaps;
15249:                             foreach my $role (@courseroles) {
15250:                                 if ($confhash{$provider}{'roles'}{$role}) {
15251:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15252:                                                  $confhash{$provider}{'roles'}{$role}.',';
15253:                                 }
15254:                             }
15255:                             if ($rolemaps) {
15256:                                 $rolemaps =~ s/,$//;
15257:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15258:                             }
15259:                         }
15260:                     }
15261:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15262:                         my $customlist;
15263:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15264:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15265:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15266:                             }
15267:                             $customlist =~ s/,$//;
15268:                         }
15269:                         if ($customlist) {
15270:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15271:                         }
15272:                     } 
15273:                     $resulttext .= '</ul></li>';
15274:                 }
15275:             }
15276:             $resulttext .= '</ul>';
15277:         } else {
15278:             $resulttext = &mt('No changes made.');
15279:         }
15280:     } else {
15281:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15282:     }
15283:     if ($errors) {
15284:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15285:                        $errors.'</ul>';
15286:     }
15287:     return $resulttext;
15288: }
15289: 
15290: sub process_proctoring_image {
15291:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15292:     my $filename = $env{'form.'.$caller.'.filename'};
15293:     my ($error,$url);
15294:     my ($width,$height) = (21,21);
15295:     if ($configuserok eq 'ok') {
15296:         if ($switchserver) {
15297:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15298:                          $switchserver);
15299:         } elsif ($author_ok eq 'ok') {
15300:             my ($result,$imageurl,$madethumb) =
15301:                 &publishlogo($r,'upload',$caller,$dom,$confname,
15302:                              "proctoring/$provider/icon",$width,$height);
15303:             if ($result eq 'ok') {
15304:                 if ($madethumb) {
15305:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15306:                     my $imagethumb = "$path/tn-".$imagefile;
15307:                     $url = $imagethumb;
15308:                 } else {
15309:                     $url = $imageurl;
15310:                 }
15311:             } else {
15312:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15313:             }
15314:         } else {
15315:             $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);
15316:         }
15317:     } else {
15318:         $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);
15319:     }
15320:     return ($url,$error);
15321: }
15322: 
15323: sub modify_lti {
15324:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15325:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15326:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
15327:     my (%posslti,%posslticrs,%posscrstype);
15328:     my @courseroles = ('cc','in','ta','ep','st');
15329:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15330:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15331:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15332:     my %coursetypetitles = &Apache::lonlocal::texthash (
15333:                                official   => 'Official',
15334:                                unofficial => 'Unofficial',
15335:                                community  => 'Community',
15336:                                textbook   => 'Textbook',
15337:                                placement  => 'Placement Test',
15338:                                lti        => 'LTI Provider',
15339:     );
15340:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15341:     my %lt = &lti_names();
15342:     map { $posslti{$_} = 1; } @ltiroles;
15343:     map { $posslticrs{$_} = 1; } @lticourseroles;
15344:     map { $posscrstype{$_} = 1; } @coursetypes;
15345: 
15346:     my %menutitles = &ltimenu_titles();
15347: 
15348:     my (@items,%deletions,%itemids);
15349:     if ($env{'form.lti_add'}) {
15350:         my $consumer = $env{'form.lti_consumer_add'};
15351:         $consumer =~ s/(`)/'/g;
15352:         ($newid,my $error) = &get_lti_id($dom,$consumer);
15353:         if ($newid) {
15354:             $itemids{'add'} = $newid;
15355:             push(@items,'add');
15356:             $changes{$newid} = 1;
15357:         } else {
15358:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15359:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15360:         }
15361:     }
15362:     if (ref($domconfig{$action}) eq 'HASH') {
15363:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15364:         if (@todelete) {
15365:             map { $deletions{$_} = 1; } @todelete;
15366:         }
15367:         my $maxnum = $env{'form.lti_maxnum'};
15368:         for (my $i=0; $i<$maxnum; $i++) {
15369:             my $itemid = $env{'form.lti_id_'.$i};
15370:             $itemid =~ s/\D+//g;
15371:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15372:                 if ($deletions{$itemid}) {
15373:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15374:                 } else {
15375:                     push(@items,$i);
15376:                     $itemids{$i} = $itemid;
15377:                 }
15378:             }
15379:         }
15380:     }
15381:     foreach my $idx (@items) {
15382:         my $itemid = $itemids{$idx};
15383:         next unless ($itemid);
15384:         my $position = $env{'form.lti_pos_'.$itemid};
15385:         $position =~ s/\D+//g;
15386:         if ($position ne '') {
15387:             $allpos[$position] = $itemid;
15388:         }
15389:         foreach my $item ('consumer','key','secret','lifetime','requser','crsinc') {
15390:             my $formitem = 'form.lti_'.$item.'_'.$idx;
15391:             $env{$formitem} =~ s/(`)/'/g;
15392:             if ($item eq 'lifetime') {
15393:                 $env{$formitem} =~ s/[^\d.]//g;
15394:             }
15395:             if ($env{$formitem} ne '') {
15396:                 if (($item eq 'key') || ($item eq 'secret')) {
15397:                     $encconfig{$itemid}{$item} = $env{$formitem};
15398:                 } else {
15399:                     $confhash{$itemid}{$item} = $env{$formitem};
15400:                     unless (($idx eq 'add') || ($changes{$itemid})) {
15401:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
15402:                             $changes{$itemid} = 1;
15403:                         }
15404:                     }
15405:                 }
15406:             }
15407:         }
15408:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
15409:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
15410:         }
15411:         if ($confhash{$itemid}{'requser'}) {
15412:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
15413:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
15414:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
15415:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
15416:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
15417:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
15418:                 $mapuser =~ s/(`)/'/g;
15419:                 $mapuser =~ s/^\s+|\s+$//g; 
15420:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
15421:             }
15422:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
15423:             my @makeuser;
15424:             foreach my $ltirole (sort(@possmakeuser)) {
15425:                 if ($posslti{$ltirole}) {
15426:                     push(@makeuser,$ltirole);
15427:                 }
15428:             }
15429:             $confhash{$itemid}{'makeuser'} = \@makeuser;
15430:             if (@makeuser) {
15431:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
15432:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
15433:                     $confhash{$itemid}{'lcauth'} = $lcauth;
15434:                     if ($lcauth ne 'internal') {
15435:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
15436:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
15437:                         $lcauthparm =~ s/`//g;
15438:                         if ($lcauthparm ne '') {
15439:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
15440:                         }
15441:                     }
15442:                 } else {
15443:                     $confhash{$itemid}{'lcauth'} = 'lti';
15444:                 }
15445:             }
15446:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
15447:             if (@possinstdata) {
15448:                 foreach my $field (@possinstdata) {
15449:                     if (exists($fieldtitles{$field})) {
15450:                         push(@{$confhash{$itemid}{'instdata'}});
15451:                     }
15452:                 }
15453:             }
15454:             if ($env{'form.lti_callback_'.$idx}) {
15455:                 if ($env{'form.lti_callbackparam_'.$idx}) {
15456:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
15457:                     $callback =~ s/^\s+|\s+$//g;
15458:                     $confhash{$itemid}{'callback'} = $callback;
15459:                 }
15460:             }
15461:             foreach my $field ('topmenu','inlinemenu') {
15462:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
15463:                     $confhash{$itemid}{$field} = 1;
15464:                 }
15465:             }
15466:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
15467:                 $confhash{$itemid}{lcmenu} = [];
15468:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
15469:                 foreach my $field (@possmenu) {
15470:                     if (exists($menutitles{$field})) {
15471:                         if ($field eq 'grades') {
15472:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
15473:                         }
15474:                         push(@{$confhash{$itemid}{lcmenu}},$field);
15475:                     }
15476:                 }
15477:             }
15478:             if ($confhash{$itemid}{'crsinc'}) {
15479:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
15480:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
15481:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
15482:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
15483:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
15484:                     $mapcrs =~ s/(`)/'/g;
15485:                     $mapcrs =~ s/^\s+|\s+$//g;
15486:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
15487:                 }
15488:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
15489:                 my @crstypes;
15490:                 foreach my $type (sort(@posstypes)) {
15491:                     if ($posscrstype{$type}) {
15492:                         push(@crstypes,$type);
15493:                     }
15494:                 }
15495:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
15496:                 if ($env{'form.lti_storecrs_'.$idx}) {
15497:                     $confhash{$itemid}{'storecrs'} = 1;
15498:                 }       
15499:                 if ($env{'form.lti_makecrs_'.$idx}) {
15500:                     $confhash{$itemid}{'makecrs'} = 1;
15501:                 }
15502:                 foreach my $ltirole (@lticourseroles) {
15503:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
15504:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
15505:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
15506:                     }
15507:                 }
15508:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
15509:                 my @selfenroll;
15510:                 foreach my $type (sort(@possenroll)) {
15511:                     if ($posslticrs{$type}) {
15512:                         push(@selfenroll,$type);
15513:                     }
15514:                 }
15515:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
15516:                 if ($env{'form.lti_crssec_'.$idx}) {
15517:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
15518:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
15519:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
15520:                         my $section = $env{'form.lti_customsection_'.$idx};
15521:                         $section =~ s/(`)/'/g;
15522:                         $section =~ s/^\s+|\s+$//g;
15523:                         if ($section ne '') {
15524:                             $confhash{$itemid}{'section'} = $section;
15525:                         }
15526:                     }
15527:                 }
15528:                 foreach my $field ('passback','roster') {
15529:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
15530:                         $confhash{$itemid}{$field} = 1;
15531:                     }
15532:                 }
15533:                 if ($env{'form.lti_passback_'.$idx}) {
15534:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
15535:                         $confhash{$itemid}{'passbackformat'} = '1.0';
15536:                     } else {
15537:                         $confhash{$itemid}{'passbackformat'} = '1.1';
15538:                     }
15539:                 }
15540:             }
15541:             unless (($idx eq 'add') || ($changes{$itemid})) {
15542:                 if ($confhash{$itemid}{'crsinc'}) {
15543:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
15544:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15545:                             $changes{$itemid} = 1;
15546:                         }
15547:                     }
15548:                     unless ($changes{$itemid}) {
15549:                         if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
15550:                             if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
15551:                                 $changes{$itemid} = 1;
15552:                             }
15553:                         }
15554:                     }
15555:                     foreach my $field ('mapcrstype','selfenroll') {
15556:                         unless ($changes{$itemid}) {
15557:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15558:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15559:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15560:                                                                                    $confhash{$itemid}{$field});
15561:                                     if (@diffs) {
15562:                                         $changes{$itemid} = 1;
15563:                                     }
15564:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15565:                                     $changes{$itemid} = 1;
15566:                                 }
15567:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15568:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15569:                                     $changes{$itemid} = 1;
15570:                                 }
15571:                             }
15572:                         }
15573:                     }
15574:                     unless ($changes{$itemid}) {
15575:                         if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
15576:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15577:                                 foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
15578:                                     if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
15579:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
15580:                                         $changes{$itemid} = 1;
15581:                                         last;
15582:                                     }
15583:                                 }
15584:                                 unless ($changes{$itemid}) {
15585:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
15586:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
15587:                                             $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
15588:                                             $changes{$itemid} = 1;
15589:                                             last;
15590:                                         }
15591:                                     }
15592:                                 }
15593:                             } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
15594:                                 $changes{$itemid} = 1;
15595:                             }
15596:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15597:                             unless ($changes{$itemid}) {
15598:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
15599:                                     $changes{$itemid} = 1;
15600:                                 }
15601:                             }
15602:                         }
15603:                     }
15604:                 }
15605:                 unless ($changes{$itemid}) {
15606:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
15607:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15608:                             $changes{$itemid} = 1;
15609:                         }
15610:                     }
15611:                     unless ($changes{$itemid}) {
15612:                         foreach my $field ('makeuser','lcmenu') {
15613:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15614:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15615:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15616:                                                                                    $confhash{$itemid}{$field});
15617:                                     if (@diffs) {
15618:                                         $changes{$itemid} = 1;
15619:                                     }
15620:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15621:                                     $changes{$itemid} = 1;
15622:                                 }
15623:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15624:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15625:                                     $changes{$itemid} = 1;
15626:                                 }
15627:                             }
15628:                         }
15629:                     }
15630:                 }
15631:             }
15632:         }
15633:     }
15634:     if (@allpos > 0) {
15635:         my $idx = 0;
15636:         foreach my $itemid (@allpos) {
15637:             if ($itemid ne '') {
15638:                 $confhash{$itemid}{'order'} = $idx;
15639:                 if (ref($domconfig{$action}) eq 'HASH') {
15640:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15641:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
15642:                             $changes{$itemid} = 1;
15643:                         }
15644:                     }
15645:                 }
15646:                 $idx ++;
15647:             }
15648:         }
15649:     }
15650:     my %ltihash = (
15651:                           $action => { %confhash }
15652:                        );
15653:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
15654:                                              $dom);
15655:     if ($putresult eq 'ok') {
15656:         my %ltienchash = (
15657:                              $action => { %encconfig }
15658:                          );
15659:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
15660:         if (keys(%changes) > 0) {
15661:             my $cachetime = 24*60*60;
15662:             my %ltiall = %confhash;
15663:             foreach my $id (keys(%ltiall)) {
15664:                 if (ref($encconfig{$id}) eq 'HASH') {
15665:                     foreach my $item ('key','secret') {
15666:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
15667:                     }
15668:                 }
15669:             }
15670:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
15671:             if (ref($lastactref) eq 'HASH') {
15672:                 $lastactref->{'lti'} = 1;
15673:             }
15674:             $resulttext = &mt('Changes made:').'<ul>';
15675:             my %bynum;
15676:             foreach my $itemid (sort(keys(%changes))) {
15677:                 my $position = $confhash{$itemid}{'order'};
15678:                 $bynum{$position} = $itemid;
15679:             }
15680:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15681:                 my $itemid = $bynum{$pos};
15682:                 if (ref($confhash{$itemid}) ne 'HASH') {
15683:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
15684:                 } else {
15685:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
15686:                     my $position = $pos + 1;
15687:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15688:                     foreach my $item ('version','lifetime') {
15689:                         if ($confhash{$itemid}{$item} ne '') {
15690:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
15691:                         }
15692:                     }
15693:                     if ($encconfig{$itemid}{'key'} ne '') {
15694:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
15695:                     }
15696:                     if ($encconfig{$itemid}{'secret'} ne '') {
15697:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15698:                         my $num = length($encconfig{$itemid}{'secret'});
15699:                         $resulttext .= ('*'x$num).'</li>';
15700:                     }
15701:                     if ($confhash{$itemid}{'requser'}) {
15702:                         if ($confhash{$itemid}{'callback'}) {
15703:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
15704:                         } else {
15705:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
15706:                         }
15707:                         if ($confhash{$itemid}{'mapuser'}) {
15708:                             my $shownmapuser;
15709:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
15710:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
15711:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
15712:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
15713:                             } else {
15714:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
15715:                             }
15716:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
15717:                         }
15718:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
15719:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
15720:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
15721:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
15722:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
15723:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
15724:                                 } else {
15725:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
15726:                                                        $confhash{$itemid}{'lcauth'});
15727:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
15728:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
15729:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
15730:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
15731:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
15732:                                         }
15733:                                     } else {
15734:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
15735:                                     }
15736:                                 }
15737:                                 $resulttext .= '</li>';
15738:                             } else {
15739:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
15740:                             }
15741:                         }
15742:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
15743:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
15744:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
15745:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
15746:                             } else {
15747:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
15748:                             }
15749:                         }
15750:                         foreach my $item ('topmenu','inlinemenu') {
15751:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15752:                             if ($confhash{$itemid}{$item}) {
15753:                                 $resulttext .= &mt('Yes');
15754:                             } else {
15755:                                 $resulttext .= &mt('No');
15756:                             }
15757:                             $resulttext .= '</li>';
15758:                         }
15759:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15760:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15761:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
15762:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
15763:                             } else {
15764:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
15765:                             }
15766:                         }
15767:                         if ($confhash{$itemid}{'crsinc'}) {
15768:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15769:                                 my $rolemaps;
15770:                                 foreach my $role (@ltiroles) {
15771:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
15772:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
15773:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
15774:                                                                                 'Course').',';
15775:                                     }
15776:                                 }
15777:                                 if ($rolemaps) {
15778:                                     $rolemaps =~ s/,$//;
15779:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15780:                                 }
15781:                             }
15782:                             if ($confhash{$itemid}{'mapcrs'}) {
15783:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
15784:                             }
15785:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
15786:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
15787:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
15788:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
15789:                                                    '</li>';
15790:                                 } else {
15791:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
15792:                                 }
15793:                             }
15794:                             if ($confhash{$itemid}{'storecrs'}) {
15795:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
15796:                             }
15797:                             if ($confhash{$itemid}{'makecrs'}) {
15798:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
15799:                             } else {
15800:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
15801:                             }
15802:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
15803:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
15804:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
15805:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
15806:                                                    '</li>';
15807:                                 } else {
15808:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
15809:                                 }
15810:                             }
15811:                             if ($confhash{$itemid}{'section'}) {
15812:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
15813:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
15814:                                                          ' (course_section_sourcedid)'.'</li>';  
15815:                                 } else {
15816:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
15817:                                                           $confhash{$itemid}{'section'}.'</li>';
15818:                                 }
15819:                             } else {
15820:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
15821:                             }
15822:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
15823:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15824:                                 if ($confhash{$itemid}{$item}) {
15825:                                     $resulttext .= &mt('Yes');
15826:                                     if ($item eq 'passback') {
15827:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
15828:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
15829:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
15830:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
15831:                                         }
15832:                                     }
15833:                                 } else {
15834:                                     $resulttext .= &mt('No');
15835:                                 }
15836:                                 $resulttext .= '</li>';
15837:                             }
15838:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15839:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15840:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
15841:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
15842:                                 } else {
15843:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
15844:                                 }
15845:                             }
15846:                         }
15847:                     }
15848:                     $resulttext .= '</ul></li>';
15849:                 }
15850:             }
15851:             $resulttext .= '</ul>';
15852:         } else {
15853:             $resulttext = &mt('No changes made.');
15854:         }
15855:     } else {
15856:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15857:     }
15858:     if ($errors) {
15859:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15860:                        $errors.'</ul>';
15861:     }
15862:     return $resulttext;
15863: }
15864: 
15865: sub get_lti_id {
15866:     my ($domain,$consumer) = @_;
15867:     # get lock on lti db
15868:     my $lockhash = {
15869:                       lock => $env{'user.name'}.
15870:                               ':'.$env{'user.domain'},
15871:                    };
15872:     my $tries = 0;
15873:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15874:     my ($id,$error);
15875: 
15876:     while (($gotlock ne 'ok') && ($tries<10)) {
15877:         $tries ++;
15878:         sleep (0.1);
15879:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15880:     }
15881:     if ($gotlock eq 'ok') {
15882:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
15883:         if ($currids{'lock'}) {
15884:             delete($currids{'lock'});
15885:             if (keys(%currids)) {
15886:                 my @curr = sort { $a <=> $b } keys(%currids);
15887:                 if ($curr[-1] =~ /^\d+$/) {
15888:                     $id = 1 + $curr[-1];
15889:                 }
15890:             } else {
15891:                 $id = 1;
15892:             }
15893:             if ($id) {
15894:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
15895:                     $error = 'nostore';
15896:                 }
15897:             } else {
15898:                 $error = 'nonumber';
15899:             }
15900:         }
15901:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
15902:     } else {
15903:         $error = 'nolock';
15904:     }
15905:     return ($id,$error);
15906: }
15907: 
15908: sub modify_autoenroll {
15909:     my ($dom,$lastactref,%domconfig) = @_;
15910:     my ($resulttext,%changes);
15911:     my %currautoenroll;
15912:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
15913:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
15914:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
15915:         }
15916:     }
15917:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
15918:     my %title = ( run => 'Auto-enrollment active',
15919:                   sender => 'Sender for notification messages',
15920:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
15921:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
15922:     my @offon = ('off','on');
15923:     my $sender_uname = $env{'form.sender_uname'};
15924:     my $sender_domain = $env{'form.sender_domain'};
15925:     if ($sender_domain eq '') {
15926:         $sender_uname = '';
15927:     } elsif ($sender_uname eq '') {
15928:         $sender_domain = '';
15929:     }
15930:     my $coowners = $env{'form.autoassign_coowners'};
15931:     my $failsafe = $env{'form.autoenroll_failsafe'};
15932:     $failsafe =~ s{^\s+|\s+$}{}g;
15933:     if ($failsafe =~ /\D/) {
15934:         undef($failsafe);
15935:     }
15936:     my %autoenrollhash =  (
15937:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
15938:                                        'sender_uname' => $sender_uname,
15939:                                        'sender_domain' => $sender_domain,
15940:                                        'co-owners' => $coowners,
15941:                                        'autofailsafe' => $failsafe,
15942:                                 }
15943:                      );
15944:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
15945:                                              $dom);
15946:     if ($putresult eq 'ok') {
15947:         if (exists($currautoenroll{'run'})) {
15948:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
15949:                  $changes{'run'} = 1;
15950:              }
15951:         } elsif ($autorun) {
15952:             if ($env{'form.autoenroll_run'} ne '1') {
15953:                  $changes{'run'} = 1;
15954:             }
15955:         }
15956:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
15957:             $changes{'sender'} = 1;
15958:         }
15959:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
15960:             $changes{'sender'} = 1;
15961:         }
15962:         if ($currautoenroll{'co-owners'} ne '') {
15963:             if ($currautoenroll{'co-owners'} ne $coowners) {
15964:                 $changes{'coowners'} = 1;
15965:             }
15966:         } elsif ($coowners) {
15967:             $changes{'coowners'} = 1;
15968:         }
15969:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
15970:             $changes{'autofailsafe'} = 1;
15971:         }
15972:         if (keys(%changes) > 0) {
15973:             $resulttext = &mt('Changes made:').'<ul>';
15974:             if ($changes{'run'}) {
15975:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
15976:             }
15977:             if ($changes{'sender'}) {
15978:                 if ($sender_uname eq '' || $sender_domain eq '') {
15979:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
15980:                 } else {
15981:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
15982:                 }
15983:             }
15984:             if ($changes{'coowners'}) {
15985:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
15986:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15987:                 if (ref($lastactref) eq 'HASH') {
15988:                     $lastactref->{'domainconfig'} = 1;
15989:                 }
15990:             }
15991:             if ($changes{'autofailsafe'}) {
15992:                 if ($failsafe ne '') {
15993:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
15994:                 } else {
15995:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
15996:                 }
15997:                 &Apache::lonnet::get_domain_defaults($dom,1);
15998:                 if (ref($lastactref) eq 'HASH') {
15999:                     $lastactref->{'domdefaults'} = 1;
16000:                 }
16001:             }
16002:             $resulttext .= '</ul>';
16003:         } else {
16004:             $resulttext = &mt('No changes made to auto-enrollment settings');
16005:         }
16006:     } else {
16007:         $resulttext = '<span class="LC_error">'.
16008: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16009:     }
16010:     return $resulttext;
16011: }
16012: 
16013: sub modify_autoupdate {
16014:     my ($dom,%domconfig) = @_;
16015:     my ($resulttext,%currautoupdate,%fields,%changes);
16016:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16017:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16018:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16019:         }
16020:     }
16021:     my @offon = ('off','on');
16022:     my %title = &Apache::lonlocal::texthash (
16023:                     run        => 'Auto-update:',
16024:                     classlists => 'Updates to user information in classlists?',
16025:                     unexpired  => 'Skip updates for users without active or future roles?',
16026:                     lastactive => 'Skip updates for inactive users?',
16027:                 );
16028:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16029:     my %fieldtitles = &Apache::lonlocal::texthash (
16030:                         id => 'Student/Employee ID',
16031:                         permanentemail => 'E-mail address',
16032:                         lastname => 'Last Name',
16033:                         firstname => 'First Name',
16034:                         middlename => 'Middle Name',
16035:                         generation => 'Generation',
16036:                       );
16037:     $othertitle = &mt('All users');
16038:     if (keys(%{$usertypes}) >  0) {
16039:         $othertitle = &mt('Other users');
16040:     }
16041:     foreach my $key (keys(%env)) {
16042:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16043:             my ($usertype,$item) = ($1,$2);
16044:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16045:                 if ($usertype eq 'default') {   
16046:                     push(@{$fields{$1}},$2);
16047:                 } elsif (ref($types) eq 'ARRAY') {
16048:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16049:                         push(@{$fields{$1}},$2);
16050:                     }
16051:                 }
16052:             }
16053:         }
16054:     }
16055:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16056:     @lockablenames = sort(@lockablenames);
16057:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16058:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16059:         if (@changed) {
16060:             $changes{'lockablenames'} = 1;
16061:         }
16062:     } else {
16063:         if (@lockablenames) {
16064:             $changes{'lockablenames'} = 1;
16065:         }
16066:     }
16067:     my %updatehash = (
16068:                       autoupdate => { run => $env{'form.autoupdate_run'},
16069:                                       classlists => $env{'form.classlists'},
16070:                                       unexpired  => $env{'form.unexpired'},
16071:                                       fields => {%fields},
16072:                                       lockablenames => \@lockablenames,
16073:                                     }
16074:                      );
16075:     my $lastactivedays;
16076:     if ($env{'form.lastactive'}) {
16077:         $lastactivedays = $env{'form.lastactivedays'};
16078:         $lastactivedays =~ s/^\s+|\s+$//g;
16079:         unless ($lastactivedays =~ /^\d+$/) {
16080:             undef($lastactivedays);
16081:             $env{'form.lastactive'} = 0;
16082:         }
16083:     }
16084:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16085:     foreach my $key (keys(%currautoupdate)) {
16086:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16087:             if (exists($updatehash{autoupdate}{$key})) {
16088:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16089:                     $changes{$key} = 1;
16090:                 }
16091:             }
16092:         } elsif ($key eq 'fields') {
16093:             if (ref($currautoupdate{$key}) eq 'HASH') {
16094:                 foreach my $item (@{$types},'default') {
16095:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16096:                         my $change = 0;
16097:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16098:                             if (!exists($fields{$item})) {
16099:                                 $change = 1;
16100:                                 last;
16101:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16102:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16103:                                     $change = 1;
16104:                                     last;
16105:                                 }
16106:                             }
16107:                         }
16108:                         if ($change) {
16109:                             push(@{$changes{$key}},$item);
16110:                         }
16111:                     } 
16112:                 }
16113:             }
16114:         } elsif ($key eq 'lockablenames') {
16115:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16116:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16117:                 if (@changed) {
16118:                     $changes{'lockablenames'} = 1;
16119:                 }
16120:             } else {
16121:                 if (@lockablenames) {
16122:                     $changes{'lockablenames'} = 1;
16123:                 }
16124:             }
16125:         }
16126:     }
16127:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16128:         if (@lockablenames) {
16129:             $changes{'lockablenames'} = 1;
16130:         }
16131:     }
16132:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16133:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16134:             $changes{'unexpired'} = 1;
16135:         }
16136:     }
16137:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16138:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16139:             $changes{'lastactive'} = 1;
16140:         }
16141:     }
16142:     foreach my $item (@{$types},'default') {
16143:         if (defined($fields{$item})) {
16144:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16145:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16146:                     my $change = 0;
16147:                     if (ref($fields{$item}) eq 'ARRAY') {
16148:                         foreach my $type (@{$fields{$item}}) {
16149:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16150:                                 $change = 1;
16151:                                 last;
16152:                             }
16153:                         }
16154:                     }
16155:                     if ($change) {
16156:                         push(@{$changes{'fields'}},$item);
16157:                     }
16158:                 } else {
16159:                     push(@{$changes{'fields'}},$item);
16160:                 }
16161:             } else {
16162:                 push(@{$changes{'fields'}},$item);
16163:             }
16164:         }
16165:     }
16166:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16167:                                              $dom);
16168:     if ($putresult eq 'ok') {
16169:         if (keys(%changes) > 0) {
16170:             $resulttext = &mt('Changes made:').'<ul>';
16171:             foreach my $key (sort(keys(%changes))) {
16172:                 if ($key eq 'lockablenames') {
16173:                     $resulttext .= '<li>';
16174:                     if (@lockablenames) {
16175:                         $usertypes->{'default'} = $othertitle;
16176:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16177:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16178:                     } else {
16179:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16180:                     }
16181:                     $resulttext .= '</li>';
16182:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16183:                     foreach my $item (@{$changes{$key}}) {
16184:                         my @newvalues;
16185:                         foreach my $type (@{$fields{$item}}) {
16186:                             push(@newvalues,$fieldtitles{$type});
16187:                         }
16188:                         my $newvaluestr;
16189:                         if (@newvalues > 0) {
16190:                             $newvaluestr = join(', ',@newvalues);
16191:                         } else {
16192:                             $newvaluestr = &mt('none');
16193:                         }
16194:                         if ($item eq 'default') {
16195:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16196:                         } else {
16197:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16198:                         }
16199:                     }
16200:                 } else {
16201:                     my $newvalue;
16202:                     if ($key eq 'run') {
16203:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16204:                     } elsif ($key eq 'lastactive') {
16205:                         $newvalue = $offon[$env{'form.lastactive'}];
16206:                         unless ($lastactivedays eq '') {
16207:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16208:                         }
16209:                     } else {
16210:                         $newvalue = $offon[$env{'form.'.$key}];
16211:                     }
16212:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16213:                 }
16214:             }
16215:             $resulttext .= '</ul>';
16216:         } else {
16217:             $resulttext = &mt('No changes made to autoupdates');
16218:         }
16219:     } else {
16220:         $resulttext = '<span class="LC_error">'.
16221: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16222:     }
16223:     return $resulttext;
16224: }
16225: 
16226: sub modify_autocreate {
16227:     my ($dom,%domconfig) = @_;
16228:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16229:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16230:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16231:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16232:         }
16233:     }
16234:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
16235:                  req => 'Auto-creation of validated requests for official courses',
16236:                  xmldc => 'Identity of course creator of courses from XML files',
16237:                );
16238:     my @types = ('xml','req');
16239:     foreach my $item (@types) {
16240:         $newvals{$item} = $env{'form.autocreate_'.$item};
16241:         $newvals{$item} =~ s/\D//g;
16242:         $newvals{$item} = 0 if ($newvals{$item} eq '');
16243:     }
16244:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
16245:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16246:     unless (exists($domcoords{$newvals{'xmldc'}})) {
16247:         $newvals{'xmldc'} = '';
16248:     } 
16249:     %autocreatehash =  (
16250:                         autocreate => { xml => $newvals{'xml'},
16251:                                         req => $newvals{'req'},
16252:                                       }
16253:                        );
16254:     if ($newvals{'xmldc'} ne '') {
16255:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
16256:     }
16257:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
16258:                                              $dom);
16259:     if ($putresult eq 'ok') {
16260:         my @items = @types;
16261:         if ($newvals{'xml'}) {
16262:             push(@items,'xmldc');
16263:         }
16264:         foreach my $item (@items) {
16265:             if (exists($currautocreate{$item})) {
16266:                 if ($currautocreate{$item} ne $newvals{$item}) {
16267:                     $changes{$item} = 1;
16268:                 }
16269:             } elsif ($newvals{$item}) {
16270:                 $changes{$item} = 1;
16271:             }
16272:         }
16273:         if (keys(%changes) > 0) {
16274:             my @offon = ('off','on'); 
16275:             $resulttext = &mt('Changes made:').'<ul>';
16276:             foreach my $item (@types) {
16277:                 if ($changes{$item}) {
16278:                     my $newtxt = $offon[$newvals{$item}];
16279:                     $resulttext .= '<li>'.
16280:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
16281:                                        '<b>','</b>').
16282:                                    '</li>';
16283:                 }
16284:             }
16285:             if ($changes{'xmldc'}) {
16286:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
16287:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
16288:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
16289:             }
16290:             $resulttext .= '</ul>';
16291:         } else {
16292:             $resulttext = &mt('No changes made to auto-creation settings');
16293:         }
16294:     } else {
16295:         $resulttext = '<span class="LC_error">'.
16296:             &mt('An error occurred: [_1]',$putresult).'</span>';
16297:     }
16298:     return $resulttext;
16299: }
16300: 
16301: sub modify_directorysrch {
16302:     my ($dom,$lastactref,%domconfig) = @_;
16303:     my ($resulttext,%changes);
16304:     my %currdirsrch;
16305:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
16306:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
16307:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
16308:         }
16309:     }
16310:     my %title = ( available => 'Institutional directory search available',
16311:                   localonly => 'Other domains can search institution',
16312:                   lcavailable => 'LON-CAPA directory search available',
16313:                   lclocalonly => 'Other domains can search LON-CAPA domain',
16314:                   searchby => 'Search types',
16315:                   searchtypes => 'Search latitude');
16316:     my @offon = ('off','on');
16317:     my @otherdoms = ('Yes','No');
16318: 
16319:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
16320:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
16321:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
16322: 
16323:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16324:     if (keys(%{$usertypes}) == 0) {
16325:         @cansearch = ('default');
16326:     } else {
16327:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
16328:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
16329:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
16330:                     push(@{$changes{'cansearch'}},$type);
16331:                 }
16332:             }
16333:             foreach my $type (@cansearch) {
16334:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
16335:                     push(@{$changes{'cansearch'}},$type);
16336:                 }
16337:             }
16338:         } else {
16339:             push(@{$changes{'cansearch'}},@cansearch);
16340:         }
16341:     }
16342: 
16343:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
16344:         foreach my $by (@{$currdirsrch{'searchby'}}) {
16345:             if (!grep(/^\Q$by\E$/,@searchby)) {
16346:                 push(@{$changes{'searchby'}},$by);
16347:             }
16348:         }
16349:         foreach my $by (@searchby) {
16350:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
16351:                 push(@{$changes{'searchby'}},$by);
16352:             }
16353:         }
16354:     } else {
16355:         push(@{$changes{'searchby'}},@searchby);
16356:     }
16357: 
16358:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
16359:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
16360:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
16361:                 push(@{$changes{'searchtypes'}},$type);
16362:             }
16363:         }
16364:         foreach my $type (@searchtypes) {
16365:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
16366:                 push(@{$changes{'searchtypes'}},$type);
16367:             }
16368:         }
16369:     } else {
16370:         if (exists($currdirsrch{'searchtypes'})) {
16371:             foreach my $type (@searchtypes) {  
16372:                 if ($type ne $currdirsrch{'searchtypes'}) { 
16373:                     push(@{$changes{'searchtypes'}},$type);
16374:                 }
16375:             }
16376:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
16377:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
16378:             }   
16379:         } else {
16380:             push(@{$changes{'searchtypes'}},@searchtypes); 
16381:         }
16382:     }
16383: 
16384:     my %dirsrch_hash =  (
16385:             directorysrch => { available => $env{'form.dirsrch_available'},
16386:                                cansearch => \@cansearch,
16387:                                localonly => $env{'form.dirsrch_instlocalonly'},
16388:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
16389:                                lcavailable => $env{'form.dirsrch_domavailable'},
16390:                                searchby => \@searchby,
16391:                                searchtypes => \@searchtypes,
16392:                              }
16393:             );
16394:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
16395:                                              $dom);
16396:     if ($putresult eq 'ok') {
16397:         if (exists($currdirsrch{'available'})) {
16398:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
16399:                  $changes{'available'} = 1;
16400:              }
16401:         } else {
16402:             if ($env{'form.dirsrch_available'} eq '1') {
16403:                 $changes{'available'} = 1;
16404:             }
16405:         }
16406:         if (exists($currdirsrch{'lcavailable'})) {
16407:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
16408:                 $changes{'lcavailable'} = 1;
16409:             }
16410:         } else {
16411:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
16412:                 $changes{'lcavailable'} = 1;
16413:             }
16414:         }
16415:         if (exists($currdirsrch{'localonly'})) {
16416:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
16417:                 $changes{'localonly'} = 1;
16418:             }
16419:         } else {
16420:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
16421:                 $changes{'localonly'} = 1;
16422:             }
16423:         }
16424:         if (exists($currdirsrch{'lclocalonly'})) {
16425:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
16426:                 $changes{'lclocalonly'} = 1;
16427:             }
16428:         } else {
16429:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
16430:                 $changes{'lclocalonly'} = 1;
16431:             }
16432:         }
16433:         if (keys(%changes) > 0) {
16434:             $resulttext = &mt('Changes made:').'<ul>';
16435:             if ($changes{'available'}) {
16436:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
16437:             }
16438:             if ($changes{'lcavailable'}) {
16439:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
16440:             }
16441:             if ($changes{'localonly'}) {
16442:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
16443:             }
16444:             if ($changes{'lclocalonly'}) {
16445:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
16446:             }
16447:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
16448:                 my $chgtext;
16449:                 if (ref($usertypes) eq 'HASH') {
16450:                     if (keys(%{$usertypes}) > 0) {
16451:                         foreach my $type (@{$types}) {
16452:                             if (grep(/^\Q$type\E$/,@cansearch)) {
16453:                                 $chgtext .= $usertypes->{$type}.'; ';
16454:                             }
16455:                         }
16456:                         if (grep(/^default$/,@cansearch)) {
16457:                             $chgtext .= $othertitle;
16458:                         } else {
16459:                             $chgtext =~ s/\; $//;
16460:                         }
16461:                         $resulttext .=
16462:                             '<li>'.
16463:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
16464:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
16465:                             '</li>';
16466:                     }
16467:                 }
16468:             }
16469:             if (ref($changes{'searchby'}) eq 'ARRAY') {
16470:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
16471:                 my $chgtext;
16472:                 foreach my $type (@{$titleorder}) {
16473:                     if (grep(/^\Q$type\E$/,@searchby)) {
16474:                         if (defined($searchtitles->{$type})) {
16475:                             $chgtext .= $searchtitles->{$type}.'; ';
16476:                         }
16477:                     }
16478:                 }
16479:                 $chgtext =~ s/\; $//;
16480:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
16481:             }
16482:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
16483:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
16484:                 my $chgtext;
16485:                 foreach my $type (@{$srchtypeorder}) {
16486:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
16487:                         if (defined($srchtypes_desc->{$type})) {
16488:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
16489:                         }
16490:                     }
16491:                 }
16492:                 $chgtext =~ s/\; $//;
16493:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
16494:             }
16495:             $resulttext .= '</ul>';
16496:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
16497:             if (ref($lastactref) eq 'HASH') {
16498:                 $lastactref->{'directorysrch'} = 1;
16499:             }
16500:         } else {
16501:             $resulttext = &mt('No changes made to directory search settings');
16502:         }
16503:     } else {
16504:         $resulttext = '<span class="LC_error">'.
16505:                       &mt('An error occurred: [_1]',$putresult).'</span>';
16506:     }
16507:     return $resulttext;
16508: }
16509: 
16510: sub modify_contacts {
16511:     my ($dom,$lastactref,%domconfig) = @_;
16512:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
16513:     if (ref($domconfig{'contacts'}) eq 'HASH') {
16514:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
16515:             $currsetting{$key} = $domconfig{'contacts'}{$key};
16516:         }
16517:     }
16518:     my (%others,%to,%bcc,%includestr,%includeloc);
16519:     my @contacts = ('supportemail','adminemail');
16520:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
16521:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
16522:     my @toggles = ('reporterrors','reportupdates','reportstatus');
16523:     my @lonstatus = ('threshold','sysmail','weights','excluded');
16524:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
16525:     foreach my $type (@mailings) {
16526:         @{$newsetting{$type}} = 
16527:             &Apache::loncommon::get_env_multiple('form.'.$type);
16528:         foreach my $item (@contacts) {
16529:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
16530:                 $contacts_hash{contacts}{$type}{$item} = 1;
16531:             } else {
16532:                 $contacts_hash{contacts}{$type}{$item} = 0;
16533:             }
16534:         }
16535:         $others{$type} = $env{'form.'.$type.'_others'};
16536:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
16537:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16538:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
16539:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
16540:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
16541:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
16542:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
16543:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16544:             }
16545:         }
16546:     }
16547:     foreach my $item (@contacts) {
16548:         $to{$item} = $env{'form.'.$item};
16549:         $contacts_hash{'contacts'}{$item} = $to{$item};
16550:     }
16551:     foreach my $item (@toggles) {
16552:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
16553:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
16554:         }
16555:     }
16556:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
16557:     foreach my $item (@lonstatus) {
16558:         if ($item eq 'excluded') {
16559:             my (%serverhomes,@excluded);
16560:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
16561:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
16562:             if (@possexcluded) {
16563:                 foreach my $id (sort(@possexcluded)) {
16564:                     if ($serverhomes{$id}) {
16565:                         push(@excluded,$id);
16566:                     }
16567:                 }
16568:             }
16569:             if (@excluded) {
16570:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
16571:             }
16572:         } elsif ($item eq 'weights') {
16573:             foreach my $type ('E','W','N','U') {
16574:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
16575:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
16576:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
16577:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
16578:                             $env{'form.error'.$item.'_'.$type};
16579:                     }
16580:                 }
16581:             }
16582:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
16583:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
16584:             if ($env{'form.error'.$item} =~ /^\d+$/) {
16585:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
16586:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
16587:                 }
16588:             }
16589:         }
16590:     }
16591:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
16592:         foreach my $field (@{$fields}) {
16593:             if (ref($possoptions->{$field}) eq 'ARRAY') {
16594:                 my $value = $env{'form.helpform_'.$field};
16595:                 $value =~ s/^\s+|\s+$//g;
16596:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
16597:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
16598:                     if ($field eq 'screenshot') {
16599:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
16600:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
16601:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
16602:                         }
16603:                     }
16604:                 }
16605:             }
16606:         }
16607:     }
16608:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16609:     my (@statuses,%usertypeshash,@overrides);
16610:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
16611:         @statuses = @{$types};
16612:         if (ref($usertypes) eq 'HASH') {
16613:             %usertypeshash = %{$usertypes};
16614:         }
16615:     }
16616:     if (@statuses) {
16617:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
16618:         foreach my $type (@possoverrides) {
16619:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
16620:                 push(@overrides,$type);
16621:             }
16622:         }
16623:         if (@overrides) {
16624:             foreach my $type (@overrides) {
16625:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
16626:                 foreach my $item (@contacts) {
16627:                     if (grep(/^\Q$item\E$/,@standard)) {
16628:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
16629:                         $newsetting{'override_'.$type}{$item} = 1;
16630:                     } else {
16631:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
16632:                         $newsetting{'override_'.$type}{$item} = 0;
16633:                     }
16634:                 }
16635:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
16636:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16637:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
16638:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16639:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
16640:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
16641:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
16642:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16643:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
16644:                 }
16645:             }     
16646:         }
16647:     }
16648:     if (keys(%currsetting) > 0) {
16649:         foreach my $item (@contacts) {
16650:             if ($to{$item} ne $currsetting{$item}) {
16651:                 $changes{$item} = 1;
16652:             }
16653:         }
16654:         foreach my $type (@mailings) {
16655:             foreach my $item (@contacts) {
16656:                 if (ref($currsetting{$type}) eq 'HASH') {
16657:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
16658:                         push(@{$changes{$type}},$item);
16659:                     }
16660:                 } else {
16661:                     push(@{$changes{$type}},@{$newsetting{$type}});
16662:                 }
16663:             }
16664:             if ($others{$type} ne $currsetting{$type}{'others'}) {
16665:                 push(@{$changes{$type}},'others');
16666:             }
16667:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16668:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
16669:                     push(@{$changes{$type}},'bcc'); 
16670:                 }
16671:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
16672:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
16673:                     push(@{$changes{$type}},'include');
16674:                 }
16675:             }
16676:         }
16677:         if (ref($fields) eq 'ARRAY') {
16678:             if (ref($currsetting{'helpform'}) eq 'HASH') {
16679:                 foreach my $field (@{$fields}) {
16680:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
16681:                         push(@{$changes{'helpform'}},$field);
16682:                     }
16683:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16684:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
16685:                             push(@{$changes{'helpform'}},'maxsize');
16686:                         }
16687:                     }
16688:                 }
16689:             } else {
16690:                 foreach my $field (@{$fields}) {
16691:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16692:                         push(@{$changes{'helpform'}},$field);
16693:                     }
16694:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16695:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16696:                             push(@{$changes{'helpform'}},'maxsize');
16697:                         }
16698:                     }
16699:                 }
16700:             }
16701:         }
16702:         if (@statuses) {
16703:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
16704:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
16705:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
16706:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
16707:                             foreach my $item (@contacts,'bcc','others','include') {
16708:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
16709:                                     push(@{$changes{'overrides'}},$key);
16710:                                     last;
16711:                                 }
16712:                             }
16713:                         } else {
16714:                             push(@{$changes{'overrides'}},$key);
16715:                         }
16716:                     }
16717:                 }
16718:                 foreach my $key (@overrides) {
16719:                     unless (exists($currsetting{'overrides'}{$key})) {
16720:                         push(@{$changes{'overrides'}},$key);
16721:                     }
16722:                 }
16723:             } else {
16724:                 foreach my $key (@overrides) {
16725:                     push(@{$changes{'overrides'}},$key); 
16726:                 }
16727:             }
16728:         }
16729:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
16730:             foreach my $key ('excluded','weights','threshold','sysmail') {
16731:                 if ($key eq 'excluded') {
16732:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16733:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
16734:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16735:                             (@{$currsetting{'lonstatus'}{$key}})) {
16736:                             my @diffs =
16737:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
16738:                                                                    $currsetting{'lonstatus'}{$key});
16739:                             if (@diffs) {
16740:                                 push(@{$changes{'lonstatus'}},$key);
16741:                             }
16742:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
16743:                             push(@{$changes{'lonstatus'}},$key);
16744:                         }
16745:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16746:                              (@{$currsetting{'lonstatus'}{$key}})) {
16747:                         push(@{$changes{'lonstatus'}},$key);
16748:                     }
16749:                 } elsif ($key eq 'weights') {
16750:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16751:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
16752:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16753:                             foreach my $type ('E','W','N','U') {
16754:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
16755:                                         $currsetting{'lonstatus'}{$key}{$type}) {
16756:                                     push(@{$changes{'lonstatus'}},$key);
16757:                                     last;
16758:                                 }
16759:                             }
16760:                         } else {
16761:                             foreach my $type ('E','W','N','U') {
16762:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
16763:                                     push(@{$changes{'lonstatus'}},$key);
16764:                                     last;
16765:                                 }
16766:                             }
16767:                         }
16768:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16769:                         foreach my $type ('E','W','N','U') {
16770:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
16771:                                 push(@{$changes{'lonstatus'}},$key);
16772:                                 last;
16773:                             }
16774:                         }
16775:                     }
16776:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
16777:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16778:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16779:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
16780:                                 push(@{$changes{'lonstatus'}},$key);
16781:                             }
16782:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
16783:                             push(@{$changes{'lonstatus'}},$key);
16784:                         }
16785:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16786:                         push(@{$changes{'lonstatus'}},$key);
16787:                     }
16788:                 }
16789:             }
16790:         } else {
16791:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16792:                 foreach my $key ('excluded','weights','threshold','sysmail') {
16793:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16794:                         push(@{$changes{'lonstatus'}},$key);
16795:                     }
16796:                 }
16797:             }
16798:         }
16799:     } else {
16800:         my %default;
16801:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
16802:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
16803:         $default{'errormail'} = 'adminemail';
16804:         $default{'packagesmail'} = 'adminemail';
16805:         $default{'helpdeskmail'} = 'supportemail';
16806:         $default{'otherdomsmail'} = 'supportemail';
16807:         $default{'lonstatusmail'} = 'adminemail';
16808:         $default{'requestsmail'} = 'adminemail';
16809:         $default{'updatesmail'} = 'adminemail';
16810:         $default{'hostipmail'} = 'adminemail';
16811:         foreach my $item (@contacts) {
16812:            if ($to{$item} ne $default{$item}) {
16813:                $changes{$item} = 1;
16814:            }
16815:         }
16816:         foreach my $type (@mailings) {
16817:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
16818:                 push(@{$changes{$type}},@{$newsetting{$type}});
16819:             }
16820:             if ($others{$type} ne '') {
16821:                 push(@{$changes{$type}},'others');
16822:             }
16823:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16824:                 if ($bcc{$type} ne '') {
16825:                     push(@{$changes{$type}},'bcc');
16826:                 }
16827:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
16828:                     push(@{$changes{$type}},'include');
16829:                 }
16830:             }
16831:         }
16832:         if (ref($fields) eq 'ARRAY') {
16833:             foreach my $field (@{$fields}) {
16834:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16835:                     push(@{$changes{'helpform'}},$field);
16836:                 }
16837:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16838:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16839:                         push(@{$changes{'helpform'}},'maxsize');
16840:                     }
16841:                 }
16842:             }
16843:         }
16844:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16845:             foreach my $key ('excluded','weights','threshold','sysmail') {
16846:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16847:                     push(@{$changes{'lonstatus'}},$key);
16848:                 }
16849:             }
16850:         }
16851:     }
16852:     foreach my $item (@toggles) {
16853:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
16854:             $changes{$item} = 1;
16855:         } elsif ((!$env{'form.'.$item}) &&
16856:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
16857:             $changes{$item} = 1;
16858:         }
16859:     }
16860:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
16861:                                              $dom);
16862:     if ($putresult eq 'ok') {
16863:         if (keys(%changes) > 0) {
16864:             &Apache::loncommon::devalidate_domconfig_cache($dom);
16865:             if (ref($lastactref) eq 'HASH') {
16866:                 $lastactref->{'domainconfig'} = 1;
16867:             }
16868:             my ($titles,$short_titles)  = &contact_titles();
16869:             $resulttext = &mt('Changes made:').'<ul>';
16870:             foreach my $item (@contacts) {
16871:                 if ($changes{$item}) {
16872:                     $resulttext .= '<li>'.$titles->{$item}.
16873:                                     &mt(' set to: ').
16874:                                     '<span class="LC_cusr_emph">'.
16875:                                     $to{$item}.'</span></li>';
16876:                 }
16877:             }
16878:             foreach my $type (@mailings) {
16879:                 if (ref($changes{$type}) eq 'ARRAY') {
16880:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16881:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
16882:                     } else {
16883:                         $resulttext .= '<li>'.$titles->{$type}.': ';
16884:                     }
16885:                     my @text;
16886:                     foreach my $item (@{$newsetting{$type}}) {
16887:                         push(@text,$short_titles->{$item});
16888:                     }
16889:                     if ($others{$type} ne '') {
16890:                         push(@text,$others{$type});
16891:                     }
16892:                     if (@text) {
16893:                         $resulttext .= '<span class="LC_cusr_emph">'.
16894:                                        join(', ',@text).'</span>';
16895:                     }
16896:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16897:                         if ($bcc{$type} ne '') {
16898:                             my $bcctext;
16899:                             if (@text) {
16900:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
16901:                             } else {
16902:                                 $bcctext = '(Bcc)';
16903:                             }
16904:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
16905:                         } elsif (!@text) {
16906:                             $resulttext .= &mt('No one');
16907:                         }   
16908:                         if ($includestr{$type} ne '') {
16909:                             if ($includeloc{$type} eq 'b') {
16910:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
16911:                             } elsif ($includeloc{$type} eq 's') {
16912:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
16913:                             }
16914:                         }
16915:                     } elsif (!@text) {
16916:                         $resulttext .= &mt('No recipients');
16917:                     }
16918:                     $resulttext .= '</li>';
16919:                 }
16920:             }
16921:             if (ref($changes{'overrides'}) eq 'ARRAY') {
16922:                 my @deletions;
16923:                 foreach my $type (@{$changes{'overrides'}}) {
16924:                     if ($usertypeshash{$type}) {
16925:                         if (grep(/^\Q$type\E/,@overrides)) {
16926:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
16927:                                                       $usertypeshash{$type}).'<ul><li>';
16928:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
16929:                                 my @text;
16930:                                 foreach my $item (@contacts) {
16931:                                     if ($newsetting{'override_'.$type}{$item}) { 
16932:                                         push(@text,$short_titles->{$item});
16933:                                     }
16934:                                 }
16935:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
16936:                                     push(@text,$newsetting{'override_'.$type}{'others'});
16937:                                 }
16938:   
16939:                                 if (@text) {
16940:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
16941:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
16942:                                 }
16943:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
16944:                                     my $bcctext;
16945:                                     if (@text) {
16946:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
16947:                                     } else {
16948:                                         $bcctext = '(Bcc)';
16949:                                     }
16950:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
16951:                                 } elsif (!@text) {
16952:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
16953:                                 }
16954:                                 $resulttext .= '</li>';
16955:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
16956:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
16957:                                     if ($loc eq 'b') {
16958:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
16959:                                     } elsif ($loc eq 's') {
16960:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
16961:                                     }
16962:                                 }
16963:                             }
16964:                             $resulttext .= '</li></ul></li>';
16965:                         } else {
16966:                             push(@deletions,$usertypeshash{$type});
16967:                         }
16968:                     }
16969:                 }
16970:                 if (@deletions) {
16971:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
16972:                                               join(', ',@deletions)).'</li>';
16973:                 }
16974:             }
16975:             my @offon = ('off','on');
16976:             my $corelink = &core_link_msu();
16977:             if ($changes{'reporterrors'}) {
16978:                 $resulttext .= '<li>'.
16979:                                &mt('E-mail error reports to [_1] set to "'.
16980:                                    $offon[$env{'form.reporterrors'}].'".',
16981:                                    $corelink).
16982:                                '</li>';
16983:             }
16984:             if ($changes{'reportupdates'}) {
16985:                 $resulttext .= '<li>'.
16986:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
16987:                                     $offon[$env{'form.reportupdates'}].'".',
16988:                                     $corelink).
16989:                                 '</li>';
16990:             }
16991:             if ($changes{'reportstatus'}) {
16992:                 $resulttext .= '<li>'.
16993:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
16994:                                     $offon[$env{'form.reportstatus'}].'".',
16995:                                     $corelink).
16996:                                 '</li>';
16997:             }
16998:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
16999:                 $resulttext .= '<li>'.
17000:                                &mt('Nightly status check e-mail settings').':<ul>';
17001:                 my (%defval,%use_def,%shown);
17002:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
17003:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
17004:                 $defval{'weights'} =
17005:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
17006:                 $defval{'excluded'} = &mt('None');
17007:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17008:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17009:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17010:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17011:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17012:                             } elsif ($item eq 'weights') {
17013:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17014:                                     foreach my $type ('E','W','N','U') {
17015:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17016:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17017:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17018:                                         } else {
17019:                                             $shown{$item} .= $lonstatus_defs->{$type};
17020:                                         }
17021:                                         $shown{$item} .= ', ';
17022:                                     }
17023:                                     $shown{$item} =~ s/, $//;
17024:                                 } else {
17025:                                     $shown{$item} = $defval{$item};
17026:                                 }
17027:                             } elsif ($item eq 'excluded') {
17028:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17029:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17030:                                 } else {
17031:                                     $shown{$item} = $defval{$item};
17032:                                 }
17033:                             }
17034:                         } else {
17035:                             $shown{$item} = $defval{$item};
17036:                         }
17037:                     }
17038:                 } else {
17039:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17040:                         $shown{$item} = $defval{$item};
17041:                     }
17042:                 }
17043:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17044:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17045:                                           $shown{$item}).'</li>';
17046:                 }
17047:                 $resulttext .= '</ul></li>';
17048:             }
17049:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17050:                 my (@optional,@required,@unused,$maxsizechg);
17051:                 foreach my $field (@{$changes{'helpform'}}) {
17052:                     if ($field eq 'maxsize') {
17053:                         $maxsizechg = 1;
17054:                         next;
17055:                     }
17056:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17057:                         push(@optional,$field);
17058:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17059:                         push(@unused,$field);
17060:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17061:                         push(@required,$field);
17062:                     }
17063:                 }
17064:                 if (@optional) {
17065:                     $resulttext .= '<li>'.
17066:                                    &mt('Help form fields changed to "Optional": [_1].',
17067:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17068:                                    '</li>';
17069:                 }
17070:                 if (@required) {
17071:                     $resulttext .= '<li>'.
17072:                                    &mt('Help form fields changed to "Required": [_1].',
17073:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17074:                                    '</li>';
17075:                 }
17076:                 if (@unused) {
17077:                     $resulttext .= '<li>'.
17078:                                    &mt('Help form fields changed to "Not shown": [_1].',
17079:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17080:                                    '</li>';
17081:                 }
17082:                 if ($maxsizechg) {
17083:                     $resulttext .= '<li>'.
17084:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17085:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17086:                                    '</li>';
17087:                 }
17088:             }
17089:             $resulttext .= '</ul>';
17090:         } else {
17091:             $resulttext = &mt('No changes made to contacts and form settings');
17092:         }
17093:     } else {
17094:         $resulttext = '<span class="LC_error">'.
17095:             &mt('An error occurred: [_1].',$putresult).'</span>';
17096:     }
17097:     return $resulttext;
17098: }
17099: 
17100: sub modify_privacy {
17101:     my ($dom,%domconfig) = @_;
17102:     my ($resulttext,%current,%changes);
17103:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17104:         %current = %{$domconfig{'privacy'}};
17105:     }
17106:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17107:     my @items = ('domain','author','course','community');
17108:     my %names = &Apache::lonlocal::texthash (
17109:                    domain => 'Assigned domain role(s)',
17110:                    author => 'Assigned co-author role(s)',
17111:                    course => 'Assigned course role(s)',
17112:                    community => 'Assigned community role',
17113:                 );
17114:     my %roles = &Apache::lonlocal::texthash (
17115:                    domain => 'Domain role',
17116:                    author => 'Co-author role',
17117:                    course => 'Course role',
17118:                    community => 'Community role',
17119:                 );
17120:     my %titles = &Apache::lonlocal::texthash (
17121:                   approval => 'Approval for role in different domain',
17122:                   othdom   => 'User information available in other domain',
17123:                   priv     => 'Information viewable by privileged user in same domain',
17124:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17125:                   instdom  => 'Other domain shares institution/provider',
17126:                   extdom   => 'Other domain has different institution/provider',
17127:                   none     => 'Not allowed',
17128:                   user     => 'User authorizes',
17129:                   domain   => 'Domain Coordinator authorizes',
17130:                   auto     => 'Unrestricted',
17131:     );
17132:     my %fieldnames = &Apache::lonlocal::texthash (
17133:                         id => 'Student/Employee ID',
17134:                         permanentemail => 'E-mail address',
17135:                         lastname => 'Last Name',
17136:                         firstname => 'First Name',
17137:                         middlename => 'Middle Name',
17138:                         generation => 'Generation',
17139:     );
17140:     my ($othertitle,$usertypes,$types) =
17141:         &Apache::loncommon::sorted_inst_types($dom);
17142:     my (%by_ip,%by_location,@intdoms,@instdoms);
17143:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17144: 
17145:     my %privacyhash = (
17146:                        'approval' => {
17147:                                        instdom => {},
17148:                                        extdom  => {},
17149:                                      },
17150:                        'othdom'   => {},
17151:                        'priv'     => {},
17152:                        'unpriv'   => {},
17153:                       );
17154:     foreach my $item (@items) {
17155:         if (@instdoms > 1) {
17156:             if ($env{'form.privacy_approval_instdom'.$item} =~ /^(none|user|domain|auto)$/) {
17157:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17158:             }
17159:             if (ref($current{'approval'}) eq 'HASH') {
17160:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17161:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17162:                         $changes{'approval'} = 1;
17163:                     }
17164:                 }
17165:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17166:                 $changes{'approval'} = 1;
17167:             }
17168:         }
17169:         if (keys(%by_location) > 0) {
17170:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17171:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17172:             }
17173:             if (ref($current{'approval'}) eq 'HASH') {
17174:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17175:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17176:                         $changes{'approval'} = 1;
17177:                     }
17178:                 }
17179:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17180:                 $changes{'approval'} = 1;
17181:             }
17182:         }
17183:         foreach my $status ('priv','unpriv') {
17184:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17185:             my @newvalues;
17186:             foreach my $field (@possibles) {
17187:                 if (grep(/^\Q$field\E$/,@fields)) {
17188:                     $privacyhash{$status}{$item}{$field} = 1;
17189:                     push(@newvalues,$field);
17190:                 }
17191:             }
17192:             @newvalues = sort(@newvalues);
17193:             if (ref($current{$status}) eq 'HASH') {
17194:                 if (ref($current{$status}{$item}) eq 'HASH') {
17195:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17196:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17197:                     if (@diffs > 0) {
17198:                         $changes{$status} = 1;
17199:                     }
17200:                 }
17201:             } else {
17202:                 my @stdfields;
17203:                 foreach my $field (@fields) {
17204:                     if ($field eq 'id') {
17205:                         next if ($status eq 'unpriv');
17206:                         next if (($status eq 'priv') && ($item eq 'community'));
17207:                     }
17208:                     push(@stdfields,$field);
17209:                 }
17210:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17211:                 if (@diffs > 0) {
17212:                     $changes{$status} = 1;
17213:                 }
17214:             }
17215:         }
17216:     }
17217:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17218:         my @statuses;
17219:         if (ref($types) eq 'ARRAY') {
17220:             @statuses = @{$types};
17221:         }
17222:         foreach my $type (@statuses,'default') {
17223:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17224:             my @newvalues;
17225:             foreach my $field (sort(@possfields)) {
17226:                 if (grep(/^\Q$field\E$/,@fields)) {
17227:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17228:                     push(@newvalues,$field);
17229:                 }
17230:             }
17231:             @newvalues = sort(@newvalues);
17232:             if (ref($current{'othdom'}) eq 'HASH') {
17233:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
17234:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
17235:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17236:                     if (@diffs > 0) {
17237:                         $changes{'othdom'} = 1;
17238:                     }
17239:                 }
17240:             } else {
17241:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
17242:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17243:                 if (@diffs > 0) {
17244:                     $changes{'othdom'} = 1;
17245:                 }
17246:             }
17247:         }
17248:     }
17249:     my %confighash = (
17250:                         privacy => \%privacyhash,
17251:                      );
17252:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17253:     if ($putresult eq 'ok') {
17254:         if (keys(%changes) > 0) {
17255:             $resulttext = &mt('Changes made: ').'<ul>';
17256:             foreach my $key ('approval','othdom','priv','unpriv') {
17257:                 if ($changes{$key}) {
17258:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17259:                     if ($key eq 'approval') {
17260:                         if (keys(%{$privacyhash{$key}{instdom}})) {
17261:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
17262:                             foreach my $item (@items) {
17263:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
17264:                             }
17265:                             $resulttext .= '</ul></li>';
17266:                         }
17267:                         if (keys(%{$privacyhash{$key}{extdom}})) {
17268:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
17269:                             foreach my $item (@items) {
17270:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
17271:                             }
17272:                             $resulttext .= '</ul></li>';
17273:                         }
17274:                     } elsif ($key eq 'othdom') {
17275:                         my @statuses;
17276:                         if (ref($types) eq 'ARRAY') {
17277:                             @statuses = @{$types};
17278:                         }
17279:                         if (ref($privacyhash{$key}) eq 'HASH') {
17280:                             foreach my $status (@statuses,'default') {
17281:                                 if ($status eq 'default') {
17282:                                     $resulttext .= '<li>'.$othertitle.': ';
17283:                                 } elsif (ref($usertypes) eq 'HASH') {
17284:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
17285:                                 } else {
17286:                                     next;
17287:                                 }
17288:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
17289:                                     if (keys(%{$privacyhash{$key}{$status}})) {
17290:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
17291:                                     } else {
17292:                                         $resulttext .= &mt('none');
17293:                                     }
17294:                                 }
17295:                                 $resulttext .= '</li>';
17296:                             }
17297:                         }
17298:                     } else {
17299:                         foreach my $item (@items) {
17300:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
17301:                                 $resulttext .= '<li>'.$names{$item}.': ';
17302:                                 if (keys(%{$privacyhash{$key}{$item}})) {
17303:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
17304:                                 } else {
17305:                                     $resulttext .= &mt('none');
17306:                                 }
17307:                                 $resulttext .= '</li>';
17308:                             }
17309:                         }
17310:                     }
17311:                     $resulttext .= '</ul></li>';
17312:                 }
17313:             }
17314:         } else {
17315:             $resulttext = &mt('No changes made to user information settings');
17316:         }
17317:     } else {
17318:         $resulttext = '<span class="LC_error">'.
17319:             &mt('An error occurred: [_1]',$putresult).'</span>';
17320:     }
17321:     return $resulttext;
17322: }
17323: 
17324: sub modify_passwords {
17325:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17326:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
17327:         $updatedefaults,$updateconf);
17328:     my $customfn = 'resetpw.html';
17329:     if (ref($domconfig{'passwords'}) eq 'HASH') {
17330:         %current = %{$domconfig{'passwords'}};
17331:     }
17332:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17333:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17334:     if (ref($types) eq 'ARRAY') {
17335:         @oktypes = @{$types};
17336:     }
17337:     push(@oktypes,'default');
17338: 
17339:     my %titles = &Apache::lonlocal::texthash (
17340:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
17341:         intauth_check  => 'Check bcrypt cost if authenticated',
17342:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
17343:         permanent      => 'Permanent e-mail address',
17344:         critical       => 'Critical notification address',
17345:         notify         => 'Notification address',
17346:         min            => 'Minimum password length',
17347:         max            => 'Maximum password length',
17348:         chars          => 'Required characters',
17349:         expire         => 'Password expiration (days)',
17350:         numsaved       => 'Number of previous passwords to save',
17351:         reset          => 'Resetting Forgotten Password',
17352:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
17353:         rules          => 'Rules for LON-CAPA Passwords',
17354:         crsownerchg    => 'Course Owner Changing Student Passwords',
17355:         username       => 'Username',
17356:         email          => 'E-mail address',
17357:     );
17358: 
17359: #
17360: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
17361: #
17362:     my (%curr_defaults,%save_defaults);
17363:     if (ref($domconfig{'defaults'}) eq 'HASH') {
17364:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
17365:             if ($key =~ /^intauth_(cost|check|switch)$/) {
17366:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
17367:             } else {
17368:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
17369:             }
17370:         }
17371:     }
17372:     my %staticdefaults = (
17373:         'resetlink'      => 2,
17374:         'resetcase'      => \@oktypes,
17375:         'resetprelink'   => 'both',
17376:         'resetemail'     => ['critical','notify','permanent'],
17377:         'intauth_cost'   => 10,
17378:         'intauth_check'  => 0,
17379:         'intauth_switch' => 0,
17380:     );
17381:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
17382:     foreach my $type (@oktypes) {
17383:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
17384:     }
17385:     my $linklife = $env{'form.passwords_link'};
17386:     $linklife =~ s/^\s+|\s+$//g;
17387:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
17388:         $newvalues{'resetlink'} = $linklife;
17389:         if ($current{'resetlink'}) {
17390:             if ($current{'resetlink'} ne $linklife) {
17391:                 $changes{'reset'} = 1;
17392:             }
17393:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17394:             if ($staticdefaults{'resetlink'} ne $linklife) {
17395:                 $changes{'reset'} = 1;
17396:             }
17397:         }
17398:     } elsif ($current{'resetlink'}) {
17399:         $changes{'reset'} = 1;
17400:     }
17401:     my @casesens;
17402:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
17403:     foreach my $case (sort(@posscase)) {
17404:         if (grep(/^\Q$case\E$/,@oktypes)) {
17405:             push(@casesens,$case);
17406:         }
17407:     }
17408:     $newvalues{'resetcase'} = \@casesens;
17409:     if (ref($current{'resetcase'}) eq 'ARRAY') {
17410:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
17411:         if (@diffs > 0) {
17412:             $changes{'reset'} = 1;
17413:         }
17414:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17415:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
17416:         if (@diffs > 0) {
17417:             $changes{'reset'} = 1;
17418:         }
17419:     }
17420:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
17421:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
17422:         if (exists($current{'resetprelink'})) {
17423:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
17424:                 $changes{'reset'} = 1;
17425:             }
17426:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17427:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
17428:                 $changes{'reset'} = 1;
17429:             }
17430:         }
17431:     } elsif ($current{'resetprelink'}) {
17432:         $changes{'reset'} = 1;
17433:     }
17434:     foreach my $type (@oktypes) {
17435:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
17436:         my @postlink;
17437:         foreach my $item (sort(@possplink)) {
17438:             if ($item =~ /^(email|username)$/) {
17439:                 push(@postlink,$item);
17440:             }
17441:         }
17442:         $newvalues{'resetpostlink'}{$type} = \@postlink;
17443:         unless ($changes{'reset'}) {
17444:             if (ref($current{'resetpostlink'}) eq 'HASH') {
17445:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
17446:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
17447:                     if (@diffs > 0) {
17448:                         $changes{'reset'} = 1;
17449:                     }
17450:                 } else {
17451:                     $changes{'reset'} = 1;
17452:                 }
17453:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17454:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
17455:                 if (@diffs > 0) {
17456:                     $changes{'reset'} = 1;
17457:                 }
17458:             }
17459:         }
17460:     }
17461:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
17462:     my @resetemail;
17463:     foreach my $item (sort(@possemailsrc)) {
17464:         if ($item =~ /^(permanent|critical|notify)$/) {
17465:             push(@resetemail,$item);
17466:         }
17467:     }
17468:     $newvalues{'resetemail'} = \@resetemail;
17469:     unless ($changes{'reset'}) {
17470:         if (ref($current{'resetemail'}) eq 'ARRAY') {
17471:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
17472:             if (@diffs > 0) {
17473:                 $changes{'reset'} = 1;
17474:             }
17475:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17476:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
17477:             if (@diffs > 0) {
17478:                 $changes{'reset'} = 1;
17479:             }
17480:         }
17481:     }
17482:     if ($env{'form.passwords_stdtext'} == 0) {
17483:         $newvalues{'resetremove'} = 1;
17484:         unless ($current{'resetremove'}) {
17485:             $changes{'reset'} = 1;
17486:         }
17487:     } elsif ($current{'resetremove'}) {
17488:         $changes{'reset'} = 1;
17489:     }
17490:     if ($env{'form.passwords_customfile.filename'} ne '') {
17491:         my $servadm = $r->dir_config('lonAdmEMail');
17492:         my ($configuserok,$author_ok,$switchserver) =
17493:             &config_check($dom,$confname,$servadm);
17494:         my $error;
17495:         if ($configuserok eq 'ok') {
17496:             if ($switchserver) {
17497:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
17498:             } else {
17499:                 if ($author_ok eq 'ok') {
17500:                     my ($result,$customurl) =
17501:                         &publishlogo($r,'upload','passwords_customfile',$dom,
17502:                                      $confname,'customtext/resetpw','','',$customfn);
17503:                     if ($result eq 'ok') {
17504:                         $newvalues{'resetcustom'} = $customurl;
17505:                         $changes{'reset'} = 1;
17506:                     } else {
17507:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
17508:                     }
17509:                 } else {
17510:                     $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);
17511:                 }
17512:             }
17513:         } else {
17514:             $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);
17515:         }
17516:         if ($error) {
17517:             &Apache::lonnet::logthis($error);
17518:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
17519:         }
17520:     } elsif ($current{'resetcustom'}) {
17521:         if ($env{'form.passwords_custom_del'}) {
17522:             $changes{'reset'} = 1;
17523:         } else {
17524:             $newvalues{'resetcustom'} = $current{'resetcustom'};
17525:         }
17526:     }
17527:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
17528:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
17529:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
17530:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
17531:             $changes{'intauth'} = 1;
17532:         }
17533:     } else {
17534:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
17535:     }
17536:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
17537:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
17538:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
17539:             $changes{'intauth'} = 1;
17540:         }
17541:     } else {
17542:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17543:     }
17544:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
17545:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
17546:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
17547:             $changes{'intauth'} = 1;
17548:         }
17549:     } else {
17550:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17551:     }
17552:     foreach my $item ('cost','check','switch') {
17553:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
17554:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
17555:             $updatedefaults = 1;
17556:         }
17557:     }
17558:     foreach my $rule ('min','max','expire','numsaved') {
17559:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
17560:         my $ruleok;
17561:         if ($rule eq 'expire') {
17562:             if (($env{'form.passwords_'.$rule} =~ /^\d+(|\.\d*)$/) &&
17563:                 ($env{'form.passwords_'.$rule} ne '0')) {
17564:                 $ruleok = 1;
17565:             }
17566:         } elsif ($rule eq 'min') {
17567:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
17568:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
17569:                     $ruleok = 1;
17570:                 }
17571:             }
17572:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
17573:                  ($env{'form.passwords_'.$rule} ne '0')) {
17574:             $ruleok = 1;
17575:         }
17576:         if ($ruleok) {
17577:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
17578:             if (exists($current{$rule})) {
17579:                 if ($newvalues{$rule} ne $current{$rule}) {
17580:                     $changes{'rules'} = 1;
17581:                 }
17582:             } elsif ($rule eq 'min') {
17583:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
17584:                     $changes{'rules'} = 1;
17585:                 }
17586:             } else {
17587:                 $changes{'rules'} = 1;
17588:             }
17589:         } elsif (exists($current{$rule})) {
17590:             $changes{'rules'} = 1;
17591:         }
17592:     }
17593:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
17594:     my @chars;
17595:     foreach my $item (sort(@posschars)) {
17596:         if ($item =~ /^(uc|lc|num|spec)$/) {
17597:             push(@chars,$item);
17598:         }
17599:     }
17600:     $newvalues{'chars'} = \@chars;
17601:     unless ($changes{'rules'}) {
17602:         if (ref($current{'chars'}) eq 'ARRAY') {
17603:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
17604:             if (@diffs > 0) {
17605:                 $changes{'rules'} = 1;
17606:             }
17607:         } else {
17608:             if (@chars > 0) {
17609:                 $changes{'rules'} = 1;
17610:             }
17611:         }
17612:     }
17613:     my %crsownerchg = (
17614:                         by => [],
17615:                         for => [],
17616:                       );
17617:     foreach my $item ('by','for') {
17618:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
17619:         foreach my $type (sort(@posstypes)) {
17620:             if (grep(/^\Q$type\E$/,@oktypes)) {
17621:                 push(@{$crsownerchg{$item}},$type);
17622:             }
17623:         }
17624:     }
17625:     $newvalues{'crsownerchg'} = \%crsownerchg;
17626:     if (ref($current{'crsownerchg'}) eq 'HASH') {
17627:         foreach my $item ('by','for') {
17628:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
17629:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
17630:                 if (@diffs > 0) {
17631:                     $changes{'crsownerchg'} = 1;
17632:                     last;
17633:                 }
17634:             }
17635:         }
17636:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
17637:         foreach my $item ('by','for') {
17638:             if (@{$crsownerchg{$item}} > 0) {
17639:                 $changes{'crsownerchg'} = 1;
17640:                 last;
17641:             }
17642:         }
17643:     }
17644: 
17645:     my %confighash = (
17646:                         defaults  => \%save_defaults,
17647:                         passwords => \%newvalues,
17648:                      );
17649:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
17650: 
17651:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17652:     if ($putresult eq 'ok') {
17653:         if (keys(%changes) > 0) {
17654:             $resulttext = &mt('Changes made: ').'<ul>';
17655:             foreach my $key ('reset','intauth','rules','crsownerchg') {
17656:                 if ($changes{$key}) {
17657:                     unless ($key eq 'intauth') {
17658:                         $updateconf = 1;
17659:                     }
17660:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17661:                     if ($key eq 'reset') {
17662:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
17663:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
17664:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
17665:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
17666:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
17667:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
17668:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
17669:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
17670:                             }
17671:                         } else {
17672:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
17673:                         }
17674:                         if ($confighash{'passwords'}{'resetlink'}) {
17675:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
17676:                         } else {
17677:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
17678:                                                   &mt('Will default to 2 hours').'</li>';
17679:                         }
17680:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
17681:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
17682:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
17683:                             } else {
17684:                                 my $casesens;
17685:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
17686:                                     if ($type eq 'default') {
17687:                                         $casesens .= $othertitle.', ';
17688:                                     } elsif ($usertypes->{$type} ne '') {
17689:                                         $casesens .= $usertypes->{$type}.', ';
17690:                                     }
17691:                                 }
17692:                                 $casesens =~ s/\Q, \E$//;
17693:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
17694:                             }
17695:                         } else {
17696:                             $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>';
17697:                         }
17698:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
17699:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
17700:                         } else {
17701:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
17702:                         }
17703:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
17704:                             my $output;
17705:                             if (ref($types) eq 'ARRAY') {
17706:                                 foreach my $type (@{$types}) {
17707:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
17708:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
17709:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
17710:                                         } else {
17711:                                             $output .= $usertypes->{$type}.' -- '.
17712:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
17713:                                         }
17714:                                     }
17715:                                 }
17716:                             }
17717:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
17718:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
17719:                                     $output .= $othertitle.' -- '.&mt('none');
17720:                                 } else {
17721:                                     $output .= $othertitle.' -- '.
17722:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
17723:                                 }
17724:                             }
17725:                             if ($output) {
17726:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
17727:                             } else {
17728:                                 $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>';
17729:                             }
17730:                         } else {
17731:                             $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>';
17732:                         }
17733:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
17734:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
17735:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
17736:                             } else {
17737:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17738:                             }
17739:                         } else {
17740:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17741:                         }
17742:                         if ($confighash{'passwords'}{'resetremove'}) {
17743:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
17744:                         } else {
17745:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
17746:                         }
17747:                         if ($confighash{'passwords'}{'resetcustom'}) {
17748:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
17749:                                                                             &mt('custom text'),600,500,undef,undef,
17750:                                                                             undef,undef,'background-color:#ffffff');
17751:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
17752:                         } else {
17753:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
17754:                         }
17755:                     } elsif ($key eq 'intauth') {
17756:                         foreach my $item ('cost','switch','check') {
17757:                             my $value = $save_defaults{$key.'_'.$item};
17758:                             if ($item eq 'switch') {
17759:                                 my %optiondesc = &Apache::lonlocal::texthash (
17760:                                                      0 => 'No',
17761:                                                      1 => 'Yes',
17762:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
17763:                                                  );
17764:                                 if ($value =~ /^(0|1|2)$/) {
17765:                                     $value = $optiondesc{$value};
17766:                                 } else {
17767:                                     $value = &mt('none -- defaults to No');
17768:                                 }
17769:                             } elsif ($item eq 'check') {
17770:                                 my %optiondesc = &Apache::lonlocal::texthash (
17771:                                                      0 => 'No',
17772:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
17773:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
17774:                                                  );
17775:                                 if ($value =~ /^(0|1|2)$/) {
17776:                                     $value = $optiondesc{$value};
17777:                                 } else {
17778:                                     $value = &mt('none -- defaults to No');
17779:                                 }
17780:                             }
17781:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
17782:                         }
17783:                     } elsif ($key eq 'rules') {
17784:                         foreach my $rule ('min','max','expire','numsaved') {
17785:                             if ($confighash{'passwords'}{$rule} eq '') {
17786:                                 if ($rule eq 'min') {
17787:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
17788:                                                    ' '.&mt('Default of [_1] will be used',
17789:                                                            $Apache::lonnet::passwdmin).'</li>';
17790:                                 } else {
17791:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
17792:                                 }
17793:                             } else {
17794:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
17795:                             }
17796:                         }
17797:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
17798:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
17799:                                 my %rulenames = &Apache::lonlocal::texthash(
17800:                                                      uc => 'At least one upper case letter',
17801:                                                      lc => 'At least one lower case letter',
17802:                                                      num => 'At least one number',
17803:                                                      spec => 'At least one non-alphanumeric',
17804:                                                    );
17805:                                 my $needed = '<ul><li>'.
17806:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
17807:                                              '</li></ul>'; 
17808:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
17809:                             } else {
17810:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17811:                             }
17812:                         } else {
17813:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17814:                         }
17815:                     } elsif ($key eq 'crsownerchg') {
17816:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
17817:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
17818:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
17819:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17820:                             } else {
17821:                                 my %crsownerstr;
17822:                                 foreach my $item ('by','for') {
17823:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
17824:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
17825:                                             if ($type eq 'default') {
17826:                                                 $crsownerstr{$item} .= $othertitle.', ';
17827:                                             } elsif ($usertypes->{$type} ne '') {
17828:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
17829:                                             }
17830:                                         }
17831:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
17832:                                     }
17833:                                 }
17834:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
17835:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
17836:                             }
17837:                         } else {
17838:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17839:                         }
17840:                     }
17841:                     $resulttext .= '</ul></li>';
17842:                 }
17843:             }
17844:             $resulttext .= '</ul>';
17845:         } else {
17846:             $resulttext = &mt('No changes made to password settings');
17847:         }
17848:         my $cachetime = 24*60*60;
17849:         if ($updatedefaults) {
17850:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17851:             if (ref($lastactref) eq 'HASH') {
17852:                 $lastactref->{'domdefaults'} = 1;
17853:             }
17854:         }
17855:         if ($updateconf) {
17856:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
17857:             if (ref($lastactref) eq 'HASH') {
17858:                 $lastactref->{'passwdconf'} = 1;
17859:             }
17860:         }
17861:     } else {
17862:         $resulttext = '<span class="LC_error">'.
17863:             &mt('An error occurred: [_1]',$putresult).'</span>';
17864:     }
17865:     if ($errors) {
17866:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
17867:                        $errors.'</ul></p>';
17868:     }
17869:     return $resulttext;
17870: }
17871: 
17872: sub modify_usercreation {
17873:     my ($dom,%domconfig) = @_;
17874:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
17875:     my $warningmsg;
17876:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17877:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17878:             if ($key eq 'cancreate') {
17879:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17880:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17881:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
17882:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17883:                         } else {
17884:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17885:                         }
17886:                     }
17887:                 }
17888:             } elsif ($key eq 'email_rule') {
17889:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17890:             } else {
17891:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17892:             }
17893:         }
17894:     }
17895:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
17896:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
17897:     my @contexts = ('author','course','requestcrs');
17898:     foreach my $item(@contexts) {
17899:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
17900:     }
17901:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17902:         foreach my $item (@contexts) {
17903:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
17904:                 push(@{$changes{'cancreate'}},$item);
17905:             }
17906:         }
17907:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
17908:         foreach my $item (@contexts) {
17909:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
17910:                 if ($cancreate{$item} ne 'any') {
17911:                     push(@{$changes{'cancreate'}},$item);
17912:                 }
17913:             } else {
17914:                 if ($cancreate{$item} ne 'none') {
17915:                     push(@{$changes{'cancreate'}},$item);
17916:                 }
17917:             }
17918:         }
17919:     } else {
17920:         foreach my $item (@contexts)  {
17921:             push(@{$changes{'cancreate'}},$item);
17922:         }
17923:     }
17924: 
17925:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
17926:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
17927:             if (!grep(/^\Q$type\E$/,@username_rule)) {
17928:                 push(@{$changes{'username_rule'}},$type);
17929:             }
17930:         }
17931:         foreach my $type (@username_rule) {
17932:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
17933:                 push(@{$changes{'username_rule'}},$type);
17934:             }
17935:         }
17936:     } else {
17937:         push(@{$changes{'username_rule'}},@username_rule);
17938:     }
17939: 
17940:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
17941:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
17942:             if (!grep(/^\Q$type\E$/,@id_rule)) {
17943:                 push(@{$changes{'id_rule'}},$type);
17944:             }
17945:         }
17946:         foreach my $type (@id_rule) {
17947:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
17948:                 push(@{$changes{'id_rule'}},$type);
17949:             }
17950:         }
17951:     } else {
17952:         push(@{$changes{'id_rule'}},@id_rule);
17953:     }
17954: 
17955:     my @authen_contexts = ('author','course','domain');
17956:     my @authtypes = ('int','krb4','krb5','loc','lti');
17957:     my %authhash;
17958:     foreach my $item (@authen_contexts) {
17959:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
17960:         foreach my $auth (@authtypes) {
17961:             if (grep(/^\Q$auth\E$/,@authallowed)) {
17962:                 $authhash{$item}{$auth} = 1;
17963:             } else {
17964:                 $authhash{$item}{$auth} = 0;
17965:             }
17966:         }
17967:     }
17968:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
17969:         foreach my $item (@authen_contexts) {
17970:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
17971:                 foreach my $auth (@authtypes) {
17972:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
17973:                         push(@{$changes{'authtypes'}},$item);
17974:                         last;
17975:                     }
17976:                 }
17977:             }
17978:         }
17979:     } else {
17980:         foreach my $item (@authen_contexts) {
17981:             push(@{$changes{'authtypes'}},$item);
17982:         }
17983:     }
17984: 
17985:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
17986:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
17987:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
17988:     $save_usercreate{'id_rule'} = \@id_rule;
17989:     $save_usercreate{'username_rule'} = \@username_rule,
17990:     $save_usercreate{'authtypes'} = \%authhash;
17991: 
17992:     my %usercreation_hash =  (
17993:         usercreation     => \%save_usercreate,
17994:     );
17995: 
17996:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
17997:                                              $dom);
17998: 
17999:     if ($putresult eq 'ok') {
18000:         if (keys(%changes) > 0) {
18001:             $resulttext = &mt('Changes made:').'<ul>';
18002:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18003:                 my %lt = &usercreation_types();
18004:                 foreach my $type (@{$changes{'cancreate'}}) {
18005:                     my $chgtext = $lt{$type}.', ';
18006:                     if ($cancreate{$type} eq 'none') {
18007:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18008:                     } elsif ($cancreate{$type} eq 'any') {
18009:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18010:                     } elsif ($cancreate{$type} eq 'official') {
18011:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18012:                     } elsif ($cancreate{$type} eq 'unofficial') {
18013:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18014:                     }
18015:                     $resulttext .= '<li>'.$chgtext.'</li>';
18016:                 }
18017:             }
18018:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18019:                 my ($rules,$ruleorder) = 
18020:                     &Apache::lonnet::inst_userrules($dom,'username');
18021:                 my $chgtext = '<ul>';
18022:                 foreach my $type (@username_rule) {
18023:                     if (ref($rules->{$type}) eq 'HASH') {
18024:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18025:                     }
18026:                 }
18027:                 $chgtext .= '</ul>';
18028:                 if (@username_rule > 0) {
18029:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18030:                 } else {
18031:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18032:                 }
18033:             }
18034:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18035:                 my ($idrules,$idruleorder) = 
18036:                     &Apache::lonnet::inst_userrules($dom,'id');
18037:                 my $chgtext = '<ul>';
18038:                 foreach my $type (@id_rule) {
18039:                     if (ref($idrules->{$type}) eq 'HASH') {
18040:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18041:                     }
18042:                 }
18043:                 $chgtext .= '</ul>';
18044:                 if (@id_rule > 0) {
18045:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18046:                 } else {
18047:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18048:                 }
18049:             }
18050:             my %authname = &authtype_names();
18051:             my %context_title = &context_names();
18052:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18053:                 my $chgtext = '<ul>';
18054:                 foreach my $type (@{$changes{'authtypes'}}) {
18055:                     my @allowed;
18056:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18057:                     foreach my $auth (@authtypes) {
18058:                         if ($authhash{$type}{$auth}) {
18059:                             push(@allowed,$authname{$auth});
18060:                         }
18061:                     }
18062:                     if (@allowed > 0) {
18063:                         $chgtext .= join(', ',@allowed).'</li>';
18064:                     } else {
18065:                         $chgtext .= &mt('none').'</li>';
18066:                     }
18067:                 }
18068:                 $chgtext .= '</ul>';
18069:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18070:                 $resulttext .= '</li>';
18071:             }
18072:             $resulttext .= '</ul>';
18073:         } else {
18074:             $resulttext = &mt('No changes made to user creation settings');
18075:         }
18076:     } else {
18077:         $resulttext = '<span class="LC_error">'.
18078:             &mt('An error occurred: [_1]',$putresult).'</span>';
18079:     }
18080:     if ($warningmsg ne '') {
18081:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18082:     }
18083:     return $resulttext;
18084: }
18085: 
18086: sub modify_selfcreation {
18087:     my ($dom,$lastactref,%domconfig) = @_;
18088:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18089:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18090:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18091:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18092:     if (ref($typesref) eq 'ARRAY') {
18093:         @types = @{$typesref};
18094:     }
18095:     if (ref($usertypesref) eq 'HASH') {
18096:         %usertypes = %{$usertypesref};
18097:     }
18098:     $usertypes{'default'} = $othertitle;
18099: #
18100: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18101: #
18102:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18103:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18104:             if ($key eq 'cancreate') {
18105:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18106:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18107:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18108:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18109:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18110:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18111:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18112:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18113:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18114:                         } else {
18115:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18116:                         }
18117:                     }
18118:                 }
18119:             } elsif ($key eq 'email_rule') {
18120:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18121:             } else {
18122:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18123:             }
18124:         }
18125:     }
18126: #
18127: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18128: #
18129:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18130:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18131:             if ($key eq 'selfcreate') {
18132:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18133:             } else {
18134:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18135:             }
18136:         }
18137:     }
18138: #
18139: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18140: #
18141:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18142:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18143:             if ($key eq 'inststatusguest') {
18144:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18145:             } else {
18146:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
18147:             }
18148:         }
18149:     }
18150: 
18151:     my @contexts = ('selfcreate');
18152:     @{$cancreate{'selfcreate'}} = ();
18153:     %{$cancreate{'emailusername'}} = ();
18154:     if (@types) {
18155:         @{$cancreate{'statustocreate'}} = ();
18156:     }
18157:     %{$cancreate{'selfcreateprocessing'}} = ();
18158:     %{$cancreate{'shibenv'}} = ();
18159:     %{$cancreate{'emailverified'}} = ();
18160:     %{$cancreate{'emailoptions'}} = ();
18161:     %{$cancreate{'emaildomain'}} = ();
18162:     my %selfcreatetypes = (
18163:                              sso   => 'users authenticated by institutional single sign on',
18164:                              login => 'users authenticated by institutional log-in',
18165:                              email => 'users verified by e-mail',
18166:                           );
18167: #
18168: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
18169: # is permitted.
18170: #
18171:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
18172: 
18173:     my (@statuses,%email_rule);
18174:     foreach my $item ('login','sso','email') {
18175:         if ($item eq 'email') {
18176:             if ($env{'form.cancreate_email'}) {
18177:                 if (@types) {
18178:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
18179:                     foreach my $status (@poss_statuses) {
18180:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
18181:                             push(@statuses,$status);
18182:                         }
18183:                     }
18184:                     $save_inststatus{'inststatusguest'} = \@statuses;
18185:                 } else {
18186:                     push(@statuses,'default');
18187:                 }
18188:                 if (@statuses) {
18189:                     my %curr_rule;
18190:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
18191:                         foreach my $type (@statuses) {
18192:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
18193:                         }
18194:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
18195:                         foreach my $type (@statuses) {
18196:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
18197:                         }
18198:                     }
18199:                     push(@{$cancreate{'selfcreate'}},'email');
18200:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
18201:                     my %curremaildom;
18202:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
18203:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
18204:                     }
18205:                     foreach my $type (@statuses) {
18206:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
18207:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
18208:                         }
18209:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
18210:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
18211:                         }
18212:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
18213: #
18214: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
18215: #
18216:                             my $chosen = $1;
18217:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
18218:                                 my $emaildom;
18219:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
18220:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
18221:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
18222:                                     if (ref($curremaildom{$type}) eq 'HASH') {
18223:                                         if (exists($curremaildom{$type}{$chosen})) {
18224:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
18225:                                                 push(@{$changes{'cancreate'}},'emaildomain');
18226:                                             }
18227:                                         } elsif ($emaildom ne '') {
18228:                                             push(@{$changes{'cancreate'}},'emaildomain');
18229:                                         }
18230:                                     } elsif ($emaildom ne '') {
18231:                                         push(@{$changes{'cancreate'}},'emaildomain');
18232:                                     } 
18233:                                 }
18234:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18235:                             } elsif ($chosen eq 'custom') {
18236:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
18237:                                 $email_rule{$type} = [];
18238:                                 if (ref($emailrules) eq 'HASH') {
18239:                                     foreach my $rule (@possemail_rules) {
18240:                                         if (exists($emailrules->{$rule})) {
18241:                                             push(@{$email_rule{$type}},$rule);
18242:                                         }
18243:                                     }
18244:                                 }
18245:                                 if (@{$email_rule{$type}}) {
18246:                                     $cancreate{'emailoptions'}{$type} = 'custom';
18247:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
18248:                                         if (@{$curr_rule{$type}} > 0) {
18249:                                             foreach my $rule (@{$curr_rule{$type}}) {
18250:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
18251:                                                     push(@{$changes{'email_rule'}},$type);
18252:                                                 }
18253:                                             }
18254:                                         }
18255:                                         foreach my $type (@{$email_rule{$type}}) {
18256:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
18257:                                                 push(@{$changes{'email_rule'}},$type);
18258:                                             }
18259:                                         }
18260:                                     } else {
18261:                                         push(@{$changes{'email_rule'}},$type);
18262:                                     }
18263:                                 }
18264:                             } else {
18265:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18266:                             }
18267:                         }
18268:                     }
18269:                     if (@types) {
18270:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18271:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
18272:                             if (@changed) {
18273:                                 push(@{$changes{'inststatus'}},'inststatusguest');
18274:                             }
18275:                         } else {
18276:                             push(@{$changes{'inststatus'}},'inststatusguest');
18277:                         }
18278:                     }
18279:                 } else {
18280:                     delete($env{'form.cancreate_email'});
18281:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18282:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18283:                             push(@{$changes{'inststatus'}},'inststatusguest');
18284:                         }
18285:                     }
18286:                 }
18287:             } else {
18288:                 $save_inststatus{'inststatusguest'} = [];
18289:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18290:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18291:                         push(@{$changes{'inststatus'}},'inststatusguest');
18292:                     }
18293:                 }
18294:             }
18295:         } else {
18296:             if ($env{'form.cancreate_'.$item}) {
18297:                 push(@{$cancreate{'selfcreate'}},$item);
18298:             }
18299:         }
18300:     }
18301:     my (%userinfo,%savecaptcha);
18302:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
18303: #
18304: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
18305: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
18306: #
18307: 
18308:     if ($env{'form.cancreate_email'}) {
18309:         push(@contexts,'emailusername');
18310:         if (@statuses) {
18311:             foreach my $type (@statuses) {
18312:                 if (ref($infofields) eq 'ARRAY') {
18313:                     foreach my $field (@{$infofields}) {
18314:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
18315:                             $cancreate{'emailusername'}{$type}{$field} = $1;
18316:                         }
18317:                     }
18318:                 }
18319:             }
18320:         }
18321: #
18322: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
18323: # queued requests for self-creation of account verified by e-mail.
18324: #
18325: 
18326:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
18327:         @approvalnotify = sort(@approvalnotify);
18328:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
18329:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18330:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
18331:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
18332:                     push(@{$changes{'cancreate'}},'notify');
18333:                 }
18334:             } else {
18335:                 if ($cancreate{'notify'}{'approval'}) {
18336:                     push(@{$changes{'cancreate'}},'notify');
18337:                 }
18338:             }
18339:         } elsif ($cancreate{'notify'}{'approval'}) {
18340:             push(@{$changes{'cancreate'}},'notify');
18341:         }
18342: 
18343:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
18344:     }
18345: #  
18346: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
18347: # institutional log-in.
18348: #
18349:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
18350:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
18351:                ($domdefaults{'auth_def'} eq 'localauth'))) {
18352:             $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.').' '.
18353:                           &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.');
18354:         }
18355:     }
18356:     my @fields = ('lastname','firstname','middlename','generation',
18357:                   'permanentemail','id');
18358:     my @shibfields = (@fields,'inststatus');
18359:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18360: #
18361: # Where usernames may created for institutional log-in and/or institutional single sign on:
18362: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
18363: # may self-create accounts 
18364: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
18365: # which the user may supply, if institutional data is unavailable.
18366: #
18367:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
18368:         if (@types) {
18369:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
18370:             push(@contexts,'statustocreate');
18371:             foreach my $type (@types) {
18372:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
18373:                 foreach my $field (@fields) {
18374:                     if (grep(/^\Q$field\E$/,@modifiable)) {
18375:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
18376:                     } else {
18377:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
18378:                     }
18379:                 }
18380:             }
18381:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
18382:                 foreach my $type (@types) {
18383:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
18384:                         foreach my $field (@fields) {
18385:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
18386:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
18387:                                 push(@{$changes{'selfcreate'}},$type);
18388:                                 last;
18389:                             }
18390:                         }
18391:                     }
18392:                 }
18393:             } else {
18394:                 foreach my $type (@types) {
18395:                     push(@{$changes{'selfcreate'}},$type);
18396:                 }
18397:             }
18398:         }
18399:         foreach my $field (@shibfields) {
18400:             if ($env{'form.shibenv_'.$field} ne '') {
18401:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
18402:             }
18403:         }
18404:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18405:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
18406:                 foreach my $field (@shibfields) {
18407:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
18408:                         push(@{$changes{'cancreate'}},'shibenv');
18409:                     }
18410:                 }
18411:             } else {
18412:                 foreach my $field (@shibfields) {
18413:                     if ($env{'form.shibenv_'.$field}) {
18414:                         push(@{$changes{'cancreate'}},'shibenv');
18415:                         last;
18416:                     }
18417:                 }
18418:             }
18419:         }
18420:     }
18421:     foreach my $item (@contexts) {
18422:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
18423:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
18424:                 if (ref($cancreate{$item}) eq 'ARRAY') {
18425:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
18426:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18427:                             push(@{$changes{'cancreate'}},$item);
18428:                         }
18429:                     }
18430:                 }
18431:             }
18432:             if (ref($cancreate{$item}) eq 'ARRAY') {
18433:                 foreach my $type (@{$cancreate{$item}}) {
18434:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
18435:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18436:                             push(@{$changes{'cancreate'}},$item);
18437:                         }
18438:                     }
18439:                 }
18440:             }
18441:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
18442:             if (ref($cancreate{$item}) eq 'HASH') {
18443:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
18444:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18445:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
18446:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18447:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18448:                                     push(@{$changes{'cancreate'}},$item);
18449:                                 }
18450:                             }
18451:                         }
18452:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18453:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18454:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18455:                                 push(@{$changes{'cancreate'}},$item);
18456:                             }
18457:                         }
18458:                     }
18459:                 }
18460:                 foreach my $type (keys(%{$cancreate{$item}})) {
18461:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18462:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18463:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18464:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18465:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18466:                                         push(@{$changes{'cancreate'}},$item);
18467:                                     }
18468:                                 }
18469:                             } else {
18470:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18471:                                     push(@{$changes{'cancreate'}},$item);
18472:                                 }
18473:                             }
18474:                         }
18475:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18476:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18477:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18478:                                 push(@{$changes{'cancreate'}},$item);
18479:                             }
18480:                         }
18481:                     }
18482:                 }
18483:             }
18484:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
18485:             if (ref($cancreate{$item}) eq 'ARRAY') {
18486:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
18487:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18488:                         push(@{$changes{'cancreate'}},$item);
18489:                     }
18490:                 }
18491:             }
18492:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18493:             if (ref($cancreate{$item}) eq 'HASH') {
18494:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18495:                     push(@{$changes{'cancreate'}},$item);
18496:                 }
18497:             }
18498:         } elsif ($item eq 'emailusername') {
18499:             if (ref($cancreate{$item}) eq 'HASH') {
18500:                 foreach my $type (keys(%{$cancreate{$item}})) {
18501:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18502:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18503:                             if ($cancreate{$item}{$type}{$field}) {
18504:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18505:                                     push(@{$changes{'cancreate'}},$item);
18506:                                 }
18507:                                 last;
18508:                             }
18509:                         }
18510:                     }
18511:                 }
18512:             }
18513:         }
18514:     }
18515: #
18516: # Populate %save_usercreate hash with updates to self-creation configuration.
18517: #
18518:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
18519:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
18520:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
18521:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
18522:     if (ref($cancreate{'notify'}) eq 'HASH') {
18523:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
18524:     }
18525:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
18526:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
18527:     }
18528:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
18529:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
18530:     }
18531:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
18532:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
18533:     }
18534:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
18535:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
18536:     }
18537:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18538:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
18539:     }
18540:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
18541:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
18542:     }
18543:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
18544:     $save_usercreate{'email_rule'} = \%email_rule;
18545: 
18546:     my %userconfig_hash = (
18547:             usercreation     => \%save_usercreate,
18548:             usermodification => \%save_usermodify,
18549:             inststatus       => \%save_inststatus,
18550:     );
18551: 
18552:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
18553:                                              $dom);
18554: #
18555: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
18556: #
18557:     if ($putresult eq 'ok') {
18558:         if (keys(%changes) > 0) {
18559:             $resulttext = &mt('Changes made:').'<ul>';
18560:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18561:                 my %lt = &selfcreation_types();
18562:                 foreach my $type (@{$changes{'cancreate'}}) {
18563:                     my $chgtext = '';
18564:                     if ($type eq 'selfcreate') {
18565:                         if (@{$cancreate{$type}} == 0) {
18566:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
18567:                         } else {
18568:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
18569:                                         '<ul>';
18570:                             foreach my $case (@{$cancreate{$type}}) {
18571:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
18572:                             }
18573:                             $chgtext .= '</ul>';
18574:                             if (ref($cancreate{$type}) eq 'ARRAY') {
18575:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
18576:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18577:                                         if (@{$cancreate{'statustocreate'}} == 0) {
18578:                                             $chgtext .= '<span class="LC_warning">'.
18579:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
18580:                                                         '</span><br />';
18581:                                         }
18582:                                     }
18583:                                 }
18584:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
18585:                                     if (!@statuses) {
18586:                                         $chgtext .= '<span class="LC_warning">'.
18587:                                                     &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.").
18588:                                                     '</span><br />';
18589: 
18590:                                     }
18591:                                 }
18592:                             }
18593:                         }
18594:                     } elsif ($type eq 'shibenv') {
18595:                         if (keys(%{$cancreate{$type}}) == 0) {
18596:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
18597:                         } else {
18598:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
18599:                                         '<ul>';
18600:                             foreach my $field (@shibfields) {
18601:                                 next if ($cancreate{$type}{$field} eq '');
18602:                                 if ($field eq 'inststatus') {
18603:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
18604:                                 } else {
18605:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
18606:                                 }
18607:                             }
18608:                             $chgtext .= '</ul>';
18609:                         }
18610:                     } elsif ($type eq 'statustocreate') {
18611:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
18612:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
18613:                             if (@{$cancreate{'selfcreate'}} > 0) {
18614:                                 if (@{$cancreate{'statustocreate'}} == 0) {
18615:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
18616:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
18617:                                         $chgtext .= '<br />'.
18618:                                                     '<span class="LC_warning">'.
18619:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
18620:                                                     '</span>';
18621:                                     }
18622:                                 } elsif (keys(%usertypes) > 0) {
18623:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18624:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
18625:                                     } else {
18626:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
18627:                                     }
18628:                                     $chgtext .= '<ul>';
18629:                                     foreach my $case (@{$cancreate{$type}}) {
18630:                                         if ($case eq 'default') {
18631:                                             $chgtext .= '<li>'.$othertitle.'</li>';
18632:                                         } else {
18633:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
18634:                                         }
18635:                                     }
18636:                                     $chgtext .= '</ul>';
18637:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18638:                                         $chgtext .= '<span class="LC_warning">'.
18639:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
18640:                                                     '</span>';
18641:                                     }
18642:                                 }
18643:                             } else {
18644:                                 if (@{$cancreate{$type}} == 0) {
18645:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
18646:                                 } else {
18647:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
18648:                                 }
18649:                             }
18650:                             $chgtext .= '<br />';
18651:                         }
18652:                     } elsif ($type eq 'selfcreateprocessing') {
18653:                         my %choices = &Apache::lonlocal::texthash (
18654:                                                                     automatic => 'Automatic approval',
18655:                                                                     approval  => 'Queued for approval',
18656:                                                                   );
18657:                         if (@types) {
18658:                             if (@statuses) {
18659:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
18660:                                             '<ul>';
18661:                                 foreach my $status (@statuses) {
18662:                                     if ($status eq 'default') {
18663:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18664:                                     } else {
18665:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18666:                                     }
18667:                                 }
18668:                                 $chgtext .= '</ul>';
18669:                             }
18670:                         } else {
18671:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
18672:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
18673:                         }
18674:                     } elsif ($type eq 'emailverified') {
18675:                         my %options = &Apache::lonlocal::texthash (
18676:                                                                     all   => 'Same as e-mail',
18677:                                                                     first => 'Omit @domain',
18678:                                                                     free  => 'Free to choose',
18679:                                                                   );
18680:                         if (@types) {
18681:                             if (@statuses) {
18682:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
18683:                                             '<ul>';
18684:                                 foreach my $status (@statuses) {
18685:                                     if ($status eq 'default') {
18686:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18687:                                     } else {
18688:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18689:                                     }
18690:                                 }
18691:                                 $chgtext .= '</ul>';
18692:                             }
18693:                         } else {
18694:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
18695:                                             $options{$cancreate{'emailverified'}{'default'}});
18696:                         }
18697:                     } elsif ($type eq 'emailoptions') {
18698:                         my %options = &Apache::lonlocal::texthash (
18699:                                                                     any     => 'Any e-mail',
18700:                                                                     inst    => 'Institutional only',
18701:                                                                     noninst => 'Non-institutional only',
18702:                                                                     custom  => 'Custom restrictions',
18703:                                                                   );
18704:                         if (@types) {
18705:                             if (@statuses) {
18706:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
18707:                                             '<ul>';
18708:                                 foreach my $status (@statuses) {
18709:                                     if ($type eq 'default') {
18710:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18711:                                     } else {
18712:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18713:                                     }
18714:                                 }
18715:                                 $chgtext .= '</ul>';
18716:                             }
18717:                         } else {
18718:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
18719:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
18720:                             } else {
18721:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
18722:                                                 $options{$cancreate{'emailoptions'}{'default'}});
18723:                             }
18724:                         }
18725:                     } elsif ($type eq 'emaildomain') {
18726:                         my $output;
18727:                         if (@statuses) {
18728:                             foreach my $type (@statuses) {
18729:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
18730:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
18731:                                         if ($type eq 'default') {
18732:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18733:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18734:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18735:                                             } else {
18736:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
18737:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
18738:                                             }
18739:                                         } else {
18740:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18741:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18742:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18743:                                             } else {
18744:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
18745:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
18746:                                             }
18747:                                         }
18748:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
18749:                                         if ($type eq 'default') {
18750:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18751:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18752:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18753:                                             } else {
18754:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
18755:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
18756:                                             }
18757:                                         } else {
18758:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18759:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18760:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18761:                                             } else {
18762:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
18763:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
18764:                                             }
18765:                                         }
18766:                                     }
18767:                                 }
18768:                             }
18769:                         }
18770:                         if ($output ne '') {
18771:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
18772:                                         '<ul>'.$output.'</ul>';
18773:                         }
18774:                     } elsif ($type eq 'captcha') {
18775:                         if ($savecaptcha{$type} eq 'notused') {
18776:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
18777:                         } else {
18778:                             my %captchas = &captcha_phrases();
18779:                             if ($captchas{$savecaptcha{$type}}) {
18780:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
18781:                             } else {
18782:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
18783:                             }
18784:                         }
18785:                     } elsif ($type eq 'recaptchakeys') {
18786:                         my ($privkey,$pubkey);
18787:                         if (ref($savecaptcha{$type}) eq 'HASH') {
18788:                             $pubkey = $savecaptcha{$type}{'public'};
18789:                             $privkey = $savecaptcha{$type}{'private'};
18790:                         }
18791:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
18792:                         if (!$pubkey) {
18793:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
18794:                         } else {
18795:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
18796:                         }
18797:                         if (!$privkey) {
18798:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
18799:                         } else {
18800:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
18801:                         }
18802:                         $chgtext .= '</ul>';
18803:                     } elsif ($type eq 'recaptchaversion') {
18804:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
18805:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
18806:                         }
18807:                     } elsif ($type eq 'emailusername') {
18808:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
18809:                             if (@statuses) {
18810:                                 foreach my $type (@statuses) {
18811:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
18812:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
18813:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
18814:                                                     '<ul>';
18815:                                             foreach my $field (@{$infofields}) {
18816:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
18817:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
18818:                                                 }
18819:                                             }
18820:                                             $chgtext .= '</ul>';
18821:                                         } else {
18822:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18823:                                         }
18824:                                     } else {
18825:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18826:                                     }
18827:                                 }
18828:                             }
18829:                         }
18830:                     } elsif ($type eq 'notify') {
18831:                         my $numapprove = 0;
18832:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
18833:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
18834:                                 if ($cancreate{'notify'}{'approval'}) {
18835:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
18836:                                     $numapprove ++;
18837:                                 }
18838:                             }
18839:                         }
18840:                         unless ($numapprove) {
18841:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
18842:                         }
18843:                     }
18844:                     if ($chgtext) {
18845:                         $resulttext .= '<li>'.$chgtext.'</li>';
18846:                     }
18847:                 }
18848:             }
18849:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
18850:                 my ($emailrules,$emailruleorder) =
18851:                     &Apache::lonnet::inst_userrules($dom,'email');
18852:                 foreach my $type (@{$changes{'email_rule'}}) {
18853:                     if (ref($email_rule{$type}) eq 'ARRAY') {
18854:                         my $chgtext = '<ul>';
18855:                         foreach my $rule (@{$email_rule{$type}}) {
18856:                             if (ref($emailrules->{$rule}) eq 'HASH') {
18857:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
18858:                             }
18859:                         }
18860:                         $chgtext .= '</ul>';
18861:                         my $typename;
18862:                         if (@types) {
18863:                             if ($type eq 'default') {
18864:                                 $typename = $othertitle;
18865:                             } else {
18866:                                 $typename = $usertypes{$type};
18867:                             } 
18868:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
18869:                         }
18870:                         if (@{$email_rule{$type}} > 0) {
18871:                             $resulttext .= '<li>'.
18872:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
18873:                                                $usertypes{$type}).
18874:                                            $chgtext.
18875:                                            '</li>';
18876:                         } else {
18877:                             $resulttext .= '<li>'.
18878:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
18879:                                            '</li>'.
18880:                                            &mt('(Affiliation: [_1])',$typename);
18881:                         }
18882:                     }
18883:                 }
18884:             }
18885:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
18886:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
18887:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
18888:                         my $chgtext = '<ul>';
18889:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
18890:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
18891:                         }
18892:                         $chgtext .= '</ul>';
18893:                         $resulttext .= '<li>'.
18894:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
18895:                                           $chgtext.
18896:                                        '</li>';
18897:                     } else {
18898:                         $resulttext .= '<li>'.
18899:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
18900:                                        '</li>';
18901:                     }
18902:                 }
18903:             }
18904:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
18905:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
18906:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18907:                 foreach my $type (@{$changes{'selfcreate'}}) {
18908:                     my $typename = $type;
18909:                     if (keys(%usertypes) > 0) {
18910:                         if ($usertypes{$type} ne '') {
18911:                             $typename = $usertypes{$type};
18912:                         }
18913:                     }
18914:                     my @modifiable;
18915:                     $resulttext .= '<li>'.
18916:                                     &mt('Self-creation of account by users with status: [_1]',
18917:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
18918:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
18919:                     foreach my $field (@fields) {
18920:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
18921:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
18922:                         }
18923:                     }
18924:                     if (@modifiable > 0) {
18925:                         $resulttext .= join(', ',@modifiable);
18926:                     } else {
18927:                         $resulttext .= &mt('none');
18928:                     }
18929:                     $resulttext .= '</li>';
18930:                 }
18931:                 $resulttext .= '</ul></li>';
18932:             }
18933:             $resulttext .= '</ul>';
18934:             my $cachetime = 24*60*60;
18935:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
18936:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18937:             if (ref($lastactref) eq 'HASH') {
18938:                 $lastactref->{'domdefaults'} = 1;
18939:             }
18940:         } else {
18941:             $resulttext = &mt('No changes made to self-creation settings');
18942:         }
18943:     } else {
18944:         $resulttext = '<span class="LC_error">'.
18945:             &mt('An error occurred: [_1]',$putresult).'</span>';
18946:     }
18947:     if ($warningmsg ne '') {
18948:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18949:     }
18950:     return $resulttext;
18951: }
18952: 
18953: sub process_captcha {
18954:     my ($container,$changes,$newsettings,$currsettings) = @_;
18955:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
18956:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
18957:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
18958:         $newsettings->{'captcha'} = 'original';
18959:     }
18960:     my %current;
18961:     if (ref($currsettings) eq 'HASH') {
18962:         %current = %{$currsettings};
18963:     }
18964:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
18965:         if ($container eq 'cancreate') {
18966:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18967:                 push(@{$changes->{'cancreate'}},'captcha');
18968:             } elsif (!defined($changes->{'cancreate'})) {
18969:                 $changes->{'cancreate'} = ['captcha'];
18970:             }
18971:         } elsif ($container eq 'passwords') {
18972:             $changes->{'reset'} = 1;
18973:         } else {
18974:             $changes->{'captcha'} = 1;
18975:         }
18976:     }
18977:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
18978:     if ($newsettings->{'captcha'} eq 'recaptcha') {
18979:         $newpub = $env{'form.'.$container.'_recaptchapub'};
18980:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
18981:         $newpub =~ s/[^\w\-]//g;
18982:         $newpriv =~ s/[^\w\-]//g;
18983:         $newsettings->{'recaptchakeys'} = {
18984:                                              public  => $newpub,
18985:                                              private => $newpriv,
18986:                                           };
18987:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
18988:         $newversion =~ s/\D//g;
18989:         if ($newversion ne '2') {
18990:             $newversion = 1;
18991:         }
18992:         $newsettings->{'recaptchaversion'} = $newversion;
18993:     }
18994:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
18995:         $currpub = $current{'recaptchakeys'}{'public'};
18996:         $currpriv = $current{'recaptchakeys'}{'private'};
18997:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
18998:             $newsettings->{'recaptchakeys'} = {
18999:                                                  public  => '',
19000:                                                  private => '',
19001:                                               }
19002:         }
19003:     }
19004:     if ($current{'captcha'} eq 'recaptcha') {
19005:         $currversion = $current{'recaptchaversion'};
19006:         if ($currversion ne '2') {
19007:             $currversion = 1;
19008:         }
19009:     }
19010:     if ($currversion ne $newversion) {
19011:         if ($container eq 'cancreate') {
19012:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19013:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19014:             } elsif (!defined($changes->{'cancreate'})) {
19015:                 $changes->{'cancreate'} = ['recaptchaversion'];
19016:             }
19017:         } elsif ($container eq 'passwords') {
19018:             $changes->{'reset'} = 1;
19019:         } else {
19020:             $changes->{'recaptchaversion'} = 1;
19021:         }
19022:     }
19023:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19024:         if ($container eq 'cancreate') {
19025:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19026:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19027:             } elsif (!defined($changes->{'cancreate'})) {
19028:                 $changes->{'cancreate'} = ['recaptchakeys'];
19029:             }
19030:         } elsif ($container eq 'passwords') {
19031:             $changes->{'reset'} = 1;
19032:         } else {
19033:             $changes->{'recaptchakeys'} = 1;
19034:         }
19035:     }
19036:     return;
19037: }
19038: 
19039: sub modify_usermodification {
19040:     my ($dom,%domconfig) = @_;
19041:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19042:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19043:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19044:             if ($key eq 'selfcreate') {
19045:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19046:             } else {  
19047:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19048:             }
19049:         }
19050:     }
19051:     my @contexts = ('author','course');
19052:     my %context_title = (
19053:                            author => 'In author context',
19054:                            course => 'In course context',
19055:                         );
19056:     my @fields = ('lastname','firstname','middlename','generation',
19057:                   'permanentemail','id');
19058:     my %roles = (
19059:                   author => ['ca','aa'],
19060:                   course => ['st','ep','ta','in','cr'],
19061:                 );
19062:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19063:     foreach my $context (@contexts) {
19064:         foreach my $role (@{$roles{$context}}) {
19065:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
19066:             foreach my $item (@fields) {
19067:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19068:                     $modifyhash{$context}{$role}{$item} = 1;
19069:                 } else {
19070:                     $modifyhash{$context}{$role}{$item} = 0;
19071:                 }
19072:             }
19073:         }
19074:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19075:             foreach my $role (@{$roles{$context}}) {
19076:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19077:                     foreach my $field (@fields) {
19078:                         if ($modifyhash{$context}{$role}{$field} ne 
19079:                                 $curr_usermodification{$context}{$role}{$field}) {
19080:                             push(@{$changes{$context}},$role);
19081:                             last;
19082:                         }
19083:                     }
19084:                 }
19085:             }
19086:         } else {
19087:             foreach my $context (@contexts) {
19088:                 foreach my $role (@{$roles{$context}}) {
19089:                     push(@{$changes{$context}},$role);
19090:                 }
19091:             }
19092:         }
19093:     }
19094:     my %usermodification_hash =  (
19095:                                    usermodification => \%modifyhash,
19096:                                  );
19097:     my $putresult = &Apache::lonnet::put_dom('configuration',
19098:                                              \%usermodification_hash,$dom);
19099:     if ($putresult eq 'ok') {
19100:         if (keys(%changes) > 0) {
19101:             $resulttext = &mt('Changes made: ').'<ul>';
19102:             foreach my $context (@contexts) {
19103:                 if (ref($changes{$context}) eq 'ARRAY') {
19104:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19105:                     if (ref($changes{$context}) eq 'ARRAY') {
19106:                         foreach my $role (@{$changes{$context}}) {
19107:                             my $rolename;
19108:                             if ($role eq 'cr') {
19109:                                 $rolename = &mt('Custom');
19110:                             } else {
19111:                                 $rolename = &Apache::lonnet::plaintext($role);
19112:                             }
19113:                             my @modifiable;
19114:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19115:                             foreach my $field (@fields) {
19116:                                 if ($modifyhash{$context}{$role}{$field}) {
19117:                                     push(@modifiable,$fieldtitles{$field});
19118:                                 }
19119:                             }
19120:                             if (@modifiable > 0) {
19121:                                 $resulttext .= join(', ',@modifiable);
19122:                             } else {
19123:                                 $resulttext .= &mt('none'); 
19124:                             }
19125:                             $resulttext .= '</li>';
19126:                         }
19127:                         $resulttext .= '</ul></li>';
19128:                     }
19129:                 }
19130:             }
19131:             $resulttext .= '</ul>';
19132:         } else {
19133:             $resulttext = &mt('No changes made to user modification settings');
19134:         }
19135:     } else {
19136:         $resulttext = '<span class="LC_error">'.
19137:             &mt('An error occurred: [_1]',$putresult).'</span>';
19138:     }
19139:     return $resulttext;
19140: }
19141: 
19142: sub modify_defaults {
19143:     my ($dom,$lastactref,%domconfig) = @_;
19144:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
19145:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19146:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
19147:                  'portal_def');
19148:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
19149:     foreach my $item (@items) {
19150:         $newvalues{$item} = $env{'form.'.$item};
19151:         if ($item eq 'auth_def') {
19152:             if ($newvalues{$item} ne '') {
19153:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
19154:                     push(@errors,$item);
19155:                 }
19156:             }
19157:         } elsif ($item eq 'lang_def') {
19158:             if ($newvalues{$item} ne '') {
19159:                 if ($newvalues{$item} =~ /^(\w+)/) {
19160:                     my $langcode = $1;
19161:                     if ($langcode ne 'x_chef') {
19162:                         if (code2language($langcode) eq '') {
19163:                             push(@errors,$item);
19164:                         }
19165:                     }
19166:                 } else {
19167:                     push(@errors,$item);
19168:                 }
19169:             }
19170:         } elsif ($item eq 'timezone_def') {
19171:             if ($newvalues{$item} ne '') {
19172:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
19173:                     push(@errors,$item);   
19174:                 }
19175:             }
19176:         } elsif ($item eq 'datelocale_def') {
19177:             if ($newvalues{$item} ne '') {
19178:                 my @datelocale_ids = DateTime::Locale->ids();
19179:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
19180:                     push(@errors,$item);
19181:                 }
19182:             }
19183:         } elsif ($item eq 'portal_def') {
19184:             if ($newvalues{$item} ne '') {
19185:                 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])\/?$/) {
19186:                     push(@errors,$item);
19187:                 }
19188:             }
19189:         }
19190:         if (grep(/^\Q$item\E$/,@errors)) {
19191:             $newvalues{$item} = $domdefaults{$item};
19192:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
19193:             $changes{$item} = 1;
19194:         }
19195:         $domdefaults{$item} = $newvalues{$item};
19196:     }
19197:     my %staticdefaults = (
19198:                            'intauth_cost'   => 10,
19199:                            'intauth_check'  => 0,
19200:                            'intauth_switch' => 0,
19201:                          );
19202:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
19203:         if (exists($domdefaults{$item})) {
19204:             $newvalues{$item} = $domdefaults{$item};
19205:         } else {
19206:             $newvalues{$item} = $staticdefaults{$item};
19207:         }
19208:     }
19209:     my %defaults_hash = (
19210:                          defaults => \%newvalues,
19211:                         );
19212:     my $title = &defaults_titles();
19213: 
19214:     my $currinststatus;
19215:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
19216:         $currinststatus = $domconfig{'inststatus'};
19217:     } else {
19218:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19219:         $currinststatus = {
19220:                              inststatustypes => $usertypes,
19221:                              inststatusorder => $types,
19222:                              inststatusguest => [],
19223:                           };
19224:     }
19225:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
19226:     my @allpos;
19227:     my %alltypes;
19228:     my @inststatusguest;
19229:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
19230:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
19231:             unless (grep(/^\Q$type\E$/,@todelete)) {
19232:                 push(@inststatusguest,$type);
19233:             }
19234:         }
19235:     }
19236:     my ($currtitles,$currorder);
19237:     if (ref($currinststatus) eq 'HASH') {
19238:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
19239:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
19240:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
19241:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
19242:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
19243:                     }
19244:                 }
19245:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
19246:                     my $position = $env{'form.inststatus_pos_'.$type};
19247:                     $position =~ s/\D+//g;
19248:                     $allpos[$position] = $type;
19249:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
19250:                     $alltypes{$type} =~ s/`//g;
19251:                 }
19252:             }
19253:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
19254:             $currtitles =~ s/,$//;
19255:         }
19256:     }
19257:     if ($env{'form.addinststatus'}) {
19258:         my $newtype = $env{'form.addinststatus'};
19259:         $newtype =~ s/\W//g;
19260:         unless (exists($alltypes{$newtype})) {
19261:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
19262:             $alltypes{$newtype} =~ s/`//g; 
19263:             my $position = $env{'form.addinststatus_pos'};
19264:             $position =~ s/\D+//g;
19265:             if ($position ne '') {
19266:                 $allpos[$position] = $newtype;
19267:             }
19268:         }
19269:     }
19270:     my @orderedstatus;
19271:     foreach my $type (@allpos) {
19272:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
19273:             push(@orderedstatus,$type);
19274:         }
19275:     }
19276:     foreach my $type (keys(%alltypes)) {
19277:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
19278:             delete($alltypes{$type});
19279:         }
19280:     }
19281:     $defaults_hash{'inststatus'} = {
19282:                                      inststatustypes => \%alltypes,
19283:                                      inststatusorder => \@orderedstatus,
19284:                                      inststatusguest => \@inststatusguest,
19285:                                    };
19286:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
19287:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
19288:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
19289:         }
19290:     }
19291:     if ($currorder ne join(',',@orderedstatus)) {
19292:         $changes{'inststatus'}{'inststatusorder'} = 1;
19293:     }
19294:     my $newtitles;
19295:     foreach my $item (@orderedstatus) {
19296:         $newtitles .= $alltypes{$item}.',';
19297:     }
19298:     $newtitles =~ s/,$//;
19299:     if ($currtitles ne $newtitles) {
19300:         $changes{'inststatus'}{'inststatustypes'} = 1;
19301:     }
19302:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
19303:                                              $dom);
19304:     if ($putresult eq 'ok') {
19305:         if (keys(%changes) > 0) {
19306:             $resulttext = &mt('Changes made:').'<ul>';
19307:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
19308:             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";
19309:             foreach my $item (sort(keys(%changes))) {
19310:                 if ($item eq 'inststatus') {
19311:                     if (ref($changes{'inststatus'}) eq 'HASH') {
19312:                         if (@orderedstatus) {
19313:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
19314:                             foreach my $type (@orderedstatus) { 
19315:                                 $resulttext .= $alltypes{$type}.', ';
19316:                             }
19317:                             $resulttext =~ s/, $//;
19318:                             $resulttext .= '</li>';
19319:                         } else {
19320:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
19321:                         }
19322:                     }
19323:                 } else {
19324:                     my $value = $env{'form.'.$item};
19325:                     if ($value eq '') {
19326:                         $value = &mt('none');
19327:                     } elsif ($item eq 'auth_def') {
19328:                         my %authnames = &authtype_names();
19329:                         my %shortauth = (
19330:                                           internal   => 'int',
19331:                                           krb4       => 'krb4',
19332:                                           krb5       => 'krb5',
19333:                                           localauth  => 'loc',
19334:                                           lti        => 'lti',
19335:                         );
19336:                         $value = $authnames{$shortauth{$value}};
19337:                     }
19338:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
19339:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
19340:                 }
19341:             }
19342:             $resulttext .= '</ul>';
19343:             $mailmsgtext .= "\n";
19344:             my $cachetime = 24*60*60;
19345:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19346:             if (ref($lastactref) eq 'HASH') {
19347:                 $lastactref->{'domdefaults'} = 1;
19348:             }
19349:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
19350:                 my $notify = 1;
19351:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
19352:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
19353:                         $notify = 0;
19354:                     }
19355:                 }
19356:                 if ($notify) {
19357:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
19358:                                                "LON-CAPA Domain Settings Change - $dom",
19359:                                                $mailmsgtext);
19360:                 }
19361:             }
19362:         } else {
19363:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
19364:         }
19365:     } else {
19366:         $resulttext = '<span class="LC_error">'.
19367:             &mt('An error occurred: [_1]',$putresult).'</span>';
19368:     }
19369:     if (@errors > 0) {
19370:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
19371:         foreach my $item (@errors) {
19372:             $resulttext .= ' "'.$title->{$item}.'",';
19373:         }
19374:         $resulttext =~ s/,$//;
19375:     }
19376:     return $resulttext;
19377: }
19378: 
19379: sub modify_scantron {
19380:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19381:     my ($resulttext,%confhash,%changes,$errors);
19382:     my $custom = 'custom.tab';
19383:     my $default = 'default.tab';
19384:     my $servadm = $r->dir_config('lonAdmEMail');
19385:     my ($configuserok,$author_ok,$switchserver) =
19386:         &config_check($dom,$confname,$servadm);
19387:     if ($env{'form.scantronformat.filename'} ne '') {
19388:         my $error;
19389:         if ($configuserok eq 'ok') {
19390:             if ($switchserver) {
19391:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
19392:             } else {
19393:                 if ($author_ok eq 'ok') {
19394:                     my ($result,$scantronurl) =
19395:                         &publishlogo($r,'upload','scantronformat',$dom,
19396:                                      $confname,'scantron','','',$custom);
19397:                     if ($result eq 'ok') {
19398:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
19399:                         $changes{'scantronformat'} = 1;
19400:                     } else {
19401:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
19402:                     }
19403:                 } else {
19404:                     $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);
19405:                 }
19406:             }
19407:         } else {
19408:             $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);
19409:         }
19410:         if ($error) {
19411:             &Apache::lonnet::logthis($error);
19412:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
19413:         }
19414:     }
19415:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19416:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
19417:             if ($env{'form.scantronformat_del'}) {
19418:                 $confhash{'scantron'}{'scantronformat'} = '';
19419:                 $changes{'scantronformat'} = 1;
19420:             } else {
19421:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
19422:             }
19423:         }
19424:     }
19425:     my @options = ('hdr','pad','rem');
19426:     my @fields = &scantroncsv_fields();
19427:     my %titles = &scantronconfig_titles();
19428:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
19429:     my ($newdat,$currdat,%newcol,%currcol);
19430:     if (grep(/^dat$/,@formats)) {
19431:         $confhash{'scantron'}{config}{dat} = 1;
19432:         $newdat = 1;
19433:     } else {
19434:         $newdat = 0;
19435:     }
19436:     if (grep(/^csv$/,@formats)) {
19437:         my %bynum;
19438:         foreach my $field (@fields) {
19439:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
19440:                 my $posscol = $1;
19441:                 if (($posscol < 20) && (!$bynum{$posscol})) {
19442:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
19443:                     $bynum{$posscol} = $field;
19444:                     $newcol{$field} = $posscol;
19445:                 }
19446:             }
19447:         }
19448:         if (keys(%newcol)) {
19449:             foreach my $option (@options) {
19450:                 if ($env{'form.scantroncsv_'.$option}) {
19451:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
19452:                 }
19453:             }
19454:         }
19455:     }
19456:     $currdat = 1;
19457:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19458:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
19459:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
19460:                 $currdat = 0;
19461:             }
19462:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19463:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19464:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
19465:                 }
19466:             }
19467:         }
19468:     }
19469:     if ($currdat != $newdat) {
19470:         $changes{'config'} = 1;
19471:     } else {
19472:         foreach my $field (@fields) {
19473:             if ($currcol{$field} ne '') {
19474:                 if ($currcol{$field} ne $newcol{$field}) {
19475:                     $changes{'config'} = 1;
19476:                     last;
19477:                 }
19478:             } elsif ($newcol{$field} ne '') {
19479:                 $changes{'config'} = 1;
19480:                 last;
19481:             }
19482:         }
19483:     }
19484:     if (keys(%confhash) > 0) {
19485:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
19486:                                                  $dom);
19487:         if ($putresult eq 'ok') {
19488:             if (keys(%changes) > 0) {
19489:                 if (ref($confhash{'scantron'}) eq 'HASH') {
19490:                     $resulttext = &mt('Changes made:').'<ul>';
19491:                     if ($changes{'scantronformat'}) {
19492:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
19493:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
19494:                         } else {
19495:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
19496:                         }
19497:                     }
19498:                     if ($changes{'config'}) {
19499:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
19500:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
19501:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
19502:                             }
19503:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19504:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19505:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
19506:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
19507:                                         foreach my $field (@fields) {
19508:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
19509:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
19510:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
19511:                                             }
19512:                                         }
19513:                                         $resulttext .= '</ul></li>';
19514:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
19515:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
19516:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
19517:                                                 foreach my $option (@options) {
19518:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
19519:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
19520:                                                     }
19521:                                                 }
19522:                                                 $resulttext .= '</ul></li>';
19523:                                             }
19524:                                         }
19525:                                     }
19526:                                 }
19527:                             }
19528:                         } else {
19529:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
19530:                         }
19531:                     }
19532:                     $resulttext .= '</ul>';
19533:                 } else {
19534:                     $resulttext = &mt('Changes made to bubblesheet format file.');
19535:                 }
19536:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
19537:                 if (ref($lastactref) eq 'HASH') {
19538:                     $lastactref->{'domainconfig'} = 1;
19539:                 }
19540:             } else {
19541:                 $resulttext = &mt('No changes made to bubblesheet format settings');
19542:             }
19543:         } else {
19544:             $resulttext = '<span class="LC_error">'.
19545:                 &mt('An error occurred: [_1]',$putresult).'</span>';
19546:         }
19547:     } else {
19548:         $resulttext = &mt('No changes made to bubblesheet format file'); 
19549:     }
19550:     if ($errors) {
19551:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
19552:                        $errors.'</ul></p>';
19553:     }
19554:     return $resulttext;
19555: }
19556: 
19557: sub modify_coursecategories {
19558:     my ($dom,$lastactref,%domconfig) = @_;
19559:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
19560:         $cathash);
19561:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
19562:     my @catitems = ('unauth','auth');
19563:     my @cattypes = ('std','domonly','codesrch','none');
19564:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19565:         $cathash = $domconfig{'coursecategories'}{'cats'};
19566:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
19567:             $changes{'togglecats'} = 1;
19568:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
19569:         }
19570:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
19571:             $changes{'categorize'} = 1;
19572:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
19573:         }
19574:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
19575:             $changes{'togglecatscomm'} = 1;
19576:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
19577:         }
19578:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
19579:             $changes{'categorizecomm'} = 1;
19580:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
19581: 
19582:         }
19583:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
19584:             $changes{'togglecatsplace'} = 1;
19585:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
19586:         }
19587:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
19588:             $changes{'categorizeplace'} = 1;
19589:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
19590:         }
19591:         foreach my $item (@catitems) {
19592:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19593:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
19594:                     $changes{$item} = 1;
19595:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19596:                 }
19597:             }
19598:         }
19599:     } else {
19600:         $changes{'togglecats'} = 1;
19601:         $changes{'categorize'} = 1;
19602:         $changes{'togglecatscomm'} = 1;
19603:         $changes{'categorizecomm'} = 1;
19604:         $changes{'togglecatsplace'} = 1;
19605:         $changes{'categorizeplace'} = 1;
19606:         $domconfig{'coursecategories'} = {
19607:                                              togglecats => $env{'form.togglecats'},
19608:                                              categorize => $env{'form.categorize'},
19609:                                              togglecatscomm => $env{'form.togglecatscomm'},
19610:                                              categorizecomm => $env{'form.categorizecomm'},
19611:                                              togglecatsplace => $env{'form.togglecatsplace'},
19612:                                              categorizeplace => $env{'form.categorizeplace'},
19613:                                          };
19614:         foreach my $item (@catitems) {
19615:             if ($env{'form.coursecat_'.$item} ne 'std') {
19616:                 $changes{$item} = 1;
19617:             }
19618:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19619:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19620:             }
19621:         }
19622:     }
19623:     if (ref($cathash) eq 'HASH') {
19624:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
19625:             push (@deletecategory,'instcode::0');
19626:         }
19627:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
19628:             push(@deletecategory,'communities::0');
19629:         }
19630:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
19631:             push(@deletecategory,'placement::0');
19632:         }
19633:     }
19634:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
19635:     if (ref($cathash) eq 'HASH') {
19636:         if (@deletecategory > 0) {
19637:             #FIXME Need to remove category from all courses using a deleted category 
19638:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
19639:             foreach my $item (@deletecategory) {
19640:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
19641:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
19642:                     $deletions{$item} = 1;
19643:                     &recurse_cat_deletes($item,$cathash,\%deletions);
19644:                 }
19645:             }
19646:         }
19647:         foreach my $item (keys(%{$cathash})) {
19648:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
19649:             if ($cathash->{$item} ne $env{'form.'.$item}) {
19650:                 $reorderings{$item} = 1;
19651:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
19652:             }
19653:             if ($env{'form.addcategory_name_'.$item} ne '') {
19654:                 my $newcat = $env{'form.addcategory_name_'.$item};
19655:                 my $newdepth = $depth+1;
19656:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19657:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
19658:                 $adds{$newitem} = 1; 
19659:             }
19660:             if ($env{'form.subcat_'.$item} ne '') {
19661:                 my $newcat = $env{'form.subcat_'.$item};
19662:                 my $newdepth = $depth+1;
19663:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19664:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
19665:                 $adds{$newitem} = 1;
19666:             }
19667:         }
19668:     }
19669:     if ($env{'form.instcode'} eq '1') {
19670:         if (ref($cathash) eq 'HASH') {
19671:             my $newitem = 'instcode::0';
19672:             if ($cathash->{$newitem} eq '') {  
19673:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19674:                 $adds{$newitem} = 1;
19675:             }
19676:         } else {
19677:             my $newitem = 'instcode::0';
19678:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19679:             $adds{$newitem} = 1;
19680:         }
19681:     }
19682:     if ($env{'form.communities'} eq '1') {
19683:         if (ref($cathash) eq 'HASH') {
19684:             my $newitem = 'communities::0';
19685:             if ($cathash->{$newitem} eq '') {
19686:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19687:                 $adds{$newitem} = 1;
19688:             }
19689:         } else {
19690:             my $newitem = 'communities::0';
19691:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19692:             $adds{$newitem} = 1;
19693:         }
19694:     }
19695:     if ($env{'form.placement'} eq '1') {
19696:         if (ref($cathash) eq 'HASH') {
19697:             my $newitem = 'placement::0';
19698:             if ($cathash->{$newitem} eq '') {
19699:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19700:                 $adds{$newitem} = 1;
19701:             }
19702:         } else {
19703:             my $newitem = 'placement::0';
19704:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19705:             $adds{$newitem} = 1;
19706:         }
19707:     }
19708:     if ($env{'form.addcategory_name'} ne '') {
19709:         if (($env{'form.addcategory_name'} ne 'instcode') &&
19710:             ($env{'form.addcategory_name'} ne 'communities') &&
19711:             ($env{'form.addcategory_name'} ne 'placement')) {
19712:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
19713:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
19714:             $adds{$newitem} = 1;
19715:         }
19716:     }
19717:     my $putresult;
19718:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19719:         if (keys(%deletions) > 0) {
19720:             foreach my $key (keys(%deletions)) {
19721:                 if ($predelallitems{$key} ne '') {
19722:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
19723:                 }
19724:             }
19725:         }
19726:         my (@chkcats,@chktrails,%chkallitems);
19727:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
19728:         if (ref($chkcats[0]) eq 'ARRAY') {
19729:             my $depth = 0;
19730:             my $chg = 0;
19731:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
19732:                 my $name = $chkcats[0][$i];
19733:                 my $item;
19734:                 if ($name eq '') {
19735:                     $chg ++;
19736:                 } else {
19737:                     $item = &escape($name).'::0';
19738:                     if ($chg) {
19739:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
19740:                     }
19741:                     $depth ++; 
19742:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
19743:                     $depth --;
19744:                 }
19745:             }
19746:         }
19747:     }
19748:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19749:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
19750:         if ($putresult eq 'ok') {
19751:             my %title = (
19752:                          togglecats     => 'Show/Hide a course in catalog',
19753:                          categorize     => 'Assign a category to a course',
19754:                          togglecatscomm => 'Show/Hide a community in catalog',
19755:                          categorizecomm => 'Assign a category to a community',
19756:                         );
19757:             my %level = (
19758:                          dom  => 'set in Domain ("Modify Course/Community")',
19759:                          crs  => 'set in Course ("Course Configuration")',
19760:                          comm => 'set in Community ("Community Configuration")',
19761:                          none     => 'No catalog',
19762:                          std      => 'Standard catalog',
19763:                          domonly  => 'Domain-only catalog',
19764:                          codesrch => 'Code search form',
19765:                         );
19766:             $resulttext = &mt('Changes made:').'<ul>';
19767:             if ($changes{'togglecats'}) {
19768:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
19769:             }
19770:             if ($changes{'categorize'}) {
19771:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
19772:             }
19773:             if ($changes{'togglecatscomm'}) {
19774:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
19775:             }
19776:             if ($changes{'categorizecomm'}) {
19777:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
19778:             }
19779:             if ($changes{'unauth'}) {
19780:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
19781:             }
19782:             if ($changes{'auth'}) {
19783:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
19784:             }
19785:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19786:                 my $cathash;
19787:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19788:                     $cathash = $domconfig{'coursecategories'}{'cats'};
19789:                 } else {
19790:                     $cathash = {};
19791:                 } 
19792:                 my (@cats,@trails,%allitems);
19793:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
19794:                 if (keys(%deletions) > 0) {
19795:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
19796:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
19797:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
19798:                     }
19799:                     $resulttext .= '</ul></li>';
19800:                 }
19801:                 if (keys(%reorderings) > 0) {
19802:                     my %sort_by_trail;
19803:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
19804:                     foreach my $key (keys(%reorderings)) {
19805:                         if ($allitems{$key} ne '') {
19806:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19807:                         }
19808:                     }
19809:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19810:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19811:                     }
19812:                     $resulttext .= '</ul></li>';
19813:                 }
19814:                 if (keys(%adds) > 0) {
19815:                     my %sort_by_trail;
19816:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
19817:                     foreach my $key (keys(%adds)) {
19818:                         if ($allitems{$key} ne '') {
19819:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19820:                         }
19821:                     }
19822:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19823:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19824:                     }
19825:                     $resulttext .= '</ul></li>';
19826:                 }
19827:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
19828:                 if (ref($lastactref) eq 'HASH') {
19829:                     $lastactref->{'cats'} = 1;
19830:                 }
19831:             }
19832:             $resulttext .= '</ul>';
19833:             if ($changes{'unauth'} || $changes{'auth'}) {
19834:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
19835:                 if ($changes{'auth'}) {
19836:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
19837:                 }
19838:                 if ($changes{'unauth'}) {
19839:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
19840:                 }
19841:                 my $cachetime = 24*60*60;
19842:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19843:                 if (ref($lastactref) eq 'HASH') {
19844:                     $lastactref->{'domdefaults'} = 1;
19845:                 }
19846:             }
19847:         } else {
19848:             $resulttext = '<span class="LC_error">'.
19849:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19850:         }
19851:     } else {
19852:         $resulttext = &mt('No changes made to course and community categories');
19853:     }
19854:     return $resulttext;
19855: }
19856: 
19857: sub modify_serverstatuses {
19858:     my ($dom,%domconfig) = @_;
19859:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
19860:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
19861:         %currserverstatus = %{$domconfig{'serverstatuses'}};
19862:     }
19863:     my @pages = &serverstatus_pages();
19864:     foreach my $type (@pages) {
19865:         $newserverstatus{$type}{'namedusers'} = '';
19866:         $newserverstatus{$type}{'machines'} = '';
19867:         if (defined($env{'form.'.$type.'_namedusers'})) {
19868:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
19869:             my @okusers;
19870:             foreach my $user (@users) {
19871:                 my ($uname,$udom) = split(/:/,$user);
19872:                 if (($udom =~ /^$match_domain$/) &&   
19873:                     (&Apache::lonnet::domain($udom)) &&
19874:                     ($uname =~ /^$match_username$/)) {
19875:                     if (!grep(/^\Q$user\E/,@okusers)) {
19876:                         push(@okusers,$user);
19877:                     }
19878:                 }
19879:             }
19880:             if (@okusers > 0) {
19881:                  @okusers = sort(@okusers);
19882:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
19883:             }
19884:         }
19885:         if (defined($env{'form.'.$type.'_machines'})) {
19886:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
19887:             my @okmachines;
19888:             foreach my $ip (@machines) {
19889:                 my @parts = split(/\./,$ip);
19890:                 next if (@parts < 4);
19891:                 my $badip = 0;
19892:                 for (my $i=0; $i<4; $i++) {
19893:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
19894:                         $badip = 1;
19895:                         last;
19896:                     }
19897:                 }
19898:                 if (!$badip) {
19899:                     push(@okmachines,$ip);     
19900:                 }
19901:             }
19902:             @okmachines = sort(@okmachines);
19903:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
19904:         }
19905:     }
19906:     my %serverstatushash =  (
19907:                                 serverstatuses => \%newserverstatus,
19908:                             );
19909:     foreach my $type (@pages) {
19910:         foreach my $setting ('namedusers','machines') {
19911:             my (@current,@new);
19912:             if (ref($currserverstatus{$type}) eq 'HASH') {
19913:                 if ($currserverstatus{$type}{$setting} ne '') { 
19914:                     @current = split(/,/,$currserverstatus{$type}{$setting});
19915:                 }
19916:             }
19917:             if ($newserverstatus{$type}{$setting} ne '') {
19918:                 @new = split(/,/,$newserverstatus{$type}{$setting});
19919:             }
19920:             if (@current > 0) {
19921:                 if (@new > 0) {
19922:                     foreach my $item (@current) {
19923:                         if (!grep(/^\Q$item\E$/,@new)) {
19924:                             $changes{$type}{$setting} = 1;
19925:                             last;
19926:                         }
19927:                     }
19928:                     foreach my $item (@new) {
19929:                         if (!grep(/^\Q$item\E$/,@current)) {
19930:                             $changes{$type}{$setting} = 1;
19931:                             last;
19932:                         }
19933:                     }
19934:                 } else {
19935:                     $changes{$type}{$setting} = 1;
19936:                 }
19937:             } elsif (@new > 0) {
19938:                 $changes{$type}{$setting} = 1;
19939:             }
19940:         }
19941:     }
19942:     if (keys(%changes) > 0) {
19943:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
19944:         my $putresult = &Apache::lonnet::put_dom('configuration',
19945:                                                  \%serverstatushash,$dom);
19946:         if ($putresult eq 'ok') {
19947:             $resulttext .= &mt('Changes made:').'<ul>';
19948:             foreach my $type (@pages) {
19949:                 if (ref($changes{$type}) eq 'HASH') {
19950:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
19951:                     if ($changes{$type}{'namedusers'}) {
19952:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
19953:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
19954:                         } else {
19955:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
19956:                         }
19957:                     }
19958:                     if ($changes{$type}{'machines'}) {
19959:                         if ($newserverstatus{$type}{'machines'} eq '') {
19960:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
19961:                         } else {
19962:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
19963:                         }
19964: 
19965:                     }
19966:                     $resulttext .= '</ul></li>';
19967:                 }
19968:             }
19969:             $resulttext .= '</ul>';
19970:         } else {
19971:             $resulttext = '<span class="LC_error">'.
19972:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
19973: 
19974:         }
19975:     } else {
19976:         $resulttext = &mt('No changes made to access to server status pages');
19977:     }
19978:     return $resulttext;
19979: }
19980: 
19981: sub modify_helpsettings {
19982:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19983:     my ($resulttext,$errors,%changes,%helphash);
19984:     my %defaultchecked = ('submitbugs' => 'on');
19985:     my @offon = ('off','on');
19986:     my @toggles = ('submitbugs');
19987:     my %current = ('submitbugs' => '',
19988:                    'adhoc'      => {},
19989:                   );
19990:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
19991:         %current = %{$domconfig{'helpsettings'}};
19992:     }
19993:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19994:     foreach my $item (@toggles) {
19995:         if ($defaultchecked{$item} eq 'on') { 
19996:             if ($current{$item} eq '') {
19997:                 if ($env{'form.'.$item} eq '0') {
19998:                     $changes{$item} = 1;
19999:                 }
20000:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20001:                 $changes{$item} = 1;
20002:             }
20003:         } elsif ($defaultchecked{$item} eq 'off') {
20004:             if ($current{$item} eq '') {
20005:                 if ($env{'form.'.$item} eq '1') {
20006:                     $changes{$item} = 1;
20007:                 }
20008:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20009:                 $changes{$item} = 1;
20010:             }
20011:         }
20012:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20013:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20014:         }
20015:     }
20016:     my $maxnum = $env{'form.helproles_maxnum'};
20017:     my $confname = $dom.'-domainconfig';
20018:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20019:     my (@allpos,%newsettings,%changedprivs,$newrole);
20020:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20021:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20022:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20023:     my %lt = &Apache::lonlocal::texthash(
20024:                     s      => 'system',
20025:                     d      => 'domain',
20026:                     order  => 'Display order',
20027:                     access => 'Role usage',
20028:                     all    => 'All with domain helpdesk or helpdesk assistant role',
20029:                     dh     => 'All with domain helpdesk role',
20030:                     da     => 'All with domain helpdesk assistant role',
20031:                     none   => 'None',
20032:                     status => 'Determined based on institutional status',
20033:                     inc    => 'Include all, but exclude specific personnel',
20034:                     exc    => 'Exclude all, but include specific personnel',
20035:     );
20036:     for (my $num=0; $num<=$maxnum; $num++) {
20037:         my ($prefix,$identifier,$rolename,%curr);
20038:         if ($num == $maxnum) {
20039:             next unless ($env{'form.newcusthelp'} == $maxnum);
20040:             $identifier = 'custhelp'.$num;
20041:             $prefix = 'helproles_'.$num;
20042:             $rolename = $env{'form.custhelpname'.$num};
20043:             $rolename=~s/[^A-Za-z0-9]//gs;
20044:             next if ($rolename eq '');
20045:             next if (exists($existing{'rolesdef_'.$rolename}));
20046:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20047:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20048:                                                      $newprivs{'c'},$confname,$dom);
20049:             if ($result ne 'ok') {
20050:                 $errors .= '<li><span class="LC_error">'.
20051:                            &mt('An error occurred storing the new custom role: [_1]',
20052:                            $result).'</span></li>';
20053:                 next;
20054:             } else {
20055:                 $changedprivs{$rolename} = \%newprivs;
20056:                 $newrole = $rolename;
20057:             }
20058:         } else {
20059:             $prefix = 'helproles_'.$num;
20060:             $rolename = $env{'form.'.$prefix};
20061:             next if ($rolename eq '');
20062:             next unless (exists($existing{'rolesdef_'.$rolename}));
20063:             $identifier = 'custhelp'.$num;
20064:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20065:             my %currprivs;
20066:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
20067:                 split(/\_/,$existing{'rolesdef_'.$rolename});
20068:             foreach my $level ('c','d','s') {
20069:                 if ($newprivs{$level} ne $currprivs{$level}) {
20070:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20071:                                                              $newprivs{'c'},$confname,$dom);
20072:                     if ($result ne 'ok') {
20073:                         $errors .= '<li><span class="LC_error">'.
20074:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
20075:                                        $rolename,$result).'</span></li>';
20076:                     } else {
20077:                         $changedprivs{$rolename} = \%newprivs;
20078:                     }
20079:                     last;
20080:                 }
20081:             }
20082:             if (ref($current{'adhoc'}) eq 'HASH') {
20083:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20084:                     %curr = %{$current{'adhoc'}{$rolename}};
20085:                 }
20086:             }
20087:         }
20088:         my $newpos = $env{'form.'.$prefix.'_pos'};
20089:         $newpos =~ s/\D+//g;
20090:         $allpos[$newpos] = $rolename;
20091:         my $newdesc = $env{'form.'.$prefix.'_desc'};
20092:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
20093:         if ($curr{'desc'}) {
20094:             if ($curr{'desc'} ne $newdesc) {
20095:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
20096:                 $newsettings{$rolename}{'desc'} = $newdesc;
20097:             }
20098:         } elsif ($newdesc ne '') {
20099:             $changes{'customrole'}{$rolename}{'desc'} = 1;
20100:             $newsettings{$rolename}{'desc'} = $newdesc;
20101:         }
20102:         my $access = $env{'form.'.$prefix.'_access'};
20103:         if (grep(/^\Q$access\E$/,@accesstypes)) {
20104:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
20105:             if ($access eq 'status') {
20106:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
20107:                 if (scalar(@statuses) == 0) {
20108:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
20109:                 } else {
20110:                     my (@shownstatus,$numtypes);
20111:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20112:                     if (ref($types) eq 'ARRAY') {
20113:                         $numtypes = scalar(@{$types});
20114:                         foreach my $type (sort(@statuses)) {
20115:                             if ($type eq 'default') {
20116:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20117:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
20118:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20119:                                 push(@shownstatus,$usertypes->{$type});
20120:                             }
20121:                         }
20122:                     }
20123:                     if (grep(/^default$/,@statuses)) {
20124:                         push(@shownstatus,$othertitle);
20125:                     }
20126:                     if (scalar(@shownstatus) == 1+$numtypes) {
20127:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
20128:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
20129:                     } else {
20130:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
20131:                         if (ref($curr{'status'}) eq 'ARRAY') {
20132:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20133:                             if (@diffs) {
20134:                                 $changes{'customrole'}{$rolename}{$access} = 1;
20135:                             }
20136:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20137:                             $changes{'customrole'}{$rolename}{$access} = 1;
20138:                         }
20139:                     }
20140:                 }
20141:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
20142:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
20143:                 my @newspecstaff;
20144:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20145:                 foreach my $person (sort(@personnel)) {
20146:                     if ($domhelpdesk{$person}) {
20147:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
20148:                     }
20149:                 }
20150:                 if (ref($curr{$access}) eq 'ARRAY') {
20151:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20152:                     if (@diffs) {
20153:                         $changes{'customrole'}{$rolename}{$access} = 1;
20154:                     }
20155:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20156:                     $changes{'customrole'}{$rolename}{$access} = 1;
20157:                 }
20158:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20159:                     my ($uname,$udom) = split(/:/,$person);
20160:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
20161:                 }
20162:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
20163:             }
20164:         } else {
20165:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
20166:         }
20167:         unless ($curr{'access'} eq $access) {
20168:             $changes{'customrole'}{$rolename}{'access'} = 1;
20169:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
20170:         }
20171:     }
20172:     if (@allpos > 0) {
20173:         my $idx = 0;
20174:         foreach my $rolename (@allpos) {
20175:             if ($rolename ne '') {
20176:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
20177:                 if (ref($current{'adhoc'}) eq 'HASH') {
20178:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20179:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
20180:                             $changes{'customrole'}{$rolename}{'order'} = 1;
20181:                             $newsettings{$rolename}{'order'} = $idx+1;
20182:                         }
20183:                     }
20184:                 }
20185:                 $idx ++;
20186:             }
20187:         }
20188:     }
20189:     my $putresult;
20190:     if (keys(%changes) > 0) {
20191:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
20192:         if ($putresult eq 'ok') {
20193:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
20194:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
20195:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
20196:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
20197:                 }
20198:             }
20199:             my $cachetime = 24*60*60;
20200:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20201:             if (ref($lastactref) eq 'HASH') {
20202:                 $lastactref->{'domdefaults'} = 1;
20203:             }
20204:         } else {
20205:             $errors .= '<li><span class="LC_error">'.
20206:                        &mt('An error occurred storing the settings: [_1]',
20207:                            $putresult).'</span></li>';
20208:         }
20209:     }
20210:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
20211:         $resulttext = &mt('Changes made:').'<ul>';
20212:         my (%shownprivs,@levelorder);
20213:         @levelorder = ('c','d','s');
20214:         if ((keys(%changes)) && ($putresult eq 'ok')) {
20215:             foreach my $item (sort(keys(%changes))) {
20216:                 if ($item eq 'submitbugs') {
20217:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
20218:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
20219:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
20220:                 } elsif ($item eq 'customrole') {
20221:                     if (ref($changes{'customrole'}) eq 'HASH') {
20222:                         my @keyorder = ('order','desc','access','status','exc','inc');
20223:                         my %keytext = &Apache::lonlocal::texthash(
20224:                                                                    order  => 'Order',
20225:                                                                    desc   => 'Role description',
20226:                                                                    access => 'Role usage',
20227:                                                                    status => 'Allowed institutional types',
20228:                                                                    exc    => 'Allowed personnel',
20229:                                                                    inc    => 'Disallowed personnel',
20230:                         );
20231:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
20232:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
20233:                                 if ($role eq $newrole) {
20234:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
20235:                                                               $role).'<ul>';
20236:                                 } else {
20237:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20238:                                                               $role).'<ul>';
20239:                                 }
20240:                                 foreach my $key (@keyorder) {
20241:                                     if ($changes{'customrole'}{$role}{$key}) {
20242:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
20243:                                                                   $keytext{$key},$newsettings{$role}{$key}).
20244:                                                        '</li>';
20245:                                     }
20246:                                 }
20247:                                 if (ref($changedprivs{$role}) eq 'HASH') {
20248:                                     $shownprivs{$role} = 1;
20249:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
20250:                                     foreach my $level (@levelorder) {
20251:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20252:                                             next if ($item eq '');
20253:                                             my ($priv) = split(/\&/,$item,2);
20254:                                             if (&Apache::lonnet::plaintext($priv)) {
20255:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20256:                                                 unless ($level eq 'c') {
20257:                                                     $resulttext .= ' ('.$lt{$level}.')';
20258:                                                 }
20259:                                                 $resulttext .= '</li>';
20260:                                             }
20261:                                         }
20262:                                     }
20263:                                     $resulttext .= '</ul>';
20264:                                 }
20265:                                 $resulttext .= '</ul></li>';
20266:                             }
20267:                         }
20268:                     }
20269:                 }
20270:             }
20271:         }
20272:         if (keys(%changedprivs)) {
20273:             foreach my $role (sort(keys(%changedprivs))) {
20274:                 unless ($shownprivs{$role}) {
20275:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20276:                                               $role).'<ul>'.
20277:                                    '<li>'.&mt('Privileges set to :').'<ul>';
20278:                     foreach my $level (@levelorder) {
20279:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20280:                             next if ($item eq '');
20281:                             my ($priv) = split(/\&/,$item,2);
20282:                             if (&Apache::lonnet::plaintext($priv)) {
20283:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20284:                                 unless ($level eq 'c') {
20285:                                     $resulttext .= ' ('.$lt{$level}.')';
20286:                                 }
20287:                                 $resulttext .= '</li>';
20288:                             }
20289:                         }
20290:                     }
20291:                     $resulttext .= '</ul></li></ul></li>';
20292:                 }
20293:             }
20294:         }
20295:         $resulttext .= '</ul>';
20296:     } else {
20297:         $resulttext = &mt('No changes made to help settings');
20298:     }
20299:     if ($errors) {
20300:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
20301:                                     $errors.'</ul>';
20302:     }
20303:     return $resulttext;
20304: }
20305: 
20306: sub modify_coursedefaults {
20307:     my ($dom,$lastactref,%domconfig) = @_;
20308:     my ($resulttext,$errors,%changes,%defaultshash);
20309:     my %defaultchecked = (
20310:                            'canuse_pdfforms' => 'off',
20311:                            'uselcmath'       => 'on',
20312:                            'usejsme'         => 'on',
20313:                            'inline_chem'     => 'on',
20314:                          );
20315:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
20316:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
20317:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
20318:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
20319:                    'mysqltables_textbook','mysqltables_placement');
20320:     my @types = ('official','unofficial','community','textbook','placement');
20321:     my %staticdefaults = (
20322:                            anonsurvey_threshold => 10,
20323:                            uploadquota          => 500,
20324:                            postsubmit           => 60,
20325:                            mysqltables          => 172800,
20326:                          );
20327:     my %texoptions = (
20328:                         MathJax  => 'MathJax',
20329:                         mimetex  => &mt('Convert to Images'),
20330:                         tth      => &mt('TeX to HTML'),
20331:                      );
20332:     $defaultshash{'coursedefaults'} = {};
20333: 
20334:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
20335:         if ($domconfig{'coursedefaults'} eq '') {
20336:             $domconfig{'coursedefaults'} = {};
20337:         }
20338:     }
20339: 
20340:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
20341:         foreach my $item (@toggles) {
20342:             if ($defaultchecked{$item} eq 'on') {
20343:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20344:                     ($env{'form.'.$item} eq '0')) {
20345:                     $changes{$item} = 1;
20346:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20347:                     $changes{$item} = 1;
20348:                 }
20349:             } elsif ($defaultchecked{$item} eq 'off') {
20350:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20351:                     ($env{'form.'.$item} eq '1')) {
20352:                     $changes{$item} = 1;
20353:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20354:                     $changes{$item} = 1;
20355:                 }
20356:             }
20357:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
20358:         }
20359:         foreach my $item (@numbers) {
20360:             my ($currdef,$newdef);
20361:             $newdef = $env{'form.'.$item};
20362:             if ($item eq 'anonsurvey_threshold') {
20363:                 $currdef = $domconfig{'coursedefaults'}{$item};
20364:                 $newdef =~ s/\D//g;
20365:                 if ($newdef eq '' || $newdef < 1) {
20366:                     $newdef = 1;
20367:                 }
20368:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
20369:             } else {
20370:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
20371:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
20372:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
20373:                 }
20374:                 $newdef =~ s/[^\w.\-]//g;
20375:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
20376:             }
20377:             if ($currdef ne $newdef) {
20378:                 if ($item eq 'anonsurvey_threshold') {
20379:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
20380:                         $changes{$item} = 1;
20381:                     }
20382:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
20383:                     my $setting = $1;
20384:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
20385:                         $changes{$setting} = 1;
20386:                     }
20387:                 }
20388:             }
20389:         }
20390:         my $texengine;
20391:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
20392:             $texengine = $env{'form.texengine'};
20393:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
20394:             if ($currdef eq '') {
20395:                 unless ($texengine eq $Apache::lonnet::deftex) {
20396:                     $changes{'texengine'} = 1;
20397:                 }
20398:             } elsif ($currdef ne $texengine) {
20399:                 $changes{'texengine'} = 1;
20400:             }
20401:         }
20402:         if ($texengine ne '') {
20403:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
20404:         }
20405:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
20406:         my @currclonecode;
20407:         if (ref($currclone) eq 'HASH') {
20408:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
20409:                 @currclonecode = @{$currclone->{'instcode'}};
20410:             }
20411:         }
20412:         my $newclone;
20413:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
20414:             $newclone = $env{'form.canclone'};
20415:         }
20416:         if ($newclone eq 'instcode') {
20417:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
20418:             my (%codedefaults,@code_order,@clonecode);
20419:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
20420:                                                     \@code_order);
20421:             foreach my $item (@code_order) {
20422:                 if (grep(/^\Q$item\E$/,@newcodes)) {
20423:                     push(@clonecode,$item);
20424:                 }
20425:             }
20426:             if (@clonecode) {
20427:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
20428:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
20429:                 if (@diffs) {
20430:                     $changes{'canclone'} = 1;
20431:                 }
20432:             } else {
20433:                 $newclone eq '';
20434:             }
20435:         } elsif ($newclone ne '') {
20436:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
20437:         }
20438:         if ($newclone ne $currclone) {
20439:             $changes{'canclone'} = 1;
20440:         }
20441:         my %credits;
20442:         foreach my $type (@types) {
20443:             unless ($type eq 'community') {
20444:                 $credits{$type} = $env{'form.'.$type.'_credits'};
20445:                 $credits{$type} =~ s/[^\d.]+//g;
20446:             }
20447:         }
20448:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
20449:             ($env{'form.coursecredits'} eq '1')) {
20450:             $changes{'coursecredits'} = 1;
20451:             foreach my $type (keys(%credits)) {
20452:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20453:             }
20454:         } else {
20455:             if ($env{'form.coursecredits'} eq '1') {
20456:                 foreach my $type (@types) {
20457:                     unless ($type eq 'community') {
20458:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
20459:                             $changes{'coursecredits'} = 1;
20460:                         }
20461:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20462:                     }
20463:                 }
20464:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20465:                 foreach my $type (@types) {
20466:                     unless ($type eq 'community') {
20467:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
20468:                             $changes{'coursecredits'} = 1;
20469:                             last;
20470:                         }
20471:                     }
20472:                 }
20473:             }
20474:         }
20475:         if ($env{'form.postsubmit'} eq '1') {
20476:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
20477:             my %currtimeout;
20478:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20479:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
20480:                     $changes{'postsubmit'} = 1;
20481:                 }
20482:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20483:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
20484:                 }
20485:             } else {
20486:                 $changes{'postsubmit'} = 1;
20487:             }
20488:             foreach my $type (@types) {
20489:                 my $timeout = $env{'form.'.$type.'_timeout'};
20490:                 $timeout =~ s/\D//g;
20491:                 if ($timeout == $staticdefaults{'postsubmit'}) {
20492:                     $timeout = '';
20493:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
20494:                     $timeout = '0';
20495:                 }
20496:                 unless ($timeout eq '') {
20497:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
20498:                 }
20499:                 if (exists($currtimeout{$type})) {
20500:                     if ($timeout ne $currtimeout{$type}) {
20501:                         $changes{'postsubmit'} = 1;
20502:                     }
20503:                 } elsif ($timeout ne '') {
20504:                     $changes{'postsubmit'} = 1;
20505:                 }
20506:             }
20507:         } else {
20508:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
20509:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20510:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
20511:                     $changes{'postsubmit'} = 1;
20512:                 }
20513:             } else {
20514:                 $changes{'postsubmit'} = 1;
20515:             }
20516:         }
20517:     }
20518:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20519:                                              $dom);
20520:     if ($putresult eq 'ok') {
20521:         if (keys(%changes) > 0) {
20522:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20523:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
20524:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
20525:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
20526:                 ($changes{'inline_chem'})) {
20527:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine') {
20528:                     if ($changes{$item}) {
20529:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
20530:                     }
20531:                 }
20532:                 if ($changes{'coursecredits'}) {
20533:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20534:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
20535:                             $domdefaults{$type.'credits'} =
20536:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
20537:                         }
20538:                     }
20539:                 }
20540:                 if ($changes{'postsubmit'}) {
20541:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20542:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
20543:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20544:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
20545:                                 $domdefaults{$type.'postsubtimeout'} =
20546:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20547:                             }
20548:                         }
20549:                     }
20550:                 }
20551:                 if ($changes{'uploadquota'}) {
20552:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20553:                         foreach my $type (@types) {
20554:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
20555:                         }
20556:                     }
20557:                 }
20558:                 if ($changes{'canclone'}) {
20559:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20560:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20561:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
20562:                             if (@clonecodes) {
20563:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
20564:                             }
20565:                         }
20566:                     } else {
20567:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
20568:                     }
20569:                 }
20570:                 my $cachetime = 24*60*60;
20571:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20572:                 if (ref($lastactref) eq 'HASH') {
20573:                     $lastactref->{'domdefaults'} = 1;
20574:                 }
20575:             }
20576:             $resulttext = &mt('Changes made:').'<ul>';
20577:             foreach my $item (sort(keys(%changes))) {
20578:                 if ($item eq 'canuse_pdfforms') {
20579:                     if ($env{'form.'.$item} eq '1') {
20580:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
20581:                     } else {
20582:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
20583:                     }
20584:                 } elsif ($item eq 'uselcmath') {
20585:                     if ($env{'form.'.$item} eq '1') {
20586:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
20587:                     } else {
20588:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
20589:                     }
20590:                 } elsif ($item eq 'usejsme') {
20591:                     if ($env{'form.'.$item} eq '1') {
20592:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
20593:                     } else {
20594:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
20595:                     }
20596:                 } elsif ($item eq 'inline_chem') {
20597:                     if ($env{'form.'.$item} eq '1') {
20598:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
20599:                     } else {
20600:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
20601:                     }
20602:                 } elsif ($item eq 'texengine') {
20603:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
20604:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
20605:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
20606:                     }
20607:                 } elsif ($item eq 'anonsurvey_threshold') {
20608:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
20609:                 } elsif ($item eq 'uploadquota') {
20610:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20611:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
20612:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
20613:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
20614:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
20615:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
20616:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
20617:                                        '</ul>'.
20618:                                        '</li>';
20619:                     } else {
20620:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
20621:                     }
20622:                 } elsif ($item eq 'mysqltables') {
20623:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
20624:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
20625:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
20626:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
20627:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
20628:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
20629:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
20630:                                        '</ul>'.
20631:                                        '</li>';
20632:                     } else {
20633:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
20634:                     }
20635:                 } elsif ($item eq 'postsubmit') {
20636:                     if ($domdefaults{'postsubmit'} eq 'off') {
20637:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
20638:                     } else {
20639:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
20640:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20641:                             $resulttext .= &mt('durations:').'<ul>';
20642:                             foreach my $type (@types) {
20643:                                 $resulttext .= '<li>';
20644:                                 my $timeout;
20645:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20646:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20647:                                 }
20648:                                 my $display;
20649:                                 if ($timeout eq '0') {
20650:                                     $display = &mt('unlimited');
20651:                                 } elsif ($timeout eq '') {
20652:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
20653:                                 } else {
20654:                                     $display = &mt('[quant,_1,second]',$timeout);
20655:                                 }
20656:                                 if ($type eq 'community') {
20657:                                     $resulttext .= &mt('Communities');
20658:                                 } elsif ($type eq 'official') {
20659:                                     $resulttext .= &mt('Official courses');
20660:                                 } elsif ($type eq 'unofficial') {
20661:                                     $resulttext .= &mt('Unofficial courses');
20662:                                 } elsif ($type eq 'textbook') {
20663:                                     $resulttext .= &mt('Textbook courses');
20664:                                 } elsif ($type eq 'placement') {
20665:                                     $resulttext .= &mt('Placement tests');
20666:                                 }
20667:                                 $resulttext .= ' -- '.$display.'</li>';
20668:                             }
20669:                             $resulttext .= '</ul>';
20670:                         }
20671:                         $resulttext .= '</li>';
20672:                     }
20673:                 } elsif ($item eq 'coursecredits') {
20674:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20675:                         if (($domdefaults{'officialcredits'} eq '') &&
20676:                             ($domdefaults{'unofficialcredits'} eq '') &&
20677:                             ($domdefaults{'textbookcredits'} eq '')) {
20678:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20679:                         } else {
20680:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
20681:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
20682:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
20683:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
20684:                                            '</ul>'.
20685:                                            '</li>';
20686:                         }
20687:                     } else {
20688:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20689:                     }
20690:                 } elsif ($item eq 'canclone') {
20691:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20692:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20693:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
20694:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
20695:                         }
20696:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
20697:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
20698:                     } else {
20699:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
20700:                     }
20701:                 }
20702:             }
20703:             $resulttext .= '</ul>';
20704:         } else {
20705:             $resulttext = &mt('No changes made to course defaults');
20706:         }
20707:     } else {
20708:         $resulttext = '<span class="LC_error">'.
20709:             &mt('An error occurred: [_1]',$putresult).'</span>';
20710:     }
20711:     return $resulttext;
20712: }
20713: 
20714: sub modify_selfenrollment {
20715:     my ($dom,$lastactref,%domconfig) = @_;
20716:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
20717:     my @types = ('official','unofficial','community','textbook','placement');
20718:     my %titles = &tool_titles();
20719:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
20720:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
20721:     $ordered{'default'} = ['types','registered','approval','limit'];
20722: 
20723:     my (%roles,%shown,%toplevel);
20724:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
20725: 
20726:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
20727:         if ($domconfig{'selfenrollment'} eq '') {
20728:             $domconfig{'selfenrollment'} = {};
20729:         }
20730:     }
20731:     %toplevel = (
20732:                   admin      => 'Configuration Rights',
20733:                   default    => 'Default settings',
20734:                   validation => 'Validation of self-enrollment requests',
20735:                 );
20736:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
20737: 
20738:     if (ref($ordered{'admin'}) eq 'ARRAY') {
20739:         foreach my $item (@{$ordered{'admin'}}) {
20740:             foreach my $type (@types) {
20741:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
20742:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
20743:                 } else {
20744:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
20745:                 }
20746:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
20747:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
20748:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
20749:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
20750:                             push(@{$changes{'admin'}{$type}},$item);
20751:                         }
20752:                     } else {
20753:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
20754:                             push(@{$changes{'admin'}{$type}},$item);
20755:                         }
20756:                     }
20757:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
20758:                     push(@{$changes{'admin'}{$type}},$item);
20759:                 }
20760:             }
20761:         }
20762:     }
20763: 
20764:     foreach my $item (@{$ordered{'default'}}) {
20765:         foreach my $type (@types) {
20766:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
20767:             if ($item eq 'types') {
20768:                 unless (($value eq 'all') || ($value eq 'dom')) {
20769:                     $value = '';
20770:                 }
20771:             } elsif ($item eq 'registered') {
20772:                 unless ($value eq '1') {
20773:                     $value = 0;
20774:                 }
20775:             } elsif ($item eq 'approval') {
20776:                 unless ($value =~ /^[012]$/) {
20777:                     $value = 0;
20778:                 }
20779:             } else {
20780:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20781:                     $value = 'none';
20782:                 }
20783:             }
20784:             $selfenrollhash{'default'}{$type}{$item} = $value;
20785:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
20786:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20787:                     if ($selfenrollhash{'default'}{$type}{$item} ne
20788:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
20789:                          push(@{$changes{'default'}{$type}},$item);
20790:                     }
20791:                 } else {
20792:                     push(@{$changes{'default'}{$type}},$item);
20793:                 }
20794:             } else {
20795:                 push(@{$changes{'default'}{$type}},$item);
20796:             }
20797:             if ($item eq 'limit') {
20798:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20799:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
20800:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
20801:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
20802:                     }
20803:                 } else {
20804:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
20805:                 }
20806:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20807:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
20808:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
20809:                          push(@{$changes{'default'}{$type}},'cap');
20810:                     }
20811:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
20812:                     push(@{$changes{'default'}{$type}},'cap');
20813:                 }
20814:             }
20815:         }
20816:     }
20817: 
20818:     foreach my $item (@{$itemsref}) {
20819:         if ($item eq 'fields') {
20820:             my @changed;
20821:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
20822:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
20823:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
20824:             }
20825:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20826:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
20827:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
20828:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
20829:                 } else {
20830:                     @changed = @{$selfenrollhash{'validation'}{$item}};
20831:                 }
20832:             } else {
20833:                 @changed = @{$selfenrollhash{'validation'}{$item}};
20834:             }
20835:             if (@changed) {
20836:                 if ($selfenrollhash{'validation'}{$item}) { 
20837:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
20838:                 } else {
20839:                     $changes{'validation'}{$item} = &mt('None');
20840:                 }
20841:             }
20842:         } else {
20843:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
20844:             if ($item eq 'markup') {
20845:                if ($env{'form.selfenroll_validation_'.$item}) {
20846:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
20847:                }
20848:             }
20849:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20850:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
20851:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
20852:                 }
20853:             }
20854:         }
20855:     }
20856: 
20857:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
20858:                                              $dom);
20859:     if ($putresult eq 'ok') {
20860:         if (keys(%changes) > 0) {
20861:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20862:             $resulttext = &mt('Changes made:').'<ul>';
20863:             foreach my $key ('admin','default','validation') {
20864:                 if (ref($changes{$key}) eq 'HASH') {
20865:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
20866:                     if ($key eq 'validation') {
20867:                         foreach my $item (@{$itemsref}) {
20868:                             if (exists($changes{$key}{$item})) {
20869:                                 if ($item eq 'markup') {
20870:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20871:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
20872:                                 } else {  
20873:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20874:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
20875:                                 }
20876:                             }
20877:                         }
20878:                     } else {
20879:                         foreach my $type (@types) {
20880:                             if ($type eq 'community') {
20881:                                 $roles{'1'} = &mt('Community personnel');
20882:                             } else {
20883:                                 $roles{'1'} = &mt('Course personnel');
20884:                             }
20885:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
20886:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
20887:                                     if ($key eq 'admin') {
20888:                                         my @mgrdc = ();
20889:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20890:                                             foreach my $item (@{$ordered{'admin'}}) {
20891:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
20892:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
20893:                                                         push(@mgrdc,$item);
20894:                                                     }
20895:                                                 }
20896:                                             }
20897:                                             if (@mgrdc) {
20898:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
20899:                                             } else {
20900:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
20901:                                             }
20902:                                         }
20903:                                     } else {
20904:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20905:                                             foreach my $item (@{$ordered{$key}}) {
20906:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20907:                                                     $domdefaults{$type.'selfenroll'.$item} =
20908:                                                         $selfenrollhash{$key}{$type}{$item};
20909:                                                 }
20910:                                             }
20911:                                         }
20912:                                     }
20913:                                 }
20914:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
20915:                                 foreach my $item (@{$ordered{$key}}) {
20916:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20917:                                         $resulttext .= '<li>';
20918:                                         if ($key eq 'admin') {
20919:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
20920:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
20921:                                         } else {
20922:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
20923:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
20924:                                         }
20925:                                         $resulttext .= '</li>';
20926:                                     }
20927:                                 }
20928:                                 $resulttext .= '</ul></li>';
20929:                             }
20930:                         }
20931:                         $resulttext .= '</ul></li>'; 
20932:                     }
20933:                 }
20934:             }
20935:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
20936:                 my $cachetime = 24*60*60;
20937:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20938:                 if (ref($lastactref) eq 'HASH') {
20939:                     $lastactref->{'domdefaults'} = 1;
20940:                 }
20941:             }
20942:             $resulttext .= '</ul>';
20943:         } else {
20944:             $resulttext = &mt('No changes made to self-enrollment settings');
20945:         }
20946:     } else {
20947:         $resulttext = '<span class="LC_error">'.
20948:             &mt('An error occurred: [_1]',$putresult).'</span>';
20949:     }
20950:     return $resulttext;
20951: }
20952: 
20953: sub modify_wafproxy {
20954:     my ($dom,$action,$lastactref,%domconfig) = @_;
20955:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20956:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
20957:         %wafproxy,%changes,%expirecache,%expiresaml);
20958:     foreach my $server (sort(keys(%servers))) {
20959:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20960:         if ($serverhome eq $server) {
20961:             my $serverdom = &Apache::lonnet::host_domain($server);
20962:             if ($serverdom eq $dom) {
20963:                 $canset{$server} = 1;
20964:             }
20965:         }
20966:     }
20967:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
20968:         %{$values{$dom}} = ();
20969:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
20970:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
20971:         }
20972:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
20973:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
20974:         }
20975:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
20976:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
20977:         }
20978:     }
20979:     my $output;
20980:     if (keys(%canset)) {
20981:         %{$wafproxy{'alias'}} = ();
20982:         %{$wafproxy{'saml'}} = ();
20983:         foreach my $key (sort(keys(%canset))) {
20984:             if ($env{'form.wafproxy_'.$dom}) {
20985:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
20986:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
20987:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
20988:                     $changes{'alias'} = 1;
20989:                 }
20990:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
20991:                     $wafproxy{'saml'}{$key} = 1;
20992:                 }
20993:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
20994:                     $changes{'saml'} = 1;
20995:                 }
20996:             } else {
20997:                 $wafproxy{'alias'}{$key} = '';
20998:                 $wafproxy{'saml'}{$key} = '';
20999:                 if ($curralias{$key}) {
21000:                     $changes{'alias'} = 1;
21001:                 }
21002:                 if ($currsaml{$key}) {
21003:                     $changes{'saml'} = 1;
21004:                 }
21005:             }
21006:             if ($wafproxy{'alias'}{$key} eq '') {
21007:                 if ($curralias{$key}) {
21008:                     $expirecache{$key} = 1;
21009:                 }
21010:                 delete($wafproxy{'alias'}{$key});
21011:             }
21012:             if ($wafproxy{'saml'}{$key} eq '') {
21013:                 if ($currsaml{$key}) {
21014:                     $expiresaml{$key} = 1;
21015:                 }
21016:                 delete($wafproxy{'saml'}{$key});
21017:             }
21018:         }
21019:         unless (keys(%{$wafproxy{'alias'}})) {
21020:             delete($wafproxy{'alias'});
21021:         }
21022:         unless (keys(%{$wafproxy{'saml'}})) {
21023:             delete($wafproxy{'saml'});
21024:         }
21025:         # Localization for values in %warn occurs in &mt() calls separately.
21026:         my %warn = (
21027:                      trusted => 'trusted IP range(s)',
21028:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
21029:                      vpnext => 'IP range(s) for backend WAF connections',
21030:                    );
21031:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21032:             my $possible = $env{'form.wafproxy_'.$item};
21033:             $possible =~ s/^\s+|\s+$//g;
21034:             if ($possible ne '') {
21035:                 if ($item eq 'remoteip') {
21036:                     if ($possible =~ /^[mhn]$/) {
21037:                         $wafproxy{$item} = $possible;
21038:                     }
21039:                 } elsif ($item eq 'ipheader') {
21040:                     if ($wafproxy{'remoteip'} eq 'h') {
21041:                         $wafproxy{$item} = $possible;
21042:                     }
21043:                 } elsif ($item eq 'sslopt') {
21044:                     if ($possible =~ /^0|1$/) {
21045:                         $wafproxy{$item} = $possible;
21046:                     }
21047:                 } else {
21048:                     my (@ok,$count);
21049:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
21050:                         unless ($env{'form.wafproxy_vpnaccess'}) {
21051:                             $possible = '';
21052:                         }
21053:                     } elsif ($item eq 'trusted') {
21054:                         unless ($wafproxy{'remoteip'} eq 'h') {
21055:                             $possible = '';
21056:                         }
21057:                     }
21058:                     unless ($possible eq '') {
21059:                         $possible =~ s/[\r\n]+/\s/g;
21060:                         $possible =~ s/\s*-\s*/-/g;
21061:                         $possible =~ s/\s+/,/g;
21062:                         $possible =~ s/,+/,/g;
21063:                     }
21064:                     $count = 0;
21065:                     if ($possible ne '') {
21066:                         foreach my $poss (split(/\,/,$possible)) {
21067:                             $count ++;
21068:                             $poss = &validate_ip_pattern($poss);
21069:                             if ($poss ne '') {
21070:                                 push(@ok,$poss);
21071:                             }
21072:                         }
21073:                         my $diff = $count - scalar(@ok);
21074:                         if ($diff) {
21075:                             push(@warnings,'<li>'.
21076:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
21077:                                      $diff,$warn{$item}).
21078:                                  '</li>');
21079:                         }
21080:                         if (@ok) {
21081:                             my @cidr_list;
21082:                             foreach my $item (@ok) {
21083:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
21084:                             }
21085:                             $wafproxy{$item} = join(',',@cidr_list);
21086:                         }
21087:                     }
21088:                 }
21089:                 if ($wafproxy{$item} ne $currvalue{$item}) {
21090:                     $changes{$item} = 1;
21091:                 }
21092:             } elsif ($currvalue{$item}) {
21093:                 $changes{$item} = 1;
21094:             } 
21095:         }
21096:     } else {
21097:         if (keys(%curralias)) {
21098:             $changes{'alias'} = 1;
21099:         }
21100:         if (keys(%currsaml)) {
21101:             $changes{'saml'} = 1;
21102:         } 
21103:         if (keys(%currvalue)) {
21104:             foreach my $key (keys(%currvalue)) {
21105:                 $changes{$key} = 1;
21106:             }
21107:         }
21108:     }
21109:     if (keys(%changes)) {
21110:         my %defaultshash = (
21111:                               wafproxy => \%wafproxy,
21112:                            ); 
21113:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21114:                                                  $dom);
21115:         if ($putresult eq 'ok') {
21116:             my $cachetime = 24*60*60;
21117:             my (%domdefaults,$updatedomdefs);
21118:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
21119:                 if ($changes{$item}) {
21120:                     unless ($updatedomdefs) {
21121:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
21122:                         $updatedomdefs = 1;
21123:                     }
21124:                     if ($wafproxy{$item}) {
21125:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
21126:                     } elsif (exists($domdefaults{'waf_'.$item})) {
21127:                         delete($domdefaults{'waf_'.$item});
21128:                     } 
21129:                 }
21130:             }
21131:             if ($updatedomdefs) {
21132:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21133:                 if (ref($lastactref) eq 'HASH') {
21134:                     $lastactref->{'domdefaults'} = 1;
21135:                 }
21136:             }
21137:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
21138:                 my %updates = %expirecache;
21139:                 foreach my $key (keys(%expirecache)) {
21140:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
21141:                 }
21142:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
21143:                     my $cachetime = 24*60*60;
21144:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
21145:                         $updates{$key} = 1;
21146:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
21147:                                                       $cachetime);
21148:                     }
21149:                 }
21150:                 if (ref($lastactref) eq 'HASH') {
21151:                     $lastactref->{'proxyalias'} = \%updates;
21152:                 }
21153:             }
21154:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
21155:                 my %samlupdates = %expiresaml;
21156:                 foreach my $key (keys(%expiresaml)) {
21157:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
21158:                 }
21159:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
21160:                     my $cachetime = 24*60*60;
21161:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
21162:                         $samlupdates{$key} = 1;
21163:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
21164:                                                       $cachetime);
21165:                     }
21166:                 }
21167:                 if (ref($lastactref) eq 'HASH') {
21168:                     $lastactref->{'proxysaml'} = \%samlupdates;
21169:                 }
21170:             }
21171:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
21172:             foreach my $item ('alias','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21173:                 if ($changes{$item}) {
21174:                     if ($item eq 'alias') {
21175:                         my $numaliased = 0;
21176:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
21177:                             my $shown;
21178:                             if (keys(%{$wafproxy{'alias'}})) {
21179:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
21180:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
21181:                                                          &Apache::lonnet::hostname($server),
21182:                                                          $wafproxy{'alias'}{$server}).'</li>';
21183:                                     $numaliased ++;
21184:                                 }
21185:                                 if ($numaliased) {
21186:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
21187:                                                           '<ul>'.$shown.'</ul>').'</li>';
21188:                                 }
21189:                             }
21190:                         }
21191:                         unless ($numaliased) {
21192:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
21193:                         }
21194:                     } elsif ($item eq 'saml') {
21195:                         my $shown; 
21196:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
21197:                             if (keys(%{$wafproxy{'saml'}})) {
21198:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
21199:                             }
21200:                         }
21201:                         if ($shown) {
21202:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
21203:                                                   $shown).'</li>';
21204:                         } else {
21205:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
21206:                         }
21207:                     } else {
21208:                         if ($item eq 'remoteip') {
21209:                             my %ip_methods = &remoteip_methods();
21210:                             if ($wafproxy{$item} =~ /^[mh]$/) {
21211:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
21212:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
21213:                             } else {
21214:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
21215:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
21216:                                                '</li>';
21217:                                 } else {
21218:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
21219:                                 }
21220:                             }
21221:                         } elsif ($item eq 'ipheader') {
21222:                             if ($wafproxy{$item}) {
21223:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
21224:                                                       $wafproxy{$item}).'</li>';
21225:                             } else {
21226:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
21227:                             }
21228:                         } elsif ($item eq 'trusted') {
21229:                             if ($wafproxy{$item}) {
21230:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
21231:                                                       $wafproxy{$item}).'</li>';
21232:                             } else {
21233:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
21234:                             }
21235:                         } elsif ($item eq 'vpnint') {
21236:                             if ($wafproxy{$item}) {
21237:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
21238:                                                        $wafproxy{$item}).'</li>';
21239:                             } else {
21240:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
21241:                             }
21242:                         } elsif ($item eq 'vpnext') {
21243:                             if ($wafproxy{$item}) {
21244:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
21245:                                                        $wafproxy{$item}).'</li>';
21246:                             } else {
21247:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
21248:                             }
21249:                         } elsif ($item eq 'sslopt') {
21250:                             if ($wafproxy{$item}) {
21251:                                 $output .= '<li>'.&mt('WAF/Reverse Proxy expected to forward requests to https on LON-CAPA node, regardless of original protocol in web browser (http or https).').'</li>';
21252:                             } else {
21253:                                 $output .= '<li>'.&mt('WAF/Reverse Proxy expected to preserve original protocol in web browser (either http or https) when forwarding to LON-CAPA node.').'</li>';
21254:                             }
21255:                         }
21256:                     }
21257:                 }
21258:             }
21259:         } else {
21260:             $output = '<span class="LC_error">'.
21261:                       &mt('An error occurred: [_1]',$putresult).'</span>';
21262:         }
21263:     } elsif (keys(%canset)) {
21264:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
21265:     }
21266:     if (@warnings) {
21267:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
21268:                        join("\n",@warnings).'</ul>';
21269:     }
21270:     return $output;
21271: }
21272: 
21273: sub validate_ip_pattern {
21274:     my ($pattern) = @_;
21275:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
21276:         my ($start,$end) = ($1,$2);
21277:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
21278:             if (($start !~ m{/}) && ($end !~ m{/})) {
21279:                 return $start.'-'.$end;
21280:             }
21281:         }
21282:     } elsif ($pattern ne '') {
21283:         $pattern = &Net::CIDR::cidrvalidate($pattern);
21284:         if ($pattern ne '') {
21285:             return $pattern;
21286:         }
21287:     }
21288:     return;
21289: }
21290: 
21291: sub modify_usersessions {
21292:     my ($dom,$lastactref,%domconfig) = @_;
21293:     my @hostingtypes = ('version','excludedomain','includedomain');
21294:     my @offloadtypes = ('primary','default');
21295:     my %types = (
21296:                   remote => \@hostingtypes,
21297:                   hosted => \@hostingtypes,
21298:                   spares => \@offloadtypes,
21299:                 );
21300:     my @prefixes = ('remote','hosted','spares');
21301:     my @lcversions = &Apache::lonnet::all_loncaparevs();
21302:     my (%by_ip,%by_location,@intdoms,@instdoms);
21303:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21304:     my @locations = sort(keys(%by_location));
21305:     my (%defaultshash,%changes);
21306:     foreach my $prefix (@prefixes) {
21307:         $defaultshash{'usersessions'}{$prefix} = {};
21308:     }
21309:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21310:     my $resulttext;
21311:     my %iphost = &Apache::lonnet::get_iphost();
21312:     foreach my $prefix (@prefixes) {
21313:         next if ($prefix eq 'spares');
21314:         foreach my $type (@{$types{$prefix}}) {
21315:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21316:             if ($type eq 'version') {
21317:                 my $value = $env{'form.'.$prefix.'_'.$type};
21318:                 my $okvalue;
21319:                 if ($value ne '') {
21320:                     if (grep(/^\Q$value\E$/,@lcversions)) {
21321:                         $okvalue = $value;
21322:                     }
21323:                 }
21324:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21325:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21326:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
21327:                             if ($inuse == 0) {
21328:                                 $changes{$prefix}{$type} = 1;
21329:                             } else {
21330:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
21331:                                     $changes{$prefix}{$type} = 1;
21332:                                 }
21333:                                 if ($okvalue ne '') {
21334:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21335:                                 } 
21336:                             }
21337:                         } else {
21338:                             if (($inuse == 1) && ($okvalue ne '')) {
21339:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21340:                                 $changes{$prefix}{$type} = 1;
21341:                             }
21342:                         }
21343:                     } else {
21344:                         if (($inuse == 1) && ($okvalue ne '')) {
21345:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21346:                             $changes{$prefix}{$type} = 1;
21347:                         }
21348:                     }
21349:                 } else {
21350:                     if (($inuse == 1) && ($okvalue ne '')) {
21351:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21352:                         $changes{$prefix}{$type} = 1;
21353:                     }
21354:                 }
21355:             } else {
21356:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21357:                 my @okvals;
21358:                 foreach my $val (@vals) {
21359:                     if ($val =~ /:/) {
21360:                         my @items = split(/:/,$val);
21361:                         foreach my $item (@items) {
21362:                             if (ref($by_location{$item}) eq 'ARRAY') {
21363:                                 push(@okvals,$item);
21364:                             }
21365:                         }
21366:                     } else {
21367:                         if (ref($by_location{$val}) eq 'ARRAY') {
21368:                             push(@okvals,$val);
21369:                         }
21370:                     }
21371:                 }
21372:                 @okvals = sort(@okvals);
21373:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21374:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21375:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21376:                             if ($inuse == 0) {
21377:                                 $changes{$prefix}{$type} = 1; 
21378:                             } else {
21379:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21380:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
21381:                                 if (@changed > 0) {
21382:                                     $changes{$prefix}{$type} = 1;
21383:                                 }
21384:                             }
21385:                         } else {
21386:                             if ($inuse == 1) {
21387:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21388:                                 $changes{$prefix}{$type} = 1;
21389:                             }
21390:                         } 
21391:                     } else {
21392:                         if ($inuse == 1) {
21393:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21394:                             $changes{$prefix}{$type} = 1;
21395:                         }
21396:                     }
21397:                 } else {
21398:                     if ($inuse == 1) {
21399:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21400:                         $changes{$prefix}{$type} = 1;
21401:                     }
21402:                 }
21403:             }
21404:         }
21405:     }
21406: 
21407:     my @alldoms = &Apache::lonnet::all_domains();
21408:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21409:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
21410:     my $savespares;
21411: 
21412:     foreach my $lonhost (sort(keys(%servers))) {
21413:         my $serverhomeID =
21414:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
21415:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
21416:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
21417:         my %spareschg;
21418:         foreach my $type (@{$types{'spares'}}) {
21419:             my @okspares;
21420:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
21421:             foreach my $server (@checked) {
21422:                 if (&Apache::lonnet::hostname($server) ne '') {
21423:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
21424:                         unless (grep(/^\Q$server\E$/,@okspares)) {
21425:                             push(@okspares,$server);
21426:                         }
21427:                     }
21428:                 }
21429:             }
21430:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
21431:             my $newspare;
21432:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
21433:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
21434:                     $newspare = $new;
21435:                 }
21436:             }
21437:             my @spares;
21438:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
21439:                 @spares = sort(@okspares,$newspare);
21440:             } else {
21441:                 @spares = sort(@okspares);
21442:             }
21443:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
21444:             if (ref($spareid{$lonhost}) eq 'HASH') {
21445:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
21446:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
21447:                     if (@diffs > 0) {
21448:                         $spareschg{$type} = 1;
21449:                     }
21450:                 }
21451:             }
21452:         }
21453:         if (keys(%spareschg) > 0) {
21454:             $changes{'spares'}{$lonhost} = \%spareschg;
21455:         }
21456:     }
21457:     $defaultshash{'usersessions'}{'offloadnow'} = {};
21458:     $defaultshash{'usersessions'}{'offloadoth'} = {};
21459:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
21460:     my @okoffload;
21461:     if (@offloadnow) {
21462:         foreach my $server (@offloadnow) {
21463:             if (&Apache::lonnet::hostname($server) ne '') {
21464:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
21465:                     push(@okoffload,$server);
21466:                 }
21467:             }
21468:         }
21469:         if (@okoffload) {
21470:             foreach my $lonhost (@okoffload) {
21471:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
21472:             }
21473:         }
21474:     }
21475:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
21476:     my @okoffloadoth;
21477:     if (@offloadoth) {
21478:         foreach my $server (@offloadoth) {
21479:             if (&Apache::lonnet::hostname($server) ne '') {
21480:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
21481:                     push(@okoffloadoth,$server);
21482:                 }
21483:             }
21484:         }
21485:         if (@okoffloadoth) {
21486:             foreach my $lonhost (@okoffloadoth) {
21487:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
21488:             }
21489:         }
21490:     }
21491:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
21492:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
21493:             if (ref($changes{'spares'}) eq 'HASH') {
21494:                 if (keys(%{$changes{'spares'}}) > 0) {
21495:                     $savespares = 1;
21496:                 }
21497:             }
21498:         } else {
21499:             $savespares = 1;
21500:         }
21501:         foreach my $offload ('offloadnow','offloadoth') {
21502:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
21503:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
21504:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
21505:                         $changes{$offload} = 1;
21506:                         last;
21507:                     }
21508:                 }
21509:                 unless ($changes{$offload}) {
21510:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
21511:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
21512:                             $changes{$offload} = 1;
21513:                             last;
21514:                         }
21515:                     }
21516:                 }
21517:             } else {
21518:                 if (($offload eq 'offloadnow') && (@okoffload)) {
21519:                      $changes{'offloadnow'} = 1;
21520:                 }
21521:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
21522:                     $changes{'offloadoth'} = 1;
21523:                 }
21524:             } 
21525:         }
21526:     } else {
21527:         if (@okoffload) {
21528:             $changes{'offloadnow'} = 1;
21529:         }
21530:         if (@okoffloadoth) {
21531:             $changes{'offloadoth'} = 1;
21532:         }
21533:     }
21534:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
21535:     if ((keys(%changes) > 0) || ($savespares)) {
21536:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21537:                                                  $dom);
21538:         if ($putresult eq 'ok') {
21539:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21540:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
21541:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
21542:                 }
21543:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
21544:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
21545:                 }
21546:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21547:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
21548:                 }
21549:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21550:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
21551:                 }
21552:             }
21553:             my $cachetime = 24*60*60;
21554:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21555:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
21556:             if (ref($lastactref) eq 'HASH') {
21557:                 $lastactref->{'domdefaults'} = 1;
21558:                 $lastactref->{'usersessions'} = 1;
21559:             }
21560:             if (keys(%changes) > 0) {
21561:                 my %lt = &usersession_titles();
21562:                 $resulttext = &mt('Changes made:').'<ul>';
21563:                 foreach my $prefix (@prefixes) {
21564:                     if (ref($changes{$prefix}) eq 'HASH') {
21565:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21566:                         if ($prefix eq 'spares') {
21567:                             if (ref($changes{$prefix}) eq 'HASH') {
21568:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
21569:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
21570:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
21571:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
21572:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
21573:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
21574:                                         foreach my $type (@{$types{$prefix}}) {
21575:                                             if ($changes{$prefix}{$lonhost}{$type}) {
21576:                                                 my $offloadto = &mt('None');
21577:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
21578:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
21579:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
21580:                                                     }
21581:                                                 }
21582:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
21583:                                             }
21584:                                         }
21585:                                     }
21586:                                     $resulttext .= '</li>';
21587:                                 }
21588:                             }
21589:                         } else {
21590:                             foreach my $type (@{$types{$prefix}}) {
21591:                                 if (defined($changes{$prefix}{$type})) {
21592:                                     my ($newvalue,$notinuse);
21593:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21594:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
21595:                                             if ($type eq 'version') {
21596:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
21597:                                             } else {
21598:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21599:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
21600:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
21601:                                                     }
21602:                                                 } else {
21603:                                                     $notinuse = 1;
21604:                                                 }
21605:                                             }
21606:                                         }
21607:                                     }
21608:                                     if ($newvalue eq '') {
21609:                                         if ($type eq 'version') {
21610:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
21611:                                         } elsif ($notinuse) {
21612:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21613:                                         } else {
21614:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21615:                                         }
21616:                                     } else {
21617:                                         if ($type eq 'version') {
21618:                                             $newvalue .= ' '.&mt('(or later)');
21619:                                         }
21620:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21621:                                     }
21622:                                 }
21623:                             }
21624:                         }
21625:                         $resulttext .= '</ul>';
21626:                     }
21627:                 }
21628:                 if ($changes{'offloadnow'}) {
21629:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21630:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
21631:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
21632:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
21633:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21634:                             }
21635:                             $resulttext .= '</ul>';
21636:                         } else {
21637:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
21638:                         }
21639:                     } else {
21640:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
21641:                     }
21642:                 }
21643:                 if ($changes{'offloadoth'}) {
21644:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21645:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
21646:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
21647:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
21648:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21649:                             }
21650:                             $resulttext .= '</ul>';
21651:                         } else {
21652:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
21653:                         }
21654:                     } else {
21655:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
21656:                     }
21657:                 }
21658:                 $resulttext .= '</ul>';
21659:             } else {
21660:                 $resulttext = $nochgmsg;
21661:             }
21662:         } else {
21663:             $resulttext = '<span class="LC_error">'.
21664:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21665:         }
21666:     } else {
21667:         $resulttext = $nochgmsg;
21668:     }
21669:     return $resulttext;
21670: }
21671: 
21672: sub modify_ssl {
21673:     my ($dom,$lastactref,%domconfig) = @_;
21674:     my (%by_ip,%by_location,@intdoms,@instdoms);
21675:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21676:     my @locations = sort(keys(%by_location));
21677:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21678:     my (%defaultshash,%changes);
21679:     my $action = 'ssl';
21680:     my @prefixes = ('connto','connfrom','replication');
21681:     foreach my $prefix (@prefixes) {
21682:         $defaultshash{$action}{$prefix} = {};
21683:     }
21684:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21685:     my $resulttext;
21686:     my %iphost = &Apache::lonnet::get_iphost();
21687:     my @reptypes = ('certreq','nocertreq');
21688:     my @connecttypes = ('dom','intdom','other');
21689:     my %types = (
21690:                   connto      => \@connecttypes,
21691:                   connfrom    => \@connecttypes,
21692:                   replication => \@reptypes,
21693:                 );
21694:     foreach my $prefix (sort(keys(%types))) {
21695:         foreach my $type (@{$types{$prefix}}) {
21696:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21697:                 my $value = 'yes';
21698:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
21699:                     $value = $env{'form.'.$prefix.'_'.$type};
21700:                 }
21701:                 if (ref($domconfig{$action}) eq 'HASH') {
21702:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21703:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
21704:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
21705:                                 $changes{$prefix}{$type} = 1;
21706:                             }
21707:                             $defaultshash{$action}{$prefix}{$type} = $value;
21708:                         } else {
21709:                             $defaultshash{$action}{$prefix}{$type} = $value;
21710:                             $changes{$prefix}{$type} = 1;
21711:                         }
21712:                     } else {
21713:                         $defaultshash{$action}{$prefix}{$type} = $value;
21714:                         $changes{$prefix}{$type} = 1;
21715:                     }
21716:                 } else {
21717:                     $defaultshash{$action}{$prefix}{$type} = $value;
21718:                     $changes{$prefix}{$type} = 1;
21719:                 }
21720:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
21721:                     delete($changes{$prefix}{$type});
21722:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
21723:                     delete($changes{$prefix}{$type});
21724:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
21725:                     delete($changes{$prefix}{$type});
21726:                 }
21727:             } elsif ($prefix eq 'replication') {
21728:                 if (@locations > 0) {
21729:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21730:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21731:                     my @okvals;
21732:                     foreach my $val (@vals) {
21733:                         if ($val =~ /:/) {
21734:                             my @items = split(/:/,$val);
21735:                             foreach my $item (@items) {
21736:                                 if (ref($by_location{$item}) eq 'ARRAY') {
21737:                                     push(@okvals,$item);
21738:                                 }
21739:                             }
21740:                         } else {
21741:                             if (ref($by_location{$val}) eq 'ARRAY') {
21742:                                 push(@okvals,$val);
21743:                             }
21744:                         }
21745:                     }
21746:                     @okvals = sort(@okvals);
21747:                     if (ref($domconfig{$action}) eq 'HASH') {
21748:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21749:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
21750:                                 if ($inuse == 0) {
21751:                                     $changes{$prefix}{$type} = 1;
21752:                                 } else {
21753:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21754:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
21755:                                     if (@changed > 0) {
21756:                                         $changes{$prefix}{$type} = 1;
21757:                                     }
21758:                                 }
21759:                             } else {
21760:                                 if ($inuse == 1) {
21761:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21762:                                     $changes{$prefix}{$type} = 1;
21763:                                 }
21764:                             }
21765:                         } else {
21766:                             if ($inuse == 1) {
21767:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
21768:                                 $changes{$prefix}{$type} = 1;
21769:                             }
21770:                         }
21771:                     } else {
21772:                         if ($inuse == 1) {
21773:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
21774:                             $changes{$prefix}{$type} = 1;
21775:                         }
21776:                     }
21777:                 }
21778:             }
21779:         }
21780:     }
21781:     if (keys(%changes)) {
21782:         foreach my $prefix (keys(%changes)) {
21783:             if (ref($changes{$prefix}) eq 'HASH') {
21784:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
21785:                     delete($changes{$prefix});
21786:                 }
21787:             } else {
21788:                 delete($changes{$prefix});
21789:             }
21790:         }
21791:     }
21792:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
21793:     if (keys(%changes) > 0) {
21794:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21795:                                                  $dom);
21796:         if ($putresult eq 'ok') {
21797:             if (ref($defaultshash{$action}) eq 'HASH') {
21798:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
21799:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
21800:                 }
21801:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
21802:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
21803:                 }
21804:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
21805:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
21806:                 }
21807:             }
21808:             my $cachetime = 24*60*60;
21809:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21810:             if (ref($lastactref) eq 'HASH') {
21811:                 $lastactref->{'domdefaults'} = 1;
21812:             }
21813:             if (keys(%changes) > 0) {
21814:                 my %titles = &ssl_titles();
21815:                 $resulttext = &mt('Changes made:').'<ul>';
21816:                 foreach my $prefix (@prefixes) {
21817:                     if (ref($changes{$prefix}) eq 'HASH') {
21818:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
21819:                         foreach my $type (@{$types{$prefix}}) {
21820:                             if (defined($changes{$prefix}{$type})) {
21821:                                 my ($newvalue,$notinuse);
21822:                                 if (ref($defaultshash{$action}) eq 'HASH') {
21823:                                     if (ref($defaultshash{$action}{$prefix})) {
21824:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21825:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
21826:                                         } else {
21827:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
21828:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
21829:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
21830:                                                 }
21831:                                             } else {
21832:                                                 $notinuse = 1;
21833:                                             }
21834:                                         }
21835:                                     }
21836:                                     if ($notinuse) {
21837:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
21838:                                     } elsif ($newvalue eq '') {
21839:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
21840:                                     } else {
21841:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
21842:                                     }
21843:                                 }
21844:                             }
21845:                         }
21846:                         $resulttext .= '</ul>';
21847:                     }
21848:                 }
21849:             } else {
21850:                 $resulttext = $nochgmsg;
21851:             }
21852:         } else {
21853:             $resulttext = '<span class="LC_error">'.
21854:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21855:         }
21856:     } else {
21857:         $resulttext = $nochgmsg;
21858:     }
21859:     return $resulttext;
21860: }
21861: 
21862: sub modify_trust {
21863:     my ($dom,$lastactref,%domconfig) = @_;
21864:     my (%by_ip,%by_location,@intdoms,@instdoms);
21865:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21866:     my @locations = sort(keys(%by_location));
21867:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
21868:     my @types = ('exc','inc');
21869:     my (%defaultshash,%changes);
21870:     foreach my $prefix (@prefixes) {
21871:         $defaultshash{'trust'}{$prefix} = {};
21872:     }
21873:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21874:     my $resulttext;
21875:     foreach my $prefix (@prefixes) {
21876:         foreach my $type (@types) {
21877:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21878:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21879:             my @okvals;
21880:             foreach my $val (@vals) {
21881:                 if ($val =~ /:/) {
21882:                     my @items = split(/:/,$val);
21883:                     foreach my $item (@items) {
21884:                         if (ref($by_location{$item}) eq 'ARRAY') {
21885:                             push(@okvals,$item);
21886:                         }
21887:                     }
21888:                 } else {
21889:                     if (ref($by_location{$val}) eq 'ARRAY') {
21890:                         push(@okvals,$val);
21891:                     }
21892:                 }
21893:             }
21894:             @okvals = sort(@okvals);
21895:             if (ref($domconfig{'trust'}) eq 'HASH') {
21896:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
21897:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21898:                         if ($inuse == 0) {
21899:                             $changes{$prefix}{$type} = 1;
21900:                         } else {
21901:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21902:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
21903:                             if (@changed > 0) {
21904:                                 $changes{$prefix}{$type} = 1;
21905:                             }
21906:                         }
21907:                     } else {
21908:                         if ($inuse == 1) {
21909:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21910:                             $changes{$prefix}{$type} = 1;
21911:                         }
21912:                     }
21913:                 } else {
21914:                     if ($inuse == 1) {
21915:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21916:                         $changes{$prefix}{$type} = 1;
21917:                     }
21918:                 }
21919:             } else {
21920:                 if ($inuse == 1) {
21921:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21922:                     $changes{$prefix}{$type} = 1;
21923:                 }
21924:             }
21925:         }
21926:     }
21927:     my $nochgmsg = &mt('No changes made to trust settings.');
21928:     if (keys(%changes) > 0) {
21929:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21930:                                                  $dom);
21931:         if ($putresult eq 'ok') {
21932:             if (ref($defaultshash{'trust'}) eq 'HASH') {
21933:                 foreach my $prefix (@prefixes) {
21934:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
21935:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
21936:                     }
21937:                 }
21938:             }
21939:             my $cachetime = 24*60*60;
21940:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21941:             if (ref($lastactref) eq 'HASH') {
21942:                 $lastactref->{'domdefaults'} = 1;
21943:             }
21944:             if (keys(%changes) > 0) {
21945:                 my %lt = &trust_titles();
21946:                 $resulttext = &mt('Changes made:').'<ul>';
21947:                 foreach my $prefix (@prefixes) {
21948:                     if (ref($changes{$prefix}) eq 'HASH') {
21949:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21950:                         foreach my $type (@types) {
21951:                             if (defined($changes{$prefix}{$type})) {
21952:                                 my ($newvalue,$notinuse);
21953:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
21954:                                     if (ref($defaultshash{'trust'}{$prefix})) {
21955:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21956:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
21957:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
21958:                                             }
21959:                                         } else {
21960:                                             $notinuse = 1;
21961:                                         }
21962:                                     }
21963:                                 }
21964:                                 if ($notinuse) {
21965:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21966:                                 } elsif ($newvalue eq '') {
21967:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21968:                                 } else {
21969:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21970:                                 }
21971:                             }
21972:                         }
21973:                         $resulttext .= '</ul>';
21974:                     }
21975:                 }
21976:                 $resulttext .= '</ul>';
21977:             } else {
21978:                 $resulttext = $nochgmsg;
21979:             }
21980:         } else {
21981:             $resulttext = '<span class="LC_error">'.
21982:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21983:         }
21984:     } else {
21985:         $resulttext = $nochgmsg;
21986:     }
21987:     return $resulttext;
21988: }
21989: 
21990: sub modify_loadbalancing {
21991:     my ($dom,%domconfig) = @_;
21992:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
21993:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
21994:     my ($othertitle,$usertypes,$types) =
21995:         &Apache::loncommon::sorted_inst_types($dom);
21996:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21997:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
21998:     my @sparestypes = ('primary','default');
21999:     my %typetitles = &sparestype_titles();
22000:     my $resulttext;
22001:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22002:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22003:         %existing = %{$domconfig{'loadbalancing'}};
22004:     }
22005:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
22006:                               \%currtargets,\%currrules,\%currcookies);
22007:     my ($saveloadbalancing,%defaultshash,%changes);
22008:     my ($alltypes,$othertypes,$titles) =
22009:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
22010:     my %ruletitles = &offloadtype_text();
22011:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
22012:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
22013:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
22014:         if ($balancer eq '') {
22015:             next;
22016:         }
22017:         if (!exists($servers{$balancer})) {
22018:             if (exists($currbalancer{$balancer})) {
22019:                 push(@{$changes{'delete'}},$balancer);
22020:             }
22021:             next;
22022:         }
22023:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
22024:             push(@{$changes{'delete'}},$balancer);
22025:             next;
22026:         }
22027:         if (!exists($currbalancer{$balancer})) {
22028:             push(@{$changes{'add'}},$balancer);
22029:         }
22030:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
22031:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
22032:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
22033:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22034:             $saveloadbalancing = 1;
22035:         }
22036:         foreach my $sparetype (@sparestypes) {
22037:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
22038:             my @offloadto;
22039:             foreach my $target (@targets) {
22040:                 if (($servers{$target}) && ($target ne $balancer)) {
22041:                     if ($sparetype eq 'default') {
22042:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
22043:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
22044:                         }
22045:                     }
22046:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
22047:                         push(@offloadto,$target);
22048:                     }
22049:                 }
22050:             }
22051:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
22052:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
22053:                     push(@offloadto,$balancer);
22054:                 }
22055:             }
22056:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
22057:         }
22058:         if ($env{'form.loadbalancing_cookie_'.$i}) {
22059:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
22060:             if (exists($currbalancer{$balancer})) { 
22061:                 unless ($currcookies{$balancer}) {
22062:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
22063:                 }
22064:             }
22065:         } elsif (exists($currbalancer{$balancer})) {
22066:             if ($currcookies{$balancer}) {
22067:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
22068:             }
22069:         }
22070:         if (ref($currtargets{$balancer}) eq 'HASH') {
22071:             foreach my $sparetype (@sparestypes) {
22072:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
22073:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
22074:                     if (@targetdiffs > 0) {
22075:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22076:                     }
22077:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22078:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22079:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22080:                     }
22081:                 }
22082:             }
22083:         } else {
22084:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
22085:                 foreach my $sparetype (@sparestypes) {
22086:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22087:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22088:                             $changes{'curr'}{$balancer}{'targets'} = 1;
22089:                         }
22090:                     }
22091:                 }
22092:             }
22093:         }
22094:         my $ishomedom;
22095:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
22096:             $ishomedom = 1;
22097:         }
22098:         if (ref($alltypes) eq 'ARRAY') {
22099:             foreach my $type (@{$alltypes}) {
22100:                 my $rule;
22101:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
22102:                          (!$ishomedom)) {
22103:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
22104:                 }
22105:                 if ($rule eq 'specific') {
22106:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
22107:                     if (exists($servers{$specifiedhost})) {
22108:                         $rule = $specifiedhost;
22109:                     }
22110:                 }
22111:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
22112:                 if (ref($currrules{$balancer}) eq 'HASH') {
22113:                     if ($rule ne $currrules{$balancer}{$type}) {
22114:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22115:                     }
22116:                 } elsif ($rule ne '') {
22117:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22118:                 }
22119:             }
22120:         }
22121:     }
22122:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
22123:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
22124:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
22125:             $defaultshash{'loadbalancing'} = {};
22126:         }
22127:         my $putresult = &Apache::lonnet::put_dom('configuration',
22128:                                                  \%defaultshash,$dom);
22129:         if ($putresult eq 'ok') {
22130:             if (keys(%changes) > 0) {
22131:                 my %toupdate;
22132:                 if (ref($changes{'delete'}) eq 'ARRAY') {
22133:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
22134:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
22135:                         $toupdate{$balancer} = 1;
22136:                     }
22137:                 }
22138:                 if (ref($changes{'add'}) eq 'ARRAY') {
22139:                     foreach my $balancer (sort(@{$changes{'add'}})) {
22140:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
22141:                         $toupdate{$balancer} = 1;
22142:                     }
22143:                 }
22144:                 if (ref($changes{'curr'}) eq 'HASH') {
22145:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
22146:                         $toupdate{$balancer} = 1;
22147:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
22148:                             if ($changes{'curr'}{$balancer}{'targets'}) {
22149:                                 my %offloadstr;
22150:                                 foreach my $sparetype (@sparestypes) {
22151:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22152:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22153:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22154:                                         }
22155:                                     }
22156:                                 }
22157:                                 if (keys(%offloadstr) == 0) {
22158:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
22159:                                 } else {
22160:                                     my $showoffload;
22161:                                     foreach my $sparetype (@sparestypes) {
22162:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
22163:                                         if (defined($offloadstr{$sparetype})) {
22164:                                             $showoffload .= $offloadstr{$sparetype};
22165:                                         } else {
22166:                                             $showoffload .= &mt('None');
22167:                                         }
22168:                                         $showoffload .= ('&nbsp;'x3);
22169:                                     }
22170:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
22171:                                 }
22172:                             }
22173:                         }
22174:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
22175:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
22176:                                 foreach my $type (@{$alltypes}) {
22177:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
22178:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22179:                                         my $balancetext;
22180:                                         if ($rule eq '') {
22181:                                             $balancetext =  $ruletitles{'default'};
22182:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
22183:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22184:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22185:                                                 foreach my $sparetype (@sparestypes) {
22186:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22187:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22188:                                                     }
22189:                                                 }
22190:                                                 foreach my $item (@{$alltypes}) {
22191:                                                     next if ($item =~  /^_LC_ipchange/);
22192:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
22193:                                                     if ($hasrule eq 'homeserver') {
22194:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
22195:                                                     } else {
22196:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
22197:                                                             if ($servers{$hasrule}) {
22198:                                                                 $toupdate{$hasrule} = 1;
22199:                                                             }
22200:                                                         }
22201:                                                     }
22202:                                                 }
22203:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
22204:                                                     $balancetext =  $ruletitles{$rule};
22205:                                                 } else {
22206:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22207:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
22208:                                                     if ($receiver) {
22209:                                                         $toupdate{$receiver};
22210:                                                     }
22211:                                                 }
22212:                                             } else {
22213:                                                 $balancetext =  $ruletitles{$rule};
22214:                                             }
22215:                                         } else {
22216:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
22217:                                         }
22218:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
22219:                                     }
22220:                                 }
22221:                             }
22222:                         }
22223:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
22224:                             if ($currcookies{$balancer}) {
22225:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
22226:                                                           $balancer).'</li>';
22227:                             } else {
22228:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
22229:                                                           $balancer).'</li>';
22230:                             }
22231:                         }
22232:                     }
22233:                 }
22234:                 if (keys(%toupdate)) {
22235:                     my %thismachine;
22236:                     my $updatedhere;
22237:                     my $cachetime = 60*60*24;
22238:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
22239:                     foreach my $lonhost (keys(%toupdate)) {
22240:                         if ($thismachine{$lonhost}) {
22241:                             unless ($updatedhere) {
22242:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
22243:                                                               $defaultshash{'loadbalancing'},
22244:                                                               $cachetime);
22245:                                 $updatedhere = 1;
22246:                             }
22247:                         } else {
22248:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
22249:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22250:                         }
22251:                     }
22252:                 }
22253:                 if ($resulttext ne '') {
22254:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
22255:                 } else {
22256:                     $resulttext = $nochgmsg;
22257:                 }
22258:             } else {
22259:                 $resulttext = $nochgmsg;
22260:             }
22261:         } else {
22262:             $resulttext = '<span class="LC_error">'.
22263:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22264:         }
22265:     } else {
22266:         $resulttext = $nochgmsg;
22267:     }
22268:     return $resulttext;
22269: }
22270: 
22271: sub recurse_check {
22272:     my ($chkcats,$categories,$depth,$name) = @_;
22273:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
22274:         my $chg = 0;
22275:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
22276:             my $category = $chkcats->[$depth]{$name}[$j];
22277:             my $item;
22278:             if ($category eq '') {
22279:                 $chg ++;
22280:             } else {
22281:                 my $deeper = $depth + 1;
22282:                 $item = &escape($category).':'.&escape($name).':'.$depth;
22283:                 if ($chg) {
22284:                     $categories->{$item} -= $chg;
22285:                 }
22286:                 &recurse_check($chkcats,$categories,$deeper,$category);
22287:                 $deeper --;
22288:             }
22289:         }
22290:     }
22291:     return;
22292: }
22293: 
22294: sub recurse_cat_deletes {
22295:     my ($item,$coursecategories,$deletions) = @_;
22296:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
22297:     my $subdepth = $depth + 1;
22298:     if (ref($coursecategories) eq 'HASH') {
22299:         foreach my $subitem (keys(%{$coursecategories})) {
22300:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
22301:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
22302:                 delete($coursecategories->{$subitem});
22303:                 $deletions->{$subitem} = 1;
22304:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
22305:             }
22306:         }
22307:     }
22308:     return;
22309: }
22310: 
22311: sub active_dc_picker {
22312:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
22313:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
22314:     my @domcoord = keys(%domcoords);
22315:     if (keys(%currhash)) {
22316:         foreach my $dc (keys(%currhash)) {
22317:             unless (exists($domcoords{$dc})) {
22318:                 push(@domcoord,$dc);
22319:             }
22320:         }
22321:     }
22322:     @domcoord = sort(@domcoord);
22323:     my $numdcs = scalar(@domcoord);
22324:     my $rows = 0;
22325:     my $table;
22326:     if ($numdcs > 1) {
22327:         $table = '<table>';
22328:         for (my $i=0; $i<@domcoord; $i++) {
22329:             my $rem = $i%($numinrow);
22330:             if ($rem == 0) {
22331:                 if ($i > 0) {
22332:                     $table .= '</tr>';
22333:                 }
22334:                 $table .= '<tr>';
22335:                 $rows ++;
22336:             }
22337:             my $check = '';
22338:             if ($inputtype eq 'radio') {
22339:                 if (keys(%currhash) == 0) {
22340:                     if (!$i) {
22341:                         $check = ' checked="checked"';
22342:                     }
22343:                 } elsif (exists($currhash{$domcoord[$i]})) {
22344:                     $check = ' checked="checked"';
22345:                 }
22346:             } else {
22347:                 if (exists($currhash{$domcoord[$i]})) {
22348:                     $check = ' checked="checked"';
22349:                 }
22350:             }
22351:             if ($i == @domcoord - 1) {
22352:                 my $colsleft = $numinrow - $rem;
22353:                 if ($colsleft > 1) {
22354:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
22355:                 } else {
22356:                     $table .= '<td class="LC_left_item">';
22357:                 }
22358:             } else {
22359:                 $table .= '<td class="LC_left_item">';
22360:             }
22361:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
22362:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22363:             $table .= '<span class="LC_nobreak"><label>'.
22364:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
22365:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
22366:             if ($user ne $dcname.':'.$dcdom) {
22367:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22368:             }
22369:             $table .= '</label></span></td>';
22370:         }
22371:         $table .= '</tr></table>';
22372:     } elsif ($numdcs == 1) {
22373:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
22374:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22375:         if ($inputtype eq 'radio') {
22376:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
22377:             if ($user ne $dcname.':'.$dcdom) {
22378:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22379:             }
22380:         } else {
22381:             my $check;
22382:             if (exists($currhash{$domcoord[0]})) {
22383:                 $check = ' checked="checked"';
22384:             }
22385:             $table = '<span class="LC_nobreak"><label>'.
22386:                      '<input type="checkbox" name="'.$name.'" '.
22387:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
22388:             if ($user ne $dcname.':'.$dcdom) {
22389:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22390:             }
22391:             $table .= '</label></span>';
22392:             $rows ++;
22393:         }
22394:     }
22395:     return ($numdcs,$table,$rows);
22396: }
22397: 
22398: sub usersession_titles {
22399:     return &Apache::lonlocal::texthash(
22400:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
22401:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
22402:                spares => 'Servers offloaded to, when busy',
22403:                version => 'LON-CAPA version requirement',
22404:                excludedomain => 'Allow all, but exclude specific domains',
22405:                includedomain => 'Deny all, but include specific domains',
22406:                primary => 'Primary (checked first)',
22407:                default => 'Default',
22408:            );
22409: }
22410: 
22411: sub id_for_thisdom {
22412:     my (%servers) = @_;
22413:     my %altids;
22414:     foreach my $server (keys(%servers)) {
22415:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22416:         if ($serverhome ne $server) {
22417:             $altids{$serverhome} = $server;
22418:         }
22419:     }
22420:     return %altids;
22421: }
22422: 
22423: sub count_servers {
22424:     my ($currbalancer,%servers) = @_;
22425:     my (@spares,$numspares);
22426:     foreach my $lonhost (sort(keys(%servers))) {
22427:         next if ($currbalancer eq $lonhost);
22428:         push(@spares,$lonhost);
22429:     }
22430:     if ($currbalancer) {
22431:         $numspares = scalar(@spares);
22432:     } else {
22433:         $numspares = scalar(@spares) - 1;
22434:     }
22435:     return ($numspares,@spares);
22436: }
22437: 
22438: sub lonbalance_targets_js {
22439:     my ($dom,$types,$servers,$settings) = @_;
22440:     my $select = &mt('Select');
22441:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
22442:     if (ref($servers) eq 'HASH') {
22443:         $alltargets = join("','",sort(keys(%{$servers})));
22444:         my @homedoms;
22445:         foreach my $server (sort(keys(%{$servers}))) {
22446:             if (&Apache::lonnet::host_domain($server) eq $dom) {
22447:                 push(@homedoms,'1');
22448:             } else {
22449:                 push(@homedoms,'0');
22450:             }
22451:         }
22452:         $allishome = join("','",@homedoms);
22453:     }
22454:     if (ref($types) eq 'ARRAY') {
22455:         if (@{$types} > 0) {
22456:             @alltypes = @{$types};
22457:         }
22458:     }
22459:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
22460:     $allinsttypes = join("','",@alltypes);
22461:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22462:     if (ref($settings) eq 'HASH') {
22463:         %existing = %{$settings};
22464:     }
22465:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
22466:                               \%currtargets,\%currrules,\%currcookies);
22467:     my $balancers = join("','",sort(keys(%currbalancer)));
22468:     return <<"END";
22469: 
22470: <script type="text/javascript">
22471: // <![CDATA[
22472: 
22473: currBalancers = new Array('$balancers');
22474: 
22475: function toggleTargets(balnum) {
22476:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22477:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
22478:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
22479:     var prevbalancer = prevhostitem.value;
22480:     var baltotal = document.getElementById('loadbalancing_total').value;
22481:     prevhostitem.value = balancer;
22482:     if (prevbalancer != '') {
22483:         var prevIdx = currBalancers.indexOf(prevbalancer);
22484:         if (prevIdx != -1) {
22485:             currBalancers.splice(prevIdx,1);
22486:         }
22487:     }
22488:     if (balancer == '') {
22489:         hideSpares(balnum);
22490:     } else {
22491:         var currIdx = currBalancers.indexOf(balancer);
22492:         if (currIdx == -1) {
22493:             currBalancers.push(balancer);
22494:         }
22495:         var homedoms = new Array('$allishome');
22496:         var ishomedom = homedoms[lonhostitem.selectedIndex];
22497:         showSpares(balancer,ishomedom,balnum);
22498:     }
22499:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
22500:     return;
22501: }
22502: 
22503: function showSpares(balancer,ishomedom,balnum) {
22504:     var alltargets = new Array('$alltargets');
22505:     var insttypes = new Array('$allinsttypes');
22506:     var offloadtypes = new Array('primary','default');
22507: 
22508:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
22509:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
22510:  
22511:     for (var i=0; i<offloadtypes.length; i++) {
22512:         var count = 0;
22513:         for (var j=0; j<alltargets.length; j++) {
22514:             if (alltargets[j] != balancer) {
22515:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
22516:                 item.value = alltargets[j];
22517:                 item.style.textAlign='left';
22518:                 item.style.textFace='normal';
22519:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
22520:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
22521:                     item.disabled = '';
22522:                 } else {
22523:                     item.disabled = 'disabled';
22524:                     item.checked = false;
22525:                 }
22526:                 count ++;
22527:             }
22528:         }
22529:     }
22530:     for (var k=0; k<insttypes.length; k++) {
22531:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
22532:             if (ishomedom == 1) {
22533:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22534:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22535:             } else {
22536:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22537:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22538:             }
22539:         } else {
22540:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22541:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22542:         }
22543:         if ((insttypes[k] != '_LC_external') && 
22544:             ((insttypes[k] != '_LC_internetdom') ||
22545:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
22546:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
22547:             item.options.length = 0;
22548:             item.options[0] = new Option("","",true,true);
22549:             var idx = 0;
22550:             for (var m=0; m<alltargets.length; m++) {
22551:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
22552:                     idx ++;
22553:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22554:                 }
22555:             }
22556:         }
22557:     }
22558:     return;
22559: }
22560: 
22561: function hideSpares(balnum) {
22562:     var alltargets = new Array('$alltargets');
22563:     var insttypes = new Array('$allinsttypes');
22564:     var offloadtypes = new Array('primary','default');
22565: 
22566:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
22567:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
22568: 
22569:     var total = alltargets.length - 1;
22570:     for (var i=0; i<offloadtypes; i++) {
22571:         for (var j=0; j<total; j++) {
22572:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
22573:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
22574:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
22575:         }
22576:     }
22577:     for (var k=0; k<insttypes.length; k++) {
22578:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22579:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22580:         if (insttypes[k] != '_LC_external') {
22581:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
22582:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
22583:         }
22584:     }
22585:     return;
22586: }
22587: 
22588: function checkOffloads(item,balnum,type) {
22589:     var alltargets = new Array('$alltargets');
22590:     var offloadtypes = new Array('primary','default');
22591:     if (item.checked) {
22592:         var total = alltargets.length - 1;
22593:         var other;
22594:         if (type == offloadtypes[0]) {
22595:             other = offloadtypes[1];
22596:         } else {
22597:             other = offloadtypes[0];
22598:         }
22599:         for (var i=0; i<total; i++) {
22600:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
22601:             if (server == item.value) {
22602:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
22603:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
22604:                 }
22605:             }
22606:         }
22607:     }
22608:     return;
22609: }
22610: 
22611: function singleServerToggle(balnum,type) {
22612:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
22613:     if (offloadtoSelIdx == 0) {
22614:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
22615:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22616: 
22617:     } else {
22618:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
22619:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22620:     }
22621:     return;
22622: }
22623: 
22624: function balanceruleChange(formname,balnum,type) {
22625:     if (type == '_LC_external') {
22626:         return;
22627:     }
22628:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
22629:     for (var i=0; i<typesRules.length; i++) {
22630:         if (formname.elements[typesRules[i]].checked) {
22631:             if (formname.elements[typesRules[i]].value != 'specific') {
22632:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
22633:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22634:             } else {
22635:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22636:             }
22637:         }
22638:     }
22639:     return;
22640: }
22641: 
22642: function balancerDeleteChange(balnum) {
22643:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22644:     var baltotal = document.getElementById('loadbalancing_total').value;
22645:     var addtarget;
22646:     var removetarget;
22647:     var action = 'delete';
22648:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
22649:         var lonhost = hostitem.value;
22650:         var currIdx = currBalancers.indexOf(lonhost);
22651:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
22652:             if (currIdx != -1) {
22653:                 currBalancers.splice(currIdx,1);
22654:             }
22655:             addtarget = lonhost;
22656:         } else {
22657:             if (currIdx == -1) {
22658:                 currBalancers.push(lonhost);
22659:             }
22660:             removetarget = lonhost;
22661:             action = 'undelete';
22662:         }
22663:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
22664:     }
22665:     return;
22666: }
22667: 
22668: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
22669:     if (baltotal > 1) {
22670:         var offloadtypes = new Array('primary','default');
22671:         var alltargets = new Array('$alltargets');
22672:         var insttypes = new Array('$allinsttypes');
22673:         for (var i=0; i<baltotal; i++) {
22674:             if (i != balnum) {
22675:                 for (var j=0; j<offloadtypes.length; j++) {
22676:                     var total = alltargets.length - 1;
22677:                     for (var k=0; k<total; k++) {
22678:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
22679:                         var server = serveritem.value;
22680:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
22681:                             if (server == addtarget) {
22682:                                 serveritem.disabled = '';
22683:                             }
22684:                         }
22685:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22686:                             if (server == removetarget) {
22687:                                 serveritem.disabled = 'disabled';
22688:                                 serveritem.checked = false;
22689:                             }
22690:                         }
22691:                     }
22692:                 }
22693:                 for (var j=0; j<insttypes.length; j++) {
22694:                     if (insttypes[j] != '_LC_external') {
22695:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
22696:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
22697:                             var currSel = singleserver.selectedIndex;
22698:                             var currVal = singleserver.options[currSel].value;
22699:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
22700:                                 var numoptions = singleserver.options.length;
22701:                                 var needsnew = 1;
22702:                                 for (var k=0; k<numoptions; k++) {
22703:                                     if (singleserver.options[k] == addtarget) {
22704:                                         needsnew = 0;
22705:                                         break;
22706:                                     }
22707:                                 }
22708:                                 if (needsnew == 1) {
22709:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
22710:                                 }
22711:                             }
22712:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22713:                                 singleserver.options.length = 0;
22714:                                 if ((currVal) && (currVal != removetarget)) {
22715:                                     singleserver.options[0] = new Option("","",false,false);
22716:                                 } else {
22717:                                     singleserver.options[0] = new Option("","",true,true);
22718:                                 }
22719:                                 var idx = 0;
22720:                                 for (var m=0; m<alltargets.length; m++) {
22721:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
22722:                                         idx ++;
22723:                                         if (currVal == alltargets[m]) {
22724:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
22725:                                         } else {
22726:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22727:                                         }
22728:                                     }
22729:                                 }
22730:                             }
22731:                         }
22732:                     }
22733:                 }
22734:             }
22735:         }
22736:     }
22737:     return;
22738: }
22739: 
22740: // ]]>
22741: </script>
22742: 
22743: END
22744: }
22745: 
22746: 
22747: sub new_spares_js {
22748:     my @sparestypes = ('primary','default');
22749:     my $types = join("','",@sparestypes);
22750:     my $select = &mt('Select');
22751:     return <<"END";
22752: 
22753: <script type="text/javascript">
22754: // <![CDATA[
22755: 
22756: function updateNewSpares(formname,lonhost) {
22757:     var types = new Array('$types');
22758:     var include = new Array();
22759:     var exclude = new Array();
22760:     for (var i=0; i<types.length; i++) {
22761:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
22762:         for (var j=0; j<spareboxes.length; j++) {
22763:             if (formname.elements[spareboxes[j]].checked) {
22764:                 exclude.push(formname.elements[spareboxes[j]].value);
22765:             } else {
22766:                 include.push(formname.elements[spareboxes[j]].value);
22767:             }
22768:         }
22769:     }
22770:     for (var i=0; i<types.length; i++) {
22771:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
22772:         var selIdx = newSpare.selectedIndex;
22773:         var currnew = newSpare.options[selIdx].value;
22774:         var okSpares = new Array();
22775:         for (var j=0; j<newSpare.options.length; j++) {
22776:             var possible = newSpare.options[j].value;
22777:             if (possible != '') {
22778:                 if (exclude.indexOf(possible) == -1) {
22779:                     okSpares.push(possible);
22780:                 } else {
22781:                     if (currnew == possible) {
22782:                         selIdx = 0;
22783:                     }
22784:                 }
22785:             }
22786:         }
22787:         for (var k=0; k<include.length; k++) {
22788:             if (okSpares.indexOf(include[k]) == -1) {
22789:                 okSpares.push(include[k]);
22790:             }
22791:         }
22792:         okSpares.sort();
22793:         newSpare.options.length = 0;
22794:         if (selIdx == 0) {
22795:             newSpare.options[0] = new Option("$select","",true,true);
22796:         } else {
22797:             newSpare.options[0] = new Option("$select","",false,false);
22798:         }
22799:         for (var m=0; m<okSpares.length; m++) {
22800:             var idx = m+1;
22801:             var selThis = 0;
22802:             if (selIdx != 0) {
22803:                 if (okSpares[m] == currnew) {
22804:                     selThis = 1;
22805:                 }
22806:             }
22807:             if (selThis == 1) {
22808:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
22809:             } else {
22810:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
22811:             }
22812:         }
22813:     }
22814:     return;
22815: }
22816: 
22817: function checkNewSpares(lonhost,type) {
22818:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
22819:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
22820:     if (chosen != '') {
22821:         var othertype;
22822:         var othernewSpare;
22823:         if (type == 'primary') {
22824:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
22825:         }
22826:         if (type == 'default') {
22827:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
22828:         }
22829:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
22830:             othernewSpare.selectedIndex = 0;
22831:         }
22832:     }
22833:     return;
22834: }
22835: 
22836: // ]]>
22837: </script>
22838: 
22839: END
22840: 
22841: }
22842: 
22843: sub common_domprefs_js {
22844:     return <<"END";
22845: 
22846: <script type="text/javascript">
22847: // <![CDATA[
22848: 
22849: function getIndicesByName(formname,item) {
22850:     var group = new Array();
22851:     for (var i=0;i<formname.elements.length;i++) {
22852:         if (formname.elements[i].name == item) {
22853:             group.push(formname.elements[i].id);
22854:         }
22855:     }
22856:     return group;
22857: }
22858: 
22859: // ]]>
22860: </script>
22861: 
22862: END
22863: 
22864: }
22865: 
22866: sub recaptcha_js {
22867:     my %lt = &captcha_phrases();
22868:     return <<"END";
22869: 
22870: <script type="text/javascript">
22871: // <![CDATA[
22872: 
22873: function updateCaptcha(caller,context) {
22874:     var privitem;
22875:     var pubitem;
22876:     var privtext;
22877:     var pubtext;
22878:     var versionitem;
22879:     var versiontext;
22880:     if (document.getElementById(context+'_recaptchapub')) {
22881:         pubitem = document.getElementById(context+'_recaptchapub');
22882:     } else {
22883:         return;
22884:     }
22885:     if (document.getElementById(context+'_recaptchapriv')) {
22886:         privitem = document.getElementById(context+'_recaptchapriv');
22887:     } else {
22888:         return;
22889:     }
22890:     if (document.getElementById(context+'_recaptchapubtxt')) {
22891:         pubtext = document.getElementById(context+'_recaptchapubtxt');
22892:     } else {
22893:         return;
22894:     }
22895:     if (document.getElementById(context+'_recaptchaprivtxt')) {
22896:         privtext = document.getElementById(context+'_recaptchaprivtxt');
22897:     } else {
22898:         return;
22899:     }
22900:     if (document.getElementById(context+'_recaptchaversion')) {
22901:         versionitem = document.getElementById(context+'_recaptchaversion');
22902:     } else {
22903:         return;
22904:     }
22905:     if (document.getElementById(context+'_recaptchavertxt')) {
22906:         versiontext = document.getElementById(context+'_recaptchavertxt');
22907:     } else {
22908:         return;
22909:     }
22910:     if (caller.checked) {
22911:         if (caller.value == 'recaptcha') {
22912:             pubitem.type = 'text';
22913:             privitem.type = 'text';
22914:             pubitem.size = '40';
22915:             privitem.size = '40';
22916:             pubtext.innerHTML = "$lt{'pub'}";
22917:             privtext.innerHTML = "$lt{'priv'}";
22918:             versionitem.type = 'text';
22919:             versionitem.size = '3';
22920:             versiontext.innerHTML = "$lt{'ver'}";
22921:         } else {
22922:             pubitem.type = 'hidden';
22923:             privitem.type = 'hidden';
22924:             versionitem.type = 'hidden';
22925:             pubtext.innerHTML = '';
22926:             privtext.innerHTML = '';
22927:             versiontext.innerHTML = '';
22928:         }
22929:     }
22930:     return;
22931: }
22932: 
22933: // ]]>
22934: </script>
22935: 
22936: END
22937: 
22938: }
22939: 
22940: sub toggle_display_js {
22941:     return <<"END";
22942: 
22943: <script type="text/javascript">
22944: // <![CDATA[
22945: 
22946: function toggleDisplay(domForm,caller) {
22947:     if (document.getElementById(caller)) {
22948:         var divitem = document.getElementById(caller);
22949:         var optionsElement = domForm.coursecredits;
22950:         var checkval = 1;
22951:         var dispval = 'block';
22952:         var selfcreateRegExp = /^cancreate_emailverified/;
22953:         if (caller == 'emailoptions') {
22954:             optionsElement = domForm.cancreate_email;
22955:         }
22956:         if (caller == 'studentsubmission') {
22957:             optionsElement = domForm.postsubmit;
22958:         }
22959:         if (caller == 'cloneinstcode') {
22960:             optionsElement = domForm.canclone;
22961:             checkval = 'instcode';
22962:         }
22963:         if (selfcreateRegExp.test(caller)) {
22964:             optionsElement = domForm.elements[caller];
22965:             checkval = 'other';
22966:             dispval = 'inline'
22967:         }
22968:         if (optionsElement.length) {
22969:             var currval;
22970:             for (var i=0; i<optionsElement.length; i++) {
22971:                 if (optionsElement[i].checked) {
22972:                    currval = optionsElement[i].value;
22973:                 }
22974:             }
22975:             if (currval == checkval) {
22976:                 divitem.style.display = dispval;
22977:             } else {
22978:                 divitem.style.display = 'none';
22979:             }
22980:         }
22981:     }
22982:     return;
22983: }
22984: 
22985: // ]]>
22986: </script>
22987: 
22988: END
22989: 
22990: }
22991: 
22992: sub captcha_phrases {
22993:     return &Apache::lonlocal::texthash (
22994:                  priv => 'Private key',
22995:                  pub  => 'Public key',
22996:                  original  => 'original (CAPTCHA)',
22997:                  recaptcha => 'successor (ReCAPTCHA)',
22998:                  notused   => 'unused',
22999:                  ver => 'ReCAPTCHA version (1 or 2)',
23000:     );
23001: }
23002: 
23003: sub devalidate_remote_domconfs {
23004:     my ($dom,$cachekeys) = @_;
23005:     return unless (ref($cachekeys) eq 'HASH');
23006:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23007:     my %thismachine;
23008:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23009:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
23010:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
23011:                       'ipaccess');
23012:     my %cache_by_lonhost;
23013:     if (exists($cachekeys->{'samllanding'})) {
23014:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
23015:             my %landing = %{$cachekeys->{'samllanding'}};
23016:             my %domservers = &Apache::lonnet::get_servers($dom);
23017:             if (keys(%domservers)) {
23018:                 foreach my $server (keys(%domservers)) {
23019:                     my @cached;
23020:                     next if ($thismachine{$server});
23021:                     if ($landing{$server}) {
23022:                         push(@cached,&escape('samllanding').':'.&escape($server));
23023:                     }
23024:                     if (@cached) {
23025:                         $cache_by_lonhost{$server} = \@cached;
23026:                     }
23027:                 }
23028:             }
23029:         }
23030:     }
23031:     if (keys(%servers)) {
23032:         foreach my $server (keys(%servers)) {
23033:             next if ($thismachine{$server});
23034:             my @cached;
23035:             foreach my $name (@posscached) {
23036:                 if ($cachekeys->{$name}) {
23037:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
23038:                         if (ref($cachekeys->{$name}) eq 'HASH') {
23039:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
23040:                                 push(@cached,&escape($name).':'.&escape($key));
23041:                             }
23042:                         }
23043:                     } else {
23044:                         push(@cached,&escape($name).':'.&escape($dom));
23045:                     }
23046:                 }
23047:             }
23048:             if ((exists($cache_by_lonhost{$server})) &&
23049:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
23050:                 push(@cached,@{$cache_by_lonhost{$server}});
23051:             }
23052:             if (@cached) {
23053:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
23054:             }
23055:         }
23056:     }
23057:     return;
23058: }
23059: 
23060: 1;

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